我一直在玩一點以了解 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 的結果繪制為影像?
uj5u.com熱心網友回復:
改用日志讓它作業:
oned_dft = np.fft.fft(grating)
plt.subplot(132)
plt.imshow(np.log(abs(oned_dft)))
plt.xlim([480, 520])
plt.ylim([520, 480])
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/415703.html
標籤:
