首页 文章

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

提问于
浏览
6

我的图像处理类已经分配了一个图像恢复项目 . 我目前正在研究反向滤波器 . 图像 - >降级 - >逆滤波器 - >恢复图像 . 我正在使用一个简单的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

非常感谢任何帮助 .

[编辑]我'm running on Mac OSX 10.6.8 using Numpy 1.6.0 via Enthought'的免费32位版本 . (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 回答

  • 2

    问题显然是 FF_HAT2 不完全相同 . 你需要调用 nan_to_num 的事实清楚地表明乘法和除法之间的某些问题是 K . 可能的原因是整数溢出 . 尝试在加载后将 f 转换为浮点类型 .

相关问题