Sie sind auf Seite 1von 67

IMAGE PROCESSING

B.Tech.
SEM. VII (EC)

DEPARTMENT OF ELECTRONICS AND COMMUNICATION,


FACULTY OF TECHNOLOGY, NADIAD

INDEX
CONTENTS

NO.
C ONT ENT S

INTRODUCTION TO IMAGE PROCESSING


TOOLBOX AND FUNCTION

FUNDAMENTAL IMAGE PROCESSING

INTENSITY TRANSFORMATION

FILTERING IN SPATIAL DOMAIN

FILTERING IN FREQUENCY DOMAIN

IMAGE RESTORATION

COLOR IMAGE PROCESSING

EDGE DETECTION

MORPHOLOGY

DATE

P.NO.

IMAGE PROCESSING

EXPERIMENT-1
INTRODUCTION TO IMAGE PROCESSING TOOLBOX AND
FUNCTION
Date:___________
Objective:
To understand basic functions of Image Processing Toolbox in SciLab.

Introduction:
When working with images in SciLab, there are many things to keep in mind such as loading an
image, using the right format, saving the data as different data types, how to display an image,
conversion between different image formats, etc.

 Different Functions for Image Processing Application:


1. Imread
Reads a grayscale or color image from the file specified by the string filename.
Syntax:
im= imread(filename. format)
im=imread(cameraman.tif)
Description:
The text string format specifies the format of the file by its standard file extension. For
example, specify 'gif' for Graphics Interchange Format files.
To read image from hard disk first we need to get path of that image using getSIVPpath function
like..
SIVP_Path=getSIVPpath();
im=imread(SIVP_Path + 'images\lena.png');

2. Imwrite
Writes the image A to the file specified in File name the format specified by fmt.
Syntax:
imwrite(A,filename.fmt)
imwrite(a,rand.png)
Description:
imwrite(A,filename,fmt) writes the matrix A to the file specified by filename in the
format specified by fmt. A can be an M-by-N (grayscale image) or M-by-N-by-3 (true color
image) array, but it cannot be an empty array.

DHARAMSINH DESAI UNIVERSITY

IMAGE PROCESSING

3. Imfinfo
Returns a structure whose fields contain information about an image in a graphics file.
Syntax:
info = imfinfo(filename.fmt)
b=imfinfo(lena.png)
Description:
It provides all information about image. For e.g. file size, compression methods etc. Following is
the output for the same.
Output:
Filename:C:\Users\BAPS\AppData\Roaming\SciLab\SCILAB~1.1\atoms\SIVP\053~1.12\images\lena.png

FileSize: 556294
Width: 512
Height: 512
BitDepth: 8
ColorType: 'truecolor'

4. Imshow
Displays the intensity image with n discrete levels of gray.
Syntax:
SIVP_Path=getSIVPpath();
im=imread(SIVP_Path + 'images\lena.png');
imshow(a)
imshow(lena.png)
Output:

Figure 1:IMAGE SHOW

DHARAMSINH DESAI UNIVERSITY

IMAGE PROCESSING

5. rgb2gray():
Converts rgb or color image to gray scale image. X = rgb2gray(I), converts the rgb image I to a
gray scale image X.
Syntax:
SIVP_Path=getSIVPpath();
im=imread(SIVP_Path + 'images\lena.png');
imshow(im);
a = rgb2gray(im);
imshow (a);
Output:

Figure 3:RGB

Figure 2:GRAY SCALE

6. Imcrop
imcrop function crops the image according to our specification.
Syntax:
rect=[x, y, width, height] is a vector. (x, y) is the top-left corner of the rentangle.
b = imcrop(im, [100, 30, 300, 300])
imshow(b)
Output:

Figure 4:CROPED IMAGE

DHARAMSINH DESAI UNIVERSITY

IMAGE PROCESSING

7.

Imresize
Resizing an image with respect to the specified dimensions.
Syntax:
H = imresize(a,[100 150]);
imshow(H);
k=imresize(a,0.5);
imshow(k);
Output:

Figure 5:0.5 TIMES ORIGINAL


Figure 6:RESIZED IMAGE

Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________

DHARAMSINH DESAI UNIVERSITY

IMAGE PROCESSING

EXPERIMENT-2
FUNDAMENTAL IMAGE PROCESSING
Date:__________
Objective:
I. Find out average intensity of any Gray Scale Image
II. Apply negative transformation on any gray scale Image
III. Flipping the gray scale Image

Average Intensity of Gray Scale Image:


Program:
clc;
clear all;
close;
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
[r p]=size(gray);
K=0;
for i=1:r
for j=1:p
k=k+gray(i,j);
end
end
h=k/(r*p);
Output:
h=0.4865

Image Negative:
Program:
clc;
clear all;
close;
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
gray=double(gray);
[r p]=size(gray);
for i=1:r
for j=1:p
k(i,j)=255-gray(i,j);
end
end

DHARAMSINH DESAI UNIVERSITY

IMAGE PROCESSING

nw=uint8(k);
figure,ShowImage(gray,'original');
title('original');
figure,ShowImage(nw,'Negetive');
title('Negetive');
Output:

Figure 7:NEGETIVE IMAGE

 Image Flipping:
1.

Image Flip(Left to Right)


Program:
clc;
clear all;
close;
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
gray=im2double(gray);
[r p]=size(gray);
for i=1:r
for j=1:p
k(i,j)=gray(i,p-j+1);
end
end
figure,ShowImage(gray,'original');
title('original');
figure,ShowImage(k,'Flip');
title('fliped');

DHARAMSINH DESAI UNIVERSITY

IMAGE PROCESSING

Output:

Figure 8:ORIGINAL IMAGE

2.

Figure 9:FLIPED IMAGE

Image Flip(Up to Down)


Program:
clc;
clear all;
close;
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
gray=im2double(gray);
[r p]=size(gray);
for i=1:r
for j=1:p
k(i,j)=gray(r-i+1,j);
end
end
figure,ShowImage(gray,'original');
title('original');
figure,ShowImage(k,'Flip');
title('fliped');
Output:

DHARAMSINH DESAI UNIVERSITY

IMAGE PROCESSING

3.

Image Flip(Edge to Edge)


Program:
clc;
clear all;
close;
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
gray=im2double(gray);
[r p]=size(gray);
for i=1:r
for j=1:p
k(i,p-j+1)=gray(r-i+1,j);
end
end
figure,ShowImage(gray,'original');
title('original');
figure,ShowImage(k,'Flip');
title('fliped');
Output:

Conclusion:

DHARAMSINH DESAI UNIVERSITY

10

IMAGE PROCESSING

EXPERIMENT-3
INTENSITY TRANSFORMATIONS
Date:__________
Objective:
I.
II.

To apply Log Transform on gray scale Image

III.

To apply Power Log Transform on gray scale Image

IV.

To apply Contrast Straching intensity transformation on any gray scale Image

V.
VI.
VII.

To observe Histogram of any gray scale Image

To observe Bit-plane Slicing of any gray scale Image


To observe checker-board effect due to different sampling rate
To observe False contouring effect due to no. of graylevel

Histogram Processing:
Program:
clc;
clear all;
close;
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
k=zeros(1,256);
[r p]=size(gray);
for i=1:r
for j=1:p
x=gray(i,j);
k(x)=k(x)+1;
end
end
figure,plot(k);
title('Histogram using udf');
m=imhist(gray);
figure,plot(m);
title('Histogram using sciEXPERIMENT function');
Output:

Figure 10:BY SCIEXPERIMENT FUNCTION

DHARAMSINH DESAI UNIVERSITY

11

IMAGE PROCESSING

Figure 11:BY UDF

Log Transform:

Program:
clc;
clear all;
close;
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
gray=im2double(gray);
[r p]=size(gray);
for i=1:r
for j=1:p
k(i,j)=log10(1+gray1(i,j));
end
end
nw=uint8(k);
figure,ShowImage(gray,'original');
title('original');
figure,ShowImage(k,'Flip');
title('Log Transform');
Output:

Power Log Transform:

Figure 12:LOG TRANSFORM

Program:
clc;
clear all;
close;
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
gray1=im2double(gray);
[r p]=size(gray1);

DHARAMSINH DESAI UNIVERSITY

12

IMAGE PROCESSING

for n=0.1:0.2:2
for i=1:r
for j=1:p
k(i,j)=gray1(i,j).^n;
end
end
figure,ShowImage(k,'log transform');
end
Output:

Figure 16:GAMMA=0.2

Figure 15:GAMMA=0.4

Figure 17:GAMMA=1.8

Figure 18:GAMMA=2

Figure 14:GAMMA=0.6

Figure 13:GAMMA=0.8

Contrast Streching:
Program:
clc;
clear all;
close;
xdel(winsid());
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);

DHARAMSINH DESAI UNIVERSITY

13

IMAGE PROCESSING

r1=95
r2=135
s1=25
s2=215
m1=(s1)/r1;
m2=(s2-s1)/(r2-r1);
m3=(256-s2)/(256-r2);
figure,ShowImage(gray,'original');
[r p]=size(gray);
for i=1:r
for j=1:p
if (gray(i,j)<r1) then
k(i,j)=m1*gray(i,j);
end
if ((gray(i,j)>=r1) & (gray(i,j)<=r2)) then
k(i,j)=m2*gray(i,j);
end
if (gray(i,j)>r2) then
k(i,j)=m3*gray(i,j);
end
end
end
figure,ShowImage(k,'contrast streching');
title('contrast streching');
Output:

Bit-plane Slicing:
Program:
clc;
clear all;
close;
xdel(winsid());
SIVP_Path=getSIVPpath()
a=imread(SIVP_Path + 'images\round.tif');
for i=1:8
k=mtlb_logical(bitget(a,i));

DHARAMSINH DESAI UNIVERSITY

Figure 19:ORIGNAL IMAGE

14

IMAGE PROCESSING

figure,ShowImage(k,'bit-plane slicing');
end
Output:

Figure 21:MSB

Figure 20:LSB

Checker-board Effect:
Program:
clc;
clear all;
close;
xdel(winsid());
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
gray=im2double(gray);
figure,ShowImage(gray,'original');
[r p]=size(gray);
for m=2:9
for i=1:(r/m)
for j=1:(p/m)
k(i,j)=gray(i*m,j*m);
end
end
figure,ShowImage(k,'Checker-board EFFECT');
clear k;
end

DHARAMSINH DESAI UNIVERSITY

15

IMAGE PROCESSING

Output:

Figure 22: SAMPLING RATE=2

Figure 26: SAMPLING RATE=3

Figure 27: SAMPLING RATE=4

Figure 25: SAMPLING RATE=5

Figure 24: SAMPLING RATE=6

Figure 23: SAMPLING RATE=7

Figure 28: SAMPLING RATE=8

False Countering Effect:


Program:
clc;
clear all;
close;
xdel(winsid());

DHARAMSINH DESAI UNIVERSITY

16

IMAGE PROCESSING

SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
[r p]=size(gray);
for i=1:r
for j=1:p
if ((gray(i,j)>=0) & (gray(i,j)<=25)) then
k(i,j)=13;
end
if ((gray(i,j)>25) & (gray(i,j)<=50)) then
k(i,j)=38;
end
if ((gray(i,j)>50) & (gray(i,j)<=75)) then
k(i,j)=63;
end
if ((gray(i,j)>75) & (gray(i,j)<=100)) then
k(i,j)=88;
end
if ((gray(i,j)>100) & (gray(i,j)<=125)) then
k(i,j)=113;
end
if ((gray(i,j)>125) & (gray(i,j)<=150)) then
k(i,j)=138;
end
if ((gray(i,j)>150) & (gray(i,j)<=175)) then
k(i,j)=163;
end
if ((gray(i,j)>175) & (gray(i,j)<=200)) then
k(i,j)=188;
end
if ((gray(i,j)>200) & (gray(i,j)<=225)) then
k(i,j)=213;
end
if ((gray(i,j)>225) & (gray(i,j)<=255)) then
k(i,j)=238;
end
end
end
figure,ShowImage(k,'False Countering Effect');

DHARAMSINH DESAI UNIVERSITY

17

IMAGE PROCESSING

Output:

Figure 29:FALSE CONTURING WITH 10 INTENSITY LEVEL

Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
ASSIGNMENT


Bit-plane slicing using user defined function

DHARAMSINH DESAI UNIVERSITY

18

IMAGE PROCESSING

EXPERIMENT-4
FILTERING IN SPATIAL DOMAIN
Date:__________
Objective:
I. To observe effect of various order of Averaging Filter
II. Comparison of Median Filter and Averaging Filter
III. To observe effect of various type of Mask on Image

To Observe Effect of Various Order of Averaging Filter


Program:
clc;
clear;
xdel(winsid());
path=getSIVPpath();
im=imread(path+"images\lena.png");
gray=rgb2gray(im);
ShowImage(gray,"gray image");
title("grayscale",'fontsize',3 );
[row,column]=size(gray);
for i=3:10
filter=fspecial('average',i)
imfilt=imfilter(gray,filter)
figure,ShowImage(imfilt,'filtered image');
title('averaging filter, order = '+string(i));
end
Output:

DHARAMSINH DESAI UNIVERSITY

19

IMAGE PROCESSING

Comparision of median and averaging filters


Program:
clc;
clear;
xdel(winsid());
path=getSIVPpath();
im=imread(path+"images\lena.png");
gray=rgb2gray(im);
ShowImage(gray,"gray image");
title("grayscale",'fontsize',3 );
noisyim=imnoise(gray,'salt & pepper');
figure,ShowImage(noisyim,'noisy image');
title('image with salt and pepper noise','fontsize',3);
filter=fspecial('average');
imfilt=imfilter(noisyim,filter);
figure,ShowImage(imfilt,'filtered image');
title('Average filter','fontsize',3);
medianimage=MedianFilter(noisyim,[3 3]);
figure,ShowImage(medianimage,'median');
title('Median filter','fontsize',3);

DHARAMSINH DESAI UNIVERSITY

20

IMAGE PROCESSING

Output:

Figure 30:GRAY SCALE IMAGE

Figure 33:AVERAGING MASK

Figure 31:SOLT AND PEPPER NOISE ADDED

Figure 32:MEDIAN MASK

To Observe Effect of Various Type of Mask On Image


