100% found this document useful (1 vote)
3K views21 pages

Practical 1: # 2D Linear Convolution, Circular Convolution Between Two 2D Matrices

This document contains code for several image processing techniques including: 1. Edge detection using operators like Sobel, Prewitt, and Roberts. The code applies the operators to an input image and displays the results. 2. Morphological operations like erosion, dilation, opening and boundary extraction. The code applies these operations to an image using structuring elements and displays the output. 3. Color image processing where an RGB image is read and separated into individual color planes for further processing.

Uploaded by

Justin Sebastian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
3K views21 pages

Practical 1: # 2D Linear Convolution, Circular Convolution Between Two 2D Matrices

This document contains code for several image processing techniques including: 1. Edge detection using operators like Sobel, Prewitt, and Roberts. The code applies the operators to an input image and displays the results. 2. Morphological operations like erosion, dilation, opening and boundary extraction. The code applies these operations to an image using structuring elements and displays the output. 3. Color image processing where an RGB image is read and separated into individual color planes for further processing.

Uploaded by

Justin Sebastian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Name:

Roll no.

Practical 1
# 2D Linear Convolution, Circular Convolution between two 2D matrices
clear all;
close;
clc;
//h=[1,2,3,3,2,1];
//x=[1,2,3,4,5];
h=[[1,2,3],[4,5,6],[7,8,9]];
x=[[1,2,3],[4,5,6],[7,8,9]];
//x=input('Enter the i/p seq');
//h=input('Enter the i/p seq');
y=convol(x,h);
for i=1;length(y)
if(y(i)<=0.0001)
y(i)=0;
end
end
disp(y,'linear convolution of y')
subplot(3,1,2)
a=gca();
a.thickness=2;
plot2d3('gnn',[0:length(h)-1],h)
xtitle('impulse response','n','h[n]');
subplot(3,1,1);
a=gca();
a.thickness=2;
plot2d3('gnn',[0:length(x)-1],x)
xtitle('impulse response','n','x[n]');
subplot(3,1,3);
a=gca();
a.thickness=2;
plot2d3('gnn',[0:length(y)-1],y)
xtitle('impulse response','n','y[n]');
Name:
Roll no.
Name:
Roll no.

Practical 2
# Circular Convolution expressed as linear convolution plus alias
clear all;
close;
clc;
h=[1,2,3,3,2,1];
x=[1,2,3,4,5,6];
//h=[[1,2,3],[4,5,6],[7,8,9]];
//x=[[1,2,3],[4,5,6],[7,8,9]];
//x=input('Enter the i/p seq');
//h=input('Enter the i/p seq');
m=length(h);
n=length(x);
a=zeros(1,max(m,n))
if(m>n)
for i=n+1;m
h(i)=0;
end
else if(n>m)
for i=m+1;n
x(i)=0;
end
end
//n=length(x);
y=zeros(1,n);
a(1)=x(1);
for j = 2:n
a(j) = x(n-j +2) ;
end
for i =1: n
y (1) = y (1) +h(i)*a(i);
end
for k = 2:n
for j =2: n
x(j) = a(j -1) ;
end
x (1) = a(n);
x
for i = 1:n
a(i) = x(i);
y(k) = y(k)+h(i)*a(i);
end
end
disp(y ,'Circular convolution' )
end
Name:
Roll no.

Practical 3
# Linear Cross correlation of a 2D matrix, Circular correlation between two
signals and Linear auto correlation of a 2D matrix, Linear Cross correlation of a
2D matrix
clear;
clc;
close;
h=[1,2,3,0]; //Impulse Response of LTI System
x=[1,2,2,1]; //Input Response of LTI System
N1=length(x);
N2=length(h);
N=N1+N2-1
disp(N,'Length of Output Response y (n)')
//Padding zeros to Make Length of h and x
//Equal to length of output response y
h1=[h,zeros(1,N-N2)];
x1=[x,zeros(1,N-N1)];
//Computing FFT
H=fft(h1, -1) ;
X=fft(x1, -1) ;
//Multiplication of 2 DFTs
Y=X.*H
//Linear Convolution Result
y=abs(fft(Y,1))
disp(X,'DFT of i/p X(k)=')
disp(H,'DFT of impulse sequence H(k)=')
disp(Y,'DFT of Linear Filter o/p Y(k)=')
disp(y,'Linear Convolution result y [n]=')
Name:
Roll no.
Name:
Roll no.

Practical 4

# DFT of 4x4 gray scale image


