By
Ali Ghanbarzadeh
ghanbarzade.ali@gmail.com
1
 Introduction to MATLAB
 Image Processing
 Image Processing MATLAB Toolbox
 Image Processing Examples
 Computer Vision
 Computer Vision MATLAB Toolbox
 Computer Vision Examples
 Applications
 Alternatives to MATLAB
Overview
2
MATLAB (matrix laboratory) is a multi-paradigm numerical computing
environment and fourth-generation programming language developed by
MathWorks.
MATLAB allows matrixmanipulations, plotting of functions and data,
implementation of algorithms, creation of user interfaces, and interfacing with
programs written in other languages, including C, C++, Java, Fortran and Python.
Introduction to MATLAB
3
MATLAB User Interface
• Command Window
• Current Folder
• Workspace
Introduction to MATLAB
4
>> clc
Clears command window
>> help
Displays help text in Command Window
>> clear
Clears variables and functions from memory.
removes all variables from the workspace.
>> clear ‘a variable/variables’
Introduction to MATLAB
5
>> ver
Displays MathWorks product family header information, followed by the current MATLAB,
Simulink and toolbox version information
to write comments use %
>> %This is a comment
Introduction to MATLAB
6
Basic Arithmatics ( + , - , * , / )
Variables
every variable is an array or matrix
>> x=10
Creates a 1-by-1 matrix named x and stores the value 10 in its element.
Once a variable is entered into the system, you can refer to it later.
Variables must have values before they are used.
Introduction to MATLAB
7
When an expression returns a result that is not assigned to any variable, the system
assigns it to a variable named ans, which can be used later.
>> 212/23
ans =
9.2174
You can use this variable ans
>> ans+5
ans =
14.2174
Introduction to MATLAB
8
 who command displays all the variable names you have used
>> who
Your variables are:
a ans b x
whos command gives more information
>> whos
Name Size Bytes Class
a 1x1 8 double
ans 1x1 8 double
b 1x1 8 double
x 1x1 8 double
Introduction to MATLAB
9
Constants
>> pi
ans =
3.1416
>> eps
ans =
2.2204e-16
Introduction to MATLAB
 Symbolic Math Toolbox
>> x=1/3
x =
0.3333
>> x=sym(1/3)
x =
1/3
>> double(x)
ans =
0.3333 10
Trigonometric Functions
>> sin(pi/6)
ans =
0.5000
>> cos(0) + tan(pi/4)
ans =
2
Introduction to MATLAB
11
>> exp(1)
ans =
2.7183
>> log(exp(1))
ans =
1
>> log10(1000)
ans =
3
Introduction to MATLAB
>> 4^3
ans =
64
>> sqrt(81)
ans =
9
>> nthroot(625,4)
ans =
5
>> log2(1024)
ans =
10
>> factorial (5)
ans =
120
12
>> m(3)
ans =
3
>> length(m)
ans =
5
>> sum(m)
ans =
15
Introduction to MATLAB
>> m=[1 2 3 4 5]
m =
1 2 3 4 5
>> m'
ans =
1
2
3
4
5
>> min(m)
ans =
1
>> max(m)
ans =
5
>> mean(m)
ans =
3
 Vectors
13
>> a.*b
ans =
3 16 35 60 91
>> p=a.^2
p =
1 16 49 100 169
>> sqrt(p)
ans =
1 4 7 10 13
Introduction to MATLAB
 Vectors
>> a=1:3:15
a =
1 4 7 10 13
>> a(2:5)
ans =
4 7 10 13
>> b=(3:7)
b =
3 4 5 6 7
14
>> n(2,3)
ans =
4
>> n(3,:)
ans =
7 8 6
>> max(n)
ans =
7 9 6
Introduction to MATLAB
>> min(n)
ans =
2 5 1
>> sum(n)
ans =
12 22 11
>> sum(sum(n))
ans =
45
 Matrices
>> n=[3 5 1;2 9 4;7 8 6]
n =
3 5 1
2 9 4
7 8 6
>> n(1:3,2:3)
ans =
5 1
9 4
8 6
15
>> diag(n)
ans =
3
9
6
>> det(n)
ans =
99.0000
Introduction to MATLAB
>> triu(n)
ans =
3 5 1
0 9 4
0 0 6
>> tril(n)
ans =
3 0 0
2 9 0
7 8 6
 Matrices
>> numel(n)
ans =
9
>> size(n)
ans =
3 3
>> n'
ans =
3 2 7
5 9 8
1 4 6
16
>> (30-20)*(rand(5,1))+20
ans =
21.5761
29.7059
29.5717
24.8538
28.0028
>> ones(2)
ans =
1 1
1 1
Introduction to MATLAB
>> zeros(3)
ans =
0 0 0
0 0 0
0 0 0
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
 Matrices
>> rand(2)
ans =
0.8147 0.1270
0.9058 0.9134
>> rand(3,2)
ans =
0.6324 0.5469
0.0975 0.9575
0.2785 0.9649
17
>> std(data)
ans =
26.0109
>> var(data)
ans =
676.5667
Introduction to MATLAB
 Statistics
