You are on page 1of 20

LAB FILE

ON
DIGITAL IMAGE PROCESSING

Session-2017-2018
SIGNAL PROCESSING
(M.TECH.IST SEM)

SUBMITTED BY- SUBMITTED TO-


ADITI SHARMA PROF. JYOTSNA SINGH
ROLL NO-2017PSP3006 (DEPARTMENT OF ECE)

NETAJI SUBHAS INSTITUTE OF TECHNOLOGY,


DWARKA,NEW DELHI
Experiment- 1

Aim- Image processing toolbox.

Commands-
 imread- Read image from graphics file
A = imread(filename)
A = imread(filename,fmt) - specifies the format of the file indicated by fmt.

 imwrite- Write image to graphics file


imwrite(A,filename)

 imshow- Display image


imshow(I)

 rgb2gray- Convert RGB image or colormap to grayscale


I = rgb2gray(RGB) - converts the true color image to grayscale intensity image I

 imhist- Histogram of image data


imhist(I) - calculates the histogram for the intensity image I and displays a plot of
the histogram. The number of bins in the histogram is determined by the image type.

 Imadjust- Adjust image intensity values or colormap


J = imadjust(I) - maps the intensity values in grayscale image I to new values in J.
This increases the contrast of the output image J.
J = imadjust(I,[low_in; high_in],[low_out; high_out]) - maps the values in I to
new values in J such that values between low_in and high_in map to values
between low_out and high_out.

MATLAB Code-

clc
clear all
a=imread('peppers.jpg');
imshow(a)
imwrite(a,'a.tif');
b=rgb2gray(a);
figure
imshow(b)
figure
imhist(b)
c=imadjust(b);
figure
imshow(c)
Result-
Experiment- 2
Aim- Histogram Equalization.

Command-
 histeq- Enhance contrast using histogram equalization
J = histeq(I,hgram) transforms the intensity image I so that the histogram of the
output intensity image J with length (hgram) bins approximately matches the target
histogram hgram.
J = histeq(I,n) transforms the intensity image I, returning in J an intensity image
with n discrete gray levels.

MATLAB Code-

clc;
clear all;
a=imread('images.jpg');
figure
imshow(a)
figure
imhist(a)
b=histeq(a,256);
figure
imshow(b)
figure
imhist(b)

Result-
Experiment- 3

Aim- To perform 2-D convolution.

MATLAB Code-

clc
x=input('enter matrix')
h=input('enter matrix')
[m,n]=size(x);
[a,b]=size(h);
fori=1:m
for j=1:n
c(i,j)=x(m-i+1,j);
end
end
for k=1:a
for l=1:b
d(k,l)=h(a-k+1,l);
end
end
fori= 1:a
for j= 1:b
p(i,j)=d(i,b-j+1);
end
end
[mr,nc]=size(c);
[ar,bc]=size(d);
yx=mr+ar-1;
yy=nc+bc-1;
for n1= 0:yx-1
for n2=0:yy-1
sum=0;
for m1=0:n1
for m2=0:n2
if (m1+1 <=mr)
if(n1-m1+1<=ar)
if(m2+1<=nc)
if(n2-m2+1<=bc)
sum=sum+c(m1+1,m2+1)*p(n1-m1+1,n2-m2+1);
end
end
end
end
end
end
y(n1+1,n2+1)=sum;
end
end
[yr,yc]=size(y);
for k=1:yr
for l=1:yc
yo(k,l)=y(yr-k+1,l);
end
end
mesh(yo)

Result-
enter matrix[1 2 3;4 5 6]

x=

1 2 3
4 5 6

enter matrix[1 2 3;4 5 6]

h=

1 2 3
4 5 6

80

70

60

50

40

30

20

10

0
3
2.8
2.6
5
2.4 4.5
2.2 4
2 3.5
1.8 3
1.6 2.5
1.4 2
1.2 1.5
1 1
Experiment- 4

Aim- To observe the effect of various noise models on an image of three intensity levels and plot its
histogram.

MATLAB Code-

a) Gaussian Noise Model

close all
a1=imread('flowers.jpg');a=rgb2
gray(a1);
for i=1:500 for
j=1:800
if a(i,j)<=61
a(i,j)=2;
else if 61<a(i,j) && a(i,j)<124
a(i,j)=61;
else
a(i,j)=250;
end
end
end
end
imshow(a)
figure
[s,t]=imhist(a);
bar(t, s/sum(s));
n=uint8(randn(500,800));
p=(a+n);
figure
imshow(p) figure
[n,x]=imhist(p);
bar(x, n/sum(n));

b) Uniform Noise Model:

clear all
close all
a1=imread('flowers.jpg');
a=rgb2gray(a1);
for i=1:500
for j=1:800
if a(i,j)<=61
a(i,j)=2;
else if 61<a(i,j) && a(i,j)<124
a(i,j)=61;
else
a(i,j)=250;
end
end
end
end
imshow(a)
figure
imhist(a)
n=uint8(rand(500,800));
p=(a+n);
figure
imshow(p)
figure
[n,x]=imhist(p);
bar(x, n);

a) Rayleigh Noise Model:

