Introduction To Matlab
Introduction To Matlab
Matrix Laboratory
4 MAIN WINDOWS
Command Window: main window in which
commands are written
Command History: list of commands recently
used in chronological order
Current Directory: it is the default directory
for saving files
Workspace: displays the list of variables defined
by you in the current session of MATLAB
LETS START
>>a = 5
a=
>>a = [1 2 3 4 5 6]
a=
1 2 3 4 5 6
MATHEMATICAL OPERATIONS
>>a= [1 2 3; 4 5 6];
>>b= [4 5 6; 7 8 9];
>>a+b
ans =
5 7 9
11 13 15
ans is default variable
You can define one yourself
>>c= a+b
GENERAL COMMANDS
clc: to clear the command window, giving you a
clear screen
clear: to remove all variable from workshop. This
frees up system memory.
Colon operator (:) j:k= [j, j+1,......,k]
j:i:k= [j, j+i, j+2i,......,k]
A(:,j)= jth column of A
A(:)= all elements of A
E.G.
>>a= (:, 2, 3)
a=
2 3
5 6
8 9
RELATIONAL OPERATORS
== equal to
~= not equal to
< less than
<= less than or equal to
> greater than
>= more than or equal to
IF ELSE
if condition
Statement;
else if
Statement;
else
Statement;
end
FOR
for variable= initial value: final value
statement;
end
while
while condition
statement
end
TAKING UP IMAGES
COLOR IMAGE
It is composed of three primary colors i.e. RGB
RGB VALUE
All colours are made of red, green and blue
Therefore any color can be described by its
unique RGB value
This triplet has each value ranging from 0 (no
component) to 255 (full component)
e.g. Pure red= [255 0 0]
white = [255 255 255]
FUNCTIONS
Functions are written to organise the code
efficiently
Set of statements within a function can be
executed by just calling it
Difference between a function and M file
Function is task specific
Whereas M file is a fully executable etity itself
performing a number of tasks
REMOVING NOISE
imclose() fills the gaps and smoothens the
edges. Degree and type of smoothening depends
on structuring element e.g. A disk, diamond, line
etc
imfill() removes holes from the image
imopen() for removing spatters
BWBOUNDARIES()
It traces the exterior boundaries of object in a
binary image
B=bwboundaries(bw)
Returns pX1 cell array, where p is number of
objects.
Cell array is one where each element is a matrix
itself
Each cell in B contains a qX2 matrix
Where q is pixel detail of each object
For better output use command
bwboundaries(bw, noholes)
LABEL MATRIX
2D matrix of same size as that of the image
Each object in binary is numbered as 1,2,3... And
corresponding all pixels in L have values 1,2,3...
Respectively
Background is 0 by default
[B L]=bwboundaries(bw, noholes);
Or
L=bwlabel(bw);
Why do we need label matrix?
REGIONPROPS()
used to measure properties of the image
E.g. Centroid, area etc of each element
>>stats=regionprops(L, Area, Centroid);
To get properties of individual object dot(.)
operator is used
>>a=stats(3).Area %to get area of 3rd object
SUPPORTED FORMATS
>>info.SupportedFormats
It shows the number of and size of supported
frame formats by your camera
Previewing Camera
>>vid=videoinput(winvideo, 1, YUY2_160X120)
>>preview(vid)
CAPTURING IMAGES
>>im=getsnapshot(vid);
>>imshow(im);