Image Processing and Pattern Recoginition Lab Manual
Image Processing and Pattern Recoginition Lab Manual
Read, Access, and Display digital image using MATLAB or SCI Lab
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Show the image
6. Stop.
Program:
a=imread('F:\lily.jpg');
//figure;
subplot(1,2,1);
title('original image');
imshow(a);
a1=rgb2gray(a);
subplot(1,2,2);
title('gray image');
imshow(a1);
Output:
Result:
The program to read, access, and display digital images using MATLAB or Scilab was
executed successfully.
EXP 1b:
Add, Subtract, Multiply and Divide two images using SCI Lab
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input images from your desktop
5. Add, subtract, multiply and divide the images
6. Show the image
7. Stop.
Program:
a=imread('F:\lily.jpg');
a=rgb2gray(a);
a=imresize(a,[100 100]);
subplot(2,3,1)
title('original image of a');
imshow(a);
b=imread('F:\doggo.jpeg');
b=rgb2gray(b);
b=imresize(b,[100 100]);
subplot(2,3,2)
title('original image of b');
imshow(b);
c=imadd(a,b);
subplot(2,3,3)
title('image addition');
imshow(c);
d=imsubtract(a,b);
subplot(2,3,4)
title('subtraction');
imshow(d);
e=immultiply(b,b);
subplot(2,3,5)
title('multiplication');
imshow(uint8(e));
f=imdivide(a,b);
f=imdivide(f,0.01);
subplot(2,3,6)
title('division');
imshow(f);
Output:
Result:
The program to add, subtract, multiply and divide two images using scilab was executed
successfully.
EXP 2:
Sampling
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Sample the image using various values
6. Show the image
7. Stop.
Program:
clc;
n=2;
img = rgb2gray(imread('F:\lily.jpg'));
a=size(img);
w=a(2);
h=a(1);
im=zeros(100);
for i=1:n:h
for j=1:n:w
for k=0:n-1
for l=0:n-1
im(i+k,j+l)=img(i,j);
end
end
end
end
n1=4;
im1=zeros(100);
for i=1:n1:h
for j=1:n1:w
for k=0:n1-1
for l=0:n1-1
im1(i+k,j+l)=img(i,j);
end
end
end
end
n2=8;
im2=zeros(100);
for i=1:n2:h
for j=1:n2:w
for k=0:n2-1
for l=0:n2-1
im2(i+k,j+l)=img(i,j);
end
end
end
end
n3=12;
im3=zeros(100);
for i=1:n3:h
for j=1:n3:w
for k=0:n3-1
for l=0:n3-1
im3(i+k,j+l)=img(i,j);
end
end
end
end
subplot(2,3,1);
imshow(uint8(img));
title('Original Image');
subplot(2,3,2);
imshow(uint8(im));
title('Sampled Image - 2');
subplot(2,3,3);
imshow(uint8(im1));
title('Sampled Image - 4');
subplot(2,3,4);
imshow(uint8(im2));
title('Sampled Image - 8');
subplot(2,3,5);
imshow(uint8(im3));
title('Sampled Image - 12');
Output:
Result:
Neighborhood Metrics
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement neighborhood metrics
6. Show the image
7. Stop.
Program:
img = rgb2gray(imread('F:\lily.jpg'));
imgnoise = imnoise(img, 'salt and pepper');
subplot(2,3,1)
title('Original Image');
imshow(img);
subplot(2,3,2)
title('Noisy Image');
imshow(imgnoise);
flinear1=1/25*ones(5,5);
imglinear1=imfilter(imgnoise,flinear1);
subplot(2,3,3)
title('Linear Average Filtered');
imshow(imglinear1);
hsize=[5,5];
sigma=1;
flinear2=fspecial('gaussian',hsize,sigma);
imglinear2=imfilter(imgnoise,flinear2);
subplot(2,3,4)
title('Linear Gaussian Filtered');
imshow(imglinear2);
fnonlinear=[3,3];
imgnonlinear=immedian(imgnoise,fnonlinear);
subplot(2,3,5)
title('Median Filtered(Non-Linear)')
imshow(imgnonlinear)
Output:
Result:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement neighborhood metrics
6. Show the image
7. Stop.
Program:
clc;
clear all;
close all;
I = rgb2gray (imread("F:\lily.jpg"));
I_noise=imnoise (I, 'salt & pepper');
FilterSize = [3 3];
I_3x3 = immedian (I_noise, FilterSize ) ;
I_5x5 = immedian ( I_noise, [5 5]);
I_7x7 = immedian ( I_noise, [7 7]);
I_9x9 = immedian ( I_noise, [9 9]);
subplot (2,3,1);
title ('original image')
imshow (I)
subplot (2,3,2);
title ('noisy-image')
imshow (I_noise)
subplot (2,3,3);
title('Filter-size-3x3')
imshow (I_3x3)
subplot (2,3,4);
title('Filter-size-5x5')
imshow (I_5x5)
subplot (2,3,5);
title('Filter size 7x7')
imshow (I_7x7)
subplot (2,3,6);
title('Filter size 9x9')
imshow (I_9x9)
Output:
Result:
The program to study the effect of the size of neighborhood on the result of processing
has been executed successfully.
EXP 4:
Image Enhancement
Aim:
To write a program to enhance the image using scilab through image negative and
gamma transformation enhancement.
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement image enhancement processes
6. Show the image
7. Stop.
Program:
clc;
clear;
close;
img= imread ("F:\lily.jpg") ;
img=rgb2gray(img) ;
I =im2double(img);
J= imcomplement(I) ;
subplot(2, 3, 1);
title('Original-Image');
imshow (img) ;
subplot(2, 3, 2) ;
title('Image -Negative');
imshow(J) ;
gamma=1.5
k=I.^gamma;
subplot(2, 3, 3) ;
title('Garma-transformation');
imshow(k) ;
contrastl=1./(1+(0.2./(I+%eps)).^4);
contrast2=1./(1+ (0.5./(I+%eps)).^5);
contrast3=1./(1+(0.7./(I+%eps)).^10);
subplot(2, 3, 4) , imshow (contrastl) ;
title ('Contrast enhancement -0.2') ;
subplot(2, 3, 5) , imshow (contrast2) ;
title ('Contrast enhancement 0.5') ;
subplot(2, 3, 6) , imshow (contrast3);
title('Contrast enhancement-0.71') ;
Output:
Result:
Histogram
Aim:
To plot histogram, histogram equalized image and correlate between histogram and
histogram equalized of original image.
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement histogram equalization
6. Implement correlation
7. Show the image
8. Stop.
Program:
clc ;
clear;
close;
img=imread ('F:\lily.jpg');
img=rgb2gray(img)
[count, cells ]= imhist (img);
subplot (2,2,1);
title('Original-image');
imshow (img)
subplot (2,2,2);
plot2d3 ('gnn', cells, count)
title('Histogram-plot-for-original-image!');
Iheq = imhistequal (img);
[count, cells ]=imhist (Iheq);
subplot (2,2,3);
title('Histogram-Equalized-image');
imshow (Iheq);
subplot (2,2,4);
plot2d3 ('gnn', cells, count);
title ('Histogram-plot-for-histogram-equalized-image');
clc ;
clear;
close;
img=imread ('F:\lily.jpg');
img=rgb2gray(img);
[count, cells ]= imhist (img);
subplot (2,2,1)
title('Original-image');
imshow (img);
subplot (2,2,2);
plot2d3 ('gnn', cells, count);
title('Histogram-plot-for-original-image!');
Iheq = imhistequal (img);
[count, cells ]=imhist (Iheq);
subplot (2,2,3);
title('Histogram-Equalized-image');
imshow (Iheq)
subplot (2,2,4);
plot2d3 ('gnn', cells, count);
title ('Histogram-plot-for-histogram-equalized-image');
Output:
Result:
The program to plot histogram, histogram equalized image and correlate between
histogram and histogram equalized of original image was executed successfully.
EXP 6:
Smoothing
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement smoothing
6. Show the image
7. Stop.
Program:
clc ;
clear ;
close;
I= imread ('F:\lily.jpg');
Iblur1 = imfilter(I,2);
Iblur2 = imfilter(I,4);
Iblur3 = imfilter(I,8);
subplot(2,4,1)
imshow(I)
title('Original image')
subplot(2,4,2)
imshow(Iblur1)
title('Smoothed image, \sigma = 2')
subplot(2,4,3)
imshow(Iblur2)
title('Smoothed image, \sigma = 4')
subplot(2,4,4)
imshow(Iblur3)
title('Smoothed image, \sigma = 8')
IblurX1 = imfilter(I,[4 1]);
IblurX2 = imfilter(I,[8 1]);
IblurY1 = imfilter(I,[1 4]);
IblurY2 = imfilter(I,[1 8]);
subplot(2,4,5)
imshow(IblurX1)
title('Smoothed image, \sigma_x = 4, \sigma_y = 1')
subplot(2,4,6)
imshow(IblurX2)
title('Smoothed image, \sigma_x = 8, \sigma_y = 1')
subplot(2,4,7)
imshow(IblurY1)
title('Smoothed image, \sigma_x = 1, \sigma_y = 4')
subplot(2,4,8)
imshow(IblurY2)
title('Smoothed image, \sigma_x = 1, \sigma_y = 8')
Output:
Result:
Thresholding
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement thresholding
6. Show the image
7. Stop.
Program:
img= imread("F:\lily.jpg");
img=rgb2gray(img);
threshold =0.2;
imagen1=~im2bw(img, threshold);
subplot(2,2,1)
title ('threshold=0.1');
imshow (imagen1)
threshold =0.3;
imagen2=~im2bw(img,threshold);
subplot(2,2,2)
title('threshold = 0.2');
imshow(imagen2)
threshold =0.4,
imagen3=~im2bw(img ,threshold);
subplot(2,2,3)
title('threshold=0.3');
imshow(imagen3)
threshold=0.5;
imagen4=~im2bw(img, threshold);
subplot(2,2,4)
title('threshold=0.4');
imshow(imagen
Output:
Result:
Edge Detection
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement edge detection
6. Show the image
7. Stop.
Program:
//edge detection
img= imread('F:\lily.jpg');
img=rgb2gray( img );
subplot(2,3,1)
imshow (img);
e=edge (img );
//Opens new figure
subplot(2,3,2)
imshow (e)
e=edge (img, 'prewitt');
subplot(2,3,3)
imshow (e)
e=edge (img,'canny',[0.06 0.2]);
// Applies canny e detection method
subplot(2,3,4)
imshow (e)
e=edge (img,'fftderiv',0.4);
subplot(2,3,5)
imshow(e)
Output:
Result:
Hough Transformation
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement Hough transformation
6. Show the image
7. Stop.
Program:
Result:
Feature Extraction
Aim:
Procedure:
1. Start
2. Open scilab
3. Open a new console window
4. Get the input image from your desktop
5. Implement feature extraction
6. Show the image
7. Stop.
Program:
S= imread ('F:\lily.jpg' );
S = rgb2gray (S) ;
S2 = imrotate(S,45);
// Use the ORB to detect features
f1 = imdetect_ORB(S)
f2 = imdetect_ORB(S2)
// Extract the descriptor
d1 = imextract_DescriptorORB(S,f1);
d2 = imextract_DescriptorORB(S2,f2);
// Feature matching
m = immatch_BruteForce(d1,d2,4)
// Find the 10 best matches
[fout1,fout2,mout] = imbestmatches(f1,f2,m,10);
// Draw the matches
SS = imdrawmatches(S,S2,fout1,fout2,mout);
// Show the comparison
imshow(SS);
Output:
Result:
Aim:
Procedure:
7. Start
8. Open Matlab
9. Open a new console window
10. Get the input image from your desktop
11. Implement connected component analysis
12. Show the image
13. Stop.
Program:
Img1 =imread('C:\Users\ACER\Desktop\fruit.jpg');
Img2=imresize(Img1,[100 100])
Img3=rgb2gray(Img2)
Img=imbinarize(Img3)
L = bwlabel(Img,8)
figure;
subplot(2,2,1)
imshow(Img1)
title('Original Image')
subplot(2,2,2)
imshow(Img2)
title('Resized Image')
subplot(2,2,3)
imshow(Img3)
title('gray level Image')
subplot(2,2,4)
imshow(L)
title('connected component')
Output:
Result:
Aim:
Procedure:
1. Start
2. Open Matlab
3. Open a new console window
4. Get the input image from your desktop
5. Implement connected component analysis
6. Show the values
7. Stop.
Input:
Img = [0 0 0 0 0 1 1
0 1 1 0 0 1 1
0 1 1 0 0 1 1
0 0 0 1 0 1 1
0 0 0 1 0 1 1
0 0 0 1 0 1 1
0 0 1 1 0 1 1
0 0 0 0 0 1 1];
L = bwlabel(Img,4)
Output:
0 0 0 0 0 3 3
0 1 1 0 0 3 3
0 1 1 0 0 3 3
0 0 0 2 0 3 3
0 0 0 2 0 3 3
0 0 0 2 0 3 3
0 0 2 2 0 3 3
0 0 0 0 0 3 3
Result:
Skeletonization
Aim:
Procedure:
1. Start
2. Open Matlab
3. Open a new console window
4. Get the input image from your desktop
5. Implement skeletonization
6. Show the image
7. Stop.
Program:
I = imread('threads.png');
figure;
subplot(2,2,1)
imshow(I)
title('Original Image')
Icomplement = imcomplement(I);
BW = imbinarize(Icomplement);
subplot(2,2,2)
imshow(BW)
title('Binary Image')
out = bwskel(BW);
subplot(2,2,3)
imshow(labeloverlay(I,out,'Transparency',0))
title('skeletonized Image')
out2 = bwskel(BW,'MinBranchLength',15);
subplot(2,2,4)
imshow(labeloverlay(I,out2,'Transparency',0))
title('skeletonized Pruned Image')
Output:
Result:
Distance Transform
Aim:
Procedure:
1. Start
2. Open Matlab
3. Open a new console window
4. Get the input image from your desktop
5. Implement direct transform procedure on the image
6. Show the image
7. Stop.
Program:
bw = zeros(200,200);
bw(50,50) = 1; bw(50,150) = 1; bw(150,100) = 1;
D1 = bwdist(bw,'euclidean');
D2 = bwdist(bw,'cityblock');
D3 = bwdist(bw,'chessboard');
D4 = bwdist(bw,'quasi-euclidean');
RGB1 = repmat(rescale(D1), [1 1 3]);
RGB2 = repmat(rescale(D2), [1 1 3]);
RGB3 = repmat(rescale(D3), [1 1 3]);
RGB4 = repmat(rescale(D4), [1 1 3]);
figure
subplot(2,2,1), imshow(RGB1), title('Euclidean')
hold on, imcontour(D1)
subplot(2,2,2), imshow(RGB2), title('Cityblock')
hold on, imcontour(D2)
subplot(2,2,3), imshow(RGB3), title('Chessboard')
hold on, imcontour(D3)
subplot(2,2,4), imshow(RGB4), title('Quasi-Euclidean')
hold on, imcontour(D4)
Output:
Result:
Aim:
Procedure:
1. Start
2. Open Matlab
3. Open a new console window
4. Get the input image from your desktop
5. Implement enhancement of color image
6. Show the image
7. Stop.
Program:
clc;
clear all;
close all;
a1 = imread('peppers.png');
a = im2double(a1);
[m,n,k] = size(a);
ar = a(:,:,1);
ag = a(:,:,2);
ab = a(:,:,3);
br = 0.4*ar;
bg = 0.4*ag;
bb = 0.4*ab;
c = zeros(m,n,k);
c(:,:,1) = br;
c(:,:,2) = bg;
c(:,:,3) = bb;
figure;
subplot(1,2,1)
imshow(a)
title("original image")
subplot(1,2,2)
imshow(c)
title("enhanced image")
Output:
Result:
Aim:
Procedure:
1. Start
2. Open Matlab
3. Open a new console window
4. Get the input image from your desktop
5. Implement dilation and erosion
6. Show the image
7. Stop.
Program:
BW1 = imread('circbw.tif');
figure;
subplot(1,3,1)
imshow(BW1)
title('original image')
SE = strel('rectangle',[40 30]);
BW2 = imerode(BW1,SE);
subplot(1,3,2)
imshow(BW2)
title('Erosion image')
BW3 = imdilate(BW2,SE);
subplot(1,3,3)
imshow(BW3)
title('Dilation image')
Output:
Result: