Department of Computer Science & Engineering: Practical File Digital Image Processing

You might also like

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

Department

Of
Computer Science & Engineering

Practical File

Digital Image Processing

SUBMITTED BY

Manish Kumar
Roll No.: - 0728710031
B.Tech. 4th Year
VIIth Semester

NARAINA COLLEGE OF ENGG. & TECH.


KANPUR-208020

2010-2011
M@TL@B code to calculate negative
clear all
clc
aa=imread('saturn.tif');
a=double(aa);
c=255; %for a 8-bit image%
b=c-a;
figure(1)
colormap (gray)
imagesc(a)
figure(2)
colormap(gray)
imagesc(b)

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 2


Gray level slicing without background

clear all
clc
p=imread ('skull.tif');
z=double (p);
[row,col]=size(z)
for i=1:1:row
for j=1:1:col
if(z(i,j)>50))&&(z(i,j)<150))
z(i,j)=55;
else
z(i,j)=0;
end
end
end
figure(1); %.……………original image
imshow (P)
figure(2); %.....................gray level slicing without backgroun d
imshow(uint8(z))
colormap(gray)
imagesc(b)

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 3


Ideal low pass filter
clear all
clc
a=imread('cameraman.tif');
a=double(a);
c=size(a);
N=c(1)
Do=input('enter the value of the cutt-off frequency');
for u=1:1:c(1)
for v=1:1:c(2)
D= ((u-(N/2))^2+(v-(N/2))^2)^0.5
if D<0;
H(u,v)=1;
else
H(u,v)=0;
end
end
end
vv=fft2(a); %we could use our code%
vc=fftshift(vv); %we could use our code%
x=vc.*H;
X=abs(ifft2(x));

%Plotting%
figure(1), imshow(uint8(a));
figure(2),mesh(H)
figure(3),imshow(uint8(X))
figure(4),imagesc(H),colormap(gray)

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 4


Ideal high pass filter
clear all
clc
a=imread('cameraman.tif');
a=double(a);
[row col]=size(a);
set=input('enter the value of the cutt-off frequency');
for u=1:1:row
for v=1:1:col
D= ((u-(row/2))^2+(v-(col/2))^2)^0.5
if D<set;
H(u,v)=0;
else
H(u,v)=1;
end
end
end
vv=fft2(a);
vc=fftshift(vv);
x=(vc.*H);
X=abs(ifft2(x));
%Plotting%
figure(1), imshow(uint8(a));
figure(2),mesh(H)
figure(3),imshow(uint8(X))
figure(4),imagesc(H),colormap(gray)

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 5


Low pass butter worth filter
clear all
clc
a=imread('cameraman.tif');
a=double(a);
a=size(a);
n=c(1);
n= input('Enter the order of the filter');
D0=input('enter the value of the cutt-off frequency');
for u=1:1:c(1)
for v=1:1:c(2)
D= ((u-(N/2))^2+(v-(N/2))^2)^0.5;
H(u,v)=1/(1+((D/Do)^(2*n)))
end
end
vv=fft2(a); %we could use our code%
vc=fftshift(vv); %we could use our code%
x=vc.*H;
X=abs(ifft2(x));

%Plotting%
figure(1), imshow(uint8(a));
figure(2),mesh(H)
figure(3),imshow(uint8(X))
figure(4),imagesc(H),colormap(gray)

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 6


