Introduction to Matlab

advertisement
Holon Academic Institute of Technology
Department of Industrial Mathematics
Digital Image Processing in
MATLAB Environment
Matlab Fundamentals, Programming
Techniques, and Working with images
using MATLAB Image Processing Toolbox
Instructor: Eugene Kruchkov,
2005
Course Purpose








This course is designed to give students and
seniors a fundamental understanding of digital
image processing techniques with Matlab
Image Processing Toolbox, including
Using different types of image representations
Enhancing image characteristics
Filtering images
Reducing the effects of noise and blurring
Extracting features and objects within an image
Registering images
Reconstructing images/objects
2
Course Properties




This is a 24 hours (about 4 weeks) course, 6 hours
weekly, and the final exam.
Time and Place:
18:00 – 21:00 PM, Monday, Room 104, Bld. 3.
18:00 – 21:00 PM, Thursday, Room 104, Bld. 3.
First Session: 01.08.2005
Last Session: 26.08.2005
Contact:
E-mail: ns_tima@bezeqint.net
Phone: 054 - 4355083
3
Course Requirements




Basic knowledge of Linear Algebra, Probability, and
MATLAB is required. A prior acquaintance with basic
Signal and Image processing concepts is
recommended, but not required.
Students are assumed to be familiar with basic
programming concepts.
Student will be in his/her seat in the lecture hall prior
to the start of every lecture. This is a common
courtesy to the instructor and to fellow students.
If the student wants more information on a topic or
needs help, he/she will make an appointment with
the instructor.
4
Course Objectives



Give the students a general understanding of
the fundamentals of digital image processing.
Introduce the student to analytical tools, which
are currently used in digital image processing as
applied to image information for human viewing.
Develop the students ability to apply these tools
in image enhancement, segmentation, and
feature extraction using MATLAB imageprocessing environment
5
Expected Results
By the end of this course, participants will:
 Learn to develop, fast prototype, and test
image processing algorithms.
 Gain workable knowledge of the powerful
MATLAB image-processing environment.
 Learn about the most recent advances in the
field.
6
Topics Covered









Covers introductory topics, including working with
matrices, data manipulation, file I/O, and
programming.
Image basics (I/O, color, files, objects)
Image acquisition
Color spaces
Histograms – correcting contrast & illumination
problems
Image Quantization
Thresholding
Linear filtering Block processing
Image enhancement & restoration
7
Topics Covered, Cont.








Block processing
Image enhancement & restoration
Image Deblurring and Sharpening
BW Morphology and Edge detection
Wavelet transform
Fourier transform
Radon & Hough transforms
Distance transform
8
Topics Covered, Cont.






Connected component analysis
Grayscale Morphology
Segmentation: shape, color, watershed
Feature extraction
Image registration & geometric
transformations
Motion detection and tracking
9
Methods of assessment




One control test (20%) and final exam (80%).
Student will pass the course taking 60 points.
The control test will assess the basic
concepts of the subject and the main
techniques used in MATLAB.
The function of the final exam is to test the
students ability to tackle a real problem,
produce working MATLAB code, and present
obtained results in a coherent way.
10
Feedback and student support during
course study



Course Notes will be provided
Feedback will be given on the homework
assignment
Instructor is available for one to one
discussions with students in the event of
difficulties
11
Session1:
Getting Started
MATLAB fundamentals and
programming techniques
What is Matlab


MATLAB® is a high-performance language
for technical computing.
It integrates computation, visualization, and
programming in an easy-to-use environment
where problems and solutions are expressed
in familiar mathematical notation.
13
Typical uses for Matlab







Math and computation
Algorithm development
Data acquisition
Modeling, simulation, and prototyping
Data analysis, exploration, and visualization
Scientific and engineering graphics
Application development, including graphical
user interface building
14
More about Matlab




MatLab is an interactive system whose basic data
element is an array that does not require
dimensioning.
The name MatLab stands for MATrix LABoratory
MatLab features a family of add-on applicationspecific solutions called toolboxes
Toolboxes are comprehensive collections of MatLab
functions (M-files) that extend the MatLab
environment to solve particular classes of problems
15
How to start and exit Matlab





On a Microsoft Windows platform, to start MATLAB,
double-click the MATLAB shortcut
icon on
your Windows desktop.
After starting MATLAB, the MATLAB desktop opens
Note the >> is the matlab command prompt
To end your MATLAB session, select Exit MATLAB
from the File menu in the desktop, or type quit in the
Command Window.
Note that nothing is saved when you exit, you will
not be prompted to save
16
MATLAB Desktop


When you start MATLAB, the MATLAB
desktop appears, containing tools (graphical
user interfaces) for managing files, variables,
and applications associated with MATLAB.
The first time MATLAB starts, the desktop
appears as shown in the following illustration,
although your Launch Pad may contain
different entries.
17
MATLAB Desktop
18
MATLAB Desktop, Cont.





When launching Matlab, version 6 brings up a desktop with pulldown menus and various windows. These windows are:
Command Window: This is the main window for issuing commands
and seeing results, and is what has been used in this class up to
now.
Command History: An ordered list of all commands issued in the
Command Window.
Current Directory: The files in the user directory currently available
for use in the Command Window.
Workspace: a list of variables that have been used in the Command
Window.
Launch Pad: a variety of packages that may be available with
Matlab. We won't consider this window further.
19
Using Matlab help









Information about Matlab commands can be
found either by issuing the help command, or
by using the Help Window.
Just typing help brings up a list of packages
containing functions and symbols:
>> help
HELP topics:
matlab/general
matlab/lang
matlab/elmat
matlab/elfun
matlab/specfun
- General purpose commands.
- Programming language constructs.
- Elementary matrices and matrix manipulation.
- Elementary math functions.
- Specialized math functions, etc.
20
Using Matlab help, Cont.


We could then look at the elfun package,
which contains some elementary functions.
>> help elfun
Elementary math functions.
Trigonometric.
sin
- Sine.
sinh
- Hyperbolic sine.
asin
- Inverse sine.
asinh
- Inverse hyperbolic sine.
cos
- Cosine.
cosh
- Hyperbolic cosine.
acos
- Inverse cosine, etc.
21
Help Window

You can also use the Help Window, activated
from the desktop. For example, let's look up
information on the Matlab’s functions by
category. (see next slide)
22
23
Help Window, Cont.




From the tabs in the Help Navigator, you can use an
index, or search for keywords or explicit function names
(e.g. atan) or concepts.
The description of the arctangent function is much more
detailed than typing help atan, and includes graphs and
examples in a nice layout.
You can also use the helpwin command to display the
above help text inside the deskptop Help Window:
helpwin atan
The command doc goes directly to the help text above,
without the extra step involved in helpwin to click on a
link: doc atan
24
Running demos




Click Help -> Demos
Then in new window, select Matlab, then
Desktop Environment
Then select Desktop overview
Run the demo (it will run in a browser)
25
Matlab Basics. What Matlab operates on?





MATLAB works with scalars, vectors and
matrices.
A scalar is just a number, a 1x1 matrix.
A vector is a list of numbers, effectively a
matrix, given as either a row or a column.
In this sense, everything that MATLAB
operates on is a matrix.
The best way to get started with MATLAB is
to learn how to handle matrices.
26
Entering Matrices
We can enter matrices into MATLAB in several
different ways:

1.
2.
3.
4.
Enter an explicit list of elements.
Load matrices from external data files.
Generate matrices using built-in functions.
Create matrices with your own functions in M-files.
We have only to follow a few basic
conventions:

1.
2.
3.
Separate the elements of a row with blanks or commas.
Use a semicolon, ; , to indicate the end of each row.
Surround the entire list of elements with square brackets, [ ].
27
Matlab Statements and Variables



MATLAB is an expression language. It
interprets and evaluates expressions typed in
the command window at the keyboard.
You are allowed to assign a name to an
expression.
Statements are usually in the form of
variable = expression,
e.g. A = magic(4)
28
Syntax - symbols and punctuation

Try these examples
Input
Output
Comments
2+3
7-5
34*212
1234/5786
2^5
ans = 5
ans = 2
Arithmetic works as expected.
ans = 7208
ans = 0.2173 Note that the result is given the name "ans" each time.
ans = 32
a = sqrt(2)
a = 1.4142
You can choose your own names for things.
29
b = a, pi, 2 + 3i
c = sin(pi)
eps
b = 1.4142
ans = 3.1416
ans = 2.0000 +
3.0000i
You can use commas to put more
than one command on a line. Pi, i,
and j are contants.
c = 1.2246e-016
ans = 2.2204e-016
"eps" is the current limit of
precision. Anything smaller than
eps is probably zero. Note that
Matlab understands (and expects
you to understand!) scientific
notation.
d=
[1 2 3 4 5 6 7 8 9 d = 1 2 3 4 5 6 7 8 9
]
e=123456789
e = [1:9]
f=123456789
f = 1:9
"d", "e", and "f" are all vectors.
They are equal. Note the use of
the ":" operator - it counts (by
ones) from one number to the
next.
g = 0:2:10
f(3)
f(2:7)
f(:)
More uses of the colon. Note that
you can use it to get slices of a
vector (or matrix, or cube, etc), or
get the whole thing.
g = 0 2 4 6 8 10
ans = 3
ans = 2 3 4 5 6 7
123456789
30
h = [1 2 3];
h'
(nothing)
ans = 1
2
3
A semi-colon ";" will prevent the output
from being displayed. A single quote " '
" computes the transpose of a matrix,
or in this case, switches between row
and column vectors.
h * h'
h .* h
h + h
ans = 14
ans = 1 4 9
ans = 2 6 8
Operations on vectors. * is matrix
multiplication, and so the dimensions
must line up correctly.
" .* " is entry-by-entry multiplication.
g = [ 1 2 3;
4 5 6; 7 8 9]
g = 1 2 3
4 5 6
7 8 9
Entering a matrix.
ans = 6
ans = 7 8 9
g = 1 2 3
4 5 4
7 8 9
Accessing matrix elements.
Note use of ":" to access an entire row.
g(2,3)
g(3,:)
g(2,3) = 4
31
Input
g^2
g .^ 2
Output
Comments
ans =
30 36 42
66 81 96
102 126 150 The first multiplies the matrix by itself.
ans = 1 4 9
The second squares each entry in the
16 25 36
matrix.
49 64 81
32
Control of output

