我有一个相机,提供拜耳RG8格式的图像。
我使用skimage来处理图像,但我找不到其他方法将拜耳RG8格式转换为标准RGB (显示在屏幕上)。
有没有办法用skimage做这件事?
我确实找到了一个关于opencv转换的参考,但我试图避免将opencv包含在我的应用程序中(除非这是绝对必要的)。
发布于 2019-11-04 12:48:37
由于您没有提供任何输入数据,所以我从这里获取灰度图像,并将其放入一个原始Bayer8文件中,使用ImageMagick进行GBRG排序,如下所示:
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公司
当然,有更复杂的插值方法,但这是最基本的,欢迎您采取并改进它!
好的,我有一些时间,我试着做一个2d插值,在拜耳数组中缺失的值。我对我的回答没有百分之百的信心,但我认为应该是相当接近的。
基本上,我以完全分辨率复制原始的拜耳数组,并用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,debayer,去马赛克,去镶嵌,图像,原始,CFA,skimage,scikit图像,图像处理.
https://stackoverflow.com/questions/58688633
复制相似问题