>> data=[12,54,23,69,31,76]
data =
12 54 23 69 31 76
>> sort(data)
ans =
12 23 31 54 69 76
>> median(ans)
ans =
42.5000
18
Functions
>> area_rectangle=inline('2*(a+b)','a','b')
area_rectangle =
Inline function:
area_rectangle(a,b) = 2*(a+b)
>> area_rectangle(5,7)
ans =
24
Introduction to MATLAB
function max = mymax(n1, n2, n3)
max = n1;
if(n2 > max)
max = n2;
end
if(n3 > max)
max = n3;
end
disp(max);
19
Calculus – differentiation
>> syms x
>> f=inline('sin(x)+2*cos(x)','x')
f =
Inline function:
f(x) = sin(x)+2*cos(x)
>> diff(f(x),x)
ans =
cos(x) – 2*sin(x)
Introduction to MATLAB
>> syms x y
>> f=inline('sin(x)+cos(y)','x','y')
f =
Inline function:
f(x,y) = sin(x)+cos(y)
>> diff(f(x,y),x)
ans =
cos(x)
20
Calculus - integral
>> syms x
>> f=inline('x/sin(x^2)','x')
f =
Inline function:
f(x) = x/sin(x^2)
>> integral=int(f(x),x)
integral =
- log(- x*2*i - x*exp(x^2*i)*2*i)/2 + log(x*2*i - x*exp(x^2*i)*2*i)/2
Introduction to MATLAB
>> pretty(integral)
2 2
log(- x 2 i - x exp(x i) 2 i) log(x 2 i - x exp(x i) 2 i)
- ------------------------------ + ----------------------------
2 2
21
Calculus – integral
>> syms x
>> f=inline('(x^3)+2','x')
f =
Inline function:
f(x) = (x^3)+2
>> integral=int(f(x),x,2,4)
integral =
64
Introduction to MATLAB
22
Calculus – limit
>> syms x
>> f=inline('sin(x)/x','x')
f =
Inline function:
f(x) = sin(x)/x
>> limit(f(x),x,0)
ans =
1
Introduction to MATLAB
>> syms x
>> g=inline('(x+x^2)/((2*x^2)+3*x) ','x')
g =
Inline function:
g(x) = (x+x^2)/((2*x^2)+3*x)
>> limit(g(x),x,Inf)
ans =
1/2
23
Plotting
>> x=[4 6 8 11 12];
>> y=[1 3 3 5 7];
>> plot(x,y)
>> bar(y)
>> pie(x)
Introduction to MATLAB
24
input
>> a=input('enter value for array: ')
enter value for array: [3 4 9 0 2]
a =
3 4 9 0 2
Introduction to MATLAB
>> name=input('enter your name: ','s')
enter your name: Ali
name =
Ali
>> whos
Name Size Bytes Class
name 1x19 38 char
25
Decisions
>> a = 100;
if a < 20
disp('less than 20');
else
disp('not less than 20');
end
disp(a);
not less than 20
100
Introduction to MATLAB
>> grade = 'B';
switch(grade)
case 'A'
disp('A');
case 'B'
disp('B');
case 'C'
disp('C');
otherwise
disp('Invalid grade');
end
26
Loops
>> for a = 1.0: -0.1: 0.0
disp(a)
end
1
0.9000
0.8000
….
0.1000
0
Introduction to MATLAB
>> a = 10;
while( a < 20 )
disp(a)
a = a + 2;
end
10
12
14
16
18
27
Image Processing is processing of images using mathematical operations by using
any form of signal processing for which the input is an image, a series of images, or a
video, and the output of image processing may be either an image or a set of
characteristics or parameters related to the image.
Image processing usually refers to digital image processing, but optical and analog
image processing also are possible.
Closely related to image processing are computer graphics and computer vision. In
computer graphics, images are manually made from physical models of objects
instead of being acquired via imaging devices. Computer vision is considered
high-level image processing out of which a machine/computer/software intends to
decipher the physical contents of an image or a sequence of images.
Image Processing
28
Features
Image Processing Toolbox supports a diverse set of image types
image analysis
image segmentation
image enhancement
 noise reduction
