Introduction to Matlab
Dr. Ouiem Bchir
MATLAB stands for MATrix LABoratory
A high performance language for technical computing.
Integrates computation, visualization, and programming in an easy-to-use environment.
Complemented by a family of application-specific functions (M-files)
Image Processing Toolbox
I. Introduction to Matlab
2
Workspace
I. Introduction to Matlab
3
Matlab desktop Command window:
user types commands
Command output displayed
Workspace browser
Workspace: set of variables the user creates in a work session.
Workspace browser: where variables are displayed.
Current directory window
Shows content of the current directory
Command history window
Contains a record of the commands the user has entered.
Can be selected and re-executed or to create an M-file
Figure windows
Shown only when the user displays graphics.
I. Introduction to Matlab
4
Digital Image Representation
5
Digital Image Representation
Image=2-D function, f(x,y)
x,y: spatial coordinates
f(x,y): intensity of the image at point (x,y).
Gray level= intensity of monochrome images
Color images = combination of individual images
RGB image = Red, Green, and Blue individual component images.
Digitizing a continuous image requires
Sampling & Quantization
I. Introduction to Matlab
6
Coordinate conventions
MxN Image
Many image processing textbooks Image Processing Toolbox
I. Introduction to Matlab
7
Intensity Images
A digital image can be represented as a MATLAB ⎡ f(1,1) f(1,2 ) L f(1,N)⎤ matrix: ⎢ f( 2,1)
f = ⎢ ⎢ M ⎢
1 ⎣ f(M, )
f( 2,2 ) L f( 2,N)⎥ ⎥
M M ⎥ ⎥
f(M,2 ) L f(M,N)⎦
⎡35 45 20⎤ ⎢43 64 52⎥ ⎢ ⎥ ⎢10 29 39⎥
I. Introduction to Matlab
8
Color Images
R(x,y)
G(x,y)
B(x,y)
I. Introduction to Matlab
9
Reading Images
Imread (‘filename’) >> f = imread (‘chestxray.jpg’); (from current dir)
>> f = imread (‘D:\myimageschestxray.jpg’);
Some of the popular image/graphics format supported by imread and imwrite.
I. Introduction to Matlab
10
>> size (f) ans =
1024 1024
For further processing
>> [M N] = size (f)
>> whos f
Name Size Bytes Class
f 1024x1024 1048567 uint8 array
Grand total is 108576 elements using 1048576 bytes
I. Introduction to Matlab
11
Displaying Images
imshow (f, G) G: number of intensity levels used to display f (default = 256
levels).
imshow (f, [low high]) values <= low displayed black
values >= high displayed white
values in between displayed as intermediate intensity values using the default No. of levels
imshow (f, [ ]) low = minimum value of array f
high = maximum value of array f
pixval Display intensity (RGB) values of individual pixels interactively
I. Introduction to Matlab
12
>> f = imread (‘rose_512.tif’);
>> imshow (f)
Can process the image:
Scale, save, export,
edit, format, print, …
I. Introduction to Matlab
13
>> imshow (g)
MATLAB replaces the image in the screen with the new image.
To keep the first image and display another image
>> figure
>> imshow (g)
To display in a specific window
>> figure (3), imshow (g)
I. Introduction to Matlab
14
imshow (h) imshow (h, [ ] )
I. Introduction to Matlab
15
Writing Images
>> imwrite (f, ‘filename’)
‘filename’ must include file format extension
>> imwrite (f, ‘patient10_run1’, ‘tif’)
or
>> imwrite (f, ‘patient10_run1.tif ’)
Can have parameters depending on the file format
>> imwrite (f, ‘filename.jpg’, ‘quality’, q)
q: integer between 0 and 100 (lower higher quality).
I. Introduction to Matlab
16
Vector Indexing
>> v = [1 3 5 7 9];
>> v(2)
ans = 3
>> w = v.’ %transpose operator
w= 1
3
5
7
9
>> v(1:3)
ans = 1 3 5
>> v (3:end)
ans = 5 7 9
>> v ( : )
>> v (1:end)
% produces a column vector
% produces a raw vector
I. Introduction to Matlab
17
>> v(1:2:end)
ans = 1 5 9
>> v(end:-2:1)
ans = 9 5 1
x = linspace (a, b, n)
generates a row vector x of n elements linearly spaced between a & b
A vector can be used as an index
>> v([1 4 5])
ans = 1 7 9
I. Introduction to Matlab
18
Matrix Indexing
Represented as a sequence of row vectors.
>> A = [ 1 2 3; 4 5 6; 7 8 9]
A= 1 2 3
4 5 6
7 8 9
>> A(2,3)
ans = 6
>> C3 = A (:,3)
C3 = 3
6
9
% same as A(1:3, 3)
I. Introduction to Matlab
19
>> R2 = A(2, :) R2 = 4 5 6
>> T2 = A (1:2, 1:3);
>> B = A;
>> B(:, 3) = 0
B= 1 2 0
4 5 0
7 8 0
>> A (2:end, end:-2:1)
ans = 6 4
9 7
% top 2 rows
I. Introduction to Matlab
20
>> D = logical ([1 0 0; 0 0 1; 0 0 0]);
>> A(D)
ans = 1
6
>> v = T2(:) % All elements column by column
v= 1
4
2
5
3
6
>> s = sum(A(:))% sum of all elements
s = 45
>> s = sum(A)
s = 12 15 18
>> s = sum(sum(A))
s = 45
Elements of a matrix are stored as a 1-D vector A(3) = 7; A(8) = 6; …
I. Introduction to Matlab
21
f
1024 x 1024
f (end: -1: 1, :) f(257:768,257:768)
f(1:2:end, 1:2:end)
Plot ( f (512, :) )
I. Introduction to Matlab
22
Standard Arrays
zeros (M, N): MxN matrix of 0’s of class double
ones (M, N): MxN matrix of 1’s of class double
true (M,N): MxN logical matrix of 1’s
false (M,N): logical matrix of 0’s
rand (M,N): random elements, uniformly distributed in [0, 1]
randn (M,N): random elements, normally distributed with mean 0 and variance 1.
I. Introduction to Matlab
23
M-Files
Scripts that execute a series of MATLAB
statements.
Functions that can accept arguments
and can produce outputs.
I. Introduction to Matlab
24
•There is much more stuff !!!
•Check textbook website & other
links for tutorials, demos, …
I. Introduction to Matlab
25