The main command to know is format

To control linespacing, use format compact

To see all 15 digits that were used in calculation,
use format long
To see just 5 digits, use format short



To suppress output completely, use a semi-colon
at the end of the command.
For more information, type help format
33
Working with Scalars



A scalar is just a number
Matlab stores them as 1x1 matrices
All operations involving a scalar and a matrix
are entry-by-entry, with on exception:

The power (“^”) operator
34
Working with Scalars, Cont.

Try these examples
Input
Output
Comments
b=2
b=2
Define b to be a scalar.
a + b
ans = 3 4
5 6
Addition works entry-by-entry.
a * b
ans = 2 4
6 8
So does multiplication.
a ^ b
ans = 7 10
15 22
This is matrix power - a*a
a .^ b
ans = 1 4
9 16
Entry-by-entry power.
35
Vectors

A vector is just a matrix with only one row or
column
Input
Output
Comments
v = [1 2 3]
u = [3 2 1]
v = 1 2 3
u = 3 2 1
Define a pair of vectors.
v * u
Error
The dimensions don't agree.
v * u'
ans = 10
Taking the transpose works.
dot(v,u)
ans = 10
The dot product is the same thing.
cross(v,u)
ans = -4 8 -4
The cross product works only for 3-d
vectors
36
Matrix Operations Synopsis




+, -, *, and / are defined in an intuitive
manner for matrices
“ ‘ “ (transposition) turns a row vector into a
column vector
“.*” (dot-star) will multiply entry-by-entry
“*” will do matrix multiplication. More
n
precisely,
C (i, j )   A(i, k ) * B(k , j )
k 1
37
Transposing Matrices



The special character “ ‘ “ (prime or apostrophe)
denotes the transposition of the matrix. The
statements
A = [1 2 3; 4 5 6; 7 8 0];
B = A';
Result in
A=
B=
1 2 3
1 4 7
4 5 6
2 5 8
7 8 0
3 6 0
38
Matrix-Vector Product



Matrix-Vector Product is a special case of
general matrix-matrix product.
Let us
A = [1 2 3; 4 5 6; 7 8 0]; x = [-1 0 2]’;
b = A*x results in the output
b=
5
8
-7
39
Using Powers with Matrices




A^p raises A to p-th power and is defined if A is a
square matrix and p is a scalar.
If p is an integer greater than 1, the power is
computed by repeated multiplication.
For other values of p, the calculation involves
eigenvalues (D) and eigenvectors (V):
if
[V,D] = eig(A), then
then
A^p = V*D.^p/V
X^P, where both X and P a matrices, is an error.
40
Matrix built-in operations

For more, type help matfun
Input
Output
Comments
k = [16 2 3;
5 11 10;
9 7 6]
k = 16 2 3
5 11 10
9 7 6
Define a matrix.
rank(k)
ans = 3
The rank.
det(k)
ans = -136
The determinant.
41
Matrix built-in operations, Cont.
Input
Output
ans =
inv(k)
[vec,val] =
eig(k)
Comments
0.0294 -0.0662 0.0956
-0.4412 -0.5074 1.0662
0.4706 0.6912 -1.2206
vec = -0.4712 -0.4975 -0.0621
-0.6884 0.8282 -0.6379
-0.5514 0.2581 0.7676
val = 22.4319
0
0
0 11.1136
0
0
0 -0.5455
Inverse of the
matrix
Eigenvectors
and eigenvalues
of the matrix.
The columns of
"vec" are the
eigenvectors,
and the diagonal
entries of "val"
are the
eigenvaules
42
Matrix built-in constructions

