ifft2不能提供所需的结果

ifft2不能提供所需的结果

本文介绍了使用scipy.fftpack进行频域过滤,ifft2不能提供所需的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在频域中的灰度输入lena图像上使用以下代码简单地应用高斯滤波器,这是我得到的错误输出:

I am trying to simply apply a Gaussian filter on a gray-scale input lena image in frequency domain with the following code and here is the wrong output I am getting:

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 我得到了所需的模糊图像输出,如下所示:

However, if I do the same but use signal.fftconvolve I get the desired blurred image output as shown below:

im_out = signal.fftconvolve(im, kernel, mode='same')  # output blurred image

我的输入图像是220x220,是否有任何填充问题?如果是这样,如何解决它并使第一个代码(没有 fftconvolve )工作?任何帮助都将受到高度赞赏。

My input image is 220x220, is there any padding issue? if so, how to solve it and make the first code (without fftconvolve) work? any help will be highly appreciated.

推荐答案

首先,没有必要将FFT的结果转移到移位在做IFFT之前它回来了。这只是相当多的转移,它们对结果没有影响。无论是否将它们都移动,两个数组相乘的方式都是相同的。

First of all, there is no need to shift the result of the FFT just to shift it back before doing the IFFT. This just amounts to a lot of shifting they has no effect on the result. Multiplying the two arrays happens in the same way whether you shift them both or not.

您在输出中注意到的问题是交换了四个象限。发生这种情况的原因是因为滤波器的大小减半,导致输出相同。

The problem you noticed in your output is that the four quadrants are swapped. The reason this happens is because the filter is shifted by half its size, causing the same shift in the output.

为什么它会移位?好吧,因为FFT将原点放在图像的左上角。这不仅适用于FFT的输出,也适用于其输入。因此,您需要生成一个原点位于左上角的内核。怎么样?只需在调用 fft 之前将 ifftshift 应用于

Why is it shifted? Well, because the FFT puts the origin in the top-left corner of the image. This is not only true for the output of the FFT, but also for its input. Thus, you need to generate a kernel whose origin is at the top-left corner. How? Simply apply ifftshift to it before calling fft:

freq = fp.fft2(im)
freq_kernel = fp.fft2(fp.ifftshift(kernel))
convolved = freq*freq_kernel
im_out = fp.ifft2(convolved).real

请注意 ifftshift 将原点从中心移动到左上角,而 fftshift 将其从角落移动到中心。

Note that ifftshift shifts the origin from the center to the top-left corner, whereas fftshift shifts it from the corner to the center.

这篇关于使用scipy.fftpack进行频域过滤,ifft2不能提供所需的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:03