from matplotlib.image import imread import numpy as np import matplotlib.pyplot as plt # The car image I used in the class is from this link (https://cdn.vox-cdn.com/thumbor/Cewj-Naj1RyOQion8cneMmutJ30=/0x0:2870x2116/1820x1213/filters:focal(1203x969:1661x1427):format(webp)/cdn.vox-cdn.com/uploads/chorus_image/image/50272225/150028_car.0.jpg). # Download it and save it in the same directory as this python file, and name it 'car.jpg'. # Run the code by python fft.py (you need Python 3). def main(): fig,axs = plt.subplots(2,3) # read image and crop it to be square A = imread('car.jpg') B = np.mean(A, -1); # Convert RGB to grayscale rows, cols = B.shape centerX = int(rows/2) centerY = int(cols/2) B = B[centerX-350:centerX+350, centerY-350:centerY+350] rows, cols = B.shape centerX = int(rows/2) centerY = int(cols/2) # show image axs[0][0].imshow(B.real, cmap='gray', vmin=0, vmax=255) axs[0][0].axis('off') # fft and show the spectrum # fft2() calls numpy's FFT implementation (recall FFT is a faster DFT algorithm) Bt = np.fft.fft2(B) # shift the zero-frequency coefficients to the center of the spectrum for better visualization. Bt = np.fft.fftshift(Bt) # show the magnitude of the coefficients (complex-valued numbers) in log scale mag = np.log(1+np.abs(Bt)) amin = int(np.amin(mag)) amax = int(np.amax(mag)) + 1 axs[1][0].imshow(mag, cmap='gray', vmin=amin, vmax=amax) axs[1][0].axis('off') # high pass # generate the mask r = 80 mask = np.ones(B.shape) mask *= 10 x, y = np.ogrid[:rows, :cols] mask_area = (x - centerX) ** 2 + (y - centerY) ** 2 <= r*r mask[mask_area] = 0 # apply the mask and show the masked spectrum Ct = Bt; Ct = Ct * mask mag = np.log(1+np.abs(Ct)) axs[1][1].imshow(mag, cmap='gray', vmin=amin, vmax=amax) axs[1][1].axis('off') # ifft and show the image Ct = np.fft.ifftshift(Ct) A = np.fft.ifft2(Ct) axs[0][1].imshow(A.real, cmap='gray', vmin=0, vmax=255) axs[0][1].axis('off') # low pass # generate the mask r = 20 mask = np.zeros(B.shape) x, y = np.ogrid[:rows, :cols] mask_area = (x - centerX) ** 2 + (y - centerY) ** 2 <= r*r mask[mask_area] = 1 # apply the mask and show the masked spectrum Ct = Bt; Ct = Ct * mask mag = np.log(1+np.abs(Ct)) axs[1][2].imshow(mag, cmap='gray', vmin=amin, vmax=amax) axs[1][2].axis('off') # ifft and show the image Ct = np.fft.ifftshift(Ct) # ifft2() calls numpy's inverse FFT A = np.fft.ifft2(Ct) axs[0][2].imshow(A.real, cmap='gray', vmin=0, vmax=255) axs[0][2].axis('off') plt.show() if __name__ == '__main__': main()