# How to convert from BGR to BayerRG

I am working with a camera with an integrated vision system. I would like to use openCV to generate test images and upload these test images to the camera to evaluate the performance of the system. I have already been able to generate some useful test images but the camera assumes the picture being uploaded is a BayerBG pattern.

I noticed cvtColor can convert a Bayer pattern to BGR but I would like to do the exact opposite. As far as I can tell from the documentation there is no way to convert from BGR to Bayer.

Is there an other easy way to do this?

edit retag close merge delete

No there is no OpenCV function to do this! You have to implement by yourself!

( 2016-11-23 21:32:12 -0600 )edit

Sort by ยป oldest newest most voted

Thanks for your response Balaji R

In case anyone is interested here is how I solved it:

Mat ConvertBGR2Bayer(Mat BGRImage)   {

/*
Assuming a Bayer filter that looks like this:

# // 0  1  2  3  4  5
/////////////////////
0 // B  G  B  G  B  G
1 // G  R  G  R  G  R
2 // B  G  B  G  B  G
3 // G  R  G  R  G  R
4 // B  G  B  G  B  G
5 // G  R  G  R  G  R

*/

Mat BayerImage(BGRImage.rows, BGRImage.cols, CV_8UC1);

int channel;

for (int row = 0; row < BayerImage.rows; row++)
{
for (int col = 0; col < BayerImage.cols; col++)
{
if (row % 2 == 0)
{
//even columns and even rows = blue = channel:0
//even columns and uneven rows = green = channel:1
channel = (col % 2 == 0) ? 0 : 1;
}
else
{
//uneven columns and even rows = green = channel:1
//uneven columns and uneven rows = red = channel:2
channel = (col % 2 == 0) ? 1 : 2;
}

BayerImage.at<uchar>(row, col) = BGRImage.at<Vec3b>(row, col).val[channel];
}
}

return BayerImage;
}

more

i f you need faster program use parallelLoopBody

( 2016-11-28 03:11:17 -0600 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2016-11-23 09:01:40 -0600

Seen: 4,272 times

Last updated: Nov 28 '16