Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Lab Manual Image Processing (CT 704C ) 1721BECE30050

KADI SARVA VISHWAVIDYALAYA


LDRP INSTITUTE OF TECHNOLOGY AND RESEARCH
GANDHINAGAR

Department of
Computer Engineering and Information Technology

Subject: Image Processing (CT704C-N)

Laboratory Manual

Prepared By

Khamar Sumit N.

1721BECE30050

CE – 7B

1
Lab Manual Image Processing (CT 704C ) 1721BECE30050

INDEX

Practical Date of
Name of Experiment Page Sign
No Submission
1 Introduction to Image Processing Toolbox  3-9  12-July-2020  
2 Read an 8 bit image and then apply different
image enhancement techniques: (a)
Brightness improvement (b) Brightness
reduction (c) Thresholding (d) Negative of an
image (e) Log transformation (f) Power Law  10-21  23-July-2020  
3 Implement different interpolation techniques
using MATLAB/ Scilab.      
4 Read an image, plot its histogram then do
histogram equalization. Comment about the
result.  22-27  01-Aug-2020  
5 (a) Implement Gray level slicing (intensity level
slicing) in to read cameraman image. (b)
Read an 8 bit image and to see the effect of
each bit on the image. (c) Read an image and
to extract 8 different planes i.e. ‘bit plane
slicing.”  28-34  15-Aug-2020  
6 Implement various Smoothing spatial filter.  35-46  20-Aug-2020  
7 Read an image and apply (1) Gaussian 3x3
mask for burring (2) High pass filter mask with
different masks (3) Laplacian operator with
center value positive and negative (4) High
boost filtering.
     
8 Write a program to implement various low
pass filters and high pass filter in frequency      
9 domain.
Write a program for erosion and dilation,
opening & closing using inbuilt and without
inbuilt function.      
10 Implement and study the effect of Different
Mask (Sobel, Prewitt and Roberts).      
11 Implement various noise models and their      

2
Lab Manual Image Processing (CT 704C ) 1721BECE30050

Experiment – 6

Aim: - Implement various Smoothing spatial filter.

Spatial Domain:
1. Average Linear Filter
The response of averaging filter is simply the average of the pixels
contained in the neighborhood of the filter mask. The output of averaging
filters is a smoothed image with reduced "sharp" transitions in gray levels.
Noise and edges consist of sharp transitions in gray levels. Thus smoothing
filters are used for noise reduction; however, they have the undesirable side
effect that they blur edges.
Averaging Filter removes the noise by blurring till it is no longer seen.
It blurs the edges too. Bigger the averaging mass more the blurring.
Sometimes the image contains ‘salt & pepper noise’. If averaging filter is
used then it will remove the noise at the cost of ruined edges. Thus a
nonlinear filter Median filter is required.

Code:
clc;
clear all;
close all;
img = imread('coin_gray.jpg');
imgd = im2double(img); % imgd in [0,1]
%figure,imshow(imgd),imtool(img);;
%% Want to add additional noise
imgd = imnoise(imgd,'salt & pepper',0.22);
% imgd = imnoise(imgd,'speckle',0.12);
% imgd = imnoise(imgd,'gaussian',0.12);
% imgd = imnoise(imgd,'poisson');
figure,imshow(imgd)

%% Averageing Filter

35
Lab Manual Image Processing (CT 704C ) 1721BECE30050

% f1 = fspecial('laplacian');
f1 = ones(3,3)/3;
f2 = ones(3,3)/5;
f3 = ones(3,3)/9;
f4 = ones(3,3)/15;
f5 = ones(3,3)/35;
%% 2-D Filter apply
img1 = filter2(f1, imgd);
img2 = filter2(f2, imgd);
img3 = filter2(f3, imgd);
img4 = filter2(f4, imgd);
img5 = filter2(f5, imgd);
%%
figure,
subplot(3,2,1),imshow(img),title('Original Image');
subplot(3,2,2),imshow(img1),title('Using Average Filter Mask size = 3');
subplot(3,2,3),imshow(img2),title('Using Average Filter Mask size = 5');
subplot(3,2,4),imshow(img3),title('Using Average Filter Mask size = 9');
subplot(3,2,5),imshow(img4),title('Using Average Filter Mask size = 15');
subplot(3,2,6),imshow(img5),title('Using Average Filter Mask size = 35');

Screen Shot:

Conclusion: In this Part we have learnt how to implement the average linear filter
and use different noise and its value to get better result.

36
Lab Manual Image Processing (CT 704C ) 1721BECE30050

2. Order-statistics filters:
a. Median Filter
It replaces the value at the center by the median pixel value in the
neighborhood, (i.e. the middle element after they are sorted). Median
filters are particularly useful in removing impulse noise (also known
as salt-and-pepper noise). Salt = 255, pepper = 0 gray levels. In a
3×3 neighborhood the median is the 5th largest value, in a 5×5
neighborhood the 13th largest value, and so on.

Steps for performing Median filter:

i. Assume a 3x3 empty mask.


