Ask Your Question

Why does opencv swap color channel when we explicitly mention CV_BayerRG2RGB?

asked 2018-04-26 05:31:30 -0500

Arumugam gravatar image

I am converting bayer raw images into 3 channel color image using cvtColor() API. Though I am explicitly mentioning the result image in RGB (CV_BayerRG2RGB) opencv returns image in BGR format. Is this a bug or expected behavior? if expected, then should i convert it again using CV_BGR2RGB?

edit retag flag offensive close merge delete


Without code it is difficult to say if it is a bug or not. How do you display your image?

LBerger gravatar imageLBerger ( 2018-04-26 08:07:53 -0500 )edit

For example, from camera i get raw bayer RG image buffer. I created a Mat object and pointing to bayer image buffer something like below,

Mat bayerImg = Mat(1200,1600, CV_8UC1, bayerBuffer); Mat rgbImg = Mat(1200,1600, CV_8UC3); cvtColor(bayerImg, rgbImg, COLOR_BayerRG2RGB_EA);

If i extract color channels from the above rgbImg, i get Blue channel first and Red channel last. I don't want to display it. I want to consume the buffer inside my application.

Here i am requesting for RGB why does opencv return BGR image?

Arumugam gravatar imageArumugam ( 2018-04-28 01:41:14 -0500 )edit

in issue you can find :

and all constants are here Hence RGB space seems equal to BGR space.

You can post an issue and gives other issue in reference and this post too

LBerger gravatar imageLBerger ( 2018-04-28 07:05:35 -0500 )edit

Do you use opencv 3.0 and vs 2010?

LBerger gravatar imageLBerger ( 2018-04-28 13:56:30 -0500 )edit

Yes Berger.

Arumugam gravatar imageArumugam ( 2018-04-28 20:00:24 -0500 )edit

You should update to 3.4.1

LBerger gravatar imageLBerger ( 2018-04-29 03:15:33 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-07-01 12:10:08 -0500

Hi everyone, i am having the same problem described above using the OpenCV version 3.4.3. In my c++ application I am using the function cvtColor (with code CV_BayerRG2RGB) to convert a Bayer RG image to an RGB image. The image I get in output is displayed correctly using cv::imshow but, using the viewer in my app or any other third party viewer (for example ImageJ), I observe that the image channels are swapped compared to what is expected (in other words the image is BGR). Conversely, using the conversion code CV_BayerRG2BGR, I display the image correctly in my RGB Viewer. Can you explain me why??

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-04-26 05:31:30 -0500

Seen: 681 times

Last updated: Apr 26 '18