First time here? Check out the FAQ!

Ask Your Question
0

How to convert from BGR to BayerRG

asked Nov 23 '16

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?

Preview: (hide)

Comments

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

Balaji R gravatar imageBalaji R (Nov 24 '16)edit

1 answer

Sort by » oldest newest most voted
3

answered Nov 28 '16

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;
}
Preview: (hide)

Comments

i f you need faster program use parallelLoopBody

LBerger gravatar imageLBerger (Nov 28 '16)edit

Question Tools

1 follower

Stats

Asked: Nov 23 '16

Seen: 4,822 times

Last updated: Nov 28 '16