scipy 如何在图像上实现插值算法的这个修改版本(Bayer滤波)?

xa9qqrwz  于 2023-03-02  发布在  其他
关注(0)|答案(2)|浏览(234)

我正在尝试实现插值算法的修改版本,其中所有来自有效邻域的值被平均。例如,在绿色通道中:see
但我想我在填写缺失值时遇到了问题。
Here is my output
This is the output I'm expecting
我似乎得到了这些网格线的输出,这是-我猜,丢失像素。我不知道我在哪里犯了这个错误,所以任何帮助将不胜感激!
下面是我的代码:

# read image
    img = io.imread(filename_Grayimage, as_gray =True)
    h,w = img.shape

    # set figure size of the plot 
    plt.figure(figsize=(10,8))

    # our final image will be a 3 dimensional image with 3 channels
    rgb = np.zeros((h,w,3),np.uint8)

    # reconstruction of the green channel IG
    IG = np.copy(img) # copy the image into each channel
    for row in range(0,h,4): # loop step is 4 since our mask size is 4.
        for col in range(0,w,4): # loop step is 4 since our mask size is 4.
            # Shaded Image Locations
            gA = int(img[row, col])
            gC = int(img[row, col+2])
            gF = int(img[row+1, col+1])
            gH = int(img[row+1, col+3])
            gI = int(img[row+2, col])
            gK = int(img[row+2, col+2])
            gN = int(img[row+3, col+1])
            gP = int(img[row+3, col+3])
    
            # Equations for valid neighbours
            # B = (A+C+F)/3
            # D = (C+H)/2
            # E = (A+F+I)/3
            # G = (F+C+H+K)/4
            # J = (F+I+K+N)/4
            # L = (H+K+P)/3
            # M = (I+N)/2
            # O = (N+K+P)/2

            # TODO: compute pixel value for each location where mask is unshaded (0) 
            # interpolate each pixel using its every valid (shaded) neighbors
            IG[row, col+1] = (gA+gC+gF)/3  # B
            IG[row, col+3] = (gC+gH)/2 # D
            IG[row+1, col+1] = (gA+gF+gI)/3 # E
            IG[row+1, col+2] = (gF+gC+gH+gK)/4 # G
            IG[row+2, col+2] = (gF+gI+gK+gN)/4 # J 
            IG[row+2, col+3] = (gH+gK+gP)/3 # L
            IG[row+3, col] = (gI+gN)/2 # M
            IG[row+3, col+2] = (gN+gK+gP)/2 # O

    # [x] TODO: show green (IR) in first subplot (221) and add title - refer to rgb one for hint on plotting
    plt.subplot(221)
    plt.imshow(IG, cmap='gray')
    plt.title('IG')
eblbsuwk

eblbsuwk1#

图表是“超级混乱”,因为它显示了图像的赏金。
不在边缘中的所有红色/蓝色像素具有4个有效的绿色相邻像素。
根本不应该有3个邻居的平均值(除了边缘)。
看一下G(假设为红色像素):
x一米一纳米一x =(x一米二纳米一x + x一米三纳米一x + x一米四纳米一x + x一米五纳米一x)/ 4

看一下J(假设为蓝色像素):
x一米七氮一x =(x一米八氮一x + x一米九氮一x + x一米十氮一x + x一米十一氮一x)/ 4

除页边空白外,我们可适用下列规则:

  • 对于每个偶数行(0,2,4...),我们用4个相邻像素的平均值替换偶数列(0,2,4...)中的像素。
  • 对于每个奇数行(1,3,5...),我们用4个相邻像素的平均值替换奇数列(1,3,5...)中的像素。

处理边距:
为了简化,我们可以在图像的顶部填充一行,底部填充一行,左侧填充一列,右侧填充一列。
在填充图像中,每个红/蓝像素有4个“有效”的绿色相邻像素。
对于填充,我们采用第二相邻行/列(而不是第一相邻行/列)以保持正确的Bayer对齐。
您没有发布输入图像...我创建了以下图像与GRBG拜耳对齐测试:

