Fourier Series
Our introduction to Fourier Analysis begins with learning about Fourier Series. Basically, if you have a function that is 1. p-periodic and 2. Riemann integrable on every bounded interval, then you can approximate (decompose) that function into a sum of trigonometric polynomials-- a sum of sines and cosines each with a weight a_n, b_n determined by the following formulas:
The Fourier transform takes the Fourier series you used to approximate your function and spits out the frequencies and amplitudes of the series to describe the basic components of your function. In other words, a Fourier Transform tastes a dish and spits out the recipe.
I followed a tutorial from OpenCV that shows how to use OpenCv and numpy in python to do some basic image processing. Here is what I got from the tutorial:
This code is the numpy version to perform a basic Fourier transform. We first import the image we will be analyzing and immediately perform a Fourier transform on the image. Then after some input/output image adjustments if necessary, we can compute the magnitude spectrum of the image and post them side by side.
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('PATH TO IMAGE',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
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()
rows, cols = img.shape
crow,ccol = rows/2 , cols/2
fshift[crow-30:crow+30, ccol-30:ccol+30] = 0
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132),plt.imshow(img_back, cmap = 'gray')
plt.title('Image after HPF'), plt.xticks([]), plt.yticks([])
plt.subplot(133),plt.imshow(img_back)
plt.title('Result in JET'), plt.xticks([]), plt.yticks([])
plt.show()
rows, cols = img.shape
crow,ccol = rows/2 , cols/2
# create a mask first, center square is 1, remaining all zeros
mask = np.zeros((rows,cols,2),np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
# apply mask and inverse DFT
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()