Ask Your Question

Revision history [back]

Mat from width*height*4 bytes BGRA Buffer

Hey guys.

Instead of wasting more time I decided to ask those people who already know a nice answer to my question ; )

I'm using the Chromium Embedded Framework off-screen rendering which provides me an image-buffer which I want to convert to an cv::Mat. According to CEFs docs: "On Windows the buffer will be widthheight4 bytes in size and represents a BGRA image with an upper-left origin." The programming language used is C++.

Unfortunately my basic OpenCV knowledge is not strong enough to convert the buffer properly. With the following code I just get some awkward results saving the Mat.

// Convert the buffer from "const void" to "unsigned char":
unsigned char* buffer = (unsigned char*)cef_buffer_data;
// Buffer to Mat:
cv::Mat image(cef_buffer_height, cef_buffer_width, CV_8UC3, buffer);
// Write Mat to file:
imwrite("image.jpg", image);

Fortunately I'm able to convert my cef_buffer to an hbitmat and save it as an reference. The following Image is what I get by saving the hbitmat and the second one is what I get saving the Mat.

image description

image description

Hopefully someone knows how to fix this. Thanks in advance!