geometric transformations
Visualization functions and apps let you
explore images and videos,
examine a region of pixels, adjust color and contrast,
create contours or histograms
Image Processing MATLAB Toolbox
29
The basic data structure in MATLAB is the array.
MATLAB stores most images as two-dimensional arrays (i.e., matrices), in which
each element of the matrix corresponds to a single pixel in the displayed image.
Images in MATLAB
30
Some images, such as truecolor images,
require a three-dimensional array, where
the first plane in the third dimension
represents the red pixel intensities,
the second plane represents the green
pixel intensities, and the third plane
represents the blue pixel intensities.
Images in MATLAB
31
I=imread('balls.jpg');
imtool(I);
Exploring Images (Measurement)
32
Exploring Images (Inspect Pixels Value)
33
An image histogram is a type of histogram that acts as a graphical
representation of the tonal distribution in a digital image.
The horizontal axis of the graph represents the tonal variations, while the
vertical axis represents the number of pixels in that particular tone.
Histogram of Image
34
Clc;clear;
I = imread('pout.tif'); %reading an image
figure;
subplot(2,2,1);
imshow(I); %shows the image
subplot(2,2,2);
imhist(I); %Views the distribution of image pixel intensities
I2 = histeq(I); %improves the contrast in an image,using the histeq
%function.
subplot(2,2,3);
imshow(I2);
subplot(2,2,4);
imhist(I2);
Enhancement (1)
35
imwrite (I2,'pout2.png','png'); %Writes the newly adjusted image to a
%file.
imfinfo('pout2.png'); %Gives the information about the image
Enhancement (1)
36
clc; clear;
I = imread('pout.tif'); %reading an image
figure;
subplot(2,2,1)
imshow(I) %shows the image
subplot(2,2,2)
imhist(I) %Views the distribution of image pixel intensities
I3=imadjust(I); %Stretches the histogram
subplot(2,2,3)
imshow(I3)
subplot(2,2,4)
imhist(I3)
Enhancement (2)
37
Enhancement (2)
38
histeq
By default, MATLAB transforms the probability distribution to a uniform
distribution, which means all the intensity ranges have equivalent probabilities.
The histogram is modified to have the counts of all pixels close to each other
(uniform distribution)
imadjust
imadjust just "Stretches" the histogram of an image in order to have the
intensity range of the image fill the entire available range (normally 0-255).
histeq vs. imadjust
39
clc; clear;
I=imread('cameraman.tif');
subplot(1,3,1)
imshow(I)
title('original')
V= I(end:-1:1,:); % Vertical Flip
subplot(1,3,2)
imshow(V)
title('vertical')
Flip
40
H= I(:,end:-1:1); % Horizontal Flip
subplot(1,3,3)
imshow(H)
title('Horizontal')
Flip
41
scale = 0.5;
R = imresize(I,scale); % Resizing
figure; imshow(R)
title('Resized')
theta = 35;
Rotation = imrotate(I,theta); % Rotating
figure; imshow(Rotation)
title('Rotated')
Imwrite(Rotation,'r.png','png') % writes image data to the file
% specified by filename
Rotate, Resize
42
Rotate, Resize
43
Thresholding is the simplest method of image segmentation.
From a grayscale image, thresholding can be used to create binary
images.
The simplest thresholding methods replace each pixel in an image
with a black pixel if the image intensity 𝐼𝑖,𝑗 is less than some fixed
constant T(that is 𝐼𝑖,𝑗 < T), or a white pixel if the image intensity is
greater than that constant.
Thresholding
44
The approach for thresholding color images is to designate a separate
threshold for each of the RGB components of the image and then combine
them with an AND operation.
Thresholding
45
clc; clear;
I=imread('balls.jpg');
red=I(:,:,1); %extracts the red channel
green=I(:,:,2); %extracts the green channel
blue=I(:,:,3); %extracts the blue channel
figure;
subplot(1,3,1); imshow(red); title('red');
subplot(1,3,2); imshow(green); title('green');
subplot(1,3,3); imshow(blue); title('blue');
Thresholding
46
RGB Channels
Thresholding
47
figure;
pix=impixel(I); %the tool for getting pixel information
Thresholding
48
maxPixels=max(pix); %finds the maximum of columns
minPixels=min(pix); %finds the minimum of columns
out = red<=maxPixels(1) & red>=minPixels(1) &...
green<=maxPixels(2) & green>=minPixels(2) &...
blue<=maxPixels(3) & blue>= minPixels(3);
%trying to thershold the selected segment
figure;
imshow(out);
Thresholding
49
Thresholding
50
out2 = imfill(out,'holes'); % filling the holes
figure;
imshow(out2);
title('without holes');
stats = regionprops(out2); %information about the segment
Thresholding
51
Thresholding
52
In signal processing, a filter is a device or process that removes from a
signal some unwanted component or feature.
fspecial
Creates predefined 2-D filter
Filters
53
medfilt2
2-D median filtering
The median filter is a nonlinear digital filtering technique, often used to
remove noise.
Filters
54
clc; clear;
I=imread('cat.png');
figure;
imshow(I);title('Original Image');
motion_f=fspecial('motion',10);
I2=imfilter(I,motion_f);
figure;
imshow(I2);title('Blurred Image');
I3=imsharpen(I2,'amount',5);
figure;imshow(I3);title('Sharpened Image');
Sharpening
55
Sharpening
56
clc; clear;
I=imread('mo.jpg');
I=rgb2gray(I);
figure;imshow(I);
Filter Noise
57
N=imnoise(I,'salt & pepper',0.25);
figure,imshow(N);
average_f=fspecial('average');
Filtered=imfilter(N,average_f);
figure,imshow(Filtered);
Filter Noise
58
Filtered=medfilt2(N);
figure,imshow(Filtered);
Filter Noise
59
clc; clear;
a=imread('shelf.png');
b=rgb2gray(a);
figure; imshow(a);
h=[-1 -1 -1;2 2 2; -1 -1 -1]; %filter for horizontal lines
c1=imfilter(b,h);
figure; imshow(c1);
h=[-1 2 -1;-1 2 -1; -1 2 -1]; %filter for vertical lines
c2=imfilter(b,h);
figure; imshow(c2);
Line Detection
60
Line Detection
61
d=imadd(c1,c2); %adding two images
figure; imshow(d);
Line Detection
62
clc; clear;
a=imread('house.jpg');
b=rgb2gray(a); %converts the RGB image to grayscale
figure; imshow(b);
Edge Detection
63
e=edge(b,'sobel'); %edge detection with 'sobel'
figure; imshow(e);
Edge Detection
64
e=edge(b,'canny'); %edge detection with 'canny'
figure; imshow(e);
Edge Detection
65
h=fspecial('laplacian');
e=imfilter(b,h);
figure; imshow(e);
Edge Detection
66
Computer vision is a field that includes methods for acquiring, processing,
analyzing, and understanding images and, in general, high-dimensional data
from the real world in order to produce numerical or symbolic information,
e.g., in the forms of decisions.
As a scientific discipline, computer vision is concerned with the theory
behind artificial systems that extract information from images. The image
data can take many forms, such as video sequences, views from multiple
cameras, or multi-dimensional data from a medical scanner.
Computer Vision
67
Computer Vision System Toolbox provides algorithms, functions, and apps for the
design and simulation of computer vision and video processing systems. You can
perform object detection and tracking, feature detection and extraction, feature
matching, camera calibration, and motion detection tasks.
The system toolbox also provides tools for video processing, including video file I/O,
video display, object annotation, drawing graphics.
Computer Vision MATLAB Toolbox
68
clc; clear;
A = imread('circlesBrightDark.png');
figure;imshow(A);
Rmin = 30;
Rmax = 65;
[centersBright, radiiBright] = imfindcircles(A,[Rmin …
Rmax],'ObjectPolarity','bright');
[centersDark, radiiDark] = imfindcircles(A,[Rmin …
Rmax],'ObjectPolarity','dark');
figure;imshow(A);
viscircles(centersBright, radiiBright,'EdgeColor','b');
viscircles(centersDark, radiiDark,'LineStyle','--');
Find Bright & Dark Circles
69
Find Bright & Dark Circles
70
Optical character recognition (optical character reader) (OCR) is the mechanical
or electronic conversion of images of typed, handwritten or printed text into
machine-encoded text.
data entry from printed paper data records, whether passport documents,
invoices, bank statements, computerised receipts, business cards, mail, printouts
of static-data, or any suitable documentation.
used in machine processes such as machine translation, text-to-speech and text
mining.
OCR is a field of research in pattern recognition, artificial intelligence and
computer vision.
OCR (Optical Character Recognition)
71
clc; clear;
I = imread('ocr_test.png');
ocrResults = ocr(I); %OCR for getting the text out of an image
recognizedText = ocrResults.Text; %the Text field
figure;
imshow(I);
disp(recognizedText) %displays the whole text in Command Window
OCR
72
OCR
73
boxed = insertObjectAnnotation(I,'rectangle',…
ocrResults.WordBoundingBoxes,…
ocrResults.WordConfidences);
%showing the boxes around the words with the
%relevant word confidence
figure;
imshow(boxed)
OCR
74
OCR
Ran Zhang
Xuemin (Sherman) Shen
Mobile Electric
Vehicles
Online Charging and
Discharging
75
The cascade object detector uses the Viola-Jones algorithm to detect people's faces,
noses, eyes, mouth, or upper body.
detector = vision.CascadeObjectDetector creates a System object, detector, that
detects objects using the Viola-Jones algorithm. The ClassificationModel property
controls the type of object to detect. By default, the detector is configured to detect
faces.
BBOX = step(detector,I) returns BBOX, an M-by-4 matrix defining M bounding boxes
containing the detected objects. This method performs multiscale object detection on
the input image, I. Each row of the output matrix, BBOX, contains a four-element
vector, [x y width height], that specifies in pixels, the upper-left corner and size of a
bounding box. The input image I, must be a grayscale or truecolor (RGB) image.
Cascade Object Detection
76
clc; clear;
for c=1:7
I=imread(num2str(c),'jpg');
detector=vision.CascadeObjectDetector;
box = step(detector, I);
out = insertObjectAnnotation(I,'rectangle',box,'face');
figure; imshow(out);
end
Face Detection
77
Face Detection
78
clc; clear;
I1=imread('b2.png');
I2=imread('sm.jpg');
detector=vision.CascadeObjectDetector('EyePairBig');
detector.MergeThreshold=10;
box1 = step(detector, I1);
box2 = step(detector, I2);
out1 = insertObjectAnnotation(I1,'rectangle',box1,'eyes');
figure; imshow(out1);
out2 = insertObjectAnnotation(I2,'rectangle',box2,'eyes');
figure; imshow(out2);
Face Feature Detection(Eyes)
79
Face Feature Detection(Eyes)
80
clc; clear;
I1=imread('b2.png');
I2=imread('sm.jpg');
detector=vision.CascadeObjectDetector('Mouth');
detector.MergeThreshold=200;
box1 = step(detector, I1);
box2 = step(detector, I2);
out1 = insertObjectAnnotation(I1,'rectangle',box1,'mouth');
figure; imshow(out1);
out2 = insertObjectAnnotation(I2,'rectangle',box2,'mouth');
figure; imshow(out2);
Face Feature Detection(Mouth)
81
Face Feature Detection(Mouth)
82
clc; clear;
I1=imread('b2.png');
I2=imread('sm.jpg');
detector=vision.CascadeObjectDetector('Nose');
detector.MergeThreshold=50;
box1 = step(detector, I1);
box2 = step(detector, I2);
out1 = insertObjectAnnotation(I1,'rectangle',box1,'nose');
figure; imshow(out1);
out2 = insertObjectAnnotation(I2,'rectangle',box2,'nose');
figure; imshow(out2);
Face Feature Detection(Nose)
83
Face Feature Detection(Nose)
84
clc; clear;
info = imaqhwinfo('winvideo');
info.DeviceInfo(1)
info.DeviceInfo.SupportedFormats
vid = videoinput('winvideo',1,'YUY2_640x480');
preview(vid)
Stop(vid)
Video Acquisition
85
clc; clear;
faceDetector = vision.CascadeObjectDetector();
obj =imaq.VideoDevice('winvideo', 1, 'YUY2_640x480','ROI', …
[1 1 640 480]);
set(obj,'ReturnedColorSpace', 'rgb');
figure('menubar','none','tag','webcam');
Face Detection from live video stream
86
while (true)
frame=step(obj);
bbox=step(faceDetector,frame);
boxInserter =
vision.ShapeInserter('BorderColor','Custom',...
'CustomBorderColor',[255 255 0]);
videoOut = step(boxInserter, frame,bbox);
imshow(videoOut,'border','tight');
f=findobj('tag','webcam');
Face Detection from live video stream
87
if (isempty(f));
[hueChannel,~,~] = rgb2hsv(frame);
hold off
noseDetector = vision.CascadeObjectDetector('Nose');
faceImage = imcrop(frame,bbox);
noseBBox = step(noseDetector,faceImage);
videoInfo = info(obj);
ROI=get(obj,'ROI');
VideoSize = [ROI(3) ROI(4)];
tracker = vision.HistogramBasedTracker;
initializeObject(tracker, hueChannel, bbox);
release(obj);
close(gcf)
break
end % End of if
pause(0.05)
end % End of While
Face Detection from live video stream
88
Pattern Recognition is a branch of machine learning
that focuses on the recognition of patterns and
regularities in data, although it is in some cases
considered to be nearly synonymous
with machine learning.
Pattern Recognition algorithms generally aim
to provide a reasonable answer for all possible
inputs and to perform "most likely" matching of
the inputs, taking into account their statistical variation.
Pattern Recognition
89
Biometric Image Processing and Recognition
Applications
90
Biometric Image Processing and Recognition
Applications
91
Medical Imaging
Applications
92
Surveillance
Applications
93
Industrial Control
Applications
94
Robotics
Applications
95
Computer Aided Surgery
Applications
96
Geospatial Computing
Applications
97
Military
Applications
98
Mathematica
GNU Octave
R (programming language)
Scilab
SciPy & NumPy
Alternatives to MATLAB
99
Thanks!
Any Question?
100