Program:
clc;
clear;
xdel(winsid());
path=getSIVPpath();
im=imread(path+"images\lena.png");
gray=rgb2gray(im);
ShowImage(gray,"gray image");
title("grayscale",'fontsize',3 );
[row,column]=size(gray);
//to apply and observe the effect of sobel mask
filter = fspecial('sobel');
imfilt=imfilter(gray,filter);

DHARAMSINH DESAI UNIVERSITY

21

IMAGE PROCESSING

figure,ShowImage(imfilt,'filtered image')
title('Effect of sobel filter','fontsize',3);
//to apply and observe the effect of prewitt mask
filter = fspecial('prewitt');
imfilt=imfilter(gray,filter);
figure,ShowImage(imfilt,'filtered image')
title('Effect of prewitt filter','fontsize',3);
//to apply and observe the effect of gaussian mask
filter = fspecial('gaussian');
imfilt=imfilter(gray,filter);
figure,ShowImage(imfilt,'filtered image')
title('Effect of gaussian filter','fontsize',3);
//to apply and observe the effect of laplacian mask
filter = fspecial('laplacian');
imfilt=imfilter(gray,filter);
figure,ShowImage(imfilt,'filtered image')
title('Effect of laplacian filter','fontsize',3);
//to apply and observe the effect of log mask
filter = fspecial('log');
imfilt=imfilter(gray,filter);
figure,ShowImage(imfilt,'filtered image')
title('Effect of logarithmic filter','fontsize',3);
//to apply and observe the effect of average mask
filter = fspecial('average');
imfilt=imfilter(gray,filter);
figure,ShowImage(imfilt,'filtered image')
title('Effect of averaging filter','fontsize',3);
//to apply and observe the effect of unsharp mask
filter = fspecial('unsharp');
imfilt=imfilter(gray,filter);
figure,ShowImage(imfilt,'filtered image')
title('Effect of unsharp filter','fontsize',3);
Output:

Figure 36:GRAY SACLE IMAGE

DHARAMSINH DESAI UNIVERSITY

Figure 35:SOBEL MASK

Figure 34:PREWITT MASK

22

IMAGE PROCESSING

Figure 39:GAUSSIAN MASK

Figure 38:LAPLACIAN MASK

Figure 41:AVERAGE MASK

Figure 40:UNSHARP MASK

Figure 37:LOGARITHMIC MASK

Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
DHARAMSINH DESAI UNIVERSITY

23

IMAGE PROCESSING

ASSIGNMENTS


Averaging filter using user defined filter

Median filter using user defined filter

DHARAMSINH DESAI UNIVERSITY

24

IMAGE PROCESSING

EXPERIMENT-5
FILTERING IN FREQUENCY DOMAIN
Date:__________
Objective:
I. To observe effect of Low-pass Filter
II. To observe effect of High-pass Filter

I. To Observe Effect of Low-pass Filter


Program:
clc;
close;
clear;
xdel(winsid())
function [H]=lowpassfilter(type1, M, N, D0, n)
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[U,V]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
select type1

//to close all currently open figure(s).


//lowpassfilter is used to filter an image .

case'ideal'then
H=double(D<=D0);
case'butterworth'then
if argn(2)==4 then
n=1;
end
H = ones(M,N)./(1+(D./D0).^(2*n));
case'gaussian'then
H=exp(-(D.^2)./(2*(D0^2)));
else
disp('Unknownfiltertype.')
end
endfunction
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\Chapter4_2.tif');

DHARAMSINH DESAI UNIVERSITY

25

IMAGE PROCESSING

gray=im2double(a);
figure,ShowImage(gray,'Gray Image');
title('Original Image');
[M,N]=size(gray);
h=fft2(gray);
i=log(1+abs(h));
in=fftshift(i);

//fft2() is used to find 2-Dimensional Fast Fourier Transform of an matrix


//fftshift() is used to rearrange the fft output, moving the zero frequency to the
center of the spectrum.

inm=mat2gray(in)
figure,ShowImage(inm,'Frequency Spectrum');
title('Frequency Spectrum');
filt=lowpassfilter('gaussian',M,N,100);

// User Define Function which generate Filter


Mask Corresponding to Low Frequency

filt_shift=fftshift(filt);
figure,ShowImage(filt_shift,'Filter Mask');
title('Filter Mask to Specific Cut-Off Frequency');
n=filt.*h;
//Multiply the Original Spectrum with the Filter Mask.
Image_filter=real(ifft(n));
Image_filter=mat2gray(Image_filter)
figure,ShowImage(Image_filter,'Filtered Image');
title('Filtered Image with Specific Cut-Off Frequency');
OUTPUT:

Figure 43:ORIGINAL IMAGE

DHARAMSINH DESAI UNIVERSITY

Figure 42:FFT

26

IMAGE PROCESSING

Figure 45:IDEAL FILTER CUT-OFF=10

Figure 47:IDEAL FILTER CUT-OFF=100

Figure 48:IDEAL FILTER CUT-OFF=500

DHARAMSINH DESAI UNIVERSITY

Figure 44:FILTERED IMAGE

Figure 46:FILTERED IMAGE

Figure 49:FILTERED IMAGE

27

IMAGE PROCESSING

Figure 51:BUTTERWORTH FILTER ORDER=1

Figure 50:FILTERED IMAGE

Figure 53:BUTTERWORTH FILTER ORDER=2

Figure 52:FILTERED IMAGE

Figure 54:BUTTERWORTH FILTER ORDER=7

Figure 55:FILTERED IMAGE

DHARAMSINH DESAI UNIVERSITY

28

IMAGE PROCESSING

Figure 57:GAUSSIAN FILTER

II.

Figure 56:FILTERED IMAGE

To Observe Effect of High-pass Filter


Program:

clc;
close;
clear;
xdel(winsid());
function [x]=filt(ty, m, n, d0, n0)
y=zeros(m,n);
x=zeros(m,n);
for i=1:m
for j=1:n
d(i,j)=sqrt(((m/2-i)*(m/2-i)+((n/2-j)*(n/2-j))));
end
end
select ty
case 'ideal' then
for i=1:m
for j=1:n
if(d(i,j)<=d0) then
y(i,j)=1;
x(i,j)=1-y(i,j);
else
x(i,j)=0;
x(i,j)=1-y(i,j);
end
end
end
case 'butterworth' then
if argn(2)==4 then

DHARAMSINH DESAI UNIVERSITY

29

IMAGE PROCESSING

n0=1;
end
y = ones(m,n)./(1+(d./d0).^(2*n0));
x=ones(m,n)-y;
case 'gaussian' then
y=exp(-(d.^2)./(2*(d0^2)));
x=ones(m,n)-y;
end
endfunction
SIVP_Path=getSIVPpath();
A1=imread(SIVP_Path + 'images\Chapter4_2.tif');
ShowImage(A1,'Actual image');
A1=im2double(A1);
B=fft2(A1);
E=fftshift(B);
figure;
ShowImage(abs(E),'Centralised Log transformed FFT image');
[rw cl]=size(B);
F=filt('butterworth',rw,cl,20,2);
G=E.*F;
figure;
ShowImage(abs(G),'Filtered image');
E2=fftshift(G);
F2=abs(ifft(E2));
F2=mat2gray(F2);
figure;
ShowImage(F2,'Filtered image1');
OUTPUT:

Figure 58:IDAEL FILTER CUT-OFF=10

DHARAMSINH DESAI UNIVERSITY

Figure 59:FILTERED IMAGE

30

IMAGE PROCESSING

Figure 60:IDEAL FILTER CUT-OFF=100

Figure 63:IDAEL FILTER CUT-OFF=500

Figure 64:BUTTERWORTH FILTER ORDER=1

DHARAMSINH DESAI UNIVERSITY

Figure 61:FILTERED IMAGE

Figure 62:FILTERED IMAGE

Figure 65:FILTERED IMAGE

31

IMAGE PROCESSING

Figure 67:BUTTERWORTH FILTER


ORDER=10

Figure 69:BUTTERWORTH FILTER


ORDER=7

Figure 71:GAUSSIAN FILTER

Figure 66:FILTERED IMAGE

Figure 68:FILTERED IMAGE

Figure 70:FILTERED IMAGE

Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________

DHARAMSINH DESAI UNIVERSITY

32

IMAGE PROCESSING

__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
_________________________________________________________________

DHARAMSINH DESAI UNIVERSITY

33

IMAGE PROCESSING

EXPERIMENT-6
IMAGE RESTORATION
Date:__________
Objective:
I. To observe effect of Varies noise on Image
II. To observe Probability Distribution Function of different noise
III.

To implement the Following Order Statistic Restoration filter Median, Max, Min, Mid
Point, Alpha trimmed.
IV.
To implement the Following Mean Restoration filter Arithmetic, Geometric,
Harmonic, Contra Harmonic.

I.

To Observe Effect of Varies noise on Image


clc;
close;
clear;
xdel(winsid());
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
a=rgb2gray(a);
a=im2double(a);
a=imresize(a,[512 512],'bicubic');
B=imnoise(a,'gaussian',0.05,0.0005);
figure,ShowImage(B,'gaussian');
C=imnoise(a,'salt & pepper');
figure,ShowImage(C,'salt & pepper');
OUTPUT:

Figure 73:IMAGE WITH GAUSSIAN NOISE

DHARAMSINH DESAI UNIVERSITY

Figure 72:IMAGE WITH SALT & PEPPER NOISE

34

IMAGE PROCESSING

II.

To Observe PDF of Varies noise

clc;
close;
clear;
xdel(winsid())//to close all currently open figure(s).
function R=imnoise2(type, M, N, a, b)
if argn(2)==3 then
a=0; b=1;
end
select type
case'gaussian'then
rand("normal")
R=a+b*rand(M,N);
case'uniform'then
R=a+(b-a)*rand(M,N,"uniform");
case'salt & pepper'then
if argn(2)==3
a = 0.15; b = 0.15;
end
if (a+b) > 1
error('The sum Pa + Pb must not exceed 1.');
end
R(1:M,1:N) = 0.5;
X = rand(M,N);
[r c] = find(X<=a);
for i=1:length(r)
R(r(i),c(i)) = 0;
end
u = a + b;
[r c] = find(X>a & X<=u);
for i=1:length(r)
R(r(i),c(i)) = 255;
end
case'lognormal'then
if argn(2)==3
a = 1; b = 0.25;
end
R = a*exp(b*mtlb_randn(M,N));
case'rayleigh'then
if argn(2)==3
a = 1; b = 0.25;
end
R = a + ((-b)*(log(1-rand(M,N,"uniform")))).^0.5;
case'exponential'then
if argn(2)==3
a = 1;
end

DHARAMSINH DESAI UNIVERSITY

35

IMAGE PROCESSING

if a<=0
error('Parameter a must be positive for exponential type.');
end
k = -1/a;
R = k*log(1-rand(M,N,"uniform"));
case'erlang'then
if (b ~= round(b) | b <= 0)
error('Param b must be positive for integer for Erlang.')
end
k = -1/a;
R = zeros(M,N);
for j=1:b
R = R + k*log(1-rand(M,N,"uniform"));
end
else
disp('Unknownfiltertype.')
end
endfunction
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\lena.png');
gray=rgb2gray(a);
figure,ShowImage(gray,'Gray Image');
title('Original Image');
[M,N]=size(gray);
[count,cell]=imhist(gray);
figure,bar(cell,count,0.2);
mtlb_axis([0 255 0 35000]);
title('Histogram of Original Image');
/////////////////////////////////////// Gaussian Noise ////////////////////
r1=imnoise2('gaussian',M,N,15,5); // Generate Gaussian Noise with Given Mean and Variance
gray_noise_gaussian=gray+(r1);
figure,ShowImage(gray_noise_gaussian,'Gray Image with Noise');
title('Gray Image with Noise gaussian');
[count,cell]=imhist(gray_noise_gaussian);
figure,bar(cell,count,1.2);
mtlb_axis([0 255 0 3000]);
/////////////////////////////////////// Uniform Noise ////////////////////
r2=imnoise2('uniform',M,N,0,20); // Generate uniform Noise
gray_noise_uniform=gray+(r2);
figure,ShowImage(gray_noise_uniform,'Gray Image with Noise');
title('Gray Image with Noise uniform');
[count,cell]=imhist(gray_noise_uniform);
figure;bar(cell,count,1.2);
mtlb_axis([0 255 0 2000]);

DHARAMSINH DESAI UNIVERSITY

36

IMAGE PROCESSING

/////////////////////////////////////// Salt & pepper Noise ////////////////////


r3=imnoise2('salt & pepper',M,N,0.15,0.15); // Generate salt & pepper Noise
gray_noise_salt_pepper=gray+(r3);
figure,ShowImage(gray_noise_salt_pepper,'Gray Image with Noise');
title('Gray Image with Noise salt&pepper');
[count,cell]=imhist(gray_noise_salt_pepper);
figure,bar(cell,count,1.2);
mtlb_axis([0 255 0 35000]);
/////////////////////////////////////// lognormal Noise ////////////////////
r4=imnoise2('lognormal',M,N,5,0.65); // Generate lognormal Noise
gray_noise_lognormal=gray+(r4);
figure,ShowImage(gray_noise_lognormal,'Gray Image with Noise');
title('Gray Image with Noise lognormal');
[count,cell]=imhist(gray_noise_lognormal);
figure,bar(cell,count,1.2);
mtlb_axis([0 255 0 5500]);
/////////////////////////////////////// Rayleigh Noise ////////////////////
r5=imnoise2('rayleigh',M,N,0,55); // Generate rayleigh Noise
gray_noise_rayleigh=gray+(r5);
figure,ShowImage(gray_noise_rayleigh,'Gray Image with Noise');
title('Gray Image with Noise rayleigh');
[count,cell]=imhist(gray_noise_rayleigh);
figure,bar(cell,count,1.2);
mtlb_axis([0 255 0 4000]);
/////////////////////////////////////// Exponential Noise ////////////////////
r6=imnoise2('exponential',M,N,0.15); //Generate exponential Noise
gray_noise_exponential=gray+(r6);
figure,ShowImage(gray_noise_exponential,'Gray Image with Noise');
title('Gray Image with Noise exponential');
[count,cell]=imhist(gray_noise_exponential);
figure,bar(cell,count,1.2);
mtlb_axis([0 255 0 4500]);
/////////////////////////////////////// Erlang Noise ////////////////////
r7=imnoise2('erlang',M,N,2,15); // Generate erlang Noise
gray_noise_erlang=gray+(r7);
figure,ShowImage(gray_noise_erlang,'Gray Image with Noise');
title('Gray Image with Noise erlang');
[count,cell]=imhist(gray_noise_erlang);
figure,bar(cell,count,1.2);
mtlb_axis([0 255 0 9500]);

