In [ ]:
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
In [ ]:
image = cv2.imread('rectangle.jpg',0) # loaded the image in grayscale

img_float32 = np.float32(image) # convert from uint8 into float32

dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT) # Computed the 2-d discrete Fourier Transform
dft_shift = np.fft.fftshift(dft) # Shift the zero-frequency component to the center of the spectrum.

magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) # compute magnitude spectrum
In [ ]:
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)

ax1.imshow(image, cmap = 'gray')
ax1.set_title('Input Image')
ax1.set_xticks([])
ax1.set_yticks([])

ax2.imshow(magnitude_spectrum, cmap = 'gray')
ax2.set_title('Magnitude Spectrum')
ax2.set_xticks([])
ax2.set_yticks([])