opencv Bayer图案,带Python皮

kxeu7u2r  于 2022-11-15  发布在  Python
关注(0)|答案(1)|浏览(195)

首先感谢阅读我的问题。
我想把白色拜耳图案图像改成彩色拜耳图案图像。

我得到了上面的图片与拜耳模式与下面的代码。

import numpy as np

im = cv2.imread('/content/drive/MyDrive/Colab Notebooks/Resources/lena.bmp')
im = cv2.resize(im,None,fx=1/3,fy=1/3,interpolation=cv2.INTER_NEAREST)
(height, width) = im.shape[:2]
(B,G,R) = cv2.split(im)

bayer = np.empty((height, width), np.uint8)

# strided slicing for this pattern:
#   G R
#   B G
bayer[0::2, 0::2] = G[0::2, 0::2] # top left
bayer[0::2, 1::2] = R[0::2, 1::2] # top right
bayer[1::2, 0::2] = B[1::2, 0::2] # bottom left
bayer[1::2, 1::2] = G[1::2, 1::2] # bottom right
bayer = cv2.resize(bayer,None,fx=3,fy=3,interpolation=cv2.INTER_NEAREST)
cv2.imwrite('/content/drive/MyDrive/Colab Notebooks/Resources/bayer.png',bayer

我没有编码任何没有颜色的东西,但我的结果没有颜色。我不明白为什么。谁能告诉我,我如何从我的图像颜色,但与拜耳过滤器?
我想要的是像使图像2作为图像3。

dw1jzc5e

dw1jzc5e1#

看来你的方向是对的...
当NumPy数组bayer是2D数组时,我们无法获得彩色输出。
为了获得彩色图像,我们希望它是3D阵列(应用BGR像素格式)。
我们还必须考虑灰度像素应用r=g=B,而我们想要的输出应用r!=g!=b。
为了获得所需的输出,我们可以在使用cv2.resize之前使用以下阶段:

  • bayer从灰度转换为BGR(该转换仅使每个像素的r=g=b):
bayer = cv2.cvtColor(bayer, cv2.COLOR_GRAY2BGR)
  • “棘手部分”:

对于所有“绿色Bayer像素”,将红色和蓝色值设置为零。
对于所有“红色拜耳像素”,将绿色和蓝色值设置为零。
对于所有“蓝色Bayer像素”,将绿色和红色值设置为零。

bayer[0::2, 0::2, 0::2] = 0  # Green pixels - set the blue and the red planes to zero (and keep the green)
 bayer[0::2, 1::2, 0:2] = 0   # Red pixels - set the blue and the green planes to zero (and keep the red)
 bayer[1::2, 0::2, 1:] = 0    # Blue pixels - set the red and the green planes to zero (and keep the blue)
 bayer[1::2, 1::2, 0::2] = 0  # Green pixels - set the blue and the red planes to zero (and keep the green)

将相关颜色通道设置为零后,调整彩色bayer图像的大小。
注意事项:
将颜色通道设置为零仅用于演示目的。
放置零在数学上是不正确的,因为它会使像素变暗很多。
代码示例:

import numpy as np
import cv2

im = cv2.imread('lena.bmp')
im = cv2.resize(im,None,fx=1/3,fy=1/3,interpolation=cv2.INTER_NEAREST)
(height, width) = im.shape[:2]
(B,G,R) = cv2.split(im)

bayer = np.empty((height, width), np.uint8)

# strided slicing for this pattern:
#   G R
#   B G
bayer[0::2, 0::2] = G[0::2, 0::2] # top left
bayer[0::2, 1::2] = R[0::2, 1::2] # top right
bayer[1::2, 0::2] = B[1::2, 0::2] # bottom left
bayer[1::2, 1::2] = G[1::2, 1::2] # bottom right
#bayer = cv2.resize(bayer, None, fx=3, fy=3, interpolation=cv2.INTER_NEAREST)

bayer = cv2.cvtColor(bayer, cv2.COLOR_GRAY2BGR)  # Convert from Grayscale to BGR (r=g=b for each pixel).
bayer[0::2, 0::2, 0::2] = 0  # Green pixels - set the blue and the red planes to zero (and keep the green)
bayer[0::2, 1::2, 0:2] = 0   # Red pixels - set the blue and the green planes to zero (and keep the red)
bayer[1::2, 0::2, 1:] = 0    # Blue pixels - set the red and the green planes to zero (and keep the blue)
bayer[1::2, 1::2, 0::2] = 0  # Green pixels - set the blue and the red planes to zero (and keep the green)

bayer = cv2.resize(bayer, None, fx=3, fy=3, interpolation=cv2.INTER_NEAREST)

cv2.imwrite('bayer.png', bayer)

注意事项:
我们可以通过将im的通道设置为零来获得相同的效果,但这有点不得要领,因为输入应该是Bayer格式。
结果:

相关问题