2

what is the meaning of bayer pattern codes in Opencv?

For example I have this code:

CV_BayerGR2BGR

my questions are:

  1. What is the color of first pixel in matrix (so what is the color of pixel 0,0).
  2. what is the color of pixel (x,y)?
  3. Is there any simple and efficient way to find the color of pixel (x,y) based on a specific Bayer pattern? (so extend the code that I am writing to be used with every Bayer pattern).

Update:

As Miki pointed out, OpenCv document says:

The two letters C_1 and C_2 in the conversion constants CV_Bayer C_1 C_2 2BGR and CV_Bayer C_1 C_2 2RGB indicate the particular pattern type. These are components from the second row, second and third columns, respectively. For example, the above pattern has a very popular “BG” type.

which answers my first question, but what about the other two questions?

Community
  • 1
  • 1
mans
  • 17,104
  • 45
  • 172
  • 321
  • 1
    OpenCV doc cover this in [cvtColor](http://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html?#cvtcolor) (at the bottom). Do you need something not explained there? – Miki Nov 10 '15 at 16:38
  • @Miki, see my updated question. – mans Nov 10 '15 at 16:48

2 Answers2

3

Knowing the type of the pattern, COLOR_Bayer<type>2BGR, you can easily find the color of each pixel checking if the coordinates are odd or even, since the pattern is simply a 2x2 that repeats over the whole image.

The OpenCV patterns are:

COLOR_BayerBG2BGR = 46,
COLOR_BayerGB2BGR = 47,
COLOR_BayerRG2BGR = 48,
COLOR_BayerGR2BGR = 49,

COLOR_BayerBG2RGB = COLOR_BayerRG2BGR,
COLOR_BayerGB2RGB = COLOR_BayerGR2BGR,
COLOR_BayerRG2RGB = COLOR_BayerBG2BGR,
COLOR_BayerGR2RGB = COLOR_BayerGB2BGR,    

so you can simply check the first four.

The function

#define BLUE 0
#define GREEN 1
#define RED 2

int getColorFromBayer(int r, int c, int type)

will output the color, given the row r, the column c, and the type of the pattern type.

The following code shows how to recover the color of each pixel, and generates a BGR color image of the Bayer pattern.

#include <opencv2\opencv.hpp>
using namespace cv;


//COLOR_BayerBG2BGR = 46,
//COLOR_BayerGB2BGR = 47,
//COLOR_BayerRG2BGR = 48,
//COLOR_BayerGR2BGR = 49,
//
//COLOR_BayerBG2RGB = COLOR_BayerRG2BGR,
//COLOR_BayerGB2RGB = COLOR_BayerGR2BGR,
//COLOR_BayerRG2RGB = COLOR_BayerBG2BGR,
//COLOR_BayerGR2RGB = COLOR_BayerGB2BGR,

#define BLUE 0
#define GREEN 1
#define RED 2

int getColorFromBayer(int r, int c, int type)
{
    static int bg[] = { RED, GREEN, GREEN, BLUE };
    static int gb[] = { GREEN, RED, BLUE, GREEN };
    static int rg[] = { BLUE, GREEN, GREEN, RED };
    static int gr[] = { GREEN, BLUE, RED, GREEN };

    int rr = r % 2;
    int cc = c % 2;

    switch (type)
    {
    case COLOR_BayerBG2BGR: return bg[2 * rr + cc];
    case COLOR_BayerGB2BGR: return gb[2 * rr + cc];
    case COLOR_BayerRG2BGR: return rg[2 * rr + cc];
    case COLOR_BayerGR2BGR: return gr[2 * rr + cc];
    }
    return -1;
}

int main()
{
    Mat3b bayer(10,10, Vec3b(0,0,0));

    // Create bayer pattern BG

    for (int r = 0; r < bayer.rows; ++r)
    {
        for (int c = 0; c < bayer.cols; ++c)
        {
            int color = getColorFromBayer(r,c,COLOR_BayerBG2BGR);

            switch (color)
            {
            case BLUE : bayer(r, c) = Vec3b(255, 0, 0); break;
            case GREEN: bayer(r, c) = Vec3b(0, 255, 0); break;
            case RED  : bayer(r, c) = Vec3b(0, 0, 255); break;
            }
        }
    }

    return 0;
}
Miki
  • 40,887
  • 13
  • 123
  • 202
0

Something like this pseudocode?
(note that CV_BayerBG2RGB = CV_BayerRG2BGR and work with one triplet type only )

if ((x+y) && 1) <> (CV_Bayerxxxxxx && 1)) then
  C(x,y) = G
else 
  if (Pattern is CV_BayerBGxxx || CV_BayerGBxxx) == (y && 1) then
     C(x,y) = B
  else
     C(x,y) = R
MBo
  • 77,366
  • 5
  • 53
  • 86