代码示例:

import imageio as io
from matplotlib import pyplot as plt
import numpy as np

# Bayer pattern of peppers_bayer.png is GRBG
filename_Grayimage = 'peppers_bayer_grbg.png'

# read image
img = io.imread(filename_Grayimage, as_gray=True)
img = img.astype(np.uint8)  # imageio reads the data float type - convert the type to uint8 (working with integer pixels).
h, w = img.shape

# set figure size of the plot 
plt.figure(figsize=(10, 8))

# our final image will be a 3 dimensional image with 3 channels
#rgb = np.zeros((h, w, 3), np.uint8)

# Add padding of 1 row at the top, one row at the bottom, one column at the left and on at the right.
img = np.pad(img, ((1, 1), (1, 1)))
img[:, 0] = img[:, 2]  # Copy the second column (not the first) for keeping the Bayer pattern correct.
img[:, -1] = img[:, -3]
img[0, :] = img[2, :]
img[-1, :] = img[-3, :]

# reconstruction of the green channel IG
IG = np.copy(img) # copy the image into each channel

# Size after padding is w+2 by h+2
w += 2
h += 2

for row in range(1, h-1, 2): # loop steps of 2 - each iteration applies one even and one odd row.
    # Padding - lower case.
    # Original pixels - upper case
    #
    # -------------- padding
    # |        |
    # V        V
    # gbgbgbgbgg <-- padding
    # rGRGRGRGRr
    # gBGBGBGBGg
    # rGRGRGRGRr
    # gBGBGBGBGg
    # rgrgrgrgrr <-- padding

    # Odd row:
    # Note: first row is odd, because row starts from 1
    for col in range(2, w-1, 2):  # Odd row: Start from col=2, because pixel in col=1 is green
        #  G 
        # G*G
        #  G
        C = int(img[row-1, col])
        E = int(img[row, col-1])
        F = int(img[row, col+1])
        H = int(img[row+1, col])
        IG[row, col] = np.round((C + E + F + H) / 4)  # Each "filled pixel" is an average of 4 neighbors

    # Even row:
    row1 = row + 1  # One row below <row>
    for col in range(1, w-2, 2):  # Even Row: start from col=1, because pixel in col=1 is blue
        #  G 
        # G*G
        #  G
        F = int(img[row1-1, col])
        I = int(img[row1, col-1])
        K = int(img[row1, col+1])
        N = int(img[row1+1, col])
        IG[row1, col] = np.round((F + I + K + N) / 4)    # Each "filled pixel" is an average of 4 neighbors

# Remove the padding:
IG = IG[2:-1, 2:-1]

# [x] TODO: show green (IR) in first subplot (221) and add title - refer to rgb one for hint on plotting
plt.subplot(221)
plt.imshow(IG, cmap='gray')
plt.title('IG')
plt.show()

输出:

voj3qocg

voj3qocg2#

这看起来像一个练习,所以我不会告诉你错误,希望这能帮助你自己找到错误。
基本算法是正确的 *,但有两个问题的实现。仔细看看你的方程和插值代码再次。你猜可能有丢失的像素,所以检查a)正确的像素被替换和b)被替换的像素有正确的值。
我将向您介绍一些可以用来捕获此类错误的方法。

**使用输出完全已知的简单图像进行测试。**我指的是全黑、全白色和全灰图像。这里这些图像的输出应该与输入相同。是这样吗?如果不是,是否有可识别的模式可以缩小问题范围?
**用更简单的算法替换该算法。**如果将插值替换为将所有插值像素设置为固定值,则应该能够找到其他问题如果您使用具有单个强度值的输入图像,则会更加明显(例如255),但你也会在输入图像中看到这个错误。这个错误通过将输入图像复制到输出图像中而被隐藏,但你会注意到实际的GBRG或GRBG图像作为输入。

  • 因为它会进行插值,而不是因为它是最好的方法,更好的方法请参见Rotem's answer

相关问题