0% found this document useful (0 votes)
66 views28 pages

Computer Vision New

This document outlines various computer vision and image processing techniques implemented in MATLAB. It includes steps for basic image operations like reading, writing and converting images. It also covers techniques like contrast adjustment, histogram processing, filtering using low pass/high pass filters and Fourier transforms. Further, it demonstrates extracting HOG features, corner detection using Moravec algorithm, image segmentation through thresholding and edge detection. Finally, it provides code to compute optical flow and demonstrate its use in an application.
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
0% found this document useful (0 votes)
66 views28 pages

Computer Vision New

This document outlines various computer vision and image processing techniques implemented in MATLAB. It includes steps for basic image operations like reading, writing and converting images. It also covers techniques like contrast adjustment, histogram processing, filtering using low pass/high pass filters and Fourier transforms. Further, it demonstrates extracting HOG features, corner detection using Moravec algorithm, image segmentation through thresholding and edge detection. Finally, it provides code to compute optical flow and demonstrate its use in an application.
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/ 28

LJ Institute of Engineering and Technology Computer Vision (3171614)

Computer Vision (3171614)


 Lab Manual

1. Implementing various basic image processing operations in MATLAB:


Reading image, writing image, conversion of images, complement of an
image.

a. Reading image

I = imread('columns.tif');
imshow(I)

Output:

1
LJ Institute of Engineering and Technology Computer Vision (3171614)

b. writing image

load clown
imwrite (X,map,'clown.tif');
image(X)

Output:

2
LJ Institute of Engineering and Technology Computer Vision (3171614)

c. conversion of images

RGB = imread('peppers.png');
imshow(RGB)

Output:

I = rgb2gray(RGB);
figure
imshow(I)

Output:

3
LJ Institute of Engineering and Technology Computer Vision (3171614)

d. complement of an image

I=imread('columns.tif');
imshow(I);
I=imcomplement(I);
figure, imshow(I);

Output:

4
LJ Institute of Engineering and Technology Computer Vision (3171614)

2. Implement contrast adjustment of an image. Implement Histogram


processing and equalization.

a. contrast adjustment of an image

imshow('cameraman.tif')

Imcontrast

5
LJ Institute of Engineering and Technology Computer Vision (3171614)

6
LJ Institute of Engineering and Technology Computer Vision (3171614)

b. Histogram processing and equalization

I = imread('columns.tif');
figure
subplot(1,2,1)
imshow(I)
subplot(1,2,2)
imhist(I,64)

Output:

J = histeq(I);
figure
subplot(1,2,1)
imshow(J)
subplot(1,2,2)
imhist(J,64)

7
LJ Institute of Engineering and Technology Computer Vision (3171614)

Output:

8
LJ Institute of Engineering and Technology Computer Vision (3171614)

3. Implement the various low pass and high pass filtering mechanisms.

a=imread('cameraman.tif');
b=size(a);

if size(b,2)==3
a = rgb2gray(a);
end
a = imnoise(a,'salt & pepper');

n=input("Enter the size of mask");


n1=ceil(n/2);
a=double(a);
lpf=(1/n^2)*ones(n);
hpf=-lpf;
hpf(n1,n1)=(n^2-1)/n^2;

c=0;
h=0;
for i=n1:b(1)-n1
for j=n1:b(2)-n1
p=1;
for k=1:n
for l=1:n
c(p)=a(i-n1+k,j-n1+l);
p=p+1;
end
end

d(i,j)=median(c);
c=0;
end
end
e=uint8(d);
figure;imshow(e);title('low pass image');

for i=n1:b(1)-n1
for j=n1:b(2)-n1
for k=1:n
for l=1:n
h=h+a(i-n1+k,j-n1+l)*hpf(k,l);
end

9
LJ Institute of Engineering and Technology Computer Vision (3171614)

end

g(i,j)=h;
h=0;
end
end
f=uint8(g);
figure;imshow(f);title('high pass image');
figure;imshow(uint8(a));title('Original image');

Output:

10
LJ Institute of Engineering and Technology Computer Vision (3171614)

4. Use of Fourier transform for filtering the image.

