gpt4 book ai didi

scikit-image - 如何将bayerrg8格式图片转成rgb图片

转载 作者:行者123 更新时间:2023-12-02 00:11:53 28 4
gpt4 key购买 nike

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

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

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

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

最佳答案

由于您没有提供任何输入数据,我从 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)

得到这个:

enter image description here

版权所有 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)))

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

关于scikit-image - 如何将bayerrg8格式图片转成rgb图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58688633/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com