我的圖像處理類已經分配了一個圖像恢復項目。我目前正在研究逆濾波器。圖像 - >降級 - >逆濾波器 - >恢復圖像。我正在使用一個簡單的5x5盒子濾鏡來降解。空間卷積與頻率卷積圖像的逆濾波器
如果我在空間域中對圖像進行卷積,移動到頻域,然後逆向濾波使用內核的fft對卷積圖像進行處理,結果是亂七八糟。如果我在頻域中對圖像進行卷積處理,然後對該圖像進行逆濾波,我會得到一個很好的圖像。
頻域和空域卷積應該是相同的。我唯一的想法是我在做內核有問題嗎?我正在使用5x5盒子過濾器。空間卷積將最終結果除以np.sum(box)。我試過通過以下方法來正常化箱子:
box = np.ones(25).reshape(5,5)/25.0
但是得到相同的垃圾箱逆濾波圖像結果。
我也注意到頻率卷積圖像(下面的代碼中的「g_freq.png」)被移位,可能是由於FFT填充了頂部,左邊是圖像的底部/右邊。這可能導致問題嗎?
空間卷積:
頻率卷積:注意沿頂部/左填充。
最簡單的可能的代碼來創建問題如下。 100%numpy/scipy/matplotlib。
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import scipy
from scipy import ndimage
def save_image(data, filename) :
print "saving",filename
plt.cla()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data, interpolation="nearest", cmap=matplotlib.cm.gray)
fig.savefig(filename)
f = scipy.misc.lena()
save_image(f, "scipylena.png")
# create a simple box filter
kernel = np.ones(25).reshape(5, 5)
kernel_padded = np.zeros_like(f,dtype="float")
# put kernel into upper left
kernel_padded[:5,:5] = kernel
# FFT kernel, save as image
K = np.fft.fftshift(np.fft.fft2(kernel_padded))
save_image(np.abs(K), "K.png")
# degrade image via spatial convolution
g = ndimage.convolve(f, kernel)
if np.sum(kernel) != 0 :
g /= np.sum(kernel)
# save spatial image
save_image(g, "g_spatial.png")
# take convolved image into frequency domain
G = np.fft.fftshift(np.fft.fft2(g))
# inverse filter the spatially convolved image
F_HAT = G/K
# back to spatial, save the reconstructed image
a = np.nan_to_num(F_HAT)
f_hat = np.fft.ifft2(np.fft.ifftshift(F_HAT))
save_image(np.abs(f_hat), "f_hat_spatial.png")
#
# now the same path but entirely in frequency domain
#
# create a frequency domain convolved image
F = np.fft.fftshift(np.fft.fft2(f))
G2 = F * K
# back to spatial, save frequency convolved image
g2 = np.fft.ifft2(np.fft.ifftshift(G2))
save_image(np.abs(g2), "g_freq.png")
# inverse filter the frequency convolved image
F_HAT2 = G2/K
a = np.nan_to_num(F_HAT2)
f_hat2 = np.fft.ifft2(np.fft.ifftshift(a))
save_image(np.abs(f_hat2), "f_hat_freq.png")
我 「f_hat_frequency」
我 「f_hat_spatial」 :-(
非常感謝任何幫助。
[編輯]我在Mac OSX上運行10.6.8通過Enthought的免費32位版本使用Numpy 1.6.0(http://www.enthought.com/products/epd_free.php)Python 2.7.2 | EPD_free 7.1-1(32位)
編輯2011年10月31日。 我想我想要做的比我理解的有更深的數學根源。 http://www.owlnet.rice.edu/~elec539/Projects99/BACH/proj2/inverse.html有點幫助。添加以下到我的代碼逆濾波器之前:
H_HAT = np.copy(K)
np.putmask(H_HAT, H_HAT>0.0001, 0.0001)
給我的形象,但有很多振鈴的(可能是因爲我的盒子過濾器;需要切換到高斯)。而且,頻率濾波圖像的偏移很可能引起問題。我的教授查看了我的代碼,找不到問題。她的建議是繼續使用頻率濾波圖像,而不是空間濾波圖像。
我有dsp.stackexchange.com一個類似的問題:https://dsp.stackexchange.com/questions/538/using-the-inverse-filter-to-correct-a-spatially-convolved-image
我運行了代碼,看起來上面給出的f_hat_spatial和f_hat_frequency的標籤是交換的(它實際上是重建失敗的「f_hat_spatial」圖像)。 – nobar
糟糕。你是對的。我正在修復我的帖子。 –