0% found this document useful (0 votes)
172 views22 pages

DIP Lab Manual No 09

The document is a lab manual for a digital image processing course that describes image enhancement in the frequency domain using filtering. It discusses: 1) How images are represented in the 2D frequency domain using tools like the Fourier transform. 2) Common types of frequency domain filters like low-pass and high-pass filters and examples like ideal, Butterworth, and Gaussian filters. 3) The process of applying frequency domain filtering which involves transforming the image into the frequency domain, applying a filter, and transforming back. 4) Examples of applying ideal and Gaussian low-pass filters to blur images with different cutoff frequencies.

Uploaded by

ShAh NawaZz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views22 pages

DIP Lab Manual No 09

The document is a lab manual for a digital image processing course that describes image enhancement in the frequency domain using filtering. It discusses: 1) How images are represented in the 2D frequency domain using tools like the Fourier transform. 2) Common types of frequency domain filters like low-pass and high-pass filters and examples like ideal, Butterworth, and Gaussian filters. 3) The process of applying frequency domain filtering which involves transforming the image into the frequency domain, applying a filter, and transforming back. 4) Examples of applying ideal and Gaussian low-pass filters to blur images with different cutoff frequencies.

Uploaded by

ShAh NawaZz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

DEPARTMENT OF SOFTWARE ENGINEERING

Digital Image Processing

Lab Manual No 09

Image Enhancement in Frequency Domain- Filtering

Semester:
Spring 2017

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

Objectives:-

The objective of this session is to understand following concepts:

Which mathematical tools are used to represent an images contents in the 2D frequency
domain?
What is the Fourier transform, what are its main properties, and how is it used in the
context of frequency-domain ltering?
How are image processing lters designed and implemented in the frequency domain?
What are the differences between low-pass and high-pass lters (HPFs)?
What are the differences among ideal, Butterworth, and Gaussian lters?

Introduction:-

Frequency-domain lters work by following a straightforward sequence of steps:

Figure 1: Frequency Domain Operations

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

1. The input image is transformed to a 2D frequency-domain representation using the 2D


Fourier transform (FT).
2. A lter of specic type (e.g., ideal, Butterworth, Gaussian) and behavior (e.g., low pass,
high pass) is specied and applied to the frequency-domain representation of the image.
3. The resulting values are transformed back to the 2D spatial domain by applying the
inverse 2D Fourier transform, producing an output (ltered) image.

The mathematical foundation of frequency-domain techniques is the convolution theorem. Let


g(x,y) be an image obtained by the convolution (denoted by the symbol) of an image f(x,y)
with a linear, position invariant operator h(x,y), that is

From the convolution theorem, the following frequency-domain relation holds:

where G, F, and H are the Fourier transforms of g, f, and h, respectively.

Many image processing problems can be expressed in the form of equation of G(u,v).

In a noise removal application, for instance, given f(x,y), the goal, after computing F(u,v), will
be to select H(u,v) such that the desired resulting image,

where F1 is the inverse 2D Fourier transform operation, exhibits a reduction in the noisy
contents present in the original image f(x,y). For certain types of noise, this result could be
achieved using a low-pass Butterworth lter for example.

There are two options for designing and implementing image lters in the frequency domain
using MATLAB and IPT:

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

Obtain the frequency-domain lter response function from spatial lter convolution mask. The
IPT has a function that does exactly that: freqz2.

Generate lters directly in the frequency domain. In this case, a meshgrid array (of the same size
as the image) is created using the MATLAB function meshgrid.

Fourier Transform: Mathematical Foundations:

The FT is a fundamental tool in signal and image processing. In this section, we discuss the
mathematical aspects of 2D transforms in general and then introduce the 2D FT and its main
properties.

Basic Concepts:-

A transform is a mathematical tool that allows the conversion of a set of values to another set of
values, creating, therefore, a new way of representing the same information. In the eld of image
processing, the original domain is referred to as spatial domain, whereas the results are said to lie
in the transform domain. The motivation for using mathematical transforms in image processing
stems from the fact that some tasks are best performed by transforming the input images,
applying selected algorithms in the transform domain, and eventually applying the inverse
transformation to the result. (Figure 2)

