pythonnumpyimage-processingfftdft

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


I have been playing around a little bit to understand how the 2D DFT works.

As far as my understanding goes the 2D-DFT performs a columns wise fft and then a row wise fft afterwards.

So now I wanted to check what the result looks like after performing a 1D DFT on an image. So for this I produced simple sinus gradients:

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()

I tried to use this line oned_dft = (oned_dft - np.min(oned_dft))/(np.max(oned_dft) - np.min(oned_dft))*255 to map the color scale from 0 to 255 but still everything is completely black.

When printing the values after scaling the look this:

[[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]]

How can I plot the result of the 1D Dft as an image accordingly?


Solution

  • Got it working using the log instead:

    oned_dft =  np.fft.fft(grating)
    plt.subplot(132)
    
    plt.imshow(np.log(abs(oned_dft)))
    plt.xlim([480, 520])
    plt.ylim([520, 480])