- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图使用以下代码简单地在频域中的灰度输入 Lena 图像上应用高斯滤波器,这是我得到的错误输出:
from scipy import signal
from skimage.io import imread
import scipy.fftpack as fp
import matplotlib.pyplot as plt
im = imread('lena.jpg') # read lena gray-scale image
# create a 2D-gaussian kernel with the same size of the image
kernel = np.outer(signal.gaussian(im.shape[0], 5), signal.gaussian(im.shape[1], 5))
freq = fp.fftshift(fp.fft2(im))
freq_kernel = fp.fftshift(fp.fft2(kernel))
convolved = freq*freq_kernel # simply multiply in the frequency domain
im_out = fp.ifft2(fp.ifftshift(convolved)).real # output blurred image
但是,如果我执行相同操作但使用 signal.fftconvolve
,我会得到所需的模糊图像输出,如下所示:
im_out = signal.fftconvolve(im, kernel, mode='same') # output blurred image
我的输入图像是220x220,是否有填充问题?如果是这样,如何解决它并使第一个代码(没有 fftconvolve
)工作?任何帮助将不胜感激。
最佳答案
首先,不需要将FFT的结果移位,只需在进行IFFT之前将其移回即可。这只是大量的转变,对结果没有影响。无论是否同时移动两个数组,乘法都会以相同的方式进行。
您在输出中注意到的问题是四个象限被交换了。发生这种情况的原因是滤波器移动了其大小的一半,导致输出发生相同的移动。
为什么要转移?嗯,因为 FFT 将原点放在图像的左上角。这不仅适用于 FFT 的输出,也适用于其输入。因此,您需要生成一个原点位于左上角的内核。如何?只需在调用 fft
之前对其应用 ifftshift
即可:
freq = fp.fft2(im)
freq_kernel = fp.fft2(fp.ifftshift(kernel))
convolved = freq*freq_kernel
im_out = fp.ifft2(convolved).real
请注意,ifftshift
将原点从中心移动到左上角,而 fftshift
将原点从角落移动到中心。
关于python - 使用 scipy.fftpack 进行频域滤波,ifft2 没有给出期望的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51200852/
我正在执行以下操作:执行 FFT/削减 FFT 结果中高于 100Hz 的每个频率/执行逆 FFT 如果原始数据集没有偏移量,它效果很好!如果它有偏移,则输出结果幅度会被损坏。 示例: Without
我从这里得到了 Bw BP C 代码,https://www-users.cs.york.ac.uk/~fisher/mkfilter ,正如其他操作系统主题中所评论的那样,并进行了 250Hz,第
我想使用窗口函数制作一个 FIR 滤波器。我有一些样本数据,size 变量是样本数。 windowSize 变量是窗口函数的大小。首先我创建窗口函数(blackman 窗口):变量 window然后我
我是一名优秀的程序员,十分优秀!