For more, type help func_name
Input
Output
rand(2)
ans = 0.9501
0.2311
ans = 0.8913
0.7621
rand(2,3)
zeros(2)
ones(2)
ans = 0
0
ans = 1
1
Comments
0.6068
0.4860
0.4565 0.8214
0.0185 0.4447
0
0
1
1
Generates a matrix with
entries randomly distributed
between 0 and 1
Generates a 2x2 matrix with
all zero (or all ones) entries.
eye(2)
ans = 1 0
0 1
Identity matrix I.
hilb(3)
ans = 1.0000 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000
3x3 Hilbert matrix.
43
Concatenating Matrices

New matrices may be formed out of old ones

Suppose we have:
a = [1 2; 3 4]
a = 1 2
3 4
44
Deleting rows and columns




You can delete rows and columns from a matrix
using just a pair of square brackets.
Start with
X = [1 2 3; 4 5 6; 7 8 0]
Then, to delete the second column of X, use
X(:,2) = [ ];
Thus,
X= [1 3
4 6
7 0]
If you delete a single element from a matrix, the
result isn't a matrix anymore.
So, expressions like
X(1,2) = [ ], result in an error.
45
Concatenating Matrices, Cont.
Input
Output
[a, a, a]
ans = 1 2 1 2 1 2
3 4 3 4 3 4
[a; a; a]
ans = 1
3
1
3
1
3
2
4
2
4
2
4
[a, zeros(2); zeros(2), a']
ans = 1
3
0
0
2
4
0
0
0
0
1
2
0
0
3
4
46
M - files




You can create your own matrices using M-files,
which are text files containing MATLAB code.
Use the MATLAB Editor or another text editor to
create a file containing the same statements you
would type at the MATLAB command line. Save the
file under a name that ends in .m.
For example, create a file containing these two lines.
A = [ 16.0 3.0 2.0 13.0; 5.0 10.0 11.0 8.0; …
9.0 6.0 7.0 12.0; 4.0 15.0 14.0 1.0 ];
Store the file under the name magik.m. Then the
statement magik reads the file and creates variable,
A, containing our example matrix.
47
Solving System of Linear Equations


One of the main uses of matrices is in representing
systems of linear equations.
If a is a matrix containing the coefficients of a
system of linear equations, x is a column vector
containing the "unknowns," and b is the column
vector of "right-hand sides," the constant terms, then
the matrix equation
a x =b
represents the system of equations
48
Solving equations, Cont.


MATLAB uses the division terminology
familiar in the scalar case to describe the
solution of a general system of simultaneous
equations.
The two division symbols, slash, /, and
backslash, \, are used for the two situations
where the unknown matrix appears on the left
or right of the coefficient matrix.
49
Solving equations, Cont.





X = A\B denotes the solution to the matrix equation
AX = B.
X = B/A denotes the solution to the matrix equation
XA = B.
The dimension compatibility conditions for X = A\B
require the two matrices A and B to have the same
number of rows.
The solution X then has the same number of
columns as B and its row dimension is equal to the
column dimension of A.
For X = B/A, the roles of rows and columns are
interchanged.
50
Solving equations, Cont.
In practice, linear equations of the form AX = B
occur more frequently than those of the form XA =
B. Consequently, backslash is used far more
frequently than slash.
The coefficient matrix A need not be square. If A is
m-by-n, there are three cases.


1.
2.
3.
m = n, Square system. Seek an exact solution.
m > n, Overdetermined system. Find a least squares
solution.
m < n, Underdetermined system. Find a basic solution
with at most m nonzero components.
51
Solving equations, an Example


To solve the equation a x =b in matlab simply type

x = a \ b

Which reads “x equals a-inverse times b”
Try it with


a = [1 2 3; 4 5 6; 7 8 10]; b = [1 1 1]';
You should get

x =
-1
1
0
52
Solving equations, an Example, Cont.

To verify this assertion, try this:
a*x, a*x - b, eps
The results are:
ans = 1 1 1
ans = 1.0e-015 *
-0.1110
-0.6661
-0.2220
ans = 2.2204e-016

Notice that a*x - b is very close to eps - which
means that it is as close to zero as possible.
53
Solving equations, an Example, Cont.


If there is no solution, a "least-squares" solution is
provided (a*x - b is as small as possible). Enter
a(3,3) = 9; b = [1 1 0]';
(which makes the matrix singular and changes b)
and try to solve the equation again.
Notice that the solution is quite inaccurate.
54
Saving and loading matrices



When you exit matlab you will not be
prompted to save
You can turn on logging

diary '~/session.txt‘

this will save output and input together and
therefore can not be used as a script
You may just want to save one or more
matrices

save x.value x -ascii
55
Saving and loading matrices

To save all variables in a file named
“mysession.mat” in a reloadable format


To restore the session, use


save mysession
load mysession
The saved files are in text format and can be
viewed using any text editor
56
Download