Figure 2: Operation in the Transform Domain

Linear 2D transforms can be expressed in generic form in terms of a forward transform T(u,v) as

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

where u (u = 0,1,2,...,M1) and v (v = 0,1,2,...,N1) are called transform variables, f(x,y) is the
input image, x and y are the row and column dimensions of f(x,y) and r(x,y,u,v) is called the
forward transformation kernel, sometimes expressed as a collection of basis images.

The original image f(x,y) can be recovered by applying the inverse transform of T(u,v):

where x = 0,1,2,...,M1, y = 0,1,2,...,N1, and s(x,y,u,v) is called the inverse transformation


kernel.

The 2D Discrete Fourier Transform: Mathematical Formulation:-

The 2D Fourier transform of an image is a special case of equations of 1-D Fourier Transform
where,

and

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

where j =1.

Substituting above equations into equations of Fourier Transform we have,

and

In MATLAB:-

The 2D FT and its inverse are implemented in MATLAB by functions fft2 and ifft2,
respectively. 2D FT results are usually shifted for visualization purposes in such a way as to
position the zero-frequency component at the center of the gure. This can be accomplished by
function fftshift. MATLAB also includes function ifftshift, whose basic function is to undo the
results of fftshift.

Example:-

Figure 3 shows an image and its frequency spectrum (the log transformation of the amplitude
component of its 2D FT coefcients). The transformed version of the image clearly bears no
resemblance to the original version. Moreover, it does not provide any obvious visual hints as to
what the original image contents were. Do not bed is courage by these facts, though. As you will
soon see, these apparent limitations of the transformed version of the image will not prevent us
from designing frequency domain lters to achieve specic goals. The resulting image
obtained after applying the inverse transformwill allow us to judge whether those goals were
successfully achieved or not.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

MATLAB code:

I = imread(Figure11_04_a.png);

Id = im2double(I);

ft = fft2(Id);

ft_shift = fftshift(ft);

imshow(log(1 + abs(ft_shift)), [])

FIGURE 3 (a) Original image (256 256 pixels); (b) Fourier spectrum of the image in (a).

LOW-PASS FILTERING:-

Low-pass lters attenuate the high-frequency components of the Fourier transform of an image,
while leaving the low-frequency components unchanged. The typical overall effect of applying a
low-pass filter (LPF) to an image is a controlled degree of blurring.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

FIGURE 4 Original image (a) and its 2D FT spectrum (b); rotated image (c) and its 2D FT
spectrum (d).

Ideal LPF:-

An ideal low-pass lter enhances all frequency components within a specied radius (from the
center of the FT), while attenuating all others. Its mathematical formulation is given as follows:

where

represents the distance between a point of coordinates (u,v) and the origin of the 2D frequency
plan, and D0 is a nonnegative value, referred to as the cutoff frequency.

Figure 5 shows the frequency response plot for an ideal LPF. Figure 6 shows an example image
and its Fourier spectrum. The rings in Figure 6b represent different values for cutoff frequencies
(D0): 8, 16, 32, 64, and 128.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

FIGURE 5 Frequency response plot for an ideal LPF: (a) 3D view; (b) 2D view from the top.

FIGURE 6 (a) Original image (256 256 pixels); (b) Fourier spectrum of the image in (a). The
rings represent cutoff frequencies for the low-pass lter examples described later.

Figure 7 shows the results of applying ideal low-pass filters with different cutoff frequencies to
the original image: lower values of D0 correspond to blurrier results. A close inspection of
Figure 7 shows that the filtered images not only are blurry versions of the input imagean
expected outcome, common to all low-pass ltersbut also exhibits noticeable ringing artifacts
that appear because of the sharp transition between passband and stopband in ideal (low-pass)
filters.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

FIGURE 7 (a) Original image (256256 pixels); (bf) ideal LPF results for filters with cutoff
frequency corresponding to the radii in Figure 6b, namely, 8, 16, 32, 64, and 128 pixels.

Gaussian LPF:-

