Ask Your Question
0

How to convert from BGR to BayerRG

asked 2016-11-23 09:05:28 -0600

Thomas Gooijers gravatar image

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 flag offensive close merge delete

Comments

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

Balaji R gravatar imageBalaji R ( 2016-11-23 21:32:12 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-11-28 02:46:52 -0600

Thomas Gooijers gravatar image

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;
}
edit flag offensive delete link more

Comments

i f you need faster program use parallelLoopBody

LBerger gravatar imageLBerger ( 2016-11-28 03:11:17 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 4,207 times

Last updated: Nov 28 '16