CSCI 5532.
01 – Summer 2021
PATTERN RECOGNITION AND
IMAGE PROCESSING
Lecture 2 – Introduction to MATLAB and
Image Processing Toolbox
Instructor:
Dr. Pradeep Buddharaju
1
MATLAB Working Environment
2
Reading Images
• imread function
– Reads images into MATLAB environment
– To read an image located in the working directory:
f = imread('[Link]');
– To read an image from a different directory, specify full path
f = imread('D:\myimages\[Link]');
• size function
– Gives the row and column dimensions of the image
[M,N] = size(f)
• whos function
– Displays additional information about an array
whos f
3
Displaying Images
• imshow function
– Displays images into MATLAB environment
– To display an image with 256 levels:
imshow(f)
– To display an image with a range of levels:
imshow(f, [150 250])
• figure function
– Allows to keep the image being currently displayed, and show another
image in a new window
figure
imshow(f)
• imtool function
– Opens the Image Tool, which provides a more interactive environment for
viewing and navigating within images
imtool(f)
4
Writing Images
• imwrite function
– Images are written in the current directory
imwrite(f,'filename');
– Examples:
imwrite(f, '[Link]');
imwrite(f, 'newImage','tif');
imwrite(f,'myImage','tif’, 'quality',25);
5
Image Types
• Image Processing Toolbox supports following image types:
– Gray-scale images, Binary images, Indexed images, and RGB images
– Syntax for conversion between various types:
B = class_name(A);
– Examples:
g = im2uint8(f);
h = im2double(g);
g = im2bw(h, 0.6);
6
Vectors & Matrices
• In MATLAB, one dimensional arrays are represented as vectors
and two dimensional arrays are represented as matrices
– Examples of vector indexing:
v = [1 3 5 7 9]
v(1:3)
v([Link]nd)
– Example of matrices indexing
A = [1 2 3; 4 5 6; 7 8 9]
A(2,3)
or
B=A([1 2], [1 2 3]) B=A(1:2,
1:3)
C3=A(:,3)
A(:,3)=0
7
Some Important Standard Matrices
Matrix Name Functionality
zeros(M,N) Generates M*N matrix of 0s of class double
ones(M,N) Generates M*N matrix of 1s of class double
true(M,N) Generates M*N logical matrix of 1s
false(M,N) Generates M*N logical matrix of 0s
eye(M,N) Generates M*N identity matrix
rand(M,N) Generates M*N matrix whose entries are uniformly
distributed random numbers in the interval [0,1]
randN(M,N) Generates M*N matrix whose entries are normally
distributed random numbers in the interval [0,1]
8
M-Files: Scripts
• M-files in MATLAB can be scripts that simply execute a series of MATLAB
statements
• M-files are created using a text editor and are stored with a name of the form
filename.m
• Execute the script by just typing filename in command prompt
9
M-Files: Functions
• In MATLAB, each function is created as a separate M-file
• The filename for M-file must be the same as function name
• Function definition syntax:
function [output s] = name (inputs)
– Example:
function [s] = add(a, b)
s = a+b;
end
• The symbol for denoting comment lines is %
• To execute, just call the function in the command prompt
x = add(10,20)
• Note that the MATLAB editor also provides a debugger where you
can set breakpoints during the function execution
10
Arithmetic Operators on
Arrays and Matrices
11
Relational and Logical Operators
• Relational Operators:
• Logical Operators:
12
Flow Control
13
Flow Control Syntax (1/4)
• if
• if, else, elseif
• if example
14
Flow Control Syntax (2/4)
• for
• Nested for
• for example:
15
Flow Control Syntax (3/4)
• while
• Nested while
• while example:
16
Flow Control Syntax (4/4)
• break
– terminates the execution of a for or while loop
• continue
– passes control to the next iteration of the for or while loop
• switch
17
Interactive I/O
• disp function
– Used to display information on the screen
– Syntax:
disp(argument)
– Example:
disp(‘This is another way to display text’)
• input function
– outputs the words contained in message and waits for an input from user
– Syntax:
t = input(‘message’)
– Example:
t = input ('Enter your data : ')
18
Plotting (1/2)
• plot function
– Creates a 2-D line plot of the data
– Syntax:
plot(V) or plot(X,Y)
– Examples:
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
plot(x);
figure;
plot(x,y1);
figure;
plot(x,y1,x,y2);
figure;
plot(x,y1,x,y2,'--',x,y3,':');
19
Plotting (2/2)
• subplot function
– Divides the current figure into an m-by-n grid and creates an axes in the
grid position specified by p
– Syntax:
subplot(m,n,p) figure
– Examples: subplot(2,2,1);
plot(x,y1);
x = linspace(0,10); title('Subplot 1: sin(x)')
y1 = sin(x);
y2 = sin(2*x); subplot(2,2,2);
y3 = sin(4*x); plot(x,y2);
y4 = sin(8*x); title('Subplot 2: sin(2x)')
subplot(2,2,3)
plot(x,y3);
title('Subplot 3: sin(4x)')
subplot(2,2,4)
plot(x,y4);
title('Subplot 4: sin(8x)') 20