图像bayer格式及bayer插值原理介绍

1 图像bayer格式介绍

Bayer格式图片源自拜耳阵列,拜耳阵列是CCD或CMOS传感器拍摄彩色图像的主要技术之一。拜耳阵列是由伊士曼·柯达公司科学家Bryce Bayer发明的,被广泛运用数字图像。

对于彩色图像,每个像素点可用RGB三种颜色来表示,最简单的采样方法就是在每个像素点上用三个滤镜,红色的滤镜透过红色的波长,绿色的滤镜透过绿色的波长,蓝色的滤镜透过蓝色的波长。这样一来为了采集RGB三个基本色,每个点都需要三块滤镜,这种方式价格昂贵,而且因为三块滤镜都必须保证对齐到同一点,也不好制造。而用bayer格式,就可以很好的解决了这个问题。每个像素点只是用一种颜色的滤镜,另外通过分析人眼对颜色的感知发现,人眼对绿色比较敏感,所以在bayer格式的图片中绿色较多,绿色的像素是是R和B像素的和。

下图是bayer色彩滤波阵列,由一半的G,1/4的R,1/4的B组成。
在这里插入图片描述
                      
  当图像传感器往外逐行输出数据时,像素的序列为GRGRGR…/BGBGBG…(顺序RGB)。这样阵列的Sensor设计,使得RGB传感器减少到了全色传感器的1/3。

2 bayer格式插值算法

每一个像素只有RGB三基色中的一种,必须通过插值来实现每个像素的RGB值。为了从Bayer格式得到每个像素的RGB格式,我们需要通过插值填补缺失的2个色彩。插值的方法有很多,包括邻域、线性等,本文采用最简单的最近邻域算法。
最近邻域算法是以相邻4个点作为运算基础,这种算法实现最简单。如下图所示,奇数行包括green和red颜色的像素,偶数行包括blue和green颜色的像素。奇数列包括green和blue颜色的像素,偶数列包括red和green颜色的像素。
在这里插入图片描述
实现方式采用2行缓冲的处理方式,首先调用RAM作为行buff,经常在图像处理程序中用到。采集进来一系列像素点数据,需要同时对2行像素点同时进行处理时,就可以用2行buff,每个buff数据宽度为图像每行像素点的个数(例如分辨率为1920x1080,buff宽度就为1920)。
在格式转换时,用到4个量,4个量组成一个以dd_0(位于左上角)为核心的模板:
在这里插入图片描述
模板中必定包含一个R、一个B、2个G像素点,那么d_0作为当前像素的R值,dd_1作为当前像素的B值,取2个G的平均值作为当前像素的G值。对这bayer颜色格式图,任取一个颜色的像素作为核心元素套进模板,这样就可以实现图像格式转化了。
每个RGB点相当于图中4个像素点中的交点。因此有根据坐标位置有4种像素排列情况。根据周围4个R、G1、G2、B像素点的值得到对应点的RGB值。

仿真代码及工程已上传。 https://download.csdn.net/download/cjie221/11070198

声明:本设计只作为个人学习交流使用,不做商业用途。

  • 8
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C# Bayer 格式插值算法的实现代码: ```csharp public static void BayerInterpolation(byte[] input, int width, int height, ref byte[] output) { int outputWidth = width * 3; int outputHeight = height * 3; output = new byte[outputWidth * outputHeight]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int outputX = x * 3; int outputY = y * 3; int inputIndex = y * width + x; int outputIndex = outputY * outputWidth + outputX; // Red pixel output[outputIndex] = input[inputIndex]; if (x < width - 1) { output[outputIndex + 3] = (byte)((input[inputIndex] + input[inputIndex + 1]) / 2); } if (y < height - 1) { output[outputIndex + outputWidth * 3] = (byte)((input[inputIndex] + input[inputIndex + width]) / 2); } if (x < width - 1 && y < height - 1) { output[outputIndex + outputWidth * 3 + 3] = (byte)((input[inputIndex] + input[inputIndex + 1] + input[inputIndex + width] + input[inputIndex + width + 1]) / 4); } // Green pixel if (y % 2 == 0) { if (x % 2 == 0) { output[outputIndex + 1] = input[inputIndex + 1]; if (x < width - 1) { output[outputIndex + 2] = (byte)((input[inputIndex] + input[inputIndex + 1] + input[inputIndex + 2]) / 3); } if (y < height - 1) { output[outputIndex + outputWidth * 3 + 1] = (byte)((input[inputIndex] + input[inputIndex + width] + input[inputIndex + width + 1]) / 3); } if (x < width - 1 && y < height - 1) { output[outputIndex + outputWidth * 3 + 4] = (byte)((input[inputIndex] + input[inputIndex + 1] + input[inputIndex + width] + input[inputIndex + width + 1] + input[inputIndex + 2] + input[inputIndex + width + 2]) / 6); } } else { output[outputIndex + 2] = input[inputIndex + 1]; if (y < height - 1) { output[outputIndex + outputWidth * 3 + 1] = (byte)((input[inputIndex] + input[inputIndex + width] + input[inputIndex + width + 1]) / 3); } if (x < width - 1 && y < height - 1) { output[outputIndex + outputWidth * 3 + 2] = (byte)((input[inputIndex] + input[inputIndex + 1] + input[inputIndex + width] + input[inputIndex + width + 1] + input[inputIndex + width * 2] + input[inputIndex + width * 2 + 1]) / 6); } } } else { if (x % 2 == 0) { output[outputIndex + outputWidth] = input[inputIndex + 1]; if (x < width - 1 && y < height - 1) { output[outputIndex + outputWidth * 2 + 3] = (byte)((input[inputIndex] + input[inputIndex + 1] + input[inputIndex + width] + input[inputIndex + width + 1] + input[inputIndex + width * 2] + input[inputIndex + width * 2 + 1]) / 6); } } else { output[outputIndex + outputWidth + 2] = input[inputIndex + 1]; if (x < width - 1) { output[outputIndex + outputWidth + 1] = (byte)((input[inputIndex] + input[inputIndex + 1] + input[inputIndex + 2]) / 3); } if (y < height - 1) { output[outputIndex + outputWidth * 2 + 2] = (byte)((input[inputIndex] + input[inputIndex + width] + input[inputIndex + width + 1]) / 3); } if (x < width - 1 && y < height - 1) { output[outputIndex + outputWidth * 2 + 4] = (byte)((input[inputIndex] + input[inputIndex + 1] + input[inputIndex + width] + input[inputIndex + width + 1] + input[inputIndex + 2] + input[inputIndex + width + 2]) / 6); } } } } } } ``` 这个算法假定输入图像Bayer 格式的,输出图像RGB 格式的。在这个算法中,我们首先定义了输出图像的大小,并初始化了一个输出数组。 接下来,我们使用两个嵌套的循环遍历输入图像的每一个像素。我们计算出每个像素在输出图像中对应的位置,并计算出它在输出数组中的索引。对于每个像素,我们执行以下步骤: 1. 对于红色像素,我们将它的值直接复制到输出图像中,并使用相邻的像素进行插值以得到其他颜色通道的值。 2. 对于绿色像素,我们需要根据它周围的像素值进行插值。具体的插值方式取决于它在 Bayer 格式中的位置。 3. 对于蓝色像素,我们需要根据它周围的像素值进行插值。具体的插值方式取决于它在 Bayer 格式中的位置。 最后,我们返回输出数组,其中包含了经过插值处理后的 RGB 图像

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值