Mat from width*height*4 bytes BGRA Buffer [closed]
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.
Hopefully someone knows how to fix this. Thanks in advance!
CV_8UC3 == 3 8bit channels (your input has 4 of them, so try CV_8UC4 instead)
Turned a 3 to 4, problem solved. Thanks a lot!
oh, cool.
just saying, careful here, this is "borrowed memory".
maybe the 1st thing you want to do is convert it to a "deep" copy
Well, that's what I started searching for one minute ago. I need to somehow remove the alpha channel anyway for template matching... I guess you brought me the solution right away! Again thanks a lot!