【问题标题】:how to convert bayerrg8 format image to rgb image如何将bayerrg8格式图像转换为rgb图像
【发布时间】:2020-02-29 11:40:36
【问题描述】:

我有一台可以提供 Bayer RG8 格式图像的相机。

我正在使用 skimage 处理图像,但我无法将 Bayer RG8 格式转换为标准 RGB(以显示在屏幕上)。

有什么方法可以用 skimage 做到这一点吗?

我确实找到了对 opencv 转换的参考,但我试图避免在我的应用程序中包含 opencv(除非绝对必要)。

【问题讨论】:

  • 您有示例数据文件要分享吗?你有一些首选算法吗,因为有很多...en.m.wikipedia.org/wiki/Demosaicing
  • 您还应该澄清您的传感器布局 - 可能是'gbrg' | 'grbg' | 'bggr' | 'rggb'
  • 我的回答解决了您的问题吗?如果是这样,请考虑接受它作为您的答案 - 通过单击计票旁边的空心对勾/复选标记。如果没有,请说出什么不起作用,以便我或其他人可以进一步为您提供帮助。谢谢。 meta.stackexchange.com/questions/5234/…

标签: scikit-image pixelformat


【解决方案1】:

由于您没有提供任何输入数据,我从here 获取了灰度图像,并使用 ImageMagick 将其制成具有 GBRG 排序的原始 Bayer8 文件,如下所示:

magick mandi.png -trim -depth 8 gray:bayer.bin

这给了我一个 680,736 字节的 1013x672 像素文件。

然后我是这样读的,然后做成这样skimage能看懂的图片:

#!/usr/bin/env python3

import numpy as np
from skimage.io import imsave

# Width and height of Bayer image, not original which is w/2 x h/2
w, h = 1013, 672
ow, oh = w//2, h//2

# Load in Bayer8 image, assumed raw 8-bit GBRG
bayer = np.fromfile('bayer.bin', dtype=np.uint8).reshape((h,w))

# Pick up raw uint8 samples
R  = bayer[1::2, 0::2]     # rows 1,3,5,7 columns 0,2,4,6
B  = bayer[0::2, 1::2]     # rows 0,2,4,6 columns 1,3,5,7
G0 = bayer[0::2, 0::2]     # rows 0,2,4,6 columns 0,2,4,6
G1 = bayer[1::2, 1::2]     # rows 1,3,5,7 columns 1,3,5,7

# Chop any left-over edges and average the 2 Green values
R = R[:oh,:ow]
B = B[:oh,:ow]
G = G0[:oh,:ow]//2 + G1[:oh,:ow]//2

# Formulate image by stacking R, G and B and save
out = np.dstack((R,G,B)) 
imsave('result.png',out)

得到这个:

版权所有 Mathworks, Inc.

当然,还有更复杂的插值方法,不过这是最基本的,欢迎大家采纳和改进!


好的,我有时间尝试对 Bayer 数组中的缺失值进行 2d 插值。我对我的答案不是 100% 有信心,但我认为应该非常接近。

基本上,我以全分辨率复制原始拜耳阵列,并用np.Nan 覆盖所有绿色和蓝色样本并将其命名为红色。然后我做一个 2d 插值来替换 Nans。

绿色和蓝色也一样,就是这样:

#!/usr/bin/env python3

import numpy as np
from skimage.io import imsave
from scipy.interpolate import griddata

def interp2d(im):
    """Interpolate in 2d array, replacing NaNs with interpolated values"""
    x, y = np.indices(im.shape)
    im[np.isnan(im)] = griddata(
       (x[~np.isnan(im)], y[~np.isnan(im)]),
       im[~np.isnan(im)],
       (x[np.isnan(im)], y[np.isnan(im)]))
    im = np.nan_to_num(im)
    return np.clip((im),0,255)

# Width and height of Bayer image
w, h = 1013, 672

# Calculate output width and height as multiples of 4
ow = (w//4) * 4
oh = (h//4) * 4

# Load in Bayer8 image, assumed raw 8-bit GBRG, reshape and make sides multiple of 4
bayer = np.fromfile('bayer.bin', dtype=np.uint8).reshape((h,w)).astype(np.float)[:oh, :ow]

# In following code you'll see "cell" which is the basic repeating 2x2 cell of a Bayer matrix
#
# cell = G B
#        R G
#

# Set everything not Red in bayer array to Nan, then replace Nans with interpolation
cell = np.array([[np.NaN, np.NaN],
                 [1.0   , np.NaN]])
R = bayer*np.tile(cell,(oh//2,ow//2))
R = interp2d(R).astype(np.uint8)

# Set everything not Green in bayer array to Nan, then replace Nans with interpolation
cell = np.array([[1.0   , np.NaN],
                 [np.NaN, 1.0   ]])
G = bayer*np.tile(cell,(oh//2,ow//2))
G = interp2d(G).astype(np.uint8)

# Set everything not Blue in bayer array to Nan, then replace Nans with interpolation
cell = np.array([[np.NaN, 1.0   ],
                 [np.NaN, np.NaN]])
B = bayer*np.tile(cell,(oh//2,ow//2))
B = interp2d(B).astype(np.uint8)

# Form image by stacking R, G and B and save
imsave('result.png',np.dstack((R,G,B)))

关键字:Python、bayer、bayer8、debayer、de-bayer、de-mosaic、de-mosaicking、image、raw、CFA、skimage、scikit-image、图像处理。

【讨论】:

  • 嗨,马克。我想我喜欢第二种方法感觉更好。我实际上是在考虑获取红色像素,然后调用resize 函数将尺寸加倍(假设发生某种插值)。对蓝色像素执行相同操作。我不知道如何对绿色像素做类似的事情(也许它们就可以了?)。最后堆叠RGB数据。对此有什么想法吗?
  • 这正是我的第一种方法所做的,除了我没有在末尾添加resize() 以使其高度和宽度加倍。至于在每个像素位置的绿色通道中有两个样本,我只是将它们平均,然后将它们与红色和蓝色相同。
  • 是的,明白了。我试图通过保留原始的绿色部分来变得可爱,并填补缺失的绿色部分。它有效,但是当我放大时有一个 # 模式。第一种方法实际上比我的方法对我产生了更好的结果。我没有用手工插值尝试你的方法。
猜你喜欢
  • 2021-12-15
  • 2021-12-17
  • 1970-01-01
  • 2017-08-02
  • 2020-07-03
  • 2015-08-15
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
相关资源
最近更新 更多