空间卷积与频率卷积图像的逆滤波器

时间:2011-10-28 14:44:55

标签: image-processing numpy scipy

我的图像处理类已经分配了一个图像恢复项目。我目前正在研究反向滤波器。图像 - >降解 - >逆滤波器 - >恢复图像。我正在使用一个简单的5x5盒式滤镜来降级。

如果我在空间域中对图像进行卷积,请移动到频域,然后使用内核的fft对卷积图像进行反向滤波,我弄得一团糟。如果我在频域中对图像进行卷积,然后对该图像进行反向滤波,我会得到一个好的图像。

频域和空间域卷积应该相同。我唯一的想法是我在做内核的错误?我正在使用5x5盒式过滤器。空间卷积将最终结果除以np.sum(方框)。我试过通过以下方式对盒子进行标准化:

box = np.ones( 25 ).reshape( 5,5 ) / 25.0

但获取相同的垃圾箱反向过滤的图像结果。

我还注意到频率卷积图像(来自下面代码的“g_freq.png”)被移位,这可能是由于FFT填充图像的顶部和左侧的顶部和左侧。这会导致问题吗?

空间卷积: spatial convolition

频率卷积:注意顶部/左侧的填充。 frequency convolution

创建问题的最简单的代码如下。 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” my f_hat_frequency

我的“f_hat_spatial”:-( my f_hat_spatial

非常感谢您的帮助。

[编辑]我使用Numpy 1.6.0通过Enthought的免费32位版本在Mac OSX 10.6.8上运行。 (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

1 个答案:

答案 0 :(得分:2)

问题显然是FF_HAT2不相同。您需要调用nan_to_num这一事实清楚地表明乘法和除法之间出现了错误K。可能的原因是整数溢出。尝试在加载后将f转换为浮点类型。

相关问题