Experiment 6
Experiment 6
Experiment 6
Department of
Computer Engineering and Information Technology
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
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.
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
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
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.
39
Lab Manual Image Processing (CT 704C ) 1721BECE30050
Code:
% MATLAB Code | Ideal Low Pass Filter
% 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;
40
Lab Manual Image Processing (CT 704C ) 1721BECE30050
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
42
Lab Manual Image Processing (CT 704C ) 1721BECE30050
Code:
% MATLAB Code | Butterworth Low Pass Filter
% 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;
43
Lab Manual Image Processing (CT 704C ) 1721BECE30050
Screen Shot:
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);
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));
%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