clear all;
clc;
x=[1 2 3 4; 1 0 1 4; 1 1 0 1; 1 0 1 0];
p=[1 1 1 1; 1 -%i -1 %i; 1 -1 1 -1; 1 %i -1 -%i];
y=x*p;
disp(y)
Name:
Roll no.

Practical 5
#Compute discrete cosine transform
Name:
Roll no.

# Program to perform KL transform for the given 2D matrix


Name:
Roll no.
Name:
Roll no.

Practical 6

# Brightness enhancement of an image, Contrast Manipulation, image


negative

 Contrast Manipulation(Stretching)
clc;
aa=imread('Picture1.jpg');
a=double(aa)
[row col]=size(a)
LT=input('Enter lower threashold value')
UT=input('Enter upper threashold value')
for x=1:1:row
for y=1:1:col
if a(x,y)<=LT
b(x,y)=0.5*a(x,y)
else if a(x,y)<=UT
b(x,y)=2*(a(x,y)-LT)+0.5*LT;
else b(x,y)=0.5*(a(x,y)-UT)+0.5*LT+2*(UT-LT);
end
end
end
end
subplot(2,1,1)
imshow(a)
title('Original image')
subplot(2,1,2)
imshow(b)
title('Image after Constrast Stretching')
Name:
Roll no.

 image negative( DigitalNegative)


clc;
aa=imread('Picture1.jpg');
a=double(aa)
c=max(max(a))
b=c-a
figure(1)
//4colormap(gray)
imshow(a)
figure(2)
//colormap(gray)
imshow(b)
Name:
Roll no.

Practical 7
# Perform threshold operation, perform gray level slicing without background

 thresholding
clc;
p=imread('Picture1.jpg');
a=p
[row col]=size(a)
T=input('Enter threashold value')
for i=1:1:row
for j=1:1:col
if (p(i,j)<T)
a(i,j)=0;
else
a(i,j)=255;
end
end
end
figure(1),imshow(p),figure(2),imshow(a)
Name:
Roll no.

 gray level slicing without background


clc;
p=imread('Picture1.jpg');
a=p
[row col]=size(a)
for i=1:1:row
for j=1:1:col
if (a(i,j)>50 & a(i,j)<150)
a(i,j)=255;
else
a(i,j)=0;
end
end
end
figure(1),imshow(p),figure(2),imshow(a)
Name:
Roll no.

Practical 8
Image Segmentation
 OrdinaryOperator.sce
clc;
aa=imread('Picture1.jpg');
a=double(aa)
[row col]=size(a)
y=zeros(a)
w1=[1 0; -1 0]
w2=[1 -1; 0 0]
for x=2:1:row-1
for y=2:1:col-1
a1(x,y)=[w1(1)*a(x,y)+w1(2)*a(x,y+1)+w1(3)*a(x+1,y)+w1(4)*a(x+1,y+1)];
a2(x,y)=[w2(1)*a(x,y)+w2(2)*a(x,y+1)+w2(3)*a(x+1,y)+w2(4)*a(x+1,y+1)];
end
end
a3=a1+a2
figure(1),imshow(a1),figure(2),imshow(a2),figure(3),imshow(a3)

 PrewittsOperation
