Can imencode() encode a large .bmp image? [closed]

asked 2016-10-27 02:46:01 -0500

corneliu.cincea gravatar image

I use imencode() method to put the header for the .bmp image. For an image with 20x20 pixels is working but for an image with 400x400 pixels is not working. There is no error when I use large .bmp. The program is a comparer algorithm.

Another question if imencode () is not working with larger images how to create manually the header for the .bmp content?

NOTE: I am using OpenCV 3.0.0

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-25 04:27:29.585212


"is not working" - means ?

please show your code, the problem might rather be on your side, not with imencode.

(and i already guess, you're using strings in some way, they should not be used...)

berak gravatar imageberak ( 2016-10-27 02:50:05 -0500 )edit

This is a C++ Addon in Nodejs and I use AsyncWorker to be async. { ... BYTE* DiffBuff ==> where I keep the image content

cv::Mat imgMatrix = cv::Mat(height, width, CV_8UC3, (void*)DiffBuff);
cv::flip (imgMatrix, imgMatrix, 0);
std::vector<uchar> diffBuffer;

returnValue = EncodeMatrix(imgMatrix, diffBuffer);

BYTE * diffImageBuffer = (BYTE *) malloc(diffBuffer.size());
memcpy(diffImageBuffer, &diffBuffer[0], diffBuffer.size());

BufferToBase64(diffImageBuffer, ...);


//method which encode bmp bool EncodeMatrix(cv::Mat &imgMatrix, std::vector<uchar> &diffBuffer) { bool result = cv::imencode(".bmp", imgMatrix, diffBuffer, std::vector<int>());

     printf(" INVALID_IMENCODE !!! \r\n ");
 return result;


corneliu.cincea gravatar imagecorneliu.cincea ( 2016-10-27 03:43:14 -0500 )edit

please note, that this constructor: cv::Mat(height, width, CV_8UC3, (void*)DiffBuff); does not copy pixels, only the pointer, and also, that your flip() works in-place. be extra careful here, and clone() your imgMatrix.

berak gravatar imageberak ( 2016-10-27 03:53:48 -0500 )edit

"is not working" - means : That I use the AyncWorker and on Execute method I do my encode above code and HandleOKCallback() is never called so in my javascript I cannot see anything . If I use below snippet is working fine but is a different result that I expect because of compression and .png extension. std::vector<int> params(1); params[0] = CV_IMWRITE_PNG_COMPRESSION; params[1] = 9;

 bool result = cv::imencode(".png", imgMatrix, diffBuffer, params);
corneliu.cincea gravatar imagecorneliu.cincea ( 2016-10-27 03:54:30 -0500 )edit

For small .bmp is working(20x20). I used clone but no difference.
Why is working with .png extension and compression? Is there a method to not use imencode and create manually the bitmap header?

corneliu.cincea gravatar imagecorneliu.cincea ( 2016-10-27 04:02:59 -0500 )edit

encoding to bmp works for any img size here (please try in an isolated, simple cpp prog), so it's something weird in your context.

berak gravatar imageberak ( 2016-10-27 04:25:11 -0500 )edit

Hi, I isolated the imencode method. I put the method in the constructor of the AsyncWorker derived class and is breaking me the flow with Execute method and OKCallback. But when I put the imencode in OKCallback the flow is working ok for me. So AsyncWorker from NodeJS is behaving weird.

There is possible to create manually the header for bitmap? I am using OpenCV3.0.0 I don't have the class cv::BitmapInfoHeader. I would like to create the header manually and attach to the content.

corneliu.cincea gravatar imagecorneliu.cincea ( 2016-10-27 06:55:34 -0500 )edit