【问题标题】:Inverse Filter of spatially convolved versus frequency convolved image空间卷积与频率卷积图像的逆滤波器
【发布时间】:2011-12-17 08:45:03
【问题描述】:

我的图像处理班被分配了一个图像修复项目。我目前正在研究逆滤波器。图像 -> 降级 -> 逆滤波器 -> 恢复图像。我正在使用一个简单的 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”:-(

非常感谢您的帮助。

[编辑] 我通过 Enthought 的免费 32 位版本使用 Numpy 1.6.0 在 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

【问题讨论】:

  • 我运行了代码,上面给出的 f_hat_spatial 和 f_hat_frequency 的标签似乎被交换了(实际上是“f_hat_spatial”图像重建失败)。
  • 糟糕。你是对的。我正在修改我的帖子。

标签: image-processing numpy scipy


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2020-05-26
  • 2013-06-19
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
相关资源
最近更新 更多