clc;
aa=imread('Picture1.jpg');
a=double(aa)
[row col]=size(a)
w1=[-1 -1 -1; 0 0 0; 1 1 1]
w2=[-1 0 1; -1 0 1; -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
figure(1),imshow(a1),figure(2),imshow(a2),figure(3),imshow(a3)
Name:
Roll no.

 RobertsOperator
clc;
aa=imread('Picture1.jpg');
a=double(aa)
[row col]=size(a)
w1=[1 0; 0 -1]
w2=[0 1; -1 0]
for x=2:1:row-1
for y=2:1:col-1
a1(x,y)=[w1(1)*a(x,y)+w1(2)*a(x,y+1)+w1(3)*a(x+1,y)+w1(4)*a(x+1,y+1)];
a2(x,y)=[w2(1)*a(x,y)+w2(2)*a(x,y+1)+w2(3)*a(x+1,y)+w2(4)*a(x+1,y+1)];
end
end
a3=a1+a2
figure(1),imshow(a1),figure(2),imshow(a2),figure(3),imshow(a3)

 SobelOperator.sce
clc;
aa=imread('Picture1.jpg');
a=double(aa)
[row col]=size(a)
w1=[-1 -2 -1; 0 0 0; 1 2 1]
w2=[-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
Name:
Roll no.

end
a3=a1+a2
figure(1),imshow(a1),figure(2),imshow(a2),figure(3),imshow(a3)
Name:
Roll no.

Practical 9
# Image Compression
close ;
clear ;
clc ;
x =[65 ,75 ,80 ,70;72 ,75 ,82 ,68;84 ,72 ,62 ,65;66 ,68 ,72 ,80];
disp (x , 'Original Block is x = ' )
[ m1 n1 ]= size ( x ) ;
blk = input ( 'Enter the block size : ') ;
for i = 1 : blk : m1
for j = 1 : blk : n1
y = x ( i: i +(blk-1) ,j : j +(blk-1) ) ;
m = mean ( mean ( y ) ) ;
disp (m , 'mean value is m = ' )
sig = stdev (y ) ;
disp ( sig , 'Standard deviation of the block is= ' )
b = y > m ; // the binary block
disp (b , 'Binary allocation matrix is B= ')
K = sum (sum ( b ) ) ;
disp (K , 'number of ones = ')
if ( K ~= blk ^2 ) & ( K ~= 0)
ml = m - sig * sqrt ( K /(( blk ^2) -K ) ) ;
disp ( ml , ' The value of a = ')
mu = m+ sig * sqrt ((( blk ^2) -K )/ K ) ;
disp ( mu , 'The value of b = ')
x ( i : i +( blk -1) , j : j +( blk -1) ) = b * mu+(1 - b ) * ml ;
end
end
end
disp ( round ( x ) , 'Reconstructed Block is x = ')
Name:
Roll no.
Name:
Roll no.

Practical 10
#Binary Image Processing and Colour Image processing
 BoundryExtraction

clc;
aa=imread('Picture1.jpg');
aa=double(aa)
p=size(aa)
da=aa
ea=aa
w1=[1 1 1; 1 1 1; 1 1 1]
for x=2:1:p(1)-100
for y=2:1:p(2)-100
a1=[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)];
A(x,y)=min(a1) //Erosion
sharp(x,y)=double(a(x,y))-double(A(x,y))
end
end
figure(1),imshow(aa)
figure(2),imshow(A)
figure(3),imshow(sharp)

 Opening

clc;
aa=imread('Picture1.jpg');
aa=double(aa)
p=size(aa)
a=aa
w1=[1 1 1; 1 1 1; 1 1 1]
for x=2:1:p(1)-100
for y=2:1:p(2)-100
a1=[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)];
A1(x,y)=min(a1) //Erosion
a=A1
end
end
Name:
Roll no.

q=size(a)
for i=2:1:q(1)-100
for j=2:1:q(2)-100
a11=[w1(1)*A1(i-1,j-1) w1(2)*A1(i-1,j) w1(3)*A1(i-1,j+1) w1(4)*A1(i,j-1) w1(5)*A1(i,j)
w1(6)*A1(i,j+1) w1(7)*A1(i+1,j-1) w1(8)*A1(i+1,j) w1(9)*A1(i+1,j+1)];
A2(i,j)=max(a11) //Dilation
end
end
figure(1),imshow(a)
figure(2),imshow(A2)

 Colorimagerocessing

clc ;
close ;
RGB = imread ('Picture1.jpg') ;
R = RGB ;
G = RGB ;
B = RGB ;
R (: ,: ,2) =0;
R (: ,: ,3) =0;
G (: ,: ,1) =0;
G (: ,: ,3) =0;
B (: ,: ,1) =0;
B (: ,: ,2) =0;
figure (1)
imshow ( RGB ) ; //IPD
//t o o l b o x
title ('Original Color Image') ;
figure (2)
imshow (R) ;
figure (3)
imshow (G) ;
figure (4)
imshow (B) ;
Name:
Roll no.

 Closing

clc;
aa=imread('Picture1.jpg');
aa=rgb2gray(aa)
aa=double(aa)
p=size(aa)
da=aa
ea=aa
w1=[1 1 1; 1 1 1; 1 1 1]
for x=2:1:p(1)-100
for y=2:1:p(2)-100
a1=[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)];
A1(x,y)=max(a1) //Dilation
da=A1
end
end
q=size(da)
for i=2:1:q(1)-100
for j=2:1:q(2)-100
a11=[w1(1)*A1(i-1,j-1) w1(2)*A1(i-1,j) w1(3)*A1(i-1,j+1) w1(4)*A1(i,j-1) w1(5)*A1(i,j)
w1(6)*A1(i,j+1) w1(7)*A1(i+1,j-1) w1(8)*A1(i+1,j) w1(9)*A1(i+1,j+1)];
A2(i,j)=min(a11) //Erosion
ea=A2
end
end
figure(1),imshow(aa)
figure(2),imshow(ea)

You might also like