DHARAMSINH DESAI UNIVERSITY

37

IMAGE PROCESSING

OUTPUT:

Figure 75: PDF


Figure 74: ORIGINAL IMAGE

Figure 77: GAUSSIAN PDF


Figure 76: GAUSSIAN NOISE

Figure 79: UNIFORM PDF


Figure 78: UNIFORM NOISE

DHARAMSINH DESAI UNIVERSITY

38

IMAGE PROCESSING

Figure 81: SALT & PEPPER NOISE

Figure 83: LOGNORMAL NOISE

Figure 85: RAYLEIGH NOISE

Figure 86: EXPONENTIAL NOISE

DHARAMSINH DESAI UNIVERSITY

Figure 80: SALT & PEPPER PDF

Figure 82: LOGNORMAL PDF

Figure 84: RAILEGH PDF

Figure 87: EXPONENTIAL PDF

39

IMAGE PROCESSING

Figure 88: ERLANG NOISE

III.

Figure 89: ERLANG PDF

To implement the Following Order Statistic Restoration filter Median, Max, Min,
Mid Point, Alpha trimmed.
clc;
close;
clear;
xdel(winsid())//to close all currently open figure(s).
function R=imnoise2(type, M, N, a, b)
select type
case'salt & pepper'then
if argn(2)==3
a = 0.15; b = 0.15;
end
if (a+b) > 1
error('The sum Pa + Pb must not exceed 1.');
end
R(1:M,1:N) = 0.5;
X = rand(M,N);
[r c] = find(X<=a);
for i=1:length(r)
R(r(i),c(i)) = 0;
end
u = a + b;
[r c] = find(X>a & X<=u);
for i=1:length(r)
R(r(i),c(i)) = 1;
end
else
disp('Unknownfiltertype.')
end
endfunction
function [f]=restoration_filter(v, type, m, n, Q, d)

DHARAMSINH DESAI UNIVERSITY

40

IMAGE PROCESSING

if argn(2) ==2 then


m=7;n=7;Q=1.5;d=10;
elseif argn(2)==5 then
Q=parameter;d=parameter;
elseif argn(2)==4 then
Q=1.5;d=2;
else
disp('wrong number of inputs');
end
select type
case'median'then
f=MedianFilter(v,[m n]);
case'MIN'then
size1=m;
[nr,nc]=size(v);
temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$);
for i=ceil(size1/2):nr+ceil(size1/2)-1
for j=ceil(size1/2):nc+ceil(size1/2)-1
t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
y=gsort(t);
temp2(i-floor(size1/2),j-floor(size1/2))=min(y);
end
end
f=mat2gray(temp2);
case'MAX'then
size1=m;
[nr,nc]=size(v);
temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$);
for i=ceil(size1/2):nr+ceil(size1/2)-1
for j=ceil(size1/2):nc+ceil(size1/2)-1
t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
y=gsort(t);
temp2(i-floor(size1/2),j-floor(size1/2))=max(y);
end
end
f=mat2gray(temp2);
case'Mid_Point'then
size1=m;
[nr,nc]=size(v);
temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$);

DHARAMSINH DESAI UNIVERSITY

41

IMAGE PROCESSING

for i=ceil(size1/2):nr+ceil(size1/2)-1
for j=ceil(size1/2):nc+ceil(size1/2)-1
t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
y=gsort(t);
temp2(i-floor(size1/2),j-floor(size1/2))=0.5*(min(y)+max(y));
end
end
f=mat2gray(temp2);
else
disp('Unknownfiltertype.')
end
endfunction
function [f]=alphatrim(g, m, n, d)//alphatrim()is used to filter an image using alpha-trimmed
mean filter
size1=m;
[nr,nc]=size(g);
temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$)
for i=ceil(size1/2):nr+ceil(size1/2)-1
for j=ceil(size1/2):nc+ceil(size1/2)-1
t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2))
y=gsort(t);
a=y(:)
b=a';
t1=b(1+d/2:$-d/2);
temp2(i-floor(size1/2),j-floor(size1/2))=mean(t1);
end
end
f=mat2gray(temp2)
endfunction
///////////////////////////////////// Main Programm ////////////////////
SIVP_Path=getSIVPpath();
gray=imread(SIVP_Path + 'images\Chapter4_2.tif');
gray=rgb2gray(a);
gray=im2double(gray);
figure,ShowImage(gray,'Gray Image');
title('Original Image');
[M,N]=size(gray);
/////////////////////////////////////
Median Filter ////////////////////
v=imnoise(gray,'salt & pepper',0.05);
figure,ShowImage(v,'Noisy Image');
title('Original Image with Salt & Pepper Noise');
//Filtering the corrupted image with median filter

DHARAMSINH DESAI UNIVERSITY

42

IMAGE PROCESSING

h=restoration_filter(v,'median',3,3);
figure,ShowImage(h,'Recovered Image');
title('Recovered Image with Median Filter');
//////////////////////////////////////
MIN Filter ////////////////////
r3=imnoise2('salt & pepper',M,N,0.05,0.05); // Generate salt & pepper Noise
gray_noise_salt=gray;
// Add salt Noise Only
[r c]=find(r3==1);
for i=1:length(r)
gray_noise_salt(r(i),c(i)) = 255;
end
figure,ShowImage(gray_noise_salt,'Noisy Image');
title('Noisy Image');
//Filtering the Salt Noise corrupted image with MIN filter
h=restoration_filter(gray_noise_salt,'MIN',3,3);
figure,ShowImage(h,'Recovered Image');
title('Recovered Image with MIN Filter');
/////////////////////////////////////
MAX Filter ////////////////////
r3=imnoise2('salt & pepper',M,N,0.05,0.05); // Generate salt & pepper Noise
gray_noise_pepper=gray;
// Add Pepper Noise Only
[r c]=find(r3==0);
for i=1:length(r)
gray_noise_pepper(r(i),c(i)) = 0;
end
figure,ShowImage(gray_noise_pepper,'Noisy Image');
title('Noisy Image with Pepper Noise');
//Filtering the Salt Noise corrupted image with MAX filter
h=restoration_filter(gray_noise_pepper,'MAX',3,3);
figure,ShowImage(h,'Recovered Image');
title('Recovered Image with MAX Filter');
/////////////////////////////////////
Mid-Point Filter ////////////////////
v=imnoise(gray,'gaussian',0,0.02);
figure,ShowImage(v,'Noisy Image');
title('Image with Gaussian Noise');
//Filtering the Salt Noise corrupted image with Mid-Point filter
h=restoration_filter(v,'Mid_Point',3,3);
figure,ShowImage(h,'Recovered Image');
title('Recovered Image with Mid_Point Filter');
/////////////////////////////////////
Alpha Trimmed Filter ////////////////////
v=imnoise(gray,'gaussian',0,0.02);
v=imnoise(v,'salt & pepper',0.05);
figure,ShowImage(v,'Noisy Image');
title('Image with Gaussian and Salt&Pepper Noise');
m=3;n=5;d=2;