I=imread(' columns.tif');
I1=rgb2gray(I);
I2=fft2(I1);
subplot(311);
imshow(I);
subplot(312);
imshow(I1);
subplot(313);
imshow(I2)

Output:

11
LJ Institute of Engineering and Technology Computer Vision (3171614)

5. Utilization of HOG features for image analysis.

i. Extract and Plot HOG features over the original image.

img = imread('cameraman.tif');
[featureVector,hogVisualization] = extractHOGFeatures(img);
figure;
imshow(img);

hold on;
plot(hogVisualization);

12
LJ Institute of Engineering and Technology Computer Vision (3171614)

ii. Extract HOG features using CellSize.

I1 = imread('gantrycrane.png');
[hog1,visualization] = extractHOGFeatures(I1,'CellSize',[32
32]);
subplot(1,2,1);
imshow(I1);

subplot(1,2,2);
plot(visualization);

iii. Extract HOG features Around Corner Points

I2 = imread('gantrycrane.png');
corners = detectFASTFeatures(im2gray(I2));
13
LJ Institute of Engineering and Technology Computer Vision (3171614)

strongest = selectStrongest(corners,3);
[hog2,validPoints,ptVis] = extractHOGFeatures(I2,strongest);
figure;
imshow(I2);

hold on;
plot(ptVis,'Color','green');

14
LJ Institute of Engineering and Technology Computer Vision (3171614)

6. Explain Moravec corner detection algorithm and write a MATLAB


code to implement this algorithm.

%file name must be Moravec.


