Professional Documents
Culture Documents
Pacv1 2 3
Pacv1 2 3
SEMESTER VII
DEPARTMENT OF
ELECTRONICS AND COMMUNICATION
ENGINEERING
GOVERNMENT ENGINEERING COLLEGE- MODASA
Practical Aspects of Computer Vision (3171113) 200160111007
CERTIFICATE
Preface
The main motto of any laboratory/practical/field work is to enhance required skills as well as create
ability amongst students to solve real-time problems by developing relevant competencies in the
psychomotor domain. By keeping this in view, GTU has designed a competency outcome-based
curriculum for engineering degree programs where sufficient weightage is given to practical work.
It shows the importance of enhancement of skills amongst the students and it pays attention to
utilising every second allotted for practical amongst students, instructors and faculty members to
achieve relevant outcomes by performing the experiments rather than having merely study-type
experiments. It is a must for the effective implementation of a competency-focused outcome-based
curriculum that every practical is keenly designed to serve as a tool to develop and enhance relevant
competency required by the various industries among every student. These psychomotor skills are
very difficult to develop through traditional chalk-and-board content delivery methods in the
classroom. Accordingly, this lab manual is designed to focus on the industry-defined relevant
outcomes, rather than the old practice of conducting practicals to prove concepts and theories.
By using this lab manual students can go through the relevant theory and procedure in advance
before the actual performance which creates interest and students can have a basic idea beforethe
performance. This in turn enhances pre-determined outcomes amongst students. Each experiment
in this manual begins with competency, industry-relevant skills, course outcomes as well and
practical outcomes (objectives). The students will also achieve safety and necessary precautions to
be taken while performing practicals.
This manual also provides guidelines to faculty members to facilitate student-centric lab activities
through each experiment by arranging and managing necessary resources so that the students
follow the procedures with required safety and necessary precautions to achieve the outcomes. It
also gives an idea of how students will be assessed by providing rubrics.
Practical Aspects of Computer Vision is an Elective course which deals with developingnecessary
practical skills among the students to build computer vision applications. It provides aplatform for
students to demonstrate their skills making the computer do what humans can do with their eyes
and brain. Students also learn programming (coding) skills and application of machine learning to
an extent.
Utmost care has been taken while preparing this lab manual however there is always room for
improvement. Therefore, we welcome constructive suggestions for improvement of the content and
removal of errors, if any.
Disclaimer
This lab manual is prepared for academic purposes only and intended to be used by
undergraduate students of the EC branch.
This manual was prepared with licensed versions of MATLAB software (which is a registered
trademark of Mathworks Inc. USA) during my tenure at Government Engineering College,
Gandhinagar and Government Engineering College, Modasa. Students may use the student
version, which may have limited validity and reduced functionalities. OR students may also
use open-source programming languages such as Python.
Before taking this course, the students are expected to have some programming knowledge in
any programming language.
Most sample codes/demos. codes given in this manual are taken from various online sources
like the website, videos and blogs of Mathworks Inc., free YouTube videos from computer
vision/image processing enthusiasts and the codes submitted by my past students as their lab
work/project work. All sample codes are run and found to work error-free on my laptop.
However, MATLAB continuously improves their set of functions and some functions which I
have used in demo codes may not be supported in your version and may be replaced by their
updated versions.
The sample codes are there for you to quickly test/visualize your understanding of the concept.
They may not be the best/optimum codes. With practice, you may write better codes than the
codes in this manual. Do write codes on your own rather than copying.
Although every care has been taken to make these codes error-free, there may be syntax errors
in some cases due to typing errors while preparing the manual. The instructors are supposed to
help/guide the students in such cases.
As this course is purely programming/software-based, we do not mention any
equipment/instrument list in any of the experiments.
In the theory part, we have mentioned only the major functions and their brief descriptions.
There are many other functions that one finds while reading the sample codes. The students
are encouraged to learn about them on their own, as they are an integral part of the
programming language.
Also, the procedure in software-based experiments is to think of logical solutions and write
codes. So, we do not mention any procedure/steps in this manual.
The instructors must see that every student uses his/her logic and variable names in the codes
for any two students are not the same.
Happy learning!
Practical Aspects of Computer Vision (3171113) 200160111007
Index
(Progressive Assessment Sheet)
Experiment No: 1
Basic image handling and processing (4 Hrs. 2 Sessions).
Date(s):
Objectives: To get familiarity with basic image handling and processing functions.
Theory:
The students are advised to go through the detailed description of all the following functions
anduse each of them at least once during their first lab practice.
• imread: To read the image as a matrix of numbers from a specified location/path in your
computer or an in-built MATLAB image database.
• imshow: To display the matrix read with ‘imread’ as an image on the display device
• figure: It creates a window that forces the current image to become visible above all
other windows.
• title: It is used to give the title to the image in the figure.
• insertObjectAnnotation: This function is used to provide annotation in the image.
• imfinfo: It gives information about the image
• imcontour: Creates contour plot on the image.
1
Practical Aspects of Computer Vision (3171113) 200160111007
• Plotting points and lines over images: In most computer vision tasks, we are interested
inshowing feature points, correspondences, detected objects, etc. in the image using
pointsand lines. It is possible to plot/draw points, lines and geometrical shapes using
simple commands in MATLAB. Students are encouraged to write small code for this.
A useful command for plotting lines is ‘line’. One can use ‘impoly’ to plot a polygon.
2
Practical Aspects of Computer Vision (3171113) 200160111007
• Crop, Copy and paste regions: It is required to crop, copy and paste part of an image
from one location to another or one image to another. ‘imcrop’ is a useful in-built
MATLAB
• Command to crop the image. Students can write code involving these tasks on their
own.
Procedure:
1. Enter program codes as demonstrated in the sample code below, prepare a file/script and
run the program(s). (One may prepare a flow chart and pseudo code, if required before entering
the code.)
Sample codes & Results: Demo. Codes and results are given below. Save output images and
anyother calculation/graph/ chart/table generated after the program is executed.
3
Practical Aspects of Computer Vision (3171113) 200160111007
Filename: 'C:\Users\parth\Downloads\i_card.jpg'
FileSize: 193703
Format: 'jpg'
FormatVersion: ''
Width: 1700
Height: 1087
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: ''
NumberOfSamples: 3 4
CodingMethod: 'Huffman'
CodingProcess: 'Progressive'
Practical Aspects of Computer Vision (3171113) 200160111007
ColorType: 'truecolor'
FormatSignature: ''
NumberOfSamples: 3
CodingMethod: 'Huffman'
CodingProcess: 'Progressive'
Comment: {}
Output Images:
Figure 1: Figure 2:
Figure 3:
5
Practical Aspects of Computer Vision (3171113) 200160111007
Figure 4:
Figure 5:
Figure 6:
6
Practical Aspects of Computer Vision (3171113) 200160111007
Figure 7: Figure 8:
Figure 9:
7
Practical Aspects of Computer Vision (3171113) 200160111007
Output Images:
Figure 1:
8
Practical Aspects of Computer Vision (3171113) 200160111007
Figure 2:
9
Practical Aspects of Computer Vision (3171113) 200160111007
itemp = imread('C:\Users\parth\Downloads\len_std.jpg');
image = itemp(:,:,1);
%decide the min. level of intensity level slicing range
rmin = 100;
%decide the max. level of intensity level slicing range
rmax = 150;
[r,c] = size(image); %dimensions of image
s = zeros(r,c); % pre allocate a variable to store the result image
%result Image
for i = 1:r
for j = 1:c
% if the current pixel of original image is in the specfied range then
make it 0 in the result image
if (rmin < image(i,j) && image(i,j) < rmax)
s(i,j) = 0;
else
% otherwise store the same value of the pixel in the result image
s(i,j) = image(i, j);
end
end
end
Output Image:
10
Practical Aspects of Computer Vision (3171113) 200160111007
img = imread('peppers.png');
img_gray = rgb2gray(img);
figure;title('Bit plane slicing'); % Figure 1
subplot(1,2,1); imshow(img);title('original peppers image in color');
subplot(1,2,2); imshow(img_gray);title('gray scale image of peppers');
f1 = bitget(img_gray,1);
f2 = bitget(img_gray,2);
f3 = bitget(img_gray,3);
f4 = bitget(img_gray,4);
f5 = bitget(img_gray,5);
f6 = bitget(img_gray,6);
f7 = bitget(img_gray,7);
f8 = bitget(img_gray,8);
Output Images:
Figure 1:
11
Practical Aspects of Computer Vision (3171113) 200160111007
Figure 2:
12
Practical Aspects of Computer Vision (3171113) 200160111007
Output Image
13
Practical Aspects of Computer Vision (3171113) 200160111007
Task 6: Contours
clc;
clear;
close all;
% Image Contour
Cimg = imread('rice.png');
figure; imshow(Cimg); title('Real rice image') %Figure 1
figure; imcontour(Cimg); title('Contours') %Figure 2
% Inserting points and lines on an image
xp = [50, 100, 150, 200];
yp = [100, 100, 200, 200];
hold on;
plot(xp,yp,'r*'); title('Inserting points and lines on am image')
hold on;
line([50,100], [100,100], 'color', 'r', 'LineWidth', 2)
h = impoly;
position = wait(h);
Output Images:
Figure 1:
Figure 2:
14
Practical Aspects of Computer Vision (3171113) 200160111007
15
Practical Aspects of Computer Vision (3171113) 200160111007
end
[x, y] = ginput(1);
% Determine the pasting boundaries
r1 = int32(y);
c1 = int32(x);
r2 = r1 + rows2 - 1;
r2 = min([r2 rows]);
c2 = c1 + columns2 -1;
c2 = min([c2, columns]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
% Paste as much of croppedImage as will fit into the original image.
grayimage(r1:r2, c1:c2) = croppedimage(1:(r2-r1+1), 1:(c2-c1+1));
subplot(2,2,4);
imshow(grayimage);
axis on;
title('Region that you defined pasted onto original', 'FontSize', 20);
Output Images:
Figure 1: Figure 2:
Figure 3:
16
Practical Aspects of Computer Vision (3171113) 200160111007
Figure 4:
17
Practical Aspects of Computer Vision (3171113) 200160111007
1. Identify the image having contours in the output from the images above and write its
title as youranswer.
Ans. In Task 6 Figure 2 has contours in the output (Page no. 16).
Title: Inserting points and lines on an image
2. State the function that will convert a colour image into a grayscale image.
Ans. The function that will convert a colour image into a grayscale image is: rgb2gray()
3. Find the size of the autumn image required to store it in computer memory.
Ans. Required size: 213210 bytes
clc;
clear;
close all;
img = imread('autumn.tif');
% Use the whos function to get information about the image variable
info = whos('img');
% Extract the memory usage in bytes from the structure
memory_usage_bytes = info.bytes;
fprintf('Memory required to store the autumn image in computer memory to : %d
bytes\n', memory_usage_bytes);
18
Practical Aspects of Computer Vision (3171113) 200160111007
Suggested Reference(s):
https://in.mathworks.com/help/images/getting-started-with-image-processing-toolbox.html
https://www.oreilly.com/library/view/programming-computer-
vision/9781449341916/ch01.html
References used by the students: (List the references in the space provided below)
https://in.mathworks.com/matlabcentral/answers
Rubrics 1 2 3 4 5 Total
Quiz + Logical Neatness Accuracy/correctness Presentationof Timely
Understanding of the output the work submission 50
of the code
(10) (10) (10) (10) (10)
Points
19
Practical Aspects of Computer Vision (3171113) 200160111007
Experiment No: 2
Mathematical, Logical & Morphological operations on images
Date(s):
Theory:
The students are advised to go through the detailed description of all the following functions
anduse each of them at least once during this lab practice.
• imresize : Images must be of same size for arithmetical operations. So, resize one of
thetwo images to make it equal to the other image, if needed.
20
Practical Aspects of Computer Vision (3171113) 200160111007
21
Practical Aspects of Computer Vision (3171113) 200160111007
• Closing is so called because it can fill holes in the regions while keeping the initial
regionsizes. Like opening, closing is idempotent: (f • s) • s = f • s, and it is dual operation
of opening (just as opening is the dual operation of closing):
• f • s = (f c s)c; f s = (f c • s)c.
• In other words, closing (opening) of a binary image can be performed by taking the
complement of that image, opening (closing) with the structuring element, and taking
thecomplement of the result.
Sample codes & Results: Demo. Codes and results are given below. Save output images and
anyother calculation/graph/ chart/table generated after the program is executed.
Output Images:
22
Practical Aspects of Computer Vision (3171113) 200160111007
Output Images:
Figure 1: Figure 2:
Figure 3:
23
Practical Aspects of Computer Vision (3171113) 200160111007
Output Image:
24
Practical Aspects of Computer Vision (3171113) 200160111007
Output Images:
Figure 1:
25
Practical Aspects of Computer Vision (3171113) 200160111007
Output Images:
26
Practical Aspects of Computer Vision (3171113) 200160111007
% Dilated Image
se = strel('disk', 7);
dilate = imdilate(Im, se);
subplot(2, 3, 2); imshow(dilate);title('Dilated image')
% Eroded image
erode = imerode(Im, se);
subplot(2, 3, 3);imshow(erode); title('Eroded image')
% Opened image
open = imopen(Im, se);
subplot(2, 3, 4);imshow(open); title('Opened image')
% Closed image
closed = imclose(Im, se);
subplot(2, 3, 5);imshow(closed); title('Closed image')
Output Images:
27
Practical Aspects of Computer Vision (3171113) 200160111007
2. Briefly explain the difference of pixel values for A-B and abs|A-B| when A<B
when A is less than B, both A - B and abs (A - B) will provide information about the difference
between the pixel values of A and B. However, A - B will retain negative values to indicate the
direction of the difference, while abs (A - B) will only give you the magnitude of the difference,
making it useful for measuring the extent of dissimilarity between the two images without regard to
the direction of the difference.
One important application of morphological operations is in text document preprocessing for tasks
like optical character recognition (OCR). Morphological operations can help clean and enhance
scanned or digital text images, removing noise, improving text quality, and separating characters from
the background, making it easier for OCR algorithms to accurately recognize and extract text from
images.
Suggested Reference(s):
https://in.mathworks.com/help/images/getting-started-with-image-processing-toolbox.html
https://www.oreilly.com/library/view/programming-computer-vision/9781449341916/ch01.html
References used by the students: (List the references in the space provided below)
https://in.mathworks.com/help/images/getting-started-with-image-processing-toolbox.html
https://www.oreilly.com/library/view/programming-computer-vision/9781449341916/ch01.html
28
Practical Aspects of Computer Vision (3171113) 200160111007
Rubrics 1 2 3 4 5 Total
Points
29
Practical Aspects of Computer Vision (3171113) 200160111007
Experiment No: 3
Image filtering (4 Hrs=2 Sessions)
Date(s):
Theory:
The students are advised to go through the detailed description of all the following functions and use
each of them at least once during this lab practice.
Image blurring and de-blurring: Image gets blurred when it down-sampled or imaging at low-
resolution. In the context of signals, it is a low pass filtering operation in 2D ( two dimension).
Another way in which blurring can take place in an image is when either scene or camera is moving.
This second case is called motion blur.
30
Practical Aspects of Computer Vision (3171113) 200160111007
Un-sharp masking technique to sharpen images : Sharpening of images is a high-pass filtering in 2D.
‘imsharpen’ is the function used for un-sharp masking.
High-boost filtering : High boost filtering is used to highlight the high-frequnecy content in an image.
It is just adding the sharpened image to the original image.
Color split & merge : A color image has three planes or channels of colors; one each for Red, Green and
Blue color. For color-based image processing techniques it is, sometimes, necessary to split the given
image into its primary color components (Red, Green and Blue). This is, also, known as color filtering,
sometimes. On the other hand, sometimes, it is required to synthesize the color image by adding the
primary colors at each pixel. Hence, color split and merge operations are useful. These operations can
be carried out using matrix manipulation in MATLAB. Students are encouraged to write a small code
for these operations.
Frequency domain filtering of images: All the filtering techniques discussed and practiced above are in
spatial domain. This is similar to time domain analysis of signals. Now we will implement frequency
domain filters of different types and make a comparative analysis in the context of images.
• Low-pass filter
• High-pass filter
• Band-pass filter
• Band-stop filter (Also called band reject filter or notch filter)
For each of the types mentioned above, there are three ways of implementation
• Ideal
• Butterworth
• Gaussian
31
Practical Aspects of Computer Vision (3171113) 200160111007
• Figure 2:
• Figure 3:
• Figure 4:
• Figure 5:
• Figure 6:
• fftshift : fftshift is useful for visualizing the Fourier transform with the zerofrequency
• component in the middle of the spectrum.
• ifft2 : Calculates 2D discrete inverse Fourier transform to bring the image back to spatial
domain from frequency domain.
Homomorphic Filter: A special type of frequency domain filtering to improve contrast and dynamic
range of an image. Students are encouraged to study the concept and implement a basic code for
homomorphic filtering.
Sample codes & Results: Demo. Codes and results are given below. Save output images and any other
calculation/graph/ chart/table generated after the program is executed.
Sample Code that covers all functions/operations mentioned in theory.
Task 1: Image denoising
clc;
close all;
clear all;
%image denoising using filter
A=rgb2gray(imread('C:\Users\parth\Downloads\pr12.jpg'));
figure;title('Image de-noising')
subplot(2,2,1); imshow(A);title('Original image');
% Add salt & pepper noise
Ispn = imnoise(A,'salt & pepper', 0.03);
subplot(2,2,2); imshow(Ispn);title('Image with salt & pepper noise');
% Remove Salt & pepper noise by median filters
K = medfilt2(Ispn);subplot(2,2,3); imshow(uint8(K)); title('Remove salt & pepper
noise by median filter');
% Remove salt & pepper noise by Wiener filter
L = wiener2(Ispn,[5 5]);
subplot(2,2,4); imshow(uint8(L)); title('Remove salt & pepper noise by Wiener
filter');
figure;title('Gaussian Noise removal')
subplot(2,2,1); imshow(A);title('Original image');
% Add gaussian noise
M = imnoise(A,'gaussian',0,0.005);
subplot(2,2,2); imshow(M); title('Image with gaussian noise');
% Remove Gaussian noise by Wiener filter
L = wiener2(M,[5 5]);
subplot(2,2,3); imshow(uint8(L));title('Remove Gaussian noise by Wiener filter');
% Remove Gaussian noise by median filter
K = medfilt2(M);
subplot(2,2,4); imshow(uint8(K)); title('Remove Gaussian noise by median filter');
% unsharp masking technique to sharpen images
32
Practical Aspects of Computer Vision (3171113) 200160111007
Iunsharp=imread('rice.png');
Isharp =imsharpen(Iunsharp);
figure;imshowpair(Iunsharp,Isharp,'montage');title('unsharp masking')
% high boost filtering technique
Ihbf=imadd(Isharp,Iunsharp);
figure;imshowpair(Isharp,Ihbf,'montage');
title('High boost filtering')
Figure 1:
Figure 2:
33
Practical Aspects of Computer Vision (3171113) 200160111007
Figure 1:
Figure 2:
34
Practical Aspects of Computer Vision (3171113) 200160111007
output images:
Figure 1:
35
Practical Aspects of Computer Vision (3171113) 200160111007
36
Practical Aspects of Computer Vision (3171113) 200160111007
hb=zeros(m,n);
nx=2;% order of the filterd
for u=1:m
for v=1:n
db= ((u-(m/2)).^2+(v-(n/2)).^2);
hb(u,v)=1./(1+(db/fc/fc).^(2.*nx));
end
end
gb = hb.*shift_ft_im;
outputb=abs(ifft2(gb));
figure;
subplot(231);imshow(im);title('Gray scale image')
subplot(232);imshow(shift_ft_im);title('DFT of gray scale image')
subplot(233);mesh(log(1+abs(fftshift(ft_im))));title('DFT of the image after
shift')
subplot(234);imshow(hb);title('Butterworth Low Pass Filter mask 2D')
subplot(235);mesh(hb);title('Butetrworth Low Pass Filter mask 3D')
subplot(236);imshow(uint8(outputb));title('Filtered image')
%High Pass Filters
%Ideal
hpi=1-h;
shift_hpi=fftshift(hpi);
ghpi=hpi.*shift_ft_im;
out_hpi=abs(ifft2(ghpi));
figure;
subplot(221);imshow(im);title('Gray scale image')
subplot(222);imshow(hpi);title('Ideal High Pass Filter Mask 2D');
subplot(223);mesh(hpi);title('Ideal HPF 3D')
subplot(224);imshow(uint8(out_hpi));title('Filtered Image')
%Gaussian
hpg=1-hg;
shift_hpg=fftshift(hpg);
ghpg=hpg.*shift_ft_im;
out_hpg=abs(ifft2(ghpg));
figure;
subplot(221);imshow(im);title('Gray scale image')
subplot(222);imshow(hpg);title('Gaussian High Pass Filter Mask 2D');
subplot(223);mesh(hpg);title('Gaussian HPF 3D')
subplot(224);imshow(uint8(out_hpg));title('Filtered Image')
%Buttrworth
hpb=1-hb;
shift_hpb=fftshift(hpb);
ghpb=hpb.*shift_ft_im;
out_hpb=abs(ifft2(ghpb));
figure;
subplot(221);imshow(im);title('Gray scale image')
subplot(222);imshow(hpb);title('Butterworth High Pass Filter Mask 2D');
subplot(223);mesh(hpb);title('Butterworth HPF 3D')
subplot(224);imshow(uint8(out_hpb));title('Filtered Image')
% Band pass filter and band stop filters require two cut off frequencies
fcl=input('enter value of lower cut-off frequency in termsof pixels: ');
fch=input('enter value of higher cut-off frequency in termsof pixels: ');
%Band pass filter
% First create an LPF mask
hl=zeros(m,n);
[al ,bl]=freqspace(256,'meshgrid');
dl= m.*sqrt(al.^2+bl.^2)<=fch;%creates non separable(circular)filter mask
hl(dl)=1;
% Then create an HPF mask
37
Practical Aspects of Computer Vision (3171113) 200160111007
hh=ones(m,n);
[ah,bh]=freqspace(256,'meshgrid');
dh= m.*sqrt(ah.^2+bh.^2)<=fcl;
hh(dh)=0;
% We are creating a BPF mask using multiplication of the above two masks
hbpf=hl.*hh;
%Frequency domain multiplication is equivalent to spatial domain convolution
ghbpf=hbpf.*shift_ft_im;
%Inverse FFT to get back the result in spatial domain
out_bpf=abs(ifft2(ghbpf));
%plotting
figure;
subplot(221);imshow(im);title('Gray scale image')
subplot(222);imshow(hbpf);title('Ideal Band Pass Filter Mask 2D');
subplot(223);mesh(hbpf);title('Ideal BPF 3D')
subplot(224);imshow(uint8(out_bpf));title('Filtered Image')
% Band stop filter(BSF)
% We create a BSF mask as complement from BPF mask here for simplicity
hbsf=1-hbpf;%cut-off frequencies are same as band pass filter here
ghbsf=hbsf.*shift_ft_im;
out_bsf=abs(ifft2(ghbsf));
figure;
subplot(221);imshow(im);title('Gray scale image')
subplot(222);imshow(hbsf);title('Ideal Band Stop Filter Mask 2D');
subplot(223);mesh(hbsf);title('Ideal BSF 3D')
subplot(224);imshow(uint8(out_bsf));title('Filtered Image')
Output Images:
Figure 1: Figure 2:
38
Practical Aspects of Computer Vision (3171113) 200160111007
Figure 3: Figure 4:
Figure 5: Figure 6:
39
Practical Aspects of Computer Vision (3171113) 200160111007
Output Image :
40
Practical Aspects of Computer Vision (3171113) 200160111007
1. Which filter is better to remove salt & pepper noise from the image?
Median Filter Method
https://blogs.mathworks.com/matlab/
References used by the students: (List the references in the space provided below)
https://in.mathworks.com/help/images/getting-started-with-image-processing-
toolbox.html
https://blogs.mathworks.com/matlab/
Points
41