-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEdge Detection using Fourier Transform.py
57 lines (42 loc) · 1.53 KB
/
Edge Detection using Fourier Transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#Importing numpy,cv2 & matplotlib
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# Reading image in grayscale
img = cv.imread('lal_minar.jpg',0)
#Frequency transform is a complex array
f = np.fft.fft2(img)
#Shifting the zero frequency component to the center
fshift = np.fft.fftshift(f)
#Finding the magnitude spectrum
A1 = 20;
magnitude_spectrum = A1*np.log(np.abs(fshift))
#Plotting
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
#Center
rows, cols = img.shape
crow,ccol = rows//2 , cols//2
# HPF masking, center 12X12 grid masked 0, remaining all ones
mask = 12;
fshift[crow-mask:crow+mask, ccol-mask:ccol+mask] = 0
# Finding the magnitude spectrum of masked Fourier Transform
A2 = 2000;
fshift_mask_mag = A2 * np.log(np.abs(fshift))
#Restoring the original indexing
f_ishift = np.fft.ifftshift(fshift)
#Inverse FFT
img_back = np.fft.ifft2(f_ishift)
#Finding the magnitude spectrum
img_back = np.real(img_back)
#Plotting
plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.title('Lal Minar'), plt.xticks([]), plt.yticks([])
plt.subplot(132),plt.imshow(fshift_mask_mag , cmap = 'gray')
plt.title('FFT + Mask'), plt.xticks([]), plt.yticks([])
plt.subplot(133),plt.imshow(img_back, cmap = 'Reds')
plt.title('Image after HPF'), plt.xticks([]), plt.yticks([])
plt.show()