Bayer转换成BGR

关于相机像素单元究竟得到了什么,可以看一下这个东西闲谈CCD

Bayer格式是相机内部的原始图片一般后缀名为.raw,一般bayer格式的图片绿色格式的像素是rg像素的和。很多软件都可以查看比如PS。我们相机拍照下来存储在存储卡上的.jpeg或其它格式的图片都是从.raw格式转化过来的。如下图,为bayer色彩滤波阵列,由一半的G1/4R1/4B组成。


1、bayer格式插值红蓝算法实现

每一个像素仅仅包括了光谱的一部分,必须通过插值来实现每个像素的RGB值。为了从Bayer格式得到每个像素的RGB格式,我们需要通过插值填补缺失的2个色彩。插值的方法有很多(包括领域、线性、3*3等),速度与质量权衡,最好的线性插值补偿算法。其中算法如下: 

 RB通过线性领域插值,但这有四种不同的分布,如下图所示:


在(a)与(b)中,RB分别取邻域的平均值。

在(c)与(d)中,取领域的4BR的均值作为中间像素的B值。 

扫描二维码关注公众号,回复: 1964856 查看本文章

2、bayer格式插值绿算法实现

(e) (f)

由于人眼对绿光反应最敏感,对紫光和红光则反应较弱,因此为了达到更好的画质,需要对G特殊照顾。在上述(c)与(d)中,扩展开来就是上图的(e)与(f)中间像素G的取值,者也有一定的算法要求,不同的算法效果上会有差异。经过相关的研究,

e)中间像素G值的算法如下:

(f)中间像素G值的算法如下:


CMOS摄像头这部分转换是在内部用ADC或者ISP完成的,生产商为了降低成本必然会使得图像失真。当然用外部处理器来实现转换,如果处理器的速度足够NB,能够胜任像素的操作,用上面的算法来进行转换,皆大欢喜。不过上述算法将直接成倍提高了算法的复杂度,速度上将会有所限制。因此为了速度的提成,可以直接通过来4领域G取均值来中间像素的G值,将会降低一倍的速率,而在性能上差之甚微,算法如下: 


如果能够通过损失图像的额质量,来达到更快的速度,还可以取G1G2的均值来实现,但是这样的做法会导致边沿以及跳变部分的失真。 


opencv中实现bayer转BGR的函数原型:

C++: void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )

Parameters
src – input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision floating-point.
dst – output image of the same size and depth as src.
code – color space conversion code (see the description below).
dstCn – number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code .

Bayer → RGB ( CV_BayerBG2BGR, CV_BayerGB2BGR, CV_BayerRG2BGR, CV_BayerGR2BGR,CV_BayerBG2RGB, CV_BayerGB2RGB, CV_BayerRG2RGB, CV_BayerGR2RGB ). The Bayer pattern is widely used in CCD and CMOS cameras. It enables you to get color pictures from a single plane where R,G,and B pixels (sensors of a particular component) are interleaved as follows:


The output RGB components of a pixel are interpolated from 1, 2, or 4 neighbors of the pixel having the same  color. There are several modifications of the above pattern that can be achieved by shifting the pattern one pixel  left and/or one pixel up. The two letters C1  and C2  in the conversion constants CV _ Bayer C1C2  2BGR and  CV _ Bayer C1C2  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.

例:opencv程序实现bayer转BGR

程序:


   
   
  1. Mat bayerImage = imread( "cam.bmp", 2| 4);
  2. Mat bgrImage;
  3. cvtColor(bayerImage,bgrImage,CV_BayerGR2BGR);


原图


结果:



参考:http://www.cnblogs.com/qiqibaby/p/5267566.html

        </div>

猜你喜欢

转载自blog.csdn.net/renhaofan/article/details/80962789