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

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

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

  1. # read image
  2. img = io.imread(filename_Grayimage, as_gray =True)
  3. h,w = img.shape
  4. # set figure size of the plot
  5. plt.figure(figsize=(10,8))
  6. # our final image will be a 3 dimensional image with 3 channels
  7. rgb = np.zeros((h,w,3),np.uint8)
  8. # reconstruction of the green channel IG
  9. IG = np.copy(img) # copy the image into each channel
  10. for row in range(0,h,4): # loop step is 4 since our mask size is 4.
  11. for col in range(0,w,4): # loop step is 4 since our mask size is 4.
  12. # Shaded Image Locations
  13. gA = int(img[row, col])
  14. gC = int(img[row, col+2])
  15. gF = int(img[row+1, col+1])
  16. gH = int(img[row+1, col+3])
  17. gI = int(img[row+2, col])
  18. gK = int(img[row+2, col+2])
  19. gN = int(img[row+3, col+1])
  20. gP = int(img[row+3, col+3])
  21. # Equations for valid neighbours
  22. # B = (A+C+F)/3
  23. # D = (C+H)/2
  24. # E = (A+F+I)/3
  25. # G = (F+C+H+K)/4
  26. # J = (F+I+K+N)/4
  27. # L = (H+K+P)/3
  28. # M = (I+N)/2
  29. # O = (N+K+P)/2
  30. # TODO: compute pixel value for each location where mask is unshaded (0)
  31. # interpolate each pixel using its every valid (shaded) neighbors
  32. IG[row, col+1] = (gA+gC+gF)/3 # B
  33. IG[row, col+3] = (gC+gH)/2 # D
  34. IG[row+1, col+1] = (gA+gF+gI)/3 # E
  35. IG[row+1, col+2] = (gF+gC+gH+gK)/4 # G
  36. IG[row+2, col+2] = (gF+gI+gK+gN)/4 # J
  37. IG[row+2, col+3] = (gH+gK+gP)/3 # L
  38. IG[row+3, col] = (gI+gN)/2 # M
  39. IG[row+3, col+2] = (gN+gK+gP)/2 # O
  40. # [x] TODO: show green (IR) in first subplot (221) and add title - refer to rgb one for hint on plotting
  41. plt.subplot(221)
  42. plt.imshow(IG, cmap='gray')
  43. 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拜耳对齐测试:

代码示例:

  1. import imageio as io
  2. from matplotlib import pyplot as plt
  3. import numpy as np
  4. # Bayer pattern of peppers_bayer.png is GRBG
  5. filename_Grayimage = 'peppers_bayer_grbg.png'
  6. # read image
  7. img = io.imread(filename_Grayimage, as_gray=True)
  8. img = img.astype(np.uint8) # imageio reads the data float type - convert the type to uint8 (working with integer pixels).
  9. h, w = img.shape
  10. # set figure size of the plot
  11. plt.figure(figsize=(10, 8))
  12. # our final image will be a 3 dimensional image with 3 channels
  13. #rgb = np.zeros((h, w, 3), np.uint8)
  14. # Add padding of 1 row at the top, one row at the bottom, one column at the left and on at the right.
  15. img = np.pad(img, ((1, 1), (1, 1)))
  16. img[:, 0] = img[:, 2] # Copy the second column (not the first) for keeping the Bayer pattern correct.
  17. img[:, -1] = img[:, -3]
  18. img[0, :] = img[2, :]
  19. img[-1, :] = img[-3, :]
  20. # reconstruction of the green channel IG
  21. IG = np.copy(img) # copy the image into each channel
  22. # Size after padding is w+2 by h+2
  23. w += 2
  24. h += 2
  25. for row in range(1, h-1, 2): # loop steps of 2 - each iteration applies one even and one odd row.
  26. # Padding - lower case.
  27. # Original pixels - upper case
  28. #
  29. # -------------- padding
  30. # | |
  31. # V V
  32. # gbgbgbgbgg <-- padding
  33. # rGRGRGRGRr
  34. # gBGBGBGBGg
  35. # rGRGRGRGRr
  36. # gBGBGBGBGg
  37. # rgrgrgrgrr <-- padding
  38. # Odd row:
  39. # Note: first row is odd, because row starts from 1
  40. for col in range(2, w-1, 2): # Odd row: Start from col=2, because pixel in col=1 is green
  41. # G
  42. # G*G
  43. # G
  44. C = int(img[row-1, col])
  45. E = int(img[row, col-1])
  46. F = int(img[row, col+1])
  47. H = int(img[row+1, col])
  48. IG[row, col] = np.round((C + E + F + H) / 4) # Each "filled pixel" is an average of 4 neighbors
  49. # Even row:
  50. row1 = row + 1 # One row below <row>
  51. for col in range(1, w-2, 2): # Even Row: start from col=1, because pixel in col=1 is blue
  52. # G
  53. # G*G
  54. # G
  55. F = int(img[row1-1, col])
  56. I = int(img[row1, col-1])
  57. K = int(img[row1, col+1])
  58. N = int(img[row1+1, col])
  59. IG[row1, col] = np.round((F + I + K + N) / 4) # Each "filled pixel" is an average of 4 neighbors
  60. # Remove the padding:
  61. IG = IG[2:-1, 2:-1]
  62. # [x] TODO: show green (IR) in first subplot (221) and add title - refer to rgb one for hint on plotting
  63. plt.subplot(221)
  64. plt.imshow(IG, cmap='gray')
  65. plt.title('IG')
  66. plt.show()

输出:

展开查看全部
voj3qocg

voj3qocg2#

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

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

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

相关问题