我有一台提供拜耳RG8格式图像的相机。
我正在使用skimage来处理图像,但无法找到将Bayer RG8格式转换为标准RGB(以在屏幕上显示)的方法。
有什么办法可以用skimage做到这一点吗?
我确实找到了有关opencv转换的参考,但我试图避免在我的应用程序中包含opencv(除非绝对必要)。
答案 0 :(得分: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%的信心,但我认为它应该非常接近。
基本上,我以全分辨率复制原始的Bayer数组,并用np.Nan
覆盖所有绿色和蓝色样本,并将其称为Red。然后我进行二维插值替换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,图像处理。