# Edge detection in images using Fourier Transform

Often while working with image processing, you end up exploring different methods to evaluate the best approach that fits your particular needs. I am going to talk about one such approach here, Fourier Transform.

# What is Fourier Transform?

In the simplest terms, a Fourier Transform helps in breaking down an incoming signal into its building blocks. For example, consider a signal f(x) having made by superimposing two or more sinusoidal functions with different frequencies. Now by just looking at the plot of f(x) you won’t be able to decide what or how many original functions were used to form f(x).

`Fs = 150.0; # sampling rate Ts = 1.0/Fs; # sampling interval t = np.arange(0,1,Ts) # time vector ff1 = 5; # frequency of the signal 1 ff2 = 10; # frequency of the signal 2 y = np.sin(2*np.pi*ff1*t) + np.sin(3*np.pi*ff2*t)`

# Fourier Transform in Image Processing

Now we know what Fourier transform does for signal processing. It converts the incoming signal from time domain to frequency domain.

`import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('scenery.jpg', 0) dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20 *    np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) plt.subplot(2, 2, 1), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([])plt.subplot(2, 2, 2), plt.imshow(magnitude_spectrum, cmap='gray')plt.title('After FFT'), plt.xticks([]), plt.yticks([])`
• Noise Reduction — Using a Low Pass filter
• Blurring of image — Using a Low Pass filter
• Feature Extractions(In some cases) — A mix and match of filters and some other openCV tools

# A Bit about filters first

Filters in image processing are just what the name suggests, Filter. They are typically a mask array of the same size as the original image which when superimposed on the ordinal image, extracts only the attributes that we are interested in.

`mask = np.ones((rows, cols, 2), np.uint8) r = 80 center = [crow, ccol] x, y = np.ogrid[:rows, :cols] mask_area = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= r*r`
• Low Pass Filter (LPF)
• Band Pass Filter (BPF)

# Edge Detection with High Pass Filter using openCV and NumPy

Detecting an edge in an image is of great use in the world of computer vision. Once we can extract edges in a image, we can use that knowledge for feature extraction or pattern detection.

`rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) # center # Circular HPF mask, center circle is 0, remaining all ones mask = np.ones((rows, cols, 2), np.uint8) r = 80 center = [crow, ccol] x, y = np.ogrid[:rows, :cols] mask_area = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= r*r # apply mask and inverse DFT fshift = dft_shift * mask fshift_mask_mag = 2000 * np.log(cv2.magnitude(fshift[:, :, 0], fshift[:, :, 1])) f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])plt.subplot(2, 2, 1), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([])plt.subplot(2, 2, 2), plt.imshow(magnitude_spectrum, cmap='gray') plt.title('After FFT'), plt.xticks([]), plt.yticks([])plt.subplot(2, 2, 3), plt.imshow(fshift_mask_mag, cmap='gray') plt.title('FFT + Mask'), plt.xticks([]), plt.yticks([])plt.subplot(2, 2, 4), plt.imshow(img_back, cmap='gray') plt.title('After FFT Inverse'), plt.xticks([]), plt.yticks([])plt.show()`

# Credits

• Special thanks to Grant Sanderson at 3Blue1Brown at youtube. He has the best video tutorials for some of the most complicated topics in mathematics. Highly recommended.
• Also a great thanks to Eugene Khutoryansky for his excellent video on Fourier Transform.

Written by