Fundamentals of Image Processing & Computer Vision with MATLAB

  • 1.
  • 2.
     Introduction toMATLAB  Image Processing  Image Processing MATLAB Toolbox  Image Processing Examples  Computer Vision  Computer Vision MATLAB Toolbox  Computer Vision Examples  Applications  Alternatives to MATLAB Overview 2
  • 3.
    MATLAB (matrix laboratory)is a multi-paradigm numerical computing environment and fourth-generation programming language developed by MathWorks. MATLAB allows matrixmanipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, Fortran and Python. Introduction to MATLAB 3
  • 4.
    MATLAB User Interface •Command Window • Current Folder • Workspace Introduction to MATLAB 4
  • 5.
    >> clc Clears commandwindow >> help Displays help text in Command Window >> clear Clears variables and functions from memory. removes all variables from the workspace. >> clear ‘a variable/variables’ Introduction to MATLAB 5
  • 6.
    >> ver Displays MathWorksproduct family header information, followed by the current MATLAB, Simulink and toolbox version information to write comments use % >> %This is a comment Introduction to MATLAB 6
  • 7.
    Basic Arithmatics (+ , - , * , / ) Variables every variable is an array or matrix >> x=10 Creates a 1-by-1 matrix named x and stores the value 10 in its element. Once a variable is entered into the system, you can refer to it later. Variables must have values before they are used. Introduction to MATLAB 7
  • 8.
    When an expressionreturns a result that is not assigned to any variable, the system assigns it to a variable named ans, which can be used later. >> 212/23 ans = 9.2174 You can use this variable ans >> ans+5 ans = 14.2174 Introduction to MATLAB 8
  • 9.
     who commanddisplays all the variable names you have used >> who Your variables are: a ans b x whos command gives more information >> whos Name Size Bytes Class a 1x1 8 double ans 1x1 8 double b 1x1 8 double x 1x1 8 double Introduction to MATLAB 9
  • 10.
    Constants >> pi ans = 3.1416 >>eps ans = 2.2204e-16 Introduction to MATLAB  Symbolic Math Toolbox >> x=1/3 x = 0.3333 >> x=sym(1/3) x = 1/3 >> double(x) ans = 0.3333 10
  • 11.
    Trigonometric Functions >> sin(pi/6) ans= 0.5000 >> cos(0) + tan(pi/4) ans = 2 Introduction to MATLAB 11
  • 12.
    >> exp(1) ans = 2.7183 >>log(exp(1)) ans = 1 >> log10(1000) ans = 3 Introduction to MATLAB >> 4^3 ans = 64 >> sqrt(81) ans = 9 >> nthroot(625,4) ans = 5 >> log2(1024) ans = 10 >> factorial (5) ans = 120 12
  • 13.
    >> m(3) ans = 3 >>length(m) ans = 5 >> sum(m) ans = 15 Introduction to MATLAB >> m=[1 2 3 4 5] m = 1 2 3 4 5 >> m' ans = 1 2 3 4 5 >> min(m) ans = 1 >> max(m) ans = 5 >> mean(m) ans = 3  Vectors 13
  • 14.
    >> a.*b ans = 316 35 60 91 >> p=a.^2 p = 1 16 49 100 169 >> sqrt(p) ans = 1 4 7 10 13 Introduction to MATLAB  Vectors >> a=1:3:15 a = 1 4 7 10 13 >> a(2:5) ans = 4 7 10 13 >> b=(3:7) b = 3 4 5 6 7 14
  • 15.
    >> n(2,3) ans = 4 >>n(3,:) ans = 7 8 6 >> max(n) ans = 7 9 6 Introduction to MATLAB >> min(n) ans = 2 5 1 >> sum(n) ans = 12 22 11 >> sum(sum(n)) ans = 45  Matrices >> n=[3 5 1;2 9 4;7 8 6] n = 3 5 1 2 9 4 7 8 6 >> n(1:3,2:3) ans = 5 1 9 4 8 6 15
  • 16.
    >> diag(n) ans = 3 9 6 >>det(n) ans = 99.0000 Introduction to MATLAB >> triu(n) ans = 3 5 1 0 9 4 0 0 6 >> tril(n) ans = 3 0 0 2 9 0 7 8 6  Matrices >> numel(n) ans = 9 >> size(n) ans = 3 3 >> n' ans = 3 2 7 5 9 8 1 4 6 16
  • 17.
    >> (30-20)*(rand(5,1))+20 ans = 21.5761 29.7059 29.5717 24.8538 28.0028 >>ones(2) ans = 1 1 1 1 Introduction to MATLAB >> zeros(3) ans = 0 0 0 0 0 0 0 0 0 >> eye(3) ans = 1 0 0 0 1 0 0 0 1  Matrices >> rand(2) ans = 0.8147 0.1270 0.9058 0.9134 >> rand(3,2) ans = 0.6324 0.5469 0.0975 0.9575 0.2785 0.9649 17
  • 18.
    >> std(data) ans = 26.0109 >>var(data) ans = 676.5667 Introduction to MATLAB  Statistics >> data=[12,54,23,69,31,76] data = 12 54 23 69 31 76 >> sort(data) ans = 12 23 31 54 69 76 >> median(ans) ans = 42.5000 18
  • 19.
    Functions >> area_rectangle=inline('2*(a+b)','a','b') area_rectangle = Inlinefunction: area_rectangle(a,b) = 2*(a+b) >> area_rectangle(5,7) ans = 24 Introduction to MATLAB function max = mymax(n1, n2, n3) max = n1; if(n2 > max) max = n2; end if(n3 > max) max = n3; end disp(max); 19
  • 20.
    Calculus – differentiation >>syms x >> f=inline('sin(x)+2*cos(x)','x') f = Inline function: f(x) = sin(x)+2*cos(x) >> diff(f(x),x) ans = cos(x) – 2*sin(x) Introduction to MATLAB >> syms x y >> f=inline('sin(x)+cos(y)','x','y') f = Inline function: f(x,y) = sin(x)+cos(y) >> diff(f(x,y),x) ans = cos(x) 20
  • 21.
    Calculus - integral >>syms x >> f=inline('x/sin(x^2)','x') f = Inline function: f(x) = x/sin(x^2) >> integral=int(f(x),x) integral = - log(- x*2*i - x*exp(x^2*i)*2*i)/2 + log(x*2*i - x*exp(x^2*i)*2*i)/2 Introduction to MATLAB >> pretty(integral) 2 2 log(- x 2 i - x exp(x i) 2 i) log(x 2 i - x exp(x i) 2 i) - ------------------------------ + ---------------------------- 2 2 21
  • 22.
    Calculus – integral >>syms x >> f=inline('(x^3)+2','x') f = Inline function: f(x) = (x^3)+2 >> integral=int(f(x),x,2,4) integral = 64 Introduction to MATLAB 22
  • 23.
    Calculus – limit >>syms x >> f=inline('sin(x)/x','x') f = Inline function: f(x) = sin(x)/x >> limit(f(x),x,0) ans = 1 Introduction to MATLAB >> syms x >> g=inline('(x+x^2)/((2*x^2)+3*x) ','x') g = Inline function: g(x) = (x+x^2)/((2*x^2)+3*x) >> limit(g(x),x,Inf) ans = 1/2 23
  • 24.
    Plotting >> x=[4 68 11 12]; >> y=[1 3 3 5 7]; >> plot(x,y) >> bar(y) >> pie(x) Introduction to MATLAB 24
  • 25.
    input >> a=input('enter valuefor array: ') enter value for array: [3 4 9 0 2] a = 3 4 9 0 2 Introduction to MATLAB >> name=input('enter your name: ','s') enter your name: Ali name = Ali >> whos Name Size Bytes Class name 1x19 38 char 25
  • 26.
    Decisions >> a =100; if a < 20 disp('less than 20'); else disp('not less than 20'); end disp(a); not less than 20 100 Introduction to MATLAB >> grade = 'B'; switch(grade) case 'A' disp('A'); case 'B' disp('B'); case 'C' disp('C'); otherwise disp('Invalid grade'); end 26
  • 27.
    Loops >> for a= 1.0: -0.1: 0.0 disp(a) end 1 0.9000 0.8000 …. 0.1000 0 Introduction to MATLAB >> a = 10; while( a < 20 ) disp(a) a = a + 2; end 10 12 14 16 18 27
  • 28.
    Image Processing isprocessing of images using mathematical operations by using any form of signal processing for which the input is an image, a series of images, or a video, and the output of image processing may be either an image or a set of characteristics or parameters related to the image. Image processing usually refers to digital image processing, but optical and analog image processing also are possible. Closely related to image processing are computer graphics and computer vision. In computer graphics, images are manually made from physical models of objects instead of being acquired via imaging devices. Computer vision is considered high-level image processing out of which a machine/computer/software intends to decipher the physical contents of an image or a sequence of images. Image Processing 28
  • 29.
    Features Image Processing Toolboxsupports a diverse set of image types image analysis image segmentation image enhancement  noise reduction geometric transformations Visualization functions and apps let you explore images and videos, examine a region of pixels, adjust color and contrast, create contours or histograms Image Processing MATLAB Toolbox 29
  • 30.
    The basic datastructure in MATLAB is the array. MATLAB stores most images as two-dimensional arrays (i.e., matrices), in which each element of the matrix corresponds to a single pixel in the displayed image. Images in MATLAB 30
  • 31.
    Some images, suchas truecolor images, require a three-dimensional array, where the first plane in the third dimension represents the red pixel intensities, the second plane represents the green pixel intensities, and the third plane represents the blue pixel intensities. Images in MATLAB 31
  • 32.
  • 33.
    Exploring Images (InspectPixels Value) 33
  • 34.
    An image histogramis a type of histogram that acts as a graphical representation of the tonal distribution in a digital image. The horizontal axis of the graph represents the tonal variations, while the vertical axis represents the number of pixels in that particular tone. Histogram of Image 34
  • 35.
    Clc;clear; I = imread('pout.tif');%reading an image figure; subplot(2,2,1); imshow(I); %shows the image subplot(2,2,2); imhist(I); %Views the distribution of image pixel intensities I2 = histeq(I); %improves the contrast in an image,using the histeq %function. subplot(2,2,3); imshow(I2); subplot(2,2,4); imhist(I2); Enhancement (1) 35
  • 36.
    imwrite (I2,'pout2.png','png'); %Writesthe newly adjusted image to a %file. imfinfo('pout2.png'); %Gives the information about the image Enhancement (1) 36
  • 37.
    clc; clear; I =imread('pout.tif'); %reading an image figure; subplot(2,2,1) imshow(I) %shows the image subplot(2,2,2) imhist(I) %Views the distribution of image pixel intensities I3=imadjust(I); %Stretches the histogram subplot(2,2,3) imshow(I3) subplot(2,2,4) imhist(I3) Enhancement (2) 37
  • 38.
  • 39.
    histeq By default, MATLABtransforms the probability distribution to a uniform distribution, which means all the intensity ranges have equivalent probabilities. The histogram is modified to have the counts of all pixels close to each other (uniform distribution) imadjust imadjust just "Stretches" the histogram of an image in order to have the intensity range of the image fill the entire available range (normally 0-255). histeq vs. imadjust 39
  • 40.
    clc; clear; I=imread('cameraman.tif'); subplot(1,3,1) imshow(I) title('original') V= I(end:-1:1,:);% Vertical Flip subplot(1,3,2) imshow(V) title('vertical') Flip 40
  • 41.
    H= I(:,end:-1:1); %Horizontal Flip subplot(1,3,3) imshow(H) title('Horizontal') Flip 41
  • 42.
    scale = 0.5; R= imresize(I,scale); % Resizing figure; imshow(R) title('Resized') theta = 35; Rotation = imrotate(I,theta); % Rotating figure; imshow(Rotation) title('Rotated') Imwrite(Rotation,'r.png','png') % writes image data to the file % specified by filename Rotate, Resize 42
  • 43.
  • 44.
    Thresholding is thesimplest method of image segmentation. From a grayscale image, thresholding can be used to create binary images. The simplest thresholding methods replace each pixel in an image with a black pixel if the image intensity 𝐼𝑖,𝑗 is less than some fixed constant T(that is 𝐼𝑖,𝑗 < T), or a white pixel if the image intensity is greater than that constant. Thresholding 44
  • 45.
    The approach forthresholding color images is to designate a separate threshold for each of the RGB components of the image and then combine them with an AND operation. Thresholding 45
  • 46.
    clc; clear; I=imread('balls.jpg'); red=I(:,:,1); %extractsthe red channel green=I(:,:,2); %extracts the green channel blue=I(:,:,3); %extracts the blue channel figure; subplot(1,3,1); imshow(red); title('red'); subplot(1,3,2); imshow(green); title('green'); subplot(1,3,3); imshow(blue); title('blue'); Thresholding 46
  • 47.
  • 48.
    figure; pix=impixel(I); %the toolfor getting pixel information Thresholding 48
  • 49.
    maxPixels=max(pix); %finds themaximum of columns minPixels=min(pix); %finds the minimum of columns out = red<=maxPixels(1) & red>=minPixels(1) &... green<=maxPixels(2) & green>=minPixels(2) &... blue<=maxPixels(3) & blue>= minPixels(3); %trying to thershold the selected segment figure; imshow(out); Thresholding 49
  • 50.
  • 51.
    out2 = imfill(out,'holes');% filling the holes figure; imshow(out2); title('without holes'); stats = regionprops(out2); %information about the segment Thresholding 51
  • 52.
  • 53.
    In signal processing,a filter is a device or process that removes from a signal some unwanted component or feature. fspecial Creates predefined 2-D filter Filters 53
  • 54.
    medfilt2 2-D median filtering Themedian filter is a nonlinear digital filtering technique, often used to remove noise. Filters 54
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
    clc; clear; a=imread('shelf.png'); b=rgb2gray(a); figure; imshow(a); h=[-1-1 -1;2 2 2; -1 -1 -1]; %filter for horizontal lines c1=imfilter(b,h); figure; imshow(c1); h=[-1 2 -1;-1 2 -1; -1 2 -1]; %filter for vertical lines c2=imfilter(b,h); figure; imshow(c2); Line Detection 60
  • 61.
  • 62.
    d=imadd(c1,c2); %adding twoimages figure; imshow(d); Line Detection 62
  • 63.
    clc; clear; a=imread('house.jpg'); b=rgb2gray(a); %convertsthe RGB image to grayscale figure; imshow(b); Edge Detection 63
  • 64.
    e=edge(b,'sobel'); %edge detectionwith 'sobel' figure; imshow(e); Edge Detection 64
  • 65.
    e=edge(b,'canny'); %edge detectionwith 'canny' figure; imshow(e); Edge Detection 65
  • 66.
  • 67.
    Computer vision isa field that includes methods for acquiring, processing, analyzing, and understanding images and, in general, high-dimensional data from the real world in order to produce numerical or symbolic information, e.g., in the forms of decisions. As a scientific discipline, computer vision is concerned with the theory behind artificial systems that extract information from images. The image data can take many forms, such as video sequences, views from multiple cameras, or multi-dimensional data from a medical scanner. Computer Vision 67
  • 68.
    Computer Vision SystemToolbox provides algorithms, functions, and apps for the design and simulation of computer vision and video processing systems. You can perform object detection and tracking, feature detection and extraction, feature matching, camera calibration, and motion detection tasks. The system toolbox also provides tools for video processing, including video file I/O, video display, object annotation, drawing graphics. Computer Vision MATLAB Toolbox 68
  • 69.
    clc; clear; A =imread('circlesBrightDark.png'); figure;imshow(A); Rmin = 30; Rmax = 65; [centersBright, radiiBright] = imfindcircles(A,[Rmin … Rmax],'ObjectPolarity','bright'); [centersDark, radiiDark] = imfindcircles(A,[Rmin … Rmax],'ObjectPolarity','dark'); figure;imshow(A); viscircles(centersBright, radiiBright,'EdgeColor','b'); viscircles(centersDark, radiiDark,'LineStyle','--'); Find Bright & Dark Circles 69
  • 70.
    Find Bright &Dark Circles 70
  • 71.
    Optical character recognition(optical character reader) (OCR) is the mechanical or electronic conversion of images of typed, handwritten or printed text into machine-encoded text. data entry from printed paper data records, whether passport documents, invoices, bank statements, computerised receipts, business cards, mail, printouts of static-data, or any suitable documentation. used in machine processes such as machine translation, text-to-speech and text mining. OCR is a field of research in pattern recognition, artificial intelligence and computer vision. OCR (Optical Character Recognition) 71
  • 72.
    clc; clear; I =imread('ocr_test.png'); ocrResults = ocr(I); %OCR for getting the text out of an image recognizedText = ocrResults.Text; %the Text field figure; imshow(I); disp(recognizedText) %displays the whole text in Command Window OCR 72
  • 73.
  • 74.
    boxed = insertObjectAnnotation(I,'rectangle',… ocrResults.WordBoundingBoxes,… ocrResults.WordConfidences); %showingthe boxes around the words with the %relevant word confidence figure; imshow(boxed) OCR 74
  • 75.
    OCR Ran Zhang Xuemin (Sherman)Shen Mobile Electric Vehicles Online Charging and Discharging 75
  • 76.
    The cascade objectdetector uses the Viola-Jones algorithm to detect people's faces, noses, eyes, mouth, or upper body. detector = vision.CascadeObjectDetector creates a System object, detector, that detects objects using the Viola-Jones algorithm. The ClassificationModel property controls the type of object to detect. By default, the detector is configured to detect faces. BBOX = step(detector,I) returns BBOX, an M-by-4 matrix defining M bounding boxes containing the detected objects. This method performs multiscale object detection on the input image, I. Each row of the output matrix, BBOX, contains a four-element vector, [x y width height], that specifies in pixels, the upper-left corner and size of a bounding box. The input image I, must be a grayscale or truecolor (RGB) image. Cascade Object Detection 76
  • 77.
    clc; clear; for c=1:7 I=imread(num2str(c),'jpg'); detector=vision.CascadeObjectDetector; box= step(detector, I); out = insertObjectAnnotation(I,'rectangle',box,'face'); figure; imshow(out); end Face Detection 77
  • 78.
  • 79.
    clc; clear; I1=imread('b2.png'); I2=imread('sm.jpg'); detector=vision.CascadeObjectDetector('EyePairBig'); detector.MergeThreshold=10; box1 =step(detector, I1); box2 = step(detector, I2); out1 = insertObjectAnnotation(I1,'rectangle',box1,'eyes'); figure; imshow(out1); out2 = insertObjectAnnotation(I2,'rectangle',box2,'eyes'); figure; imshow(out2); Face Feature Detection(Eyes) 79
  • 80.
  • 81.
    clc; clear; I1=imread('b2.png'); I2=imread('sm.jpg'); detector=vision.CascadeObjectDetector('Mouth'); detector.MergeThreshold=200; box1 =step(detector, I1); box2 = step(detector, I2); out1 = insertObjectAnnotation(I1,'rectangle',box1,'mouth'); figure; imshow(out1); out2 = insertObjectAnnotation(I2,'rectangle',box2,'mouth'); figure; imshow(out2); Face Feature Detection(Mouth) 81
  • 82.
  • 83.
    clc; clear; I1=imread('b2.png'); I2=imread('sm.jpg'); detector=vision.CascadeObjectDetector('Nose'); detector.MergeThreshold=50; box1 =step(detector, I1); box2 = step(detector, I2); out1 = insertObjectAnnotation(I1,'rectangle',box1,'nose'); figure; imshow(out1); out2 = insertObjectAnnotation(I2,'rectangle',box2,'nose'); figure; imshow(out2); Face Feature Detection(Nose) 83
  • 84.
  • 85.
    clc; clear; info =imaqhwinfo('winvideo'); info.DeviceInfo(1) info.DeviceInfo.SupportedFormats vid = videoinput('winvideo',1,'YUY2_640x480'); preview(vid) Stop(vid) Video Acquisition 85
  • 86.
    clc; clear; faceDetector =vision.CascadeObjectDetector(); obj =imaq.VideoDevice('winvideo', 1, 'YUY2_640x480','ROI', … [1 1 640 480]); set(obj,'ReturnedColorSpace', 'rgb'); figure('menubar','none','tag','webcam'); Face Detection from live video stream 86
  • 87.
    while (true) frame=step(obj); bbox=step(faceDetector,frame); boxInserter = vision.ShapeInserter('BorderColor','Custom',... 'CustomBorderColor',[255255 0]); videoOut = step(boxInserter, frame,bbox); imshow(videoOut,'border','tight'); f=findobj('tag','webcam'); Face Detection from live video stream 87
  • 88.
    if (isempty(f)); [hueChannel,~,~] =rgb2hsv(frame); hold off noseDetector = vision.CascadeObjectDetector('Nose'); faceImage = imcrop(frame,bbox); noseBBox = step(noseDetector,faceImage); videoInfo = info(obj); ROI=get(obj,'ROI'); VideoSize = [ROI(3) ROI(4)]; tracker = vision.HistogramBasedTracker; initializeObject(tracker, hueChannel, bbox); release(obj); close(gcf) break end % End of if pause(0.05) end % End of While Face Detection from live video stream 88
  • 89.
    Pattern Recognition isa branch of machine learning that focuses on the recognition of patterns and regularities in data, although it is in some cases considered to be nearly synonymous with machine learning. Pattern Recognition algorithms generally aim to provide a reasonable answer for all possible inputs and to perform "most likely" matching of the inputs, taking into account their statistical variation. Pattern Recognition 89
  • 90.
    Biometric Image Processingand Recognition Applications 90
  • 91.
    Biometric Image Processingand Recognition Applications 91
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
    Mathematica GNU Octave R (programminglanguage) Scilab SciPy & NumPy Alternatives to MATLAB 99
  • 100.