Ask Your Question

How do I capture, process and output an image in RGB (not BGR)?

asked 2014-05-23 07:42:11 -0500

Magicbean gravatar image

I have read that opencv captures images in BGR rather than RGB. In my first experiments I am capturing a video image and output it as follows:

(C++ code snippet - error checking and control flow left out for clarity):

Mat frame;
VideoCapture capture(0);

capture >> frame;
/* some processing code here*/
imshow(window_name, frame);

The code works except that the capture works in BGR and the output of imshow() is RGB which means that the red and blue channels are swapped in the display.

So... I can work with data in BGR but I would like to know if it is possible to a) capture in RGB (like raspistill does) or b) convert to RGB before display without a nested loop to convert each pixel?

(This is running on a Raspberrry Pi with opencv 2.3.1 and the Video4Linux2 Driver v1.9.8 in case it makes any difference).


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-05-23 12:05:57 -0500

isarandi gravatar image

updated 2014-05-23 12:10:46 -0500

imshow expects BGR images. If the result is messed up, I think the problem is with the VideoCapture. Maybe VideoCapture gives RGB? Anyway you can use cv::cvtColor to convert between BGR and RGB.

cv::cvtColor(rgbMat, bgrMat, cv::COLOR_RGB2BGR);
cv::cvtColor(bgrMat, rgbMat, cv::COLOR_BGR2RGB);

Disclaimer: I only have experience with OpenCV on desktop.

edit flag offensive delete link more

Question Tools


Asked: 2014-05-23 07:42:11 -0500

Seen: 5,811 times

Last updated: May 23 '14