Python傅里叶分析:高斯滤波后逆变换图像偏移问题求助
傅里叶逆变换图像偏移问题
我用Python对自制形状图像做傅里叶分析,已经拿到2D傅里叶变换结果并应用了高斯滤波,但滤波后的逆傅里叶变换图像出现偏移。试过仅取实部解决,没有效果。以下是代码和运行结果,求解决思路。
#importing necessary libraries from PIL import Image, ImageDraw import matplotlib.pyplot as plt import numpy as np from scipy.ndimage import gaussian_filter #function to apply Gaussian filter to the Fourier transform def apply_gaussian_filter(fourier_transform, sigma): return gaussian_filter(fourier_transform, sigma) ##code to produce an image of a box and perform Fourier analysis on it #defining the size bounds of the box w, h = 65, 65 box = [(50, 29), (w-50, h-30)] #creating the image with a white background im = Image.new("I", (w,h)) img = ImageDraw.Draw(im) #producing the box shape img.rectangle(box, fill = "white", outline = "white") #convering image to numpy library im_array = np.array(im) #taking the 2D Fourier transform fourier_box = np.fft.fft2(im_array) #shifting the zero frequency component to the center fourier_box_shifted = np.fft.fftshift(fourier_box) #applying Gaussian filter to the Fourier transform of the box sigma_box = 5 filtered_fourier_box = apply_gaussian_filter(np.abs(fourier_box_shifted), sigma_box) ##code to produce an image of a circle and perform Fourier analysis on it #defining the size bounds of the shape w_1, h_1 = 65, 65 circle = [(10, 10), (w_1-10, h_1-10)] #creating the image with a white background im_1 = Image.new("I", (w_1,h_1)) img_1 = ImageDraw.Draw(im_1) #producing the circle shape img_1.ellipse(circle, fill = "white", outline = "white") #convering image to numpy library im_1_array = np.array(im_1) #taking the 2D Fourier transform fourier_circle = np.fft.fft2(im_1_array) #shifting the zero frequency component to the center fourier_circle_shifted = np.fft.fftshift(fourier_circle) #applying Gaussian filter to the Fourier transform of the circle sigma_circle = 5 filtered_fourier_circle = apply_gaussian_filter(np.abs(fourier_circle_shifted), sigma_circle) #applying inverse Fourier transform to the filtered Fourier data inverse_filtered_box = np.fft.ifft2(np.fft.ifftshift(filtered_fourier_box)).real inverse_filtered_circle = np.fft.ifft2(np.fft.ifftshift(filtered_fourier_circle)).real #adjusting size of the graphs plt.figure(figsize=(13, 8)) #displaying the Fourier transform of the box plt.subplot(231) plt.imshow(np.log(np.abs(fourier_box_shifted) + 1)) plt.title('2D Fourier Transform Box') #displaying the Fourier transform of the box with Gaussian filter plt.subplot(232) plt.imshow(np.log(filtered_fourier_box + 1)) plt.title('Gaussian Filtered 2D Fourier Transform Box') #displaying the inverse of the filtered Fourier box plt.subplot(233) plt.imshow(np.abs(np.real(inverse_filtered_box))) plt.title('Inverse Filtered Box') #displaying the Fourier transform of the circle plt.subplot(234) plt.imshow(np.log(np.abs(fourier_circle_shifted) + 1)) plt.title('2D Fourier Transform Circle') #displaying the Fourier transform of the circle with Gaussian filter plt.subplot(235) plt.imshow(np.log(filtered_fourier_circle + 1)) plt.title('Gaussian Filtered 2D Fourier Transform Circle') #displaying the inverse of the filtered Fourier circle plt.subplot(236) plt.imshow(np.abs(np.real(inverse_filtered_circle))) plt.title('Inverse Filtered Circle')

问题根源
你在滤波时只对傅里叶变换的幅值(np.abs(fourier_box_shifted))做了高斯滤波,完全丢失了相位信息!傅里叶变换由幅值和相位共同构成:幅值决定图像的明暗、频率分布,而相位直接决定图像中各元素的位置。仅保留幅值会彻底破坏位置关联,导致逆变换图像偏移、失真。
修正方案
必须对傅里叶变换的复数形式做高斯滤波,同时保留幅值和相位。具体修改:
- 无需修改滤波函数,
gaussian_filter原生支持对复数数组的实部、虚部分别处理 - 去掉滤波步骤中的
np.abs,直接传入移位后的复数傅里叶变换结果
修正后的关键代码片段
# 直接对复数傅里叶变换做滤波,不再取幅值 filtered_fourier_box = apply_gaussian_filter(fourier_box_shifted, sigma_box) filtered_fourier_circle = apply_gaussian_filter(fourier_circle_shifted, sigma_circle) # 逆变换后取实部(原图像为实数,逆变换虚部极小,可忽略) inverse_filtered_box = np.fft.ifft2(np.fft.ifftshift(filtered_fourier_box)).real inverse_filtered_circle = np.fft.ifft2(np.fft.ifftshift(filtered_fourier_circle)).real
额外说明
- 原图像是实数,其傅里叶变换满足共轭对称性,高斯核是对称核,滤波后逆变换的虚部会非常小,取实部是合理操作。
- 如果虚部数值仍不可忽略,可使用
np.around做近似,或结合np.abs与实部处理,但核心前提是必须保留相位信息。
内容的提问来源于stack exchange,提问作者Elisa
相关产品推荐
相关产品推荐

