Lab 1: Matlab primer for Image Processing
March 13, 2002
This tutorial aims at getting you to a level in Matlab
programming so that you will be able to follow subsequent exercises in
the ``Advanced Image Processing'' course given at University of Geneva.
This tutorial does not
aim at being complete and is surely not the
best you may find (on the Web for
The first thing to do is to call the Matlab environment. Depending on which
platform you are using, this can be clicking on an icon (Windows) or typing
command in a shell (Unix, Linux, DOS). Whatever this is, you should end
up with a prompt that resembles:
< M A T L A B >
Copyright 1984-1999 The MathWorks, Inc.
Version 126.96.36.199215a (R11.1)
Oct 6 1999
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, type tour or visit www.mathworks.com.
You just entered the Matlab interpreter. You may now type commands and
the result will be displayed as a response, just as in any shell.
Some useful general commands are:
exits the environment.
is a Matlab command. By itself, help
gives the list of toolboxes (packages) that are installed on your system.
<something> where <something>
is what you are looking for in plain English. Actually, lookfor
searches the expression <something>
in the help pages. Try lookfor
Fourier for example.
allow you to navigate in the directory structure (Unix-like notation).
gives you the list of variables currently defined. whos
gives you the same list with their size.
all clears all variables. clear
<var1 var2> clears only the variables <var1
all close all figures. close
closes the current figure.
extends this list of generic commands.
Online calculation and multidimensional
The matlab interpreter will respond to commands you will input at the prompt.
These commands can either be ``system commands'' (eg cd)
or ``calculation commands'' (eg 1+1).
the system responds and prompts for the next command:
(which makes worthy a CHF 1000.- investment!).
By default the result is set into a variable called
Therefore, typing whos
gives you something like
Name Size Bytes Class
ans 1x1 8 double array
Grand total is 1 elements using 8 bytes
which is fairly explicit. If you would like the result to be stored in
some other variable
This therefore declares and sets the variable a
to 2. From now on, a
may be used for its value
and so on. You may declare any variable, provided its name follows some
simple rules (eg, starts with a letter, no +,-,/-* signs, etc).
Anytime you type a command, Matlab outputs the result. This may be avoided
with terminating the command by a semi-colon (;). Therefore:
Note the difference with
You now know how to create (and delete - clear)
variables. All generic operations are available in Matlab, use help
elfun (elementary functions) for a list (see
also help ops
and help specfun).
creates a scalar variable a.
Matlab generalises this principle to vector and matrices (a vector being
a matrix with one dimension set to 1). Therefore,
>> u=[1 2 3]
1 2 3
create a row (u)
and a column (v)
vector. More generally,
>> m=[1 2 3 4; 5 6 7 8; 8 9 10 11]
1 2 3 4
5 6 7 8
8 9 10 11
Provided their sizes correspond, most operations are available for matrices.
A lot of specific operations related to linear algebra are also available
and can be listed using help
matfun. Note that some operations operate on
the term level (cos,
Matlab offers two other types of operations on matrices. Firstly, one may
add (or multiply) a constant value to all the matrix elements
2 3 4 5
6 7 8 9
9 10 11 12
Secondly, one may combine matrices globally
>> m=[1 2;3 4];
>> n=[1 0;0 1];
(classical matrix multiplication). Or locally
(term per term multiplication). The fact of adding the ``.'' in front of
operators makes them operate locally.
Matlab offers several matrix manipulation commands (see
elmat for a list (these include commands for
creating some specific matrices). The most basic command being the transpose
command given by '
>> m=[1 2;3 4]
1 y 2
See also help
sparfun for a list of commands operating on sparse
Whenever it comes to creating a list, Matlab uses matrices. Matlab offers
a simple mechanism for creating some specific lists For example, the integers
between 1 and 5 are given by
1 2 3 4 5
The syntax actually generalises with a step from:step:to
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000
Accessing variable elements
Matlab uses parenthesis for accessing matrix (or vector) elements
>> m=[1 2 3 4; 5 6 7 8; 8 9 10 11];
(note that for matrices we have M(row,column)).
The previous mechanism gives us a technique for extracting parts of vectors
>> m=[1 2 3 4; 5 6 7 8; 8 9 10 11];
9 10 11
9 10 11
Since version 5, Matlab offers the management of 3D arrays.
>> d(:,:,1)=[1 2 3;3 4 5];
>> d(:,:,2)=[2 3 4;5 2 4];
>> d(:,:,3)=[3 4 6;7 9 3];
>> d(:,:,4)=[3 6 7;2 3 5];
>> whos d
Name Size Bytes Class
d 2x3x4 192 double array
Provided sizes are consistent, matrices operations may apply on these 3D
This is one form under which a RGB images will be stored.
Matlab goes beyond this simple interaction. It offers a complete set of
programming commands that can be used to create loops, tests and other
structure any other programming language can offer (see
lang for a list).
Up to now, commands were typed one after each other within the interpretor.
Matlab offers the possibility to create batch programs (and functions)
that can be called from the interpretor (and from other batch programs).
Actually most of high-level commands of matlab correspond to batch programs.
The technique is simple:
create a file mycommand.m
in the current directory
type commands in this file
execute these commands by typing mycommand
in the interpretor.
Write these lines in the mycommand.m
% this is my first Matlab program
fprintf(1,'c is now %d\n',c);
and type mycommand
c is now 4
The detail of commands in the mycommand.m
file can be found via the help facility. One thing to note is that anything
after a percent (%)
is a comment.
One may also create functions in a similar way. Type this in the
% adds 2 to the input
% this is my first Matlab function
Here, it is important that the function has the same name as its file.
One nice feature is that this permits online help, created by the first
comments in the function file.
>> help myfunction
adds 2 to the input
this is my first Matlab function
From then on, one can build a complete library (toolbox) of functions with
the appropriate documentation. Simply put all function files in a directory
add its path on the search path (with addpath('dir')).
dir will then display the first comments of each
M-file. Any toolbox is done like this (try which
logm) to see the file corresponding to the logm
On top of calculations commands, Matlab proposes generic programming instruction
for creating loops, test and so on.
if I == J
A(I,J) = 2;
elseif abs(I-J) == 1
A(I,J) = -1;
A(I,J) = 0;
Note: The NOT condition is given by the tilde (~=, not
for I = 1:N,
for J = 1:N,
A(I,J) = 1/(I+J-1);
Note that instead of 1:N
we could have any integer row vector.
E = 0*A; F = E + eye(size(E)); N = 1;
while norm(E+F-E,1) > 0,
E = E + F;
F = A*F/N;
N = N + 1;
disp('Method is linear')
disp('Method is cubic')
disp('Method is nearest')
See help lang
for further instructions.
Some useful commands and tips
When manipulating matrices and images, the following commands are often
very useful (see their respective help).
returns the largest dimension of an array.
returns the dimension of an array
sums the element of an array along its first non-singleton dimension (columns
for a matrix)
returns the largest element of an array along its first non-singleton dimension
(columns for a matrix)
returns the smallest element of an array along its first non-singleton
dimension (columns for a matrix)
generates random numbers
operates a 2D convolution between a matrix and a mask
reads an image in a given format as a matrix
write a matrix as an image
2D- Fourier and DCT transform of an array.
Have explicit names for variables
When creating a matrix, declare it first as a complete array rather
than element by element. Compare:
Use global operations rather than local, ie
use [value index]=sort(rand(1,n))
to generate a random selection within the set of the first n
Test whether a given matrix is symmetric, orthogonal, singular or otherwise.
Load an image and save it after a rotation of 90 degrees.
Load an image and calculate its RGB histograms (in at least two different
Imagine what you can do with Matlab :o)
Mon Mar 12 12:10:06 MET 2001