【发布时间】:2022-01-15 11:21:44
【问题描述】:
将周期性二维信号从图像空间转换到傅立叶空间并返回时,重构信号的频率是原始信号的 两倍(见下图)。我尝试使用 NumPy 和 OpenCV 的离散傅里叶变换,结果相同。使用 1D DFT 和 IDFT 时不会出现此问题。
您知道这个问题的根源是什么以及如何解决它吗?
以下是演示该问题的 Python 示例代码:
import matplotlib.pyplot as plt
import numpy as np
# image width and height
w = 320
h = 320
# frequency of cosine wave w.r.t. the image width
frequency = 1
# function to create a horizontal 2D cosine wave
def create_cos_horizontal(frequency, w, h):
img = np.zeros((h,w),np.float32)
base_period = w/(2*np.pi)
print(frequency)
for x in range(0, w):
img[0:, x] = np.cos(x*frequency/base_period)
return img
img = create_cos_horizontal(frequency, w, h)
# transform from image space to fourier space
dft = np.fft.fft2(img)
# transform back from fourier space to image space
im_back = np.fft.ifft2(dft)
# show original and reconstructed image side by side
ax1 = plt.subplot(1,2,1)
ax1.imshow(img, cmap='gray')
ax1.set_title("original signal")
ax2 = plt.subplot(1,2,2)
ax2.imshow(np.abs(im_back), cmap='gray')
ax2.set_title("signal after DFT and IDFT")
非常感谢您。
【问题讨论】:
-
只是因为
np.abs(cosine)的频率是cosine的两倍...尝试绘制np.real(im_back)。 -
非常感谢。这解决了问题。
标签: python numpy image-processing fft dft