DHARAMSINH DESAI UNIVERSITY

43

IMAGE PROCESSING

f=alphatrim(v,m,n,d);
figure,ShowImage(f,'Recovered Image');
title('Recovered Image with Alpha Trimmed Filter');

Figure 90: IMAGE WITH PEPPER NOISE

Figure 91: MAX FILTER

Figure 92: MID POINT FILTER


Figure 93: IMAGE WITH GAUSSIAN NOISE

Figure 95: IMAGE WITH GAUSSIAN AND SALT &


PEPPER NOISE

DHARAMSINH DESAI UNIVERSITY

Figure 94: ALPHA TRIMMED FILTER

44

IMAGE PROCESSING

Figure 96: SALT & PEPPER NOISE

Figure 99: IMAGE WITH SALT NOISE

IV.

Figure 97: MEDIAN FILTER

Figure 98: MIN FILTER

To implement the Following Mean Statistic Restoration filter Median, Max, Min,
Mid Point, Alpha trimmed.
clc;
close;
clear;
xdel(winsid())//to close all currently open figure(s).
function R=imnoise2(type, M, N, a, b)
select type
case'salt & pepper'then
if argn(2)==3
a = 0.15; b = 0.15;
end
if (a+b) > 1
error('The sum Pa + Pb must not exceed 1.');
end
R(1:M,1:N) = 0.5;
X = rand(M,N);

DHARAMSINH DESAI UNIVERSITY

45

IMAGE PROCESSING

[r c] = find(X<=a);
for i=1:length(r)
R(r(i),c(i)) = 0;
end
u = a + b;
[r c] = find(X>a & X<=u);
for i=1:length(r)
R(r(i),c(i)) = 1;
end
else
disp('Unknownfiltertype.')
end
endfunction
function [f]=arithmetic_mean(v, m, n)
w=fspecial('average',m);
f=imfilter(v,w);
endfunction
function [f]=geometric_mean1(g, m, n);//gmean1() is used to filter an image using Geometric
mean filter
size1=m;
q=m*n;
g=double(g);
[nr,nc]=size(g);
temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$)
temp=temp+1;
for i=ceil(size1/2):nr+ceil(size1/2)-1
for j=ceil(size1/2):nc+ceil(size1/2)-1
t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
temp2(i,j)=prod(t);
end
end
temp3=temp2.^(1/q);
nn=temp3(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)
f1=nn-1;
f=mat2gray(f1)
endfunction
function [f]=geometric_mean2(g, m, n);//gmean2() is used to filter an image using Geometric
mean filter
size1=m;
q=m*n;
[nr,nc]=size(g);
temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$)
for i=ceil(size1/2):nr+ceil(size1/2)-1

DHARAMSINH DESAI UNIVERSITY

46

IMAGE PROCESSING

for j=ceil(size1/2):nc+ceil(size1/2)-1
t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
temp2(i,j)=geomean(t);
end
end
nn=temp2(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)
f=mat2gray(nn)
endfunction
function [f]=Harmonic_mean(g, m, n) //harmean1() is used to filter an image using Harmonic
mean filter.
size1=m;
d=m*n;
g=double(g);
[nr,nc]=size(g);
temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$);
for i=ceil(size1/2):nr+ceil(size1/2)-1
for j=ceil(size1/2):nc+ceil(size1/2)-1
t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
t1=ones(m,n)./(t+%eps);
t2=sum(t1);
temp2(i,j)=d/t2;
end
end
nn=temp2(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1);
f=mat2gray(nn);
endfunction
function [f]=Contra_Harmonic_mean(g, m, n, Q) //charmean1() is use to filter an image using
Contra Harmonic mean filter
size1=m;
d=m*n;
g=double(g);
[nr,nc]=size(g);
temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$)
disp(Q)
for i=ceil(size1/2):nr+ceil(size1/2)-1
for j=ceil(size1/2):nc+ceil(size1/2)-1
t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
d1=(t+%eps).^Q;
n1=(t+%eps).^(Q+1);
d2=sum(d1);
n2=sum(n1);
temp2(i,j)=n2/(d2);
end

DHARAMSINH DESAI UNIVERSITY

47

IMAGE PROCESSING

end
nn=temp2(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)
f=nn;
endfunction
///////////////////////////////////// Main Programm ////////////////////
SIVP_Path=getSIVPpath();
gray=imread(SIVP_Path + 'images\Chapter4_2.tif');
gray=rgb2gray(a);
gray=im2double(gray);
figure,ShowImage(gray,'Gray Image');
title('Original Image');
[M,N]=size(gray);
/////////////////////////////////////////

Arithmetical Mean Filter ////////////////////

v=imnoise(gray,'gaussian',0,0.02);
figure,ShowImage(v,'Noisy Image');
title('Image with Gaussian Noise');
m=3;n=3;
[f]=arithmetic_mean(v,m,n);
figure,ShowImage(f,'Recovered Image');
title('Recovered Image with Arithmetical Mean Filter');
/////////////////////////////////////////

Geometric Mean Filter

////////////////////

v=imnoise(gray,'gaussian',0,0.02);
figure,ShowImage(v,'Noisy Image');
title('Image with Gaussian Noise');
m=3;n=3;
[f]=geometric_mean1(v,m,n);
figure,ShowImage(f,'Recovered Image');
title('Recovered Image with Geometric Mean Filter');
/////////////////////////////////////////

Geometric Mean Filter

////////////////////

v=imnoise(gray,'gaussian',0,0.02);
figure,ShowImage(v,'Noisy Image');
title('Image with Gaussian Noise');
m=3;n=3;
[f]=geometric_mean2(v,m,n);
figure,ShowImage(f,'Recovered Image');
title('Recovered Image with Geometric Mean Filter');
///////////////////////////////////////// Harmonic Mean Filter ////////////////////
r3=imnoise2('salt & pepper',M,N,0.05,0.05); // Generate salt & pepper Noise
gray_noise_salt=gray;
// Add salt Noise Only
[r c]=find(r3==1);

DHARAMSINH DESAI UNIVERSITY

48

IMAGE PROCESSING

for i=1:length(r)
gray_noise_salt(r(i),c(i)) =255;
end
figure,ShowImage(gray_noise_salt,'Noisy Image');
title('Image with Salt Noise');
m=3;n=3;
[f]=Harmonic_mean(gray_noise_salt,m,n);
figure,ShowImage(f,'Recovered Image');
title('Recovered Image with Harmonic Mean Filter');
/////////////////////////////////////////

Contra_Harmonic Mean Filter (Salt) ////////////////////

r3=imnoise2('salt & pepper',M,N,0.05,0.05); // Generate salt & pepper Noise


