Beruflich Dokumente
Kultur Dokumente
B.Tech.
SEM. VII (EC)
INDEX
CONTENTS
NO.
C ONT ENT S
INTENSITY TRANSFORMATION
IMAGE RESTORATION
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.
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.
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:
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
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:
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:
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
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
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
IMAGE PROCESSING
nw=uint8(k);
figure,ShowImage(gray,'original');
title('original');
figure,ShowImage(nw,'Negetive');
title('Negetive');
Output:
Image Flipping:
1.
IMAGE PROCESSING
Output:
2.
IMAGE PROCESSING
3.
Conclusion:
10
IMAGE PROCESSING
EXPERIMENT-3
INTENSITY TRANSFORMATIONS
Date:__________
Objective:
I.
II.
III.
IV.
V.
VI.
VII.
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:
11
IMAGE PROCESSING
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:
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);
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);
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));
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
15
IMAGE PROCESSING
Output:
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');
17
IMAGE PROCESSING
Output:
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
ASSIGNMENT
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
19
IMAGE PROCESSING
20
IMAGE PROCESSING
Output:
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:
22
IMAGE PROCESSING
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
DHARAMSINH DESAI UNIVERSITY
23
IMAGE PROCESSING
ASSIGNMENTS
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
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');
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);
inm=mat2gray(in)
figure,ShowImage(inm,'Frequency Spectrum');
title('Frequency Spectrum');
filt=lowpassfilter('gaussian',M,N,100);
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 42:FFT
26
IMAGE PROCESSING
27
IMAGE PROCESSING
28
IMAGE PROCESSING
II.
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
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:
30
IMAGE PROCESSING
31
IMAGE PROCESSING
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
32
IMAGE PROCESSING
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
_________________________________________________________________
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.
34
IMAGE PROCESSING
II.
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
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]);
36
IMAGE PROCESSING
37
IMAGE PROCESSING
OUTPUT:
38
IMAGE PROCESSING
39
IMAGE PROCESSING
III.
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)
40
IMAGE PROCESSING
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
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;
43
IMAGE PROCESSING
f=alphatrim(v,m,n,d);
figure,ShowImage(f,'Recovered Image');
title('Recovered Image with Alpha Trimmed Filter');
44
IMAGE PROCESSING
IV.
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);
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
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
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);
/////////////////////////////////////////
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');
/////////////////////////////////////////
////////////////////
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');
/////////////////////////////////////////
////////////////////
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);
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');
/////////////////////////////////////////
49
IMAGE PROCESSING
OUTPUT
50
IMAGE PROCESSING
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
51
IMAGE PROCESSING
EXPERIMENT-7
COLOR IMAGE PROCESSING
Date:__________
Objective: Program to illustrate conversion between the following image types
I.
II.
III.
IV.
52
IMAGE PROCESSING
53
IMAGE PROCESSING
54
IMAGE PROCESSING
55
IMAGE PROCESSING
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 124: R
Figure 123: G
Figure 122: B
56
IMAGE PROCESSING
Figure 128: H
Figure 129: S
Figure 127: V
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
57
IMAGE PROCESSING
EXPERIMENT-8
EDGE DETECTION
Date:__________
OBJECTIVE:
I.
II.
I.
1. Prewitt operator:
a b
c d
Figure 123: (a) Original Image
(b) Horizontal line detection
(c) Vertical line detection (d) Combine effect
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
59
IMAGE PROCESSING
II.
a b
c d
Figure 126: (a) Original Image (b) Laplacian mask
(c) Sobel mask
(d) Prewitt mask
60
IMAGE PROCESSING
III.
imshow(all);
a b
c d
Figure 127: (a) Horizontal
(c) 45 degree
(b) Vertical
(d) -45 degree
61
IMAGE PROCESSING
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
62
IMAGE PROCESSING
EXPERIMENT-9
MORPHOLOGY
Date:__________
Objectives:
I.
II.
III.
IV.
a b
c d
Figure 13027: (a) Original image
(b) Erosion by 3x3
(c) Erosion by 15x15 (d) Erosion by 45x45 using square SE
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
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:
64
IMAGE PROCESSING
a b
c d
Figure 129: (a) Original image (b) SE-square 3x3
(c) SE-circle R-3 (d) SE-Custom
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:
III.
IV.
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:
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
67