当它只显示完全黑暗的图像时,如何在具有正弦光栅的图像上绘制一维 DFT 结果

How to plot the result of 1D DFT on an image with sinus gratings when it only shows completely dark image instead

我一直在尝试了解 2D DFT 的工作原理。

据我了解,2D-DFT 执行列式 fft,然后执行行式 fft。

所以现在我想检查一下对图像执行一维 DFT 后的结果。所以为此我制作了简单的正弦梯度:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-500, 501, 1)
X, Y = np.meshgrid(x, x)
wavelength = 100
angle = np.pi/2
# use np.sign(np.sin(...)) for square wave grating instead of sine wave grating
grating = np.sign(np.sin(
    2*np.pi*(X*np.cos(angle) + Y*np.sin(angle)) / wavelength
))

plt.set_cmap("gray")
plt.subplot(131)
plt.imshow(grating)

oned_dft =  np.fft.fft(grating)
plt.subplot(132)
oned_dft = abs(oned_dft)
oned_dft = (oned_dft - np.min(oned_dft))/(np.max(oned_dft) - np.min(oned_dft))*255

plt.imshow(oned_dft, cmap='gray', vmin=0, vmax=255)
plt.xlim([480, 520])
plt.ylim([520, 480])

ft = np.fft.fft2(grating)
ft = np.fft.fftshift(ft)
plt.subplot(133)
plt.imshow(abs(ft))
plt.xlim([480, 520])
plt.ylim([520, 480])

plt.show()

我尝试使用这条线 oned_dft = (oned_dft - np.min(oned_dft))/(np.max(oned_dft) - np.min(oned_dft))*255 来映射从 0 到 255 的色标,但仍然全黑。

缩放外观后打印值时:

[[2.36658342e+02 1.83026900e+01 1.81860826e+01 ... 1.79927265e+01
  1.81860826e+01 1.83026900e+01]
 [2.55000000e+02 7.81996160e-15 7.04557665e-15 ... 2.92858750e-15
  7.04557665e-15 7.81996160e-15]
 [2.55000000e+02 7.81996160e-15 7.04557665e-15 ... 2.92858750e-15
  7.04557665e-15 7.81996160e-15]
 ...
 [2.55000000e+02 7.81996160e-15 7.04557665e-15 ... 2.92858750e-15
  7.04557665e-15 7.81996160e-15]
 [2.55000000e+02 7.81996160e-15 7.04557665e-15 ... 2.92858750e-15
  7.04557665e-15 7.81996160e-15]
 [2.36658342e+02 1.83026900e+01 1.81860826e+01 ... 1.79927265e+01
  1.81860826e+01 1.83026900e+01]]

如何将 1D Dft 的结果相应地绘制为图像?

使用日志代替:

oned_dft =  np.fft.fft(grating)
plt.subplot(132)

plt.imshow(np.log(abs(oned_dft)))
plt.xlim([480, 520])
plt.ylim([520, 480])