Mat from width*height*4 bytes BGRA Buffer [closed]

asked 2016-04-24 07:51:21 -0600

DoctorJohn gravatar image

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!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by DoctorJohn
close date 2016-04-24 08:00:12.809125



CV_8UC3 == 3 8bit channels (your input has 4 of them, so try CV_8UC4 instead)

berak gravatar imageberak ( 2016-04-24 07:53:16 -0600 )edit

Turned a 3 to 4, problem solved. Thanks a lot!

DoctorJohn gravatar imageDoctorJohn ( 2016-04-24 07:59:14 -0600 )edit

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

Mat bgr;
cvtColor(image, bgr, COLOR_BGRA2BGR); // CV_8UC3 output now.
// use bgr Mat instead image.
berak gravatar imageberak ( 2016-04-24 08:28:42 -0600 )edit

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!

DoctorJohn gravatar imageDoctorJohn ( 2016-04-24 08:36:14 -0600 )edit