gray_noise_salt=gray;
// Add salt Noise Only
[r c]=find(r3==1);
for i=1:length(r)
gray_noise_salt(r(i),c(i)) = 255;
end
figure,ShowImage(gray_noise_salt,'Noisy Image');
title('Image with Salt Noise');
m=3;n=3;Q=-1.5;
[f]=Contra_Harmonic_mean(gray_noise_salt,m,n,Q);
figure,ShowImage(f,'Recovered Image');
title('Recovered Image with Contra Harmonic Mean Filter');
//////////////////////////////////// Contra_Harmonic Mean Filter (Pepper) ////////////////////
r3=imnoise2('salt & pepper',M,N,0.05,0.05); // Generate salt & pepper Noise
gray_noise_salt=gray;
// Add salt Noise Only
[r c]=find(r3==0);
for i=1:length(r)
gray_noise_salt(r(i),c(i)) = 0;
end
figure,ShowImage(gray_noise_salt,'Noisy Image');
title('Image with Salt Noise');
m=3;n=3;Q=1.5;
[f]=Contra_Harmonic_mean(gray_noise_salt,m,n,Q);
figure,ShowImage(f,'Recovered Image');
title('Recovered Image with Contra Harmonic Mean Filter');

DHARAMSINH DESAI UNIVERSITY

49

IMAGE PROCESSING

OUTPUT

Figure 100: GAUSSIAN NOISE

Figure 103: GAUSSAIN NOISE

Figure 105: SALT NOISE

Figure 107: SALT NOISE

DHARAMSINH DESAI UNIVERSITY

Figure 101: ARITHMETIC MEAN FILTER

Figure 102: GEOMETRIC FILTER

Figure 104: HARMONIC FILTER

Figure 106: CONTRA HARMONIC FILTER

50

IMAGE PROCESSING

Figure 109: PEPPER NOISE

Figure 108: CONTRA HARMONIC FILTER

Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________

DHARAMSINH DESAI UNIVERSITY

51

IMAGE PROCESSING

EXPERIMENT-7
COLOR IMAGE PROCESSING
Date:__________
Objective: Program to illustrate conversion between the following image types
I.
II.
III.
IV.

To convert RGB image or color map to Grayscale


To convert Indexed image to RGB image and vice versa
To convert Indexed image to grayscale image and vice versa
To convert Grayscale or Binary image to indexed image

 RGB image to grayscale intensity image


I = imread('dev.png');
figure(1); ShowColorImage(I,'Original Image');
title('Original Color Image');
J = rgb2gray(I);
figure(2); ShowImage(J,'Gray Scale Image');
title('Gray Scale Image');
Output:

 RGB image to indexed image


I = imread('lena.png');
figure(1); ShowColorImage(I,'Original Image');
title('Original Color Image');
[IndexImage ColorMap] = RGB2Ind(I);
figure(2); ShowImage(IndexImage,'Indexed Image',ColorMap);
title('Indexed Image');
Output:

DHARAMSINH DESAI UNIVERSITY

52

IMAGE PROCESSING

 Grayscale image to binary image


I = imread('original.png');
figure(1); ShowImage(I,'Original Image');
title('Original Gray Image');
J = im2bw(I,0.5);
figure(2); ShowImage(J,'Binary Image');
title('Binary Image');
Output:

 RGB image to binary image


I = imread('dev.png');
figure(1); ShowColorImage(I,'Original Image');
title('Original Color Image');
J = im2bw(I,0.5);
figure(2); ShowImage(J,'Binary Image');
title('Binary Image');
Output:

DHARAMSINH DESAI UNIVERSITY

53

IMAGE PROCESSING

Objective: Program to illustrate conversion between following device-dependent color space







To convert RGB to NTSC and back to RGB.


To convert RGB to YCbCr and back to RGB
To convert RGB to HSV and back to RGB
To convert RGB to CMY and back to RGB

RGB to NTSC and back to RGB


I = imread('teaset.png');
figure(1); ShowColorImage(I,'Original Image');
title('Original Color Image');
YIQ = rgb2ntsc(I);
figure(2); ShowColorImage(YIQ,'NTSC Image');
title('NTSC Image');
P = ntsc2rgb(YIQ);
figure(3);ShowColorImage(P,'Original Image');
title('Original Image');
RGB to YCbCr and back to RGB
I = imread('lena.png');
figure(1); ShowColorImage(I,'Original Image');
title('Original Color Image');
YCC = rgb2ycbcr(I);
figure(2); ShowColorImage(YCC,'YCBCR Image');
title('YCBCR Image');
P = ycbcr2rgb(YCC);
figure(3);ShowColorImage(P,'Original Image');
title('Original Image');
RGB to HSV and back to RGB
I = imread('lena.png');
figure(1); ShowColorImage(I,'Original Image');
title('Original Color Image');
HSV = rgb2hsv(I);
figure(2); ShowColorImage(HSV,'HSV Image');
title('HSV Image');
P = hsv2rgb(HSV);
figure(3);ShowColorImage(P,'Original Image');
title('Original Image');

DHARAMSINH DESAI UNIVERSITY

54

IMAGE PROCESSING

RGB to CMY and back to RGB


I = imread('teaset.png');
figure(1); ShowColorImage(I,'Original Image');
title('Original Color Image');
imout = imcomplement(I);
figure(2); ShowColorImage(imout,'CMY Image');
title('CMY Image');
P = imcomplement(imout);
figure(3);ShowColorImage(P,'Original Image');
title('Original Image');
Output

Figure 118: ORIGINAL IMAGE

Figure 117: RGB TO NTSC

Figure 116: NTSC TO RGB

Figure 113: ORIGINAL IMAGE

Figure 115: RGB TO YCBCR

Figure 114: YCBCR TO RGB

Figure 112: ORIGINAL IMAGE

Figure 111: RGB TO HSV

Figure 110: HSV TO RGB

DHARAMSINH DESAI UNIVERSITY

55

IMAGE PROCESSING

Figure 120: RGB TO CMY

Figure 121: ORIGINAL IMAGE

Figure 119: CMY TO RGB

 To read RGB image and separate its R,G and B component, convert RGB to HSV and separate
H, S and V component.
rgb = imread('peppers.png');
figure(1); ShowColorImage(rgb,'Original Image');
title('Original Color Image');
R=rgb(:,:,1);
G=rgb(:,:,2);
B=rgb(:,:,3);
figure(2);ShowImage(R,'Red Component');title('Red Component');
figure(3);ShowImage(G,'Green Component');title('Green Component');
figure(4);ShowImage(B,'Blue Component');title('Blue Component');
hsv=rgb2hsv(rgb);
figure(5); ShowColorImage(hsv,'HSV Image');
title('HSV Image');
H=hsv(:,:,1);
S=hsv(:,:,2);
V=hsv(:,:,3);
figure(6);ShowImage(H,'Hue Component');title('Hue Component');
figure(7);ShowImage(S,'Saturation Component');title('Saturation Component');
figure(8);ShowImage(H,'Vector Component');title('Vector Component');
Output:

Figure 125: RGB IMAGE

Figure 124: R

DHARAMSINH DESAI UNIVERSITY

Figure 123: G

Figure 122: B

56

IMAGE PROCESSING

Figure 126: HSV IMAGE

Figure 128: H

Figure 129: S

Figure 127: V

Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________

DHARAMSINH DESAI UNIVERSITY

57

IMAGE PROCESSING

EXPERIMENT-8
EDGE DETECTION
Date:__________
OBJECTIVE:
I.
II.

To perform edge detection using Laplacian, Sobel and Prewitt operator


To perform edge detection using scilab inbuilt function EdgeFilter

I.

To perform edge detection using Laplacian, Sobel and Prewitt operator


