You are on page 1of 35

stanford hci group / cs377s

Designing Applications that See Designing Applications that See


Lecture 4: Matlab Tutorial
Dan Maynes-Aminzade
17 January 2007 17 January 2007
Designing Applications that See http://cs377s.stanford.edu
R i d Reminders
A i t # d t T d Assignment #1 due next Tuesday
All the readings are now available, linked g ,
from course calendar
17 January 2007 2 Lecture 4: Matlab Tutorial
T d G l Todays Goals
T k th t h i d i th l t Take the techniques covered in the last
lecture and learn how to use them in Matlab
Work through the process of building a
complete example of a simple computer complete example of a simple computer
vision application
17 January 2007 3 Lecture 4: Matlab Tutorial
I P i i M tl b Image Processing in Matlab
17 January 2007 4 Lecture 4: Matlab Tutorial
I C i Image Conversion
r gb2gr ay
i m2bw
17 January 2007 5 Lecture 4: Matlab Tutorial
Dil ti d E i Dilation and Erosion
i mer ode
i mdi l at e i mdi l at e
17 January 2007 6 Lecture 4: Matlab Tutorial
C t d C t Connected Components
bwf i l l , bwsel ect
17 January 2007 7 Lecture 4: Matlab Tutorial
Li Filt i Linear Filtering
10 5 3 0 0 0 10 5 3
4 5 1
1 1 7
7
*
0 0 0
0 0.5 0 =
1 1 7
0 1.0 0.5
kernel
i mf i l t er f i l t er 2 i mf i l t er , f i l t er 2
17 January 2007 8 Lecture 4: Matlab Tutorial
G i K l Gaussian Kernel
f speci al ( gaussi an , . . . )
17 January 2007 9 Lecture 4: Matlab Tutorial
S b l Ed D t ti Sobel Edge Detection
1 2 1 -1 0 1
-1 -2 -1
0 0 0
1 2 1
-1 0 1
-2 0 2
1 0 1
1 2 1 1 0 1
Sobel x Sobel y
edge( I , sobel )
17 January 2007 10 Lecture 4: Matlab Tutorial
C Ed D t ti Canny Edge Detection
edge( I , canny )
17 January 2007 11 Lecture 4: Matlab Tutorial
H h T f Hough Transform
houghl i nes( BW, t het a, r ho, peaks)
17 January 2007 12 Lecture 4: Matlab Tutorial
O tli Outline
M tl b f d t l * Matlab fundamentals*
Walkthrough of developing a computer vision
li ti i M tl b* application in Matlab*
Designing an image processing algorithm
Building a GUI
Running on live video
Deploying an application
*Based on slides by Christopher Rasmussen (University of
Delaware)
*Based on Image Processing seminar by Bruce Tannenbaum
(M thW k I ) (MathWorks, Inc.)
17 January 2007 13 Lecture 4: Matlab Tutorial
Wh t i M tl b? What is Matlab?
A hi h l l l f t i l l ti A high-level language for matrix calculations,
numerical analysis, & scientific computing
Language features
No variable declarations
Automatic memory management (but
preallocation helps) p p
Variable argument lists control function
behavior
Vectorized: Can use f or loops, but largely
unnecessary (and less efficient) y
17 January 2007 14 Lecture 4: Matlab Tutorial
N d M tl b H l ? Need Matlab Help?
I M tl b In Matlab
Highlight a term, right-click, and select help
Type hel p to get a listing of topics
hel p <t opi c> gets help for that topic hel p <t opi c> gets help for that topic
On the web
CS377S Resources page has links
In particular, the MathWorks help desk: p , p
www.mathworks.com/access/helpdesk/help/helpdesk.shtml
17 January 2007 15 Lecture 4: Matlab Tutorial
E t i V i bl Entering Variables
E t i t t i Entering a vector, matrix
V = [ 10, 4. 5, 1] ;
M = [ 3, 4 ; - 6, 5] ;
Without semi colon input is echoed (this is Without semi-colon, input is echoed (this is
bad when youre loading images!)
Comma to separate statements on same
line
size: Number of rows, columns
17 January 2007 16 Lecture 4: Matlab Tutorial
C t ti M t i Constructing Matrices
B i b ilt i Basic built-ins:
All zeroes, ones: zer os, ones
Identity: eye
Random: r and (uniform) r andn (unit normal) Random: r and (uniform), r andn (unit normal)
Ranges: m: n, m: i : n (i is step size)
Composing big matrices out of small matrix
blocks blocks
r epmat ( A, m, n) : Tile a big matrix
ith i f A with mx n copies of A
17 January 2007 17 Lecture 4: Matlab Tutorial
M lti li ti & C l l ti Multiplications & Calculations
T () i (i ) Transpose (), inverse (i nv)
Matrix arithmetic: +, - , *, / , ^
Elementwise arithmetic: . *, . / , . ^
Functions
Vectorized
si n, cos, etc.
17 January 2007 18 Lecture 4: Matlab Tutorial
D t ti M t i Deconstructing Matrices
I d i i di id l t i b l Indexing individual entries by row, col:
A( 1, 1) is upper-left entry
Ranges: e.g., A( 1: 10, 3) , A( : , 1)
M t i t t d i b l Matrix to vector and vice versa by column:
B = A( : ) , A( : ) = B
Transpose to use row order
f i nd: Indices of non-zero elements f i nd: Indices of non zero elements
17 January 2007 19 Lecture 4: Matlab Tutorial
M t i A l i Matrix Analysis
B i (b l ) Basics (by column)
nor m
i max, mi n
sum
More advanced
Linear systems: A\ b solves A*x = b
QR decomposition: qr
Singular value decomposition: svd
Eigenvalues: ei g
Etc. Etc.
17 January 2007 20 Lecture 4: Matlab Tutorial
C t l St t Control Structures
E i l ti ( >| & f ti t ) Expressions, relations (==, >, | , &, functions, etc.)
i f / whi l e expression statements end
Use comma to separate expression from statements if
on same line
i f a == b & i spr i me( n) , M = i nv( K) ;
el se M = K; end
bl f or variable =expression statements end
f or i =1: 2: 100, s = s / 10; end
17 January 2007 21 Lecture 4: Matlab Tutorial
Th M Fil The M-Files
A t t fil di i Any text file ending in .m
Use pat h or addpat h to tell Matlab where
code is (or select in directory window)
Script: Collection of command line statements Script: Collection of command line statements
Function: Take argument(s), return value(s).
Fi t li d fi First line defines:
f unct i on y = f oo( A)
f unct i on [ x, y] = f oo2( a, M, N)
Comment: Start line with %
17 January 2007 22 Lecture 4: Matlab Tutorial
Pl tti Plotting
2 D vectors pl ot ( x y) 2-D vectors: pl ot ( x, y)
pl ot ( 0: 0. 01: 2*pi , si n( 0: 0. 01: 2*pi ) )
3 D l t 3( ) ( ) 3-D: pl ot 3( x, y, z) (space curve)
Surfaces
i k f f l t it meshgr i d makes surface from axes, mesh plots it
[ X, Y] = meshgr i d( - 2: . 2: 2, - 2: . 2: 2) ;
Z X * e p( X ^2 Y ^2) Z = X . * exp( - X. ^2 - Y. ^2) ;
mesh( Z)
sur f Solid version of mesh sur f : Solid version of mesh
Saving figures, plots: pr i nt depsc2 filename
17 January 2007 23 Lecture 4: Matlab Tutorial
I P i T lb Image Processing Toolbox
L di di l i i Loading, displaying images:
I =i mr ead( i m1. j pg ) , i mshow( I )
Saving images:
i mwr i t e( I newi mj pg ) i mwr i t e( I , newi m. j pg )
Image representation
Grayscale: Matrix of ui nt 8
Color: Stack of 3 matrices for R, G, and B Color: Stack of 3 matrices for R, G, and B
Conversion: I 2 = doubl e( I 1)
17 January 2007 24 Lecture 4: Matlab Tutorial
B ildi E l A li ti Building an Example Application
I l i ith th M tl b I Image analysis with the Matlab Image
Processing Toolbox
Getting live data with the Matlab Image
Acquisition Toolbox Acquisition Toolbox
Building a GUI with GUIDE
Deploying an application with the Matlab
compiler compiler
Try to follow along!
17 January 2007 25 Lecture 4: Matlab Tutorial
M tl b W kfl Matlab Workflow
17 January 2007 26 Lecture 4: Matlab Tutorial
I P i T lb Image Processing Toolbox
I i li ti Image visualization
Image pre- and post-processing g p p p g
Image analysis
l f Spatial transformations
Color processing Color processing
17 January 2007 27 Lecture 4: Matlab Tutorial
Traditional Image Processing Tasks Traditional Image Processing Tasks
17 January 2007 28 Lecture 4: Matlab Tutorial
I A i iti T lb Image Acquisition Toolbox
St id d i i t M tl b Stream video and images into Matlab
Supports a wide variety of frame grabbers pp y g
and digital cameras
C fi d i Configure device
properties
Live video previewing
Background image Background image
acquisition
17 January 2007 29 Lecture 4: Matlab Tutorial
D i i GUI ith GUIDE Designing a GUI with GUIDE
D i d dit GUI Design and edit GUI
Add buttons, pull-down menus, etc. , p ,
Generate Matlab code
h h d lf Finish the code yourself
17 January 2007 30 Lecture 4: Matlab Tutorial
Ni Thi b t M tl b Nice Things about Matlab
U ifi d i t Unified environment
Quick iteration through different algorithms Q g g
Interactive graphics and visualizations
h l l l High level language
Lots of built-in routines, useful Toolbox Lots of built in routines, useful Toolbox
functions, and code available on the web
17 January 2007 31 Lecture 4: Matlab Tutorial
T L M To Learn More
Di it l I P i U i M tl b Digital Image Processing Using Matlab
by Gonzalez, Woods, and Eddins
17 January 2007 32 Lecture 4: Matlab Tutorial
T t i l Fil Tutorial Files
D l d th t t i l fil Download the tutorial files:
http://cs377s.stanford.edu/code/matlab-tutorial.zip
Copy them to your Matlab working
di t ( b bl C \ MATLAB701\ k)
p // / / p
directory (probably C: \ MATLAB701\ wor k)
17 January 2007 33 Lecture 4: Matlab Tutorial
D i i GUI ith GUIDE Designing a GUI with GUIDE
D i d dit GUI Design and edit GUI
Add buttons, pull-down menus, etc. , p ,
Generate Matlab code
h h d lf Finish the code yourself
17 January 2007 34 Lecture 4: Matlab Tutorial
T L M To Learn More
Di it l I P i U i M tl b Digital Image Processing Using Matlab
by Gonzalez, Woods, and Eddins
17 January 2007 35 Lecture 4: Matlab Tutorial

You might also like