Electrical & Computer Engineering Program Lab Exercise Computer Vision and Image Processing

Electrical & Computer Engineering Program
Lab Exercise
Computer Vision and Image Processing
COMP4500
3
Module 2 Lab Exercise
Introduction to MATLAB Digital Image Processing
Lab Objectives:
Computing of the Fourier Transform for an image and displaying the spectral image.
Designing of filters in the frequency domain (lowpass and highpass filters) and apply them to images.
Theory
2.1 Fourier Transform:
The Fourier transform is a representation of an image as a sum of complex exponentials of varying magnitudes, frequencies, and phases. The Fourier transform plays a critical role in a broad range of image processing applications, including enhancement, analysis, restoration, and compression.
Working with the Fourier transform on a computer usually involves a form of the transform known as the discrete Fourier transform (DFT). There are two principal reasons for using this form:
The input and output of the DFT are both discrete, which makes it convenient for computer manipulations.
There is a fast algorithm for computing the DFT known as the fast Fourier transform (FFT).
The DFT is usually defined for a discrete function f(x,y) that is nonzero only over the
finite region 0 ≤ x ≤ M-1 and 0 ≤ y ≤ N-1.
[Type here]
COMP4500
FACULTY OF ENGINEERING – SOHAR
UNIVERSITY
The general idea is that the image (f(x,y) of size M x N) will be represented in the frequency domain (F(u,v)). The equation for the two-dimensional discrete Fourier transform (DFT)
The concept behind the Fourier transform is that any waveform that can be constructed using a sum of sine and cosine waves of different frequencies. The exponential in the above formula can be expanded into sins and cosines with the variables u and v determining these frequencies
The inverse of the above discrete Fourier transform is given by the following equation:
Thus, if we have F(u,v), we can obtain the corresponding image (f(x,y)) using the inverse discrete Fourier transform.
Things to note about the discrete Fourier transform are the following:
The value of the transform at the origin of the frequency domain, at F(0,0), is called the DC component
F(0,0) is equal to MN times the average value of f(x,y) .
In MATLAB, F(0,0) is actually F(1,1) because array indices in MATLAB start at 1 rather than 0.
[Type here]
The values of the Fourier transform are complex, meaning they have real and imaginary parts. The imaginary parts are represented by i, which is the square root of -1
We visually analyze a Fourier transform by computing a Fourier spectrum (the magnitude of F(u,v)) and display it as an image. o The Fourier spectrum is symmetric about the center.
The fast Fourier transform (FFT) is a fast algorithm for computing the discrete Fourier transform.
MATLAB has three functions to compute the DFT:
1. fft – for one dimension (useful for audio)
fft2 – for two dimensions (useful for images)
fftn – for n dimensions
MATLAB has three functions that compute the inverse DFT:
ifft
ifft2
ifftn
The function fftshift is used to shift the zero-frequency component to center of spectrum. Note that it is so important to apply a logarithmic transformation function on the spectral image before displaying so as spectral details are efficiently displayed.
How does the Discrete Fourier Transform relate to Spatial Domain Filtering?
The following convolution theorem shows an interesting relationship between the spatial domain and frequency domain:
And, conversely,
[Type here]
The symbol “*” indicates convolution of the two functions. The important thing to extract out of this is that the multiplication of two Fourier transforms corresponds to the convolution of the associated functions in the spatial domain.
Basic Steps in DFT Filtering
The following summarize the basic steps in DFT Filtering
Obtain the Fourier transform: F=fft2(f);
Generate a filter function, H
Multiply the transform by the filter: G=H.*F;
Compute the inverse DFT: g=ifft2(G);
Obtain the real part of the inverse FFT of g: g2=real(g);
2.2 Filters in the Frequency Domain
Based on the property that multiplying the FFT of two functions from the spatial domain produces the convolution of those functions, you can use Fourier transforms as a fast convolution on large images. As a note, on small images, it is faster to work in the spatial domain.
[Type here]
However, you can also create filters directly in the frequency domain. There are three commonly discussed filters in the frequency domain:
Lowpass filters, sometimes known as smoothing filters
Highpass filters, sometimes known as sharpening filters
Bandpass filters.
A Lowpass filter attenuates high frequencies and retains low frequencies unchanged.
A Highpass filter blocks all frequencies smaller than Do and leaves the others unchanged.
Bandpass filters are a combination of both lowpass and highpass filters. They attenuate all frequencies smaller than a frequency Do and higher than a frequency D1, while the frequencies between the two cut-offs remain in the resulting output image.
Lowpass filters:
create a blurred (or smoothed) image attenuate the high frequencies and leave the low frequencies of the Fourier transform relatively unchanged
Three main lowpass filters are discussed in Digital Image Processing Using MATLAB:
Ideal lowpass filter (ILPF): The simplest low pass filter that cutoff all high frequency components of the Fourier transform that are at the distance greater than distance D0 from the center.
[Type here]
Where D0 is a specified nonnegative quantity (cutoff frequency), and D(u,v) is the distance from point (u,v) to the center of the frequency rectangle. The center of frequency rectangle is (M/2,N/2)
The distance from any point (u,v) to the center D(u,v) of the Fourier transform is given by:
M and N are sizes of the image.
Butterworth lowpass filter (BLPF): of order n, and with cutoff frequency at a distance D0 from the center.
Gaussian lowpass filter (GLPF)
The GLPF did not achieve as much smoothing as the BLPF of order 2 for the same value of cutoff frequency
The corresponding formulas and visual representations of these filters are shown in the table below. In the formulae, D0 is a specified nonnegative number (cutoff frequency). D(u,v) is the distance from point (u,v) to the center of the filter. Butterworth low pass filter (BLPF) of order n.
[Type here]
[Type here]
Highpass filters:
sharpen (or shows the edges of) an image attenuate the low frequencies and leave the high frequencies of the Fourier transform relatively unchanged
The highpass filter (Hhp) is often represented by its relationship to the lowpass filter (Hlp):
Because highpass filters can be created in relationship to lowpass filters, the following table shows the three corresponding highpass filters by their visual representations:
Ideal highpass filter (IHPF)
Butterworth highpass filter (BHPF)
3. Gaussian highpass filter (GHPF)
[Type here]
In Matlab, to get lowpass filter we use this command:
H = fspecial(‘gaussian’,HSIZE,SIGMA)
Returns a rotationally symmetric Gaussian lowpass filter of size HSIZE with standard deviation SIGMA (positive).
HSIZE can be a vector specifying the number of rows and columns in H or scalar, in which case H is a square matrix.
[Type here]
– The default HSIZE is [3 3], the default SIGMA is 0.5.
In Matlab, to get highpass laplacian filter we use this command:
H = fspecial(‘laplacian’,ALPHA)
Returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator.
The parameter ALPHA controls the shape of the Laplacian and must be in the range 0.0 to 1.0.
The default ALPHA is 0.2
3. Exercises
Exercise1: Apply FFT and IFFT.
%ex1.m close all clear
clc
%====================================
% 1) Displaying the Fourier Spectrum:
%==================================== I=imread(‘Lab8_1.jpg’); I=im2double(I);
FI=fft2(I); %(DFT) get the frequency for the image FI_S=abs(fftshift(FI));%Shift zero-frequency component to center of img_spectrum.
I1=ifft2(FI); I2=real(I1); subplot(131),imshow(I),title(‘Original’),
subplot(132),imagesc(0.5*log(1+FI_S)),title(‘Fourier Spectrum’),axis off
subplot(133),imshow(I2),title(‘Reconstructed’)
%imagesc: the data is scaled to use the full colormap.
[Type here]
Output:
Exercise2: Apply lowpass filter.
%ex2.m close all clear
clc
%=============================
2) Low-Pass Gaussian Filter: %============================= I=imread(‘Lab8_1.jpg’); I=im2double(I);
FI=fft2(I); %1.Obtain the Fourier transform
LP=fspecial(‘gaussian’,[11 11],1.3); %2.Generate a Low-Pass filter FLP=fft2(LP,size(I,1),size(I,2)); %3. Filter padding LP_OUT=FLP.*FI; %4.Multiply the transform by the filter I_OUT_LP=ifft2(LP_OUT); %5.inverse DFT
I_OUT_LP=real(I_OUT_LP); %6.Obtain the real part(Output)
%%%%spectrum%%%%
[Type here]
FLP_S=abs(fftshift(FLP));%Filter spectrum
LP_OUT_S=abs(fftshift(LP_OUT));%output spectrum
subplot(221),imshow(I),title(‘Original’),
subplot(222),imagesc(0.5*log(1+FLP_S)),title(‘LowPass Filter
Spectrum’),axis off
subplot(223),imshow(I_OUT_LP),title(‘LowPass Filtered
Output’)
subplot(224),imagesc(0.5*log(1+LP_OUT_S)),title(‘LowPass Spectrum’), axis off
Output:
Exercise3: Apply Ideal lowpass filter.
%ex3.m close all clear
clc
a=imread(‘Lab8_2.tif’); [M N]=size(a); a=im2double(a);
F1=fft2(a); %1.Obtain the Fourier transform
[Type here]
Set up range of variables. u = 0:(M-1); %0-255 v = 0:(N-1);%0-255
center (u,v) = (M/2,N/2)
Compute the indices for use in meshgrid
idx = find(u > M/2);% indices 130-255
u(idx) = u(idx) – M;
idy = find(v > N/2);
v(idy) = v(idy) – N;
%set up the meshgrid arrays needed for
computing the required distances. [U, V] = meshgrid(u, v);
Compute the distances D(U, V).
disp(‘IDEAL LOW PASS FILTERING IN FREQUENCY DOMAIN’); D0=input(‘Enter the cutoff distance==>’);
Begin filter computations. H = double(D 30
Homework
Write a Matlab code to apply highpass laplacian filter on Lab8_1.jpg image.
Write a Matlab code to apply ideal highpass filter on Lab8_1.jpg image for D0=100
Apply FFT2, IFFT2, lowpass Gaussian filter, and highpass laplacian filter onLab8_3.jpg image.
[Type here]