A Gaussian low pass filter attenuates high frequencies using a transfer function whose shape is
based on a Gaussian curve. The width of the bell-shaped curve can be controlled by specifying
the parameter , which is functionally equivalent to the cutoff frequency D0 dened previously:
lower values of correspond to more strict filtering, resulting in blurrier results. The smooth
transition between passband and stopband of the Gaussian LPF guarantees that there will be no
noticeable ringing artifacts in the output image. The Gaussian LPF can be mathematically
described as

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

Figure 8 shows the frequency response plot for a Gaussian LPF. Figure 9 shows the results of
applying Gaussian low-pass lters with different values of .

FIGURE 8 Frequency response plot for a Gaussian LPF: (a) 3D view; (b) 2D view from the top.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

FIGURE 9 (a) Original image (256256 pixels); (bf) Gaussian LPF results for lters with
different values for : 5, 10, 20, 30, and 75.

Butterworth LPF:-

The Butterworth family of lterswidely used in 1D analog signal processing provides an


alternative ltering strategy whose behavior is a function of the cutoff frequency, D0, and the
order of the lter, n. The shape of the lters frequency response, particularly the steepness of the
transition between passband and stopband, is also controlled by the value of n: higher values of n
correspond to steeper transitions, approaching the ideal lter behavior.

The Butterworth lter can be mathematically described as

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

where D0 is the cutoff frequency and n is the order of the lter. Figure 10 shows the frequency
response plot for a Butterworth LPF of order n = 4.

FIGURE 10 Frequency response plot for a Butterworth LPF of order n = 4: (a) 3D view; (b) 2D
view from the top.

Figure 11 shows the results of applying Butterworth low-pass lters with n =4 and different
cutoff frequencies to the original image.

HIGH-PASS FILTERING:-

High-pass lters attenuate the low-frequency components of the Fourier transform of an image,
while enhancing the high-frequency components (or leaving them unchanged). The typical
overall effect of applying a high-pass lter to an image is a controlled degree of sharpening.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

FIGURE 11 (a) Original image (512512 pixels); (bf) fourth-order Butterworth LPF results for
lters with cutoff frequency corresponding to the radii in Figure 6b, namely, 8, 16, 32, 64, and
128 pixels.

Ideal HPF:-

An ideal high-pass lter attenuates all frequency components with in a specied radius (from the
center of the FT), while enhancing all others. Its mathematical formulation is given as follows:

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

where

represents the distance between a point of coordinates (u,v) and the origin of the 2D frequency
plan, and D0 is a nonnegative value, referred to as the cutoff frequency (or cutoff radius). Figure
12 shows the frequency response plot for an ideal HPF.

FIGURE 12 Frequency response plot for an ideal HPF: (a) 3D view; (b) 2D view from the top.

Gaussian HPF:-

A Gaussian high pass lter attenuates low frequencies using a transfer function whose shape is
based on a Gaussian curve. The behavior of the lter can be controlled by specifying the
parameter , which is functionally equivalent to the cutoff frequency D0. The Gaussian HPF can
be mathematically described as

Figure 13 shows the frequency response plot for a Gaussian HPF.


Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq
Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

FIGURE 13 Frequency response plot for a Gaussian HPF: (a) 3D view; (b) 2D view from the
top.

Butterworth HPF:-

The Butterworth high-pass lter can be mathematically described as

where D0 is the cutoff frequency and n is the order of the lter.

Figure 14 shows the frequency response plot for a Butterworth HPF of order n = 4.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

FIGURE 14 Frequency response plot for a Butterworth HPF of order n = 4: (a) 3D view; (b) 2D
view from the top.

ACTIVITY1: 2D FOURIER TRANSFORM

Procedure:-

To generate the FT of an image (a 2D function), we use the IPT function fft2, which implements
the FFT algorithm.

1. Load the cameraman image, convert it to double (one of the data classes accepted as an
input to fft2), and generate its FT.

I = imread(cameraman.tif);

Id = im2double(I);

ft = fft2(Id);

To view the spectrum of the image, that is, the amplitude component of the FT results, we must
shift the zero-frequency (DC) component to the center of the image using the fftshift function.

2. Shift the FT array of results.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