clc;
clear all;
close;
xdel(winsid());
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\house.tif');
f=fspecial('average');
f1=fspecial('prewitt'); //f1=fspecial(sobel) ; //f2=fspecial(laplacian);
f2=mtlb_t(f1);
y=imfilter(a,f);
y1=imfilter(y,f1);
y2=imfilter(y,f2);
imshow([y y1;y2 y1+y2]);
Output :

1. Prewitt operator:

a b
c d
Figure 123: (a) Original Image
(b) Horizontal line detection
(c) Vertical line detection (d) Combine effect

DHARAMSINH DESAI UNIVERSITY

58

IMAGE PROCESSING

2. Sobel operator:

a b
c d
Figure 124: (a) Original Image
(b) Horizontal line detection
(c) Vertical line detection (d) Combine effect

3. Laplacian operator:

a b
c d
Figure 125: (a) Original Image
(b) Horizontal line detection
(c) Vertical line detection (d) Combine effect

DHARAMSINH DESAI UNIVERSITY

59

IMAGE PROCESSING

II.

To perform edge detection using scilab inbuilt function EdgeFilter


clc;
clear all;
close;
xdel(winsid());
global EDGE_SOBEL
global EDGE_PREWITT
global EDGE_LAPLACE
SIVP_Path=getSIVPpath();
x=imread(SIVP_Path + 'images\house.tif');
y1=EdgeFilter(x,EDGE_LAPLACE);
y2=EdgeFilter(x,EDGE_SOBEL);
y3=EdgeFilter(x,EDGE_PREWITT);
all=[x y1;y2 y3]
imshow(all);
Output :

a b
c d
Figure 126: (a) Original Image (b) Laplacian mask
(c) Sobel mask
(d) Prewitt mask

DHARAMSINH DESAI UNIVERSITY

60

IMAGE PROCESSING

III.

To perform edge detection using scilab inbuilt function EdgeFilter


clc;
clear all;
xdel(winsid());
SIVP_Path=getSIVPpath();
a=imread(SIVP_Path + 'images\house.tif');
im=im2double(a);
f1=[-1 -2 -1;0 0 0;1 2 1]
//imshow(f1);
b=imfilter(im,f1);
figure,ShowImage(b,'horizontal');
f2=[-1 0 1;-2 0 2;-1 0 1]
//imshow(f2);
c=imfilter(im,f2);
figure,ShowImage(c,'vertical');
f3=[0 -1 -2;1 0 -1;2 1 0]
//imshow(f3);
d=imfilter(im,f3);
figure,ShowImage(d,'+45 degree');
f4=[-2 -1 0;-1 0 1;0 1 2]
//imshow(f3);
e=imfilter(im,f4);
figure,ShowImage(e,'-45 degree');
all=[b c;d e]

imshow(all);

a b
c d
Figure 127: (a) Horizontal
(c) 45 degree

DHARAMSINH DESAI UNIVERSITY

(b) Vertical
(d) -45 degree

61

IMAGE PROCESSING

Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________

DHARAMSINH DESAI UNIVERSITY

62

IMAGE PROCESSING

EXPERIMENT-9
MORPHOLOGY
Date:__________
Objectives:
I.
II.
III.
IV.

Perform erosion operation on binary image with various structuring elements


Perform dilation operation on binary image with various structuring elements
Illustrating morphological open by erosion followed by dilation
Illustrating morphological open by erosion followed by dilation

I. Perform erosion operation on binary image with various structuring elements


clc;
clear all;
SIVP_Path=getSIVPpath();
x=imread(SIVP_Path + 'images\erod.tif');
se=CreateStructureElement('square',3);
erosion=ErodeImage(x,se);
se1=CreateStructureElement('square',15);
erosion1=ErodeImage(x,se1);
se2=CreateStructureElement('square',45);
erosion2=ErodeImage(x,se2);
all=[x erosion;erosion1 erosion2];
imshow(all);
Output:

a b
c d
Figure 13027: (a) Original image
(b) Erosion by 3x3
(c) Erosion by 15x15 (d) Erosion by 45x45 using square SE

DHARAMSINH DESAI UNIVERSITY

63

IMAGE PROCESSING

II. Perform dilation operation on binary image with various structuring elements
(i) Effect of size of structure element:
clc;
clear all;
SIVP_Path=getSIVPpath();
x=imread(SIVP_Path + 'images\dilat.tif');
se=CreateStructureElement('square',2);
se1=CreateStructureElement('square',4);
dilate=DilateImage(x,se);
dilate1=DilateImage(x,se1);
all=[x dilate dilate1];
imshow(all);
Output:

(i) E
a fb
f

Figure 128: (a) Original image (b) Dilated by 3x3 (c) Dilated by 5x5

(ii) Effect of different structure elements:

clc;
SIVP_Path=getSIVPpath();
x=imread(SIVP_Path + 'images\dilat.tif');
se=CreateStructureElement('square',3);
se1=CreateStructureElement('circle',3);
se2=CreateStructureElement('custom',[%f %t %f;%t %t %t;%f %t %f]);
dilate=DilateImage(x,se);
dilate1=DilateImage(x,se1);
dilate2=DilateImage(x,se2);
all=[x dilate; dilate1 dilate2];
imshow(all);

Output:

DHARAMSINH DESAI UNIVERSITY

64

IMAGE PROCESSING

a b
c d
Figure 129: (a) Original image (b) SE-square 3x3
(c) SE-circle R-3 (d) SE-Custom

III. Opening using sciEXPERIMENT in built function:


clc;
clear all;
SIVP_Path=getSIVPpath();
x=imread(SIVP_Path + 'images\Capture.tif');
se=CreateStructureElement('circle',7);
open=OpenImage(x,se);
all=[x open; x closing];
imshow(all);
Output:

Figure 130: Opening using inbuilt function

Opening using erosion followed by dilation:

DHARAMSINH DESAI UNIVERSITY

65

IMAGE PROCESSING

clc;
clear
SIVP_Path=getSIVPpath();
x=imread(SIVP_Path + 'images\Capture.tif');
SE1 = CreateStructureElement('square',3);
EI = ErodeImage(A,SE1);
SE2 = CreateStructureElement('square',3);
DI = DilateImage(EI,SE2);
B = [A EI DI];
imshow(B);
Output:

Figure 131: Erosion followed by dilation

III.

Closing using sciEXPERIMENT in built function


clc;
clear all;
SIVP_Path=getSIVPpath();
x=imread(SIVP_Path + 'images\Capture.tif');
se=CreateStructureElement('circle',7);
open=CloseImage(x,se);
all=[x closing];
imshow(all);
Output:

Figure 132: Closing using inbuilt function

IV.

Closing using dilation followed by erosion:

DHARAMSINH DESAI UNIVERSITY

66

IMAGE PROCESSING

clc;
clear
SIVP_Path=getSIVPpath();
x=imread(SIVP_Path + 'images\Capture.tif');
SE1 = CreateStructureElement('square',3);
DI = DilateImage(A,SE1);
SE2 = CreateStructureElement('square',3);
EI = ErodeImage(EI,SE2);
B = [A DI EI];
imshow(B);
Output:

Figure 133: Dilation followed by erosion

Conclusion:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

DHARAMSINH DESAI UNIVERSITY

67

Das könnte Ihnen auch gefallen