You are on page 1of 28

MATLAB

Matrix Laboratory

WHAT DOES MATLAB STANDS FOR?


As the name suggests, we are about to play with
matrices.
Hence any data (image or sound) is first
converted into in to matrices and then operated
on.
Image processing deals with identifying colours,
texture, pattern, intensity and edges in an image.

GETTING ACQUAINTED WITH


MATLAB

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

HOW TO REPRESENT A MATRIX


>>b= [1 2 3; 4 5 6; 7 8 9]
b=
1 2 3
4 5 6
7 8 9
To avoid display of variable b use
semicolon (;) at the end of instruction
How to refer to a particular element of the
matrix?
>>b(2,1)
ans =
4

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

zeros(): create array/ matrix of all zeros


B= zeros(n) returns an nXn matrix of zeros
B= zeros(m,n) returns an mXn matrix of zeros
size(A) returns matrix dimensions i.e. No, of
rows and colums
length(A) it returns the size of longest
dimension
dot(A, B) it returns dot product of two vectors

sqrt() returns square root of each element


min() returns smallest element in an array
max() returns largest element in an array
sort(A, mode) sort array elements
ascend: Ascending order
descend: Descending order
plot() creates a 2-D line plot

TAKING UP IMAGES

pixel: building blocks of an image


Binary Image: it contains only black and white
pixels. 0 is black and 1 is white

GRAY SCALE IMAGE


it contains intensity values ranging from black to
white and in between varying shades of gray.
This range is from 0 to 255.

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]

READING AND DISPLAYING AN


IMAGE
imread() to read an image and store it in a
matrix
>>im=imread(filename.extension);

imshow() displays the image


>>imshow(filename.extension);
>>imshow(im);

MAKING M FILES AND FUNCTIONS


Using a M file you can execute multiple
commands using single statement.
These group of commands are stored as MATLAB
files (extension .m)
File>>New>>Script
MATLAB editor window opens where you can
write the statements which you want to execute
and save the file
Comments are written after symbol %

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

WORKING IN REAL TIME


For checking if your cam is accessible by
MATLAB
>>imaqhwinfo
>>cam=imaqhwinfo;
>>cam.InstalledAdaptors
>>info=imaqhwinfo(winvideo, 1)
Be careful commands are case sensitive
Why did we use a variable cam and didnt call
function imaqhwinfo directly?

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);

To convert YUY format to RGB format


>>im=ycbcr2rgb(im);
>>imshow(im);

Storing captured image


>>imwrite(im, myimage.jpg);
Saves myimage.jpg in current directory

You might also like