You are on page 1of 6

Experiment No.

4
Aim : Write a program for image enhancement in frequency domain filtering
(Butterworth, Gaussian).
Software : Spyder, Python Coding
Theory :
❖ Frequency Domain Filters:
Frequency Domain Filters are used for smoothing and sharpening of image by
removal of high or low frequency components. Sometimes it is possible of removal
of very high and very low frequency. Frequency domain filters are different from
spatial domain filters as it basically focuses on the frequency of the images.
• Butterworth Highpass Filter (BHPF) : -
Butterworth Highpass Filter (BHPF) is used for image sharpening in the frequency
domain. Image Sharpening is a technique to enhance the fine details and highlight
the edges in a digital image. It removes low-frequency components from an image
and preserves high-frequency components.
It can be determined using the relation- HHP(u, v) = 1- HLP(u, v) where, HHP(u, v)i
s the transfer function of the highpass filter and HLP(u, v) is the transfer function
of the corresponding lowpass filter.
The transfer function of BHPF of order n is defined as :-
H(u,v) = 1/1+[Do/D(u,v)]^2n
Do is a positive constant. BHPF passes all the frequencies greater than Do value
without attenuation and cuts off all the frequencies less than it.
This Do is the transition point between H(u, v) = 1 and H(u, v) = 0, so this is termed
as cutoff frequency. But instead of making a sharp cut-off (like, Ideal Highpass Filter
(IHPF)), it introduces a smooth transition from 0 to 1 to reduce ringing artifacts.
D(u, v) is the Euclidean Distance from any point (u, v) to the origin of the frequency
plane, i.c, D(u, v) = √(u² + v²).
• Butterworth Lowpass Filter (BLPF) : -
It is used for image smoothing in the frequency domain. It removes high-frequency
noise from a digital image and preserves low-frequency components. The transfer
function of BLPF of order n is defined as :- H(u, v) = 1/1+ [D(u,v)/Do]^2n
Where,
Do is a positive constant. BLPF passes all the frequencies less than Do value without
attenuation and cuts off all the frequencies greater than it.
• Gaussian Filter:
A Gaussian Filter is a low pass filter used for reducing noise (high frequency
components) and blurring regions of an image. The filter is implemented as an Odd
sized Symmetric Kernel (DIP version of a Matrix) which is passed through each pixel
of the Region of Interest to get the desired effect. The kernel is not hard towards
drastic color changed (edges) due to it the pixels towards the center of the kernel
having more weightage towards the final value then the periphery. A Gaussian
Filter could be considered as an approximation of the Gaussian Function
(mathematics).
In the process of using Gaussian Filter on an image we firstly define the size of the
Kernel/Matrix that would be used for demising the image. The sizes are generally
odd numbers, i.e. the overall results can be computed on the central pixel. Also the
Kernels are symmetric & therefore have the same number of rows and column. The
values inside the kernel are computed by the Gaussian function, which is as follows:
2 Dimensional gaussian function
Where,
x → X coordinate value
y → Y coordinate value
???? → Mathematical Constant PI (value = 3.13)
σ → Standard Deviation
Using the above function a gaussian kernel of any size can be calculated, by
providing it with appropriate values. A 3×3 Gaussian Kernel.
Approximation(two-dimensional)with Standard Deviation =1, appears as
Code :
• For Butterworth Filter: -

# Libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt

#open the image


f = cv2.imread(“noisy image name”,0)
plt.imshow(f, cmap= ‘gray')
plt.axis(‘off’)
plt.show()

#transform image into freq. domain and shifted


F = np.fft.fft2(f)
Fshift = np. fft.fftshift(F)

plt.imshow(np.loglp (np.abs (Fshift)), cmap=’gray’)


plt.axis(‘off’)
plt.show()

#Butterwort Low Pass Filter


M,N = f.shape
H = np.zeros((M,N), dtype=np.float32)
D0 = 20 # cut of frequency
N = 15 # order
for u in range(M):
For v in range(N):
D = np.sqrt((u-M/2)**2+ (v-N/2)**2)
H[u,v] = 1 / (1 + (D/D0)**n)
plt.imshow(H, cmap=’gray’)
plt.axis(‘off’)
plt.show()

# frequency domain image filters


Gshift = Fshift * H
G = np.fft.ifftshift(Gshift)
g = np.abs(np. fft.Ifft2(G))

plt.imshow(g, cmap = ‘gray')


plt.axis(‘off’)
plt.show()
• For Gaussian Filter : -

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread(‘’image name”)

krrnel = np.array([[-1, -1, -1], [-1, 9, -1],[-1, -1, -1]])

blurReading = cv2.GaussianBlur (img,(5,5),0)


filterReading = cv2.filter2D(blurReading,-1, kernel)

plt.subplot(121), plt.imshow(blurReading ),plt.title(“blurred img”)


plt.xticks([ ]),plt.yticks([ ])
plt.subplot(122), plt.imshow(filterReading),plt.title(“filtered img”)
plt.xticks([ ]), plt.yticks([ ])
plt.show( )

Conclusion : In this experiment,we performed frequency domain filtering using


Butterworth and Gaussian filter.

You might also like