Python中的傅立叶变换可提供空白图像 [英] Fourier Transform in Python giving blank images

查看:74
本文介绍了Python中的傅立叶变换可提供空白图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我只是在图像上尝试2D傅立叶变换,并在numpy中使用ifft2对其进行了重构.但是,频谱幅度和重建的图像都是白色的.这可能表明存在缩放问题,但我不知道如何解决.

I am new to python and I am simply trying the 2d Fourier transform on an image and simply reconstruct it using ifft2 in numpy. However, the spectrum magnitude and the reconstructed are white images. This might indicate some scaling issue but I don't understand how to resolve it.

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
imgloc="C:\\Users\\AnacondaCodes\\cameraman.png"
img=mpimg.imread(imgloc,0)
import numpy as np
f=np.fft.fft2(img)
fshift=np.fft.fftshift(f)
magnitude_spectrum=20*np.log(np.abs(fshift))
f_ishift=np.fft.ifftshift(magnitude_spectrum)
img_back=np.fft.ifft2(f_ishift)
img_back=np.abs(img_back)
plt.subplot(131),plt.imshow(img, cmap='gray')
plt.title('input image'), plt.xticks([]),plt.yticks([])
plt.subplot(132),plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(img_back,cmap='gray')
plt.title('reconstructed'), plt.xticks([]), plt.yticks([])`

推荐答案

您对频谱绝对值的对数进行了逆变换.这将极大地改变频谱.即使是绝对部分也不是一件好事,因为FFT将真实图像转换成复杂的频谱(现在显然是2N个数据点,由于对称性,其中的1/2个是多余的).现在,必须要查看空间频域中的频谱的绝对部分,但这是因为它由实部和虚部组成.该日志还有助于可视化,因为您有相当大的DC偏移量(所有像素的平均值都不为零),因此,如果没有该日志,您只会在黑海中看到单个白点.

You backtransformed the log of the absolute value of the spectrum. That changes the spectrum dramatically. Even the absolute portion would not be a good thing, since the FFT transforms the real image into a complex spectrum (apparently 2N datapoints now , 1/2 of them is redundant because of symmetry). Now the absolute part is necessary to look at the spectrum in the spatial frequency domain, but that's because it consists of a real and imaginary part. The log also help in visualization, since you have a substantial DC offset (average of all pixels is not zero), so w/o the log you'll see just a single white dot in a sea of black.

如果将频谱本身变回原样,一切都很好.

If you transform the spectrum itself back, all is fine.

import matplotlib.pyplot as p
import numpy as np

img = p.imread("c:/pddata/cameraman.png").astype(float)    
spectrum = np.fft.fftshift(np.fft.fft2(img))

img_back=np.fft.ifft2(np.fft.ifftshift(spectrum))


p.figure(figsize=(20,6))
p.subplot(131)
p.imshow(img, cmap='gray')
p.title('input image') 
p.colorbar()

p.subplot(132)
p.imshow( np.log(np.abs(spectrum)) , cmap='gray')
p.title('Magnitude Spectrum') 
p.colorbar()

p.subplot(133)
p.imshow( np.abs(img_back),cmap='gray')
p.title('reconstructed') 
p.colorbar();

这篇关于Python中的傅立叶变换可提供空白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