Program for boundary extraction
clear all
clc
a=imread('circlesm.tif');
a=double(a);
p=size(a);
w=[1 1 1;1 1 1;1 1 1];
for x=2:1:p(1)-1
for x=2:1:p(2)-1
a1=[w(1)*a(x-1,y-1) w(2)*a(x-1,y) w(3)*a(x-1,y+1) w(4)*a(x,y-1) w(5)*a(x,y)…
w(6)*a(x,y+1) w(7)*a(x+1,y-1) w(8)*a(x+1,y) w(9)*a(x+1,y+1)
A(x,y)=min(a1); %%erosion%%
sharp(x,y)=double(a(x,y))-double(A(x,y));
end
end
imshow(a)
figure,imshow(A)
figure,imshow(sharp)
% Normalization may be required

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 7


Prewitts operator
clear all
clc
aa=imread('test.tif');
a=double(aa);
[row col]=size(a);
w2=[-1 0 1; -1 0 1;1 0 1];
w1=[-1 -1 -1;0 0 0;1 1 1];

for x= 2: 1:row-1;
for y=2:1:col-1;
a1(x,y)=w1(1)*a(x-1,y-1)+w1(2)*a(x-1,y)+w1(3)*a(x-1,y+1)+w1(4)*a(x,y-1)
+w1(5)*a(x,y)+w1(6)*a(x,y+1)+w1(7)*a(x+1,y-1)+w1(8)*a(x+1,y)
+w1(9)*a(x+1,y+1);
a2(x,y)=w2(1)*a(x-1,y-1)+w2(2)*a(x-1,y)+w2(3)*a(x-1,y+1)+w2(4)*a(x,y-1)
+w2(5)*a(x,y)+w2(6)*a(x,y+1)+w2(7)*a(x+1,y-1) +w2(8)*a(x+1,y)
+w2(9)*a(x+1,y+1);
end
end
a3=a1+a2; %%the final gradient value%%
figure(1)
imshow(uint8(a1) %%the x-gradient image, normalisation might be required
figure(2)
imshow(uint8(a2) %%the y -gradient image%%
figure(3)
imshow(uint8(a3)

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 8


Sobel opoerator
clear all
clc
aa=imread('test.tif');
a=double(aa);
[row col]=size(a);
w2=[-1 -2 -1; 0 0 0; 1 2 1];
w1=[-1 0 -1; -2 0 -2; -1 0 1];

for x= 2: 1:row-1;
for y=2:1:col-1;
a1(x,y)=w1(1)*a(x-1,y-1)+w1(2)*a(x-1,y)+w1(3)*a(x-1,y+1)+w1(4)*a(x,y-1)
+w1(5)*a(x,y)+w1(6)*a(x,y+1)+w1(7)*a(x+1,y-1)+w1(8)*a(x+1,y)
+w1(9)*a(x+1,y+1);
a2(x,y)=w2(1)*a(x-1,y-1)+w2(2)*a(x-1,y)+w2(3)*a(x-1,y+1)+w2(4)*a(x,y-1)
+w2(5)*a(x,y)+w2(6)*a(x,y+1)+w2(7)*a(x+1,y-1) +w2(8)*a(x+1,y)
+w2(9)*a(x+1,y+1);
end
end
a3=a1+a2; %%the final gradient value%%
figure(1)
imshow(uint8(a1) %%the x-gradient image, normalisation might be required
figure(2)
imshow(uint8(a2) %%the y -gradient image%%
figure(3)
imshow(uint8(a3) %%Final gradient image, Normalisation might be required

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 9


Dilation and erosion of real images
clear all
clc
a=imread('circlesm.tif');
a=double(a);
p=size(a);
%%Using the inbuilt MATLAB function for comparision%%
s=strel('square',3);
d1=imdiate(a,s);
d2=imerode(a,s);
%%Writing our own program%%
w=[1 1 1;1 1 1;1 1] ;
%%structuring element%%
for x=2:1:p(1)-1
for x=2:1:p(2)-1
a1=[w(1)*a(x-1,y-1) w(2)*a(x-1,y) w(3)*a(x-1,y+1) w(4)*a(x,y-1)
w(5)*a(x,y) w(6)*a(x,y+1) w(7)*a(x+1,y-1) w(8)*a(x+1,y)
w(9)*a(x+1,y+1)
A1(x,y)=max(a1); %Dilation%
A2(x,y)=min(a1); %erosion%
end
end
figure(1),imshow(q)%Normalizing might be required
figure(2),imshow(d1)
figure(3),imshow(d2)
figure(4),imshow(A1)
figure(5),imshow(A2)

Manish Kumar(0728710031) Dept. of Computer Science & Engg. Page 10

You might also like