ft_shift = fftshift(ft);

3. Display the FT results, remapped to a grayscale range.

figure,

subplot(1,2,1),imshow(abs(ft_shift),[]),title(Direct remap);

4. Display the log of the shifted FT image.

subplot(1,2,2), imshow(log(1 + abs(ft_shift)), []), title(Log


remap);

ACTIVITY 2: LOW-PASS FILTERS IN THE FREQUENCY DOMAIN

1. Load the eight image, generate a FT, and display it.

I = imread(eight.tif);

Id = im2double(I);

I_dft = fft2(Id);

figure, imshow(Id), title(Original Image);

figure, imshow(log(1 + abs(fftshift(I_dft))),[]),title(FT of


original image);

2. Generate a distance matrix with size equal to the input image.

[M, N] = size(I);

dist = distmatrix(M, N);

figure, mesh(fftshift(dist)), title(Distance Matrix);

3. Create initial lter with all values of zero.

H = zeros(M, N);

4. Create the ideal lter.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

radius = 35;

ind = dist <= radius;

H(ind) = 1;

Hd = double(H);

We can visualize the lters frequency response by displaying it as an image.

5. Display the lters frequency response.

figure, imshow(fftshift(H)), title(Ideal low-pass filter);

To apply the lter to the image, we simply multiply each value of the lter by its corresponding
frequency value in the FT image.

6. Apply the lter to the FT image.

DFT_filt = Hd .* I_dft;

I2 = real(ifft2(DFT_filt));

7. Display both the ltered FT image and the nal ltered image.

figure,imshow(log(1+abs(fftshift(DFT_filt))),[]),title(Filtered
FT);

figure, imshow(I2), title(Filtered Image);

Gaussian LPF:-

The Gaussian low-pass lter is usually specied by providing a value for the standard deviation
. We can use the distance matrix previously generated to create a Gaussian lter.

8. Create a Gaussian low-pass lter with = 30.

sigma = 30;

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

H_gau = exp(-(dist . 2) / (2 * (sigma 2)));

figure, imshow(Id), title(Original Image);

figure, imshow(log(1 + abs(fftshift(I_dft))),[]),title(DFT of


original image);

figure, mesh(fftshift(dist)), title(Distance Matrix);

figure, imshow(fftshift(H_gau)), title(Gaussian low-pass);

9. Filter the FT image with the Gaussian low-pass lter and display the ltered image.

DFT_filt_gau = H_gau .* I_dft;

I3 = real(ifft2(DFT_filt_gau));

figure, imshow(log(1 + abs(fftshift(DFT_filt_gau))),[]),


title(Filtered FT);

figure, imshow(I3), title(Filtered Image);

Butterworth LPF:-

The Butterworth low-pass lter is usually specied by providing two parameters: the order of the
lter, n, and the cutoff value, D0. In our implementation of the ideal low-pass lter, earlier in this
tutorial, we set the cutoff value to 35. For comparison purposes, we will use the same value for
our Butterworth lter.

10. Generate a third-order Butterworth lter, where the cutoff value is 35.

D0 = 35;

n = 3;

H_but=1./(1+(dist./D0).(2*n));

figure, imshow(Id), title(Original Image);

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

figure, imshow(log(1 + abs(fftshift(I_dft))),[]), title(FT of


original image);

figure, mesh(fftshift(dist)), title(Distance Matrix);

figure, imshow(fftshift(H_but)), title(Butterworth low-pass);

11. Filter the image with the Butterworth low-pass lter and display the resulting image.

DFT_filt_but = H_but .* I_dft;

I4 = real(ifft2(DFT_filt_but));

figure, imshow(log(1 + abs(fftshift(DFT_filt_but))),[]),


title(Filtered FT);

figure, imshow(I4), title(Filtered Image);

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6
DEPARTMENT OF SOFTWARE ENGINEERING

TASKS:

Write MATLAB code to apply HIGH-PASS filters (Idea, Gaussian, and Butterworth) on
images in frequency domain and show the output results.

Digital Image Processing Lab Instructor:-Engr. Muhammad Ehatisham-ul-Haq


Session:-SE-6

You might also like