1import numpy as np
2import cv2
3from matplotlib import pyplot as plt
4
5img = cv2.imread('xfiles.jpg',0)
6
7img_float32 = np.float32(img)
8
9dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
10dft_shift = np.fft.fftshift(dft)
11
12rows, cols = img.shape
13crow, ccol = rows/2 , cols/2 # center
14
15# create a mask first, center square is 1, remaining all zeros
16mask = np.zeros((rows, cols, 2), np.uint8)
17mask[crow-30:crow+30, ccol-30:ccol+30] = 1
18
19# apply mask and inverse DFT
20fshift = dft_shift*mask
21f_ishift = np.fft.ifftshift(fshift)
22img_back = cv2.idft(f_ishift)
23img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
24
25plt.subplot(121),plt.imshow(img, cmap = 'gray')
26plt.title('Input Image'), plt.xticks([]), plt.yticks([])
27plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
28plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
29
30plt.show()
31
1import cv2
2import numpy as np
3from matplotlib import pyplot as plt
4
5img = cv2.imread('xfiles.jpg',0)
6f = np.fft.fft2(img)
7fshift = np.fft.fftshift(f)
8magnitude_spectrum = 20*np.log(np.abs(fshift))
9
10plt.subplot(121),plt.imshow(img, cmap = 'gray')
11plt.title('Input Image'), plt.xticks([]), plt.yticks([])
12plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
13plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
14plt.show()
15
1import numpy as np
2import cv2
3from matplotlib import pyplot as plt
4
5img = cv2.imread('xfiles.jpg',0)
6
7img_float32 = np.float32(img)
8
9dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
10dft_shift = np.fft.fftshift(dft)
11
12magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
13
14plt.subplot(121),plt.imshow(img, cmap = 'gray')
15plt.title('Input Image'), plt.xticks([]), plt.yticks([])
16plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
17plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
18plt.show()
19
1f = np.fft.fft2(img)
2fshift = np.fft.fftshift(f)
3fshift = np.fft.ifftshift(fshift)
4