Demo Fourier Transform and Frequency filtering

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('minion.jpg',1)
#convert BGR to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# show the image
plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(img, cmap = "gray")

# display the historgram
plt.subplot(1,2,2)
plt.hist(img.flatten())
plt.show()

png

# create the centering matrix
centering_m = np.full((img.shape[0] , img.shape[1]), -1)

for x in range(0,img.shape[0]):
    for y in range(0,img.shape[1]):
        centering_m[x,y] = (-1)**(x+y)
        
plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(centering_m[0:10,0:10])
plt.subplot(1,2,2)
plt.hist(centering_m.flat)
plt.show()

png

centered_img = img *centering_m
plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(centered_img, cmap = "gray")
plt.subplot(1,2,2)
plt.hist(centered_img.flatten())
plt.show()

png

# compute the 2D FFT
dft =  np.fft.fft2(centered_img)
plt.figure(figsize=(20,10))
plt.imshow(np.log(1+np.abs(dft)), cmap = "gray")

<matplotlib.image.AxesImage at 0x1dfe72c33c8>

png

def distance(point1,point2):
    return np.sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)

Filters

Ideal LowPass Filter

def idealFilterLP(D0,imgShape):
    base = np.zeros(imgShape[:2])
    rows, cols = imgShape[:2]
    center = (rows/2,cols/2)
    for x in range(cols):
        for y in range(rows):
            if distance((y,x),center) < D0:
                base[y,x] = 1
    return base

plt.imshow(idealFilterLP(50,img.shape), cmap='gray'),plt.title('Low Pass Filter')
plt.show()

png

Ideal HighPass Filter

def idealFilterHP(D0,imgShape):
    base = np.ones(imgShape[:2])
    rows, cols = imgShape[:2]
    center = (rows/2,cols/2)
    for x in range(cols):
        for y in range(rows):
            if distance((y,x),center) < D0:
                base[y,x] = 0
    return base


plt.imshow(idealFilterHP(50,img.shape), cmap='gray'),plt.title('High Pass Filter')
plt.show()

png

Gaussian LowPass Filter

def gaussianLP(D0,imgShape):
    base = np.zeros(imgShape[:2])
    rows, cols = imgShape[:2]
    center = (rows/2,cols/2)
    for x in range(cols):
        for y in range(rows):
            base[y,x] = np.exp(((-distance((y,x),center)**2)/(2*(D0**2))))
    return base

plt.imshow(gaussianLP(80,img.shape), cmap='gray'),plt.title('Gaussian Low Pass Filter')
plt.xticks([]), plt.yticks([])
plt.show()

png

Gaussian HighPass Filter

def gaussianHP(D0,imgShape):
    base = np.zeros(imgShape[:2])
    rows, cols = imgShape[:2]
    center = (rows/2,cols/2)
    for x in range(cols):
        for y in range(rows):
            base[y,x] = 1 - np.exp(((-distance((y,x),center)**2)/(2*(D0**2))))
    return base

plt.imshow(gaussianHP(80,img.shape), cmap='gray'),plt.title('Gaussian High Pass Filter')
plt.xticks([]), plt.yticks([])
plt.show()

png

Apply Filter on DFT

# filter the DFT
fdft = dft * idealFilterLP(50,img.shape) ## note that we do a multiplication cause we are in the frequency domain

# inverse the filtered DFT
idft = np.fft.ifft2(fdft)

# take the real part and de center it
real_centered = idft.real * centering_m

plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(np.log(1+np.abs(fdft)), cmap = "gray")
plt.title('Filtered DFT')
plt.subplot(1,2,2)
plt.imshow(real_centered, cmap = "gray",vmin=0, vmax=255)
plt.title('Inversed Filtered DFT')
plt.show()

png

# filter the DFT
fdft = dft * gaussianLP(50,img.shape)

# inverse the filtered DFT
idft = np.fft.ifft2(fdft)

# take the real part and de center it
real_centered = idft.real * centering_m

plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(np.log(1+np.abs(fdft)), cmap = "gray")
plt.title('Filtered DFT')
plt.subplot(1,2,2)
plt.imshow(real_centered, cmap = "gray", vmin=0, vmax=255)
plt.title('Inversed Filtered DFT')
plt.show()

png

Demonstraring that we can go back and forth from image to frequency without loss

# inverse the filtered DFT
idft = np.fft.ifft2(dft)

# take the real part and de center it
real_centered = idft.real * centering_m


plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(img, cmap = "gray")
plt.title(' Original Image')
plt.subplot(1,2,2)
plt.imshow(real_centered, cmap = "gray", vmin=0, vmax=255)
plt.title('Inversed DFT')
plt.show()

plt.imshow(real_centered - img, cmap = "gray", vmin=0, vmax=255)
plt.title('Difference')
plt.show()

png

png

Related