天天看点

opencv中Bayer 图像到RGB图像装换的问题

在将bayer图像转换成为rgb的时候遇到的问题

Mat bayer = imread("/home/pan/Desktop/data/Testaufbau_Ecoflac153.tif",-1);
    Mat bayer2rgb;
    bayer2rgb.create(bayer.rows,bayer.cols,CV_8UC3);
    cvtColor(bayer,bayer2rgb,CV_BayerRG2BGR);
    vector<Mat> difChan;
    split(bayer2rgb,difChan);
//    imshow("Bayer Pattern",difChan[0]);
    imwrite("img_b.jpg",difChan[0]);
    imwrite("img_r.jpg",difChan[1]);
    imwrite("img_g.jpg",difChan[2]);
           

一直解决不了的问题出现在imread的使用上。

bayer图像是 one channel的图像,

如果简单的用imread,用defualt的参数的话,读出来的是3 channels的matrix。

而 cvtColor(source, destination, CV_BayerRG2BGR) 是将one channel 转换成 3 channel 图像的。

解决办法就是把imread 的参数设为0 或者 -1

此处的bayer pattern filter 需要从camera处获取

具体bayer pattern 介绍如下

copy 自:http://www.tldp.org/HOWTO/libdc1394-HOWTO/concepts.html

4.6. How to get color images: Bayer Pattern Concepts

The image grabbed by the sample code in the previous section is not colored (we have intentionally used the words "not colored," since the image is not gray-scale either). It is actually a Bayer Pattern. We will give an overview of Bayer Patterns and how they are used to get a colored image in this section.

Digital cameras use a solid-state device called an image sensor. These fingernail-sized silicon chips contain millions of photosensitive diodes called photosites. When you take a picture with a digital camera, the intensity of light hitting each photo site on the sensor is recorded as a signal. Depending on the camera, either 12 or 14 bits of data are recorded. At 12 bits, the camera can record 4,096 levels of brightness. At 14 bits, the camera can record 16,384 levels of brightness. This is referred to as bit depth. The higher the bit depth, the finer is the detail, the smoother the transition between tones, and the higher the dynamic range (the ability of the camera to hold detail in both highlighted and shadowed areas). But at capture, digital images are grayscale, not color. To record color information, each pixel on the sensor is covered with a red, green, or blue filter, with the colors alternating. A common arrangement of color filters is the Bayer Pattern array that alternates colors, but that also uses twice as many green filters as red and blue. Twice as many green filters are used because our eyes are more sensitive to green. This pattern, or sequence, of filters can vary, but the widely adopted Bayer Pattern, which was invented at Kodak, is a repeating 2x2 arrangement. Each pixel has been made sensitive only to one color (one spectral band).

A Typical Bayer Pattern will look like this:

Figure 5. Bayer Pattern

opencv中Bayer 图像到RGB图像装换的问题

The tile or square (pixel) labeled B means this particular tile is sensitive only to Blue light, and so on.

The Bayer Patterns may be classified into 4 types, depending on how we have arranged the colors. The naming of the Bayer Pattern is done by taking a 2x2 matrix from the top most corner of the pattern and the colors being read in (0,0),(0,1),(1,0),(1,1) order. So for the above Bayer Pattern, if we take the 2x2 matrix as:

Figure 6. BGGR Pattern

opencv中Bayer 图像到RGB图像装换的问题

The pattern is therefore known as BGGR

The other possible patterns are:

Figure 7. Other Patterns

opencv中Bayer 图像到RGB图像装换的问题

The image we obtained in the previous example was a Bayer Pattern image also known as a RAW image. This was stored in camera.capture_buffer. In order to view what we have captured we convert this RAW image to .PGM by adding a header (look at the explanation in Section 4.4).

In order to get a colored image, the Bayer Pattern image is converted to a RGB image. A RGB image is an enhanced version of the Bayer Pattern image; we try to find the value of the two missing colors at each pixel (remember that each pixel of the sensor is covered by Bayer Pattern filter so we get a single color at any pixel by default). This is done by using different algorithms like Nearest Neighbor, Edge Sense, and so on:

Figure 8. RAW to RGB

opencv中Bayer 图像到RGB图像装换的问题

where the shaded values are to be calculated by the algorithm. Subscript denotes the tile on the Bayer Pattern to which the value of R, G, and B belongs. Note that the image size will become 3 times the Bayer Pattern. In order to view the RGB image we convert it to a Bit Map, or .BMP image, by adding a bitmap header.

To get a clear picture of what's happening, we have provided the following diagram:

继续阅读