Unsolved fill a buffer of unsigned char* from QImage
-
@LeaA
QImageBits is the way to go, show us what you tried, that resulted in a broken image.from the top of my head, this should make a copy of your raw data.
QImage img; uchar copiedData[img.byteCount()]; memcpy(copiedData, img.bits(), img.byteCount());
-
this is my code that I getting a destroyed image:
QImage image("/tmp/myImage.pgm");
unsigned char* image_bytes = image.bits();
cv:Mat cv_image(image.height(),image.width(),CV_8UC3,(void*)image_bytes);
imwrite("/tmp/new_image.pgm",cv_image); -
@J-Hilk
I'm getting the same result -
Just read
cv::Mat
documentation:Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.
QImage* image = new QImage("/tmp/myImage.pgm",this/*parent that will take care of cleaning up*/); cv:Mat cv_image(image.height(),image.width(),CV_8UC3,image.bits()); imwrite("/tmp/new_image.pgm",cv_image);
@J.Hilk said in fill a buffer of unsigned char* from QImage:
uchar copiedData[img.byteCount()];
This is not valid in C++, only in C99
-
@VRonin
So, because of it I want to move with for loop on the QImage and copy the data to a buffer and then sent it to CV:Mat -
Same problem, you have to let the buffer survive. allocate it on the heap and then handle the cleanup
-
yet, I think I not do it good..
this is my code:
unsigned char* image_buff = new char [image.bytesCount()];
int step = 0;
for( int i=0; i < image.height(); ){
image_buff[step] = image.scanLine(i);
step += image.bytesPerLine() - image.width();
}and then I using CV:Mat with the buffer
and delete [] buffer.
but I doesn't work... -
@VRonin said in fill a buffer of unsigned char* from QImage:
@J.Hilk said in fill a buffer of unsigned char* from QImage:
uchar copiedData[img.byteCount()];
This is not valid in C++, only in C99
ups 😳
uchar *copiedData = new uchar[img.byteCount()];
-
the QImage store the data non-sequential and because of it I want copy the data to a buffer in sequential order
-
@LeaA said in fill a buffer of unsigned char* from QImage:
image_buff[step] = image.scanLine(i);
This only copies 1
char
usememcpy
as @J-Hilk suggested -
I did so and I'm getting the same result
-
I want copy pixel pixel and jump on the headers that the QImage add to the buffer
-
-
I get the same result but the image is swapped..