clc
clear all
close all
a1=imread('flowers.jpg');
a=rgb2gray(a1);
for i=1:500
for j=1:800
if a(i,j)<=61
a(i,j)=2;
else if 61<a(i,j) && a(i,j)<124
a(i,j)=61;
else
a(i,j)=250;
end
end
end
end
imshow(a)
figure
imhist(a)
x=wgn(500,800,10);
y=wgn(500,800,10);
z=uint8((x.^2+y.^2).^.5);
p=(a+z);
figure
imshow(p)
figure
[n,x]=imhist(p);
bar(x, n);
b) Exponential Noise Model:

clc
clear all
close all
a1=imread('flowers.jpg');
a=rgb2gray(a1);
for i=1:500
for j=1:800
if a(i,j)<=61
a(i,j)=2;
else if 61<a(i,j) && a(i,j)<124
a(i,j)=61;
else
a(i,j)=250;
end
end
end
end
imshow(a)
figure
imhist(a)
z=uint8(exprnd(2 ,500,800));
p=(a+z);
figure
imshow(p)
figure
[n,x]=imhist(p);
bar(x, n);

c) Salt and Pepper Noise Model:

clear all
close all
a1=imread('flowers.jpg');
a=rgb2gray(a1);
for i=1:500
for j=1:800
if a(i,j)<=61
a(i,j)=2;
else if 61<a(i,j) && a(i,j)<124
a(i,j)=61;
else
a(i,j)=250;
end
end
end
end
imshow(a)
figure
imhist(a)
q = randi(2, 500,800); q=q-
ones(500,800);
p=(a+uint8(q));
figure
imshow(p)
figure
[n,x]=imhist(p);
bar(x, n);

Results-

RGB image with three intensity levels

Histogram of image with three intensity levels:


4
x 10
3.5

2.5

1.5

0.5

0 50 100 150 200 250


a) Gaussian Noise Model:

0.35

0.3

0.25

0.2

0.15

0.1

0.05

0
-50 0 50 100 150 200 250 300

b) Uniform Noise Model:

4
x 10
8

0
-50 0 50 100 150 200 250 300
c) Rayleigh Noise Model:

4
x 10
4

3.5

2.5

1.5

0.5

0
-50 0 50 100 150 200 250 300

d) Exponential Noise Model:

4
x 10
5

4.5

3.5

2.5

1.5

0.5

0
-50 0 50 100 150 200 250 300
e) Salt and Pepper Noise Model:

4
x 10
8

0
-50 0 50 100 150 200 250 300
Experiment- 5

Aim- To perform edge detection in an image.

MATLAB Code-

a) Sobel Operator:
% horizontal and vertical edge detection using sobel operator.
clc
clear all
close all
a1=imread('flowers.jpg');
a=rgb2gray(a1);
imshow(a)
q=[-1 -2 -1;0 0 0;1 2 1];
c1=conv2(a,q);
figure
imshow(c1)
w=[-1 0 1;-2 0 2;-1 0 1];
c2=conv2(a,w);
figure
imshow(c2)

b) Prewitt operator:
% horizontal and vertical edge detection using prewitt operator.
clc
clear all
close all
a1=imread('flowers.jpg');
a=rgb2gray(a1);
imshow(a)
q=[-1 -1 -1;0 0 0;1 1 1];
c1=conv2(a,q);
figure
imshow(c1)
w=[-1 0 1;-1 0 1;-1 0 1];
c2=conv2(a,w);
figure
imshow(c2)

c) Laplacian Operator:
% inward and outward edge detection using laplacian operator.
clc
clear all
close all
a1=imread('flowers.jpg');
a=rgb2gray(a1);
imshow(a)
q=[0 -1 0;-1 4 -1;0 -1 0];
c1=conv2(a,q);
figure
imshow(c1)
w=[0 1 0;1 -4 1;0 1 0];
c2=conv2(a,w);
figure
imshow(c2)

Results-

RGB image:

a) Sobel Operator:
b) Prewitt Operator:

c) Laplacian Operator:
Experiment- 4

Aim- To implement Wiener filter on one-dimensional data and two-dimensional image.

MATLAB Code-

a) 1-D Wiener Filter:

clc
clear all
close all
qn=[];
gn=[];
rq=[];
for n=0:10
gn=[gn (.5)^n];
qn=[qn sin(2*pi*n/7)];
rq=[5 zeros(1,n)];
end
tq=autocorr(qn);
pq=fft(tq);
qw=fft(qn);
gw=fft(gn);
gws=conj(gw);
rw=fft(rq);
hw=(gws.*pq)./(((abs(gw).^2).*pq)+rw);
a=hw.*gw;
w=qw.*a;
wn=ifft(w);
plot(qn)
hold on
plot(wn,'r')

b) Wiener Filter implementation on an image:

clc
clear all
close all
n=imread('flowers.jpg');
i=rgb2gray(n);
j=imnoise(i,'gaussian',0,.025);
imshow(j)
k=wiener2(j,[7,7]);
figure
imshow(k)
Results-
a) 1-D Wiener Filter:
1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
1 2 3 4 5 6 7 8 9 10 11

b) Wiener Filter implementation on an image:

i. Noise variance=0.025
Noisy image:

Filtered image:
ii. Noise variance=0.05
Noisy image:

Filtered image:

iii. Noise variance=0.25


Noisy image:

You might also like