Bre*_*mon 3 pixelformat scikit-image
我有一台可以提供拜耳 RG8 格式图像的相机。
我正在使用 skimage 处理图像,但我找不到将 Bayer RG8 格式转换为标准 RGB(以在屏幕上显示)的方法。
有什么办法可以用 skimage 做到这一点吗?
我确实找到了 opencv 转换的参考,但我试图避免在我的应用程序中包含 opencv (除非绝对必要)。
由于您没有提供任何输入数据,我从这里获取了灰度图像,并使用ImageMagick将其制作成带有 GBRG 排序的原始 Bayer8 文件,如下所示:
magick mandi.png -trim -depth 8 gray:bayer.bin
Run Code Online (Sandbox Code Playgroud)
这给了我一个 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)
Run Code Online (Sandbox Code Playgroud)
并得到这个:
版权所有 Mathworks, Inc.
当然,还有更复杂的插值方法,但这是最基本的,欢迎您采用并改进它!
好的,我有一些时间,我尝试对拜耳数组中的缺失值进行二维插值。我对我的答案不是 100% 有信心,但我认为它应该非常接近。
基本上,我以全分辨率复制原始拜耳阵列,并用红色覆盖所有绿色和蓝色样本并将np.Nan其称为红色。然后我进行二维插值来替换 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)))
Run Code Online (Sandbox Code Playgroud)
关键词:Python、bayer、bayer8、debayer、de-bayer、去马赛克、去马赛克、图像、raw、CFA、skimage、scikit-image、图像处理。
| 归档时间: |
|
| 查看次数: |
11199 次 |
| 最近记录: |