function C = moravec(I)
%MORAVEC Moravec corner detector
%
% C = MORAVEC(I)
%
% [email protected] 2015-05-20
if nargin == 0 % simple test
I = zeros(100);
I(40:60, 40:60) = 1;
C = moravec(I);
imshow(0.5*C+I, [])
if nargout == 0, clear C, end
return
end
D(:,:,1) = diff([I I(:,end)]')';
D(:,:,2) = diff([I(:,1) I]')';
D(:,:,3) = diff([I; I(end,:)]);
D(:,:,4) = diff([I(1,:); I]);
C = sum(D.^2, 3);
if nargout == 0, clear C, end

Output:

15
LJ Institute of Engineering and Technology Computer Vision (3171614)

7. Performing/Implementing image segmentation.


a. Gray Level Thresholding

I = imread('coins.png');
level = graythresh(I)

BW = imbinarize(I,level);
imshowpair(I,BW,'montage')

b. Edge Detection

I = imread('coins.png');
imshow(I)

16
LJ Institute of Engineering and Technology Computer Vision (3171614)

BW1 = edge(I,'sobel');
BW2 = edge(I,'canny');
tiledlayout(1,2)

nexttile
imshow(BW1)
title('Sobel Filter')

nexttile
imshow(BW2)
title('Canny Filter')

c. Morphological Processing
i. Dilation
ii. Erosion

I=imread('circles.png');
%structruring element
sel=strel('disk',11);
er=imerode(I,sel);
figure,
imshow(I);
title('original Image');
figure,
imshow(er);
title('Image after erosion');
A=imread('cameraman.tif');
se2=strel('ball',5,5);
de=imdilate(A,se2);
figure,I
imshow(A);
title('original Image');

17
LJ Institute of Engineering and Technology Computer Vision (3171614)

figure,
imshow(de);
title('Image after Dilation');

18
LJ Institute of Engineering and Technology Computer Vision (3171614)

8. Implement optical flow computation algorithm.

flow = opticalFlow

Vx = randn(100,100);
Vy = randn(100,100);
opflow = opticalFlow(Vx,Vy);
opflow

plot(opflow,'DecimationFactor',[10 10],'ScaleFactor',10);

19
LJ Institute of Engineering and Technology Computer Vision (3171614)

20
LJ Institute of Engineering and Technology Computer Vision (3171614)

9. Demonstrate the use of optical flow in any image processing


application.

vidReader = VideoReader('visiontraffic.avi','CurrentTime',11);
opticFlow = opticalFlowHS

opticFlow =

opticalFlowHS with properties:

Smoothness: 1
MaxIteration: 10
VelocityDifference: 0

h = figure;
movegui(h);
hViewPanel = uipanel(h,'Position',[0 0 1 1],'Title','Plot of Optical Flow
Vectors');
hPlot = axes(hViewPanel);
while hasFrame(vidReader)
frameRGB = readFrame(vidReader);
frameGray = im2gray(frameRGB);
flow = estimateFlow(opticFlow,frameGray);
imshow(frameRGB)
hold on
plot(flow,'DecimationFactor',[5 5],'ScaleFactor',60,'Parent',hPlot);
hold off
pause(10^-3)
end

21
LJ Institute of Engineering and Technology Computer Vision (3171614)

Output:

22
LJ Institute of Engineering and Technology Computer Vision (3171614)

10.Object detection and Recognition on available online image datasets.

data = load('fasterRCNNVehicleTrainingData.mat', 'detector');


detector = data.detector;
I = imread('highway.png');
imshow(I)

[bboxes,scores,labels] = detect(detector,I)

23
LJ Institute of Engineering and Technology Computer Vision (3171614)

detectedI = insertObjectAnnotation(I,'Rectangle',bboxes,cellstr(labels));
figure
imshow(detectedI)

24
LJ Institute of Engineering and Technology Computer Vision (3171614)

11.face classification project.

We need to do three things to implement such a system. These are –

1. Preparing dataset
Dataset link =
https://drive.google.com/file/d/14g_fD74pDmX9AsgkAQG59cxQSh0fgRr2/view

2. Loading the dataset


After preparing the dataset, next task is loading the dataset. We will
implement a function in Matlab to load the dataset. You can use this
function to load other dataset as well.  Let’s name it
‘load_database.m’.

function output_value = load_database();

persistent loaded;

persistent numeric_Image;

if(isempty(loaded))

    all_Images = zeros(10304,40);

    for i=1:40

        cd(strcat('s',num2str(i)));

        for j=1:10

            image_Container =
imread(strcat(num2str(j),'.pgm'));

            all_Images(:,(i-
1)*10+j)=reshape(image_Container,size(image_Container,1)
*size(image_Container,2),1);

        end

        display('Doading Database');

25
LJ Institute of Engineering and Technology Computer Vision (3171614)

        cd ..

    end

    numeric_Image = uint8(all_Images);

end

loaded = 1;

output_value = numeric_Image;

3. Recognizing the face

We will implement a function in Matlab to load the dataset. Let’s


name it ‘Face_Recognition.m’.

loaded_Image=load_database();

random_Index=round(400*rand(1,1));          

random_Image=loaded_Image(:,random_Index);               
          

rest_of_the_images=loaded_Image(:,[1:random_Index-1
random_Index+1:end]);         

image_Signature=20;                            

white_Image=uint8(ones(1,size(rest_of_the_images,2)));

mean_value=uint8(mean(rest_of_the_images,2));            
    

mean_Removed=rest_of_the_images-
uint8(single(mean_value)*single(white_Image)); 

L=single(mean_Removed)'*single(mean_Removed);

[V,D]=eig(L);

V=single(mean_Removed)*V;

26
LJ Institute of Engineering and Technology Computer Vision (3171614)

V=V(:,end:-1:end-(image_Signature-1));          

all_image_Signatire=zeros(size(rest_of_the_images,2),ima
ge_Signature);

for i=1:size(rest_of_the_images,2);

   
all_image_Signatire(i,:)=single(mean_Removed(:,i))'*V;  

end

subplot(121);

imshow(reshape(random_Image,112,92));

title('Looking for this


Face','FontWeight','bold','Fontsize',16,'color','red');

subplot(122);

p=random_Image-mean_value;

s=single(p)'*V;

z=[];

for i=1:size(rest_of_the_images,2)

    z=[z,norm(all_image_Signatire(i,:)-s,2)];

   
if(rem(i,20)==0),imshow(reshape(rest_of_the_images(:,i),
112,92)),end;

    drawnow;

end

[a,i]=min(z);

subplot(122);

imshow(reshape(rest_of_the_images(:,i),112,92));

27
LJ Institute of Engineering and Technology Computer Vision (3171614)

title('Recognition
Completed','FontWeight','bold','Fontsize',16,'color','re
d');

Output:

28

You might also like