ii. Place the empty mask at the Left Hand corner.
iii. Arrange the 9 pixels in ascending or descending order.
iv. Choose the median from these 9 values.
v. Place the median at the Centre.
vi. Move the mask in same manner as averaging filter.
Code:
clc;
clear all;
close all;
I = imread('coin_gray.jpg');
%% Add different noises into the image
J = imnoise(I,'salt & pepper',0.12);
K = imnoise(I,'speckle',0.12);
P = imnoise(I,'gaussian',0.12);
Q = imnoise(I,'poisson');
%% Median Filter
L1 = medfilt2(J);
L2 = medfilt2(K);
L3 = medfilt2(P);
L4 = medfilt2(Q);
%%
figure,imshow(I),title('Original Image');
figure,
subplot(4,2,1);imshow(J),title('With Salt and Pepper Noise');
subplot(4,2,2);imshow(L1),title('After Remove Salt and Pepper Noise');
subplot(4,2,3);imshow(K),title('With Speckle Noise');
subplot(4,2,4);imshow(L2),title('After Remove Speckle Noise');
subplot(4,2,5);imshow(P),title('With Gaussian Noise');
subplot(4,2,6);imshow(L3),title('After Remove Gaussian Noise');
subplot(4,2,7);imshow(Q),title('With Poisson Noise');
subplot(4,2,8);imshow(L4),title('After Remove Poisson Noise');

37
Lab Manual Image Processing (CT 704C ) 1721BECE30050

Screen Shot:

Conclusion: In this Practical we have learnt how to apply the median filter(Known
as Salt and Pepper Noise) on the image with different noise and observe the
changes with respect to different noise

Special Function for Filters:

fspecial ():-
Matlab provides a method to create a predefined 2-D filter. Its fspecial():
h = fspecial(type)
h = fspecial(type, parameters)
h = fspecial(type) creates a two-dimensional filter h of the specified type. It
returns h as a correlation kernel, which is the appropriate form to use
with imfilter(). The type is a string having one of these values

Value Description
average Averaging filter
disk Circular averaging filter (pillbox)
gaussian Gaussian low pass filter

38
Lab Manual Image Processing (CT 704C ) 1721BECE30050

laplacian Laplacian of Gaussian filter


motion Approximates the linear motion of a camera
prewitt Prewitt horizontal edge-emphasizing filter
sobel Sobel horizontal edge-emphasizing filter

Frequency Domain:
1. Ideal low pass Filter
ILPF is the simplest lowpass filter that “cuts off” all high frequency
components of the DFT that are at a distance greater than a specified
distance D0 from the origin of the (centered) transform.

Steps for performing IDLF:


i. Input – Read an image
ii. Saving the size of the input image in pixels
iii. Get the Fourier Transform of the input image
iv. Assign the Cut-off Frequency 
v. Designing filter: Ideal Low Pass Filter
vi. Convolution between the Fourier Transformed input image and
the filtering mask
vii. Take Inverse Fourier Transform of the convoluted image

39
Lab Manual Image Processing (CT 704C ) 1721BECE30050

viii. Display the resultant image as output

Code:
% MATLAB Code | Ideal Low Pass Filter

% Reading input image : input_image


input_image = imread('testpattern.tif');

% Saving the size of the input_image in pixels-


% M : no of rows (height of the image)
% N : no of columns (width of the image)
[M, N] = size(input_image);

% Getting Fourier Transform of the input_image


% using MATLAB library function fft2 (2D fast fourier transform)
FT_img = fft2(double(input_image));

% Assign Cut-off Frequency


D0 = 150; % one can change this value accordingly d0=1[5,20,50,100,150}

% Designing filter
u = 0:(M-1);
idx = find(u>M/2); %find indices of nonzero elements
u(idx) = u(idx)-M;
v = 0:(N-1);
idy = find(v>N/2);
v(idy) = v(idy)-N;

% MATLAB library function meshgrid(v, u) returns


% 2D grid which contains the coordinates of vectors
% v and u. Matrix V with each row is a copy
% of v, and matrix U with each column is a copy of u
[V, U] = meshgrid(v, u); % It produce the coordinates of a rectangular grid
(X, Y).

% Calculating Euclidean Distance


D = sqrt(U.^2+V.^2);

% Comparing with the cut-off frequency and


% determining the filtering mask
H = double(D <= D0);

% Convolution between the Fourier Transformed


% image and the mask
G = H.*FT_img;

% Getting the resultant image by Inverse Fourier Transform


% of the convoluted image using MATLAB library function
% ifft2 (2D inverse fast fourier transform)
output_image = real(ifft2(double(G)));

% Displaying Input Image and Output Image


subplot(2, 1, 1), imshow(input_image),title('Original Image');

40
Lab Manual Image Processing (CT 704C ) 1721BECE30050

subplot(2, 1, 2), imshow(output_image, [ ]),title('After applying IDLF');

Screen Shot:

D0=5 D0=20

D0=50 D0=100

D0=150

Conclusion: In this Practical we have seen how to apply the ideal low pass filter
and observe the changes in the image.

41
Lab Manual Image Processing (CT 704C ) 1721BECE30050

2. Butterworth Low Pass Filter


The BLPF of order n and with cutoff frequency at a distance D 0 from the
origin is defined as Unlike ILPF, the BLPF transfer function does not have a
sharp transition that establishes a clear cutoff between passed and filtered
frequencies. Instead, BLPF has a smooth transition between low and high
frequencies.

Steps for performing BLPF:


i. Input – Read an image
ii. Saving the size of the input image in pixels
iii. Get the Fourier Transform of the input_image
iv. Assign the order n and cut-off frequency D0
v. Designing filter: Butterworth Low Pass Filter
vi. Convolution between the Fourier Transformed input image and the
filtering mask
vii. Take Inverse Fourier Transform of the convoluted image
viii. Display the resultant image as output

42
Lab Manual Image Processing (CT 704C ) 1721BECE30050

Code:
% MATLAB Code | Butterworth Low Pass Filter

% Reading input image : input_image


input_image = imread('testpattern.tif');

% Saving the size of the input_image in pixels-


% M : no of rows (height of the image)
% N : no of columns (width of the image)
[M, N] = size(input_image);

% Getting Fourier Transform of the input_image


% using MATLAB library function fft2 (2D fast fourier transform)
FT_img = fft2(double(input_image));

% Assign the order value


n = 15; % {2,5,10,15} % one can change this value accordingly

% Assign Cut-off Frequency


D0 = 150; % {20,50,100,150} % one can change this value accordingly

% Designing filter
u = 0:(M-1);
v = 0:(N-1);
idx = find(u > M/2); %Find indices of nonzero elements.
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;

% MATLAB library function meshgrid(v, u) returns


% 2D grid which contains the coordinates of vectors
% v and u. Matrix V with each row is a copy of v
% and matrix U with each column is a copy of u
[V, U] = meshgrid(v, u);

% Calculating Euclidean Distance


D = sqrt(U.^2 + V.^2);

% determining the filtering mask


H = 1./(1 + (D./D0).^(2*n));

% Convolution between the Fourier Transformed


% image and the mask
G = H.*FT_img;

% Getting the resultant image by Inverse Fourier Transform


% of the convoluted image using MATLAB library function
% ifft2 (2D inverse fast fourier transform)
output_image = real(ifft2(double(G)));

% Displaying Input Image and Output Image


subplot(2, 1, 1), imshow(input_image),title('Original Image');
subplot(2, 1, 2), imshow(output_image, [ ]),title('After applying BLPF');

43
Lab Manual Image Processing (CT 704C ) 1721BECE30050

Screen Shot:

D0=20, n=5 D0=50, n=5

D0=100, n=10 D=150, n=15

Conclusion: In this part we have learnt how to apply the butterworth low pass
filter with different value of n and do and also observe the changes in the images.

44
Lab Manual Image Processing (CT 704C ) 1721BECE30050

3. Gaussian Filter
The GLPF with cutoff frequency D0 is defined as: Unlike ILPF, the GLPF
transfer function does not have a sharp transition that establishes a clear
cutoff between passed and filtered frequencies. Instead, GLPF has a smooth
transition between low and high frequencies.

Code:
% %% Gaussian Filter using Built-in Function
% %Read an Image
% Img = imread('coins.tif');
% A = imnoise(Img,'Gaussian',0.04,0.003);
% %Image with noise
%
% % H = fspecial('Gaussian',[9 9],1.76);
% GaussF = imfilter(A,H);
%
% figure,subplot(1,2,1),imshow(A),title('Original Image');
% subplot(1,2,2),imshow(GaussF),title('With Gaussian Filter')
%% Gaussian Filter without using Built-in Function
clc
clear all
close all
%Read an Image
Img = imread('testpattern.tif');
A = imnoise(Img,'Gaussian',0.04,0.003);

%Image with noise


%figure,imshow(A);
I = double(A);

%Design the Gaussian Kernel


%Standard Deviation

45
Lab Manual Image Processing (CT 704C ) 1721BECE30050

sigma = 1.76;

%Window size
sz = 1;
[x,y]=meshgrid(-sz:sz,-sz:sz);

M = size(x,1)-1;
N = size(y,1)-1;
Exp_comp = -(x.^2+y.^2)/(2*sigma*sigma);
Kernel= exp(Exp_comp)/(2*pi*sigma*sigma);

%Initialize
Output=zeros(size(I));

%Pad the vector with zeros


I = padarray(I,[sz sz]);

%Convolution
for i = 1:size(I,1)-M
for j =1:size(I,2)-N
Temp = I(i:i+M,j:j+M).*Kernel;
Output(i,j)=sum(Temp(:));
end
end
%Image without Noise after Gaussian blur
Output = uint8(Output);
%figure,imshow(Output);
figure,subplot(1,2,1),imshow(A),title('Original Image');
subplot(1,2,2),imshow(Output),title('With Gaussian Filter')

Screen Shot:

Conclusion: In this Part we have learnt how to apply the Gaussian filter on the
image and observe after applying we are getting darker image.

46

You might also like