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());
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 -
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... -
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 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()];
-
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...