Designing Applications that See Lecture 4: Matlab Tutorial Dan - - PowerPoint PPT Presentation

designing applications that see lecture 4 matlab tutorial
SMART_READER_LITE
LIVE PREVIEW

Designing Applications that See Lecture 4: Matlab Tutorial Dan - - PowerPoint PPT Presentation

stanford hci group / cs377s Designing Applications that See Lecture 4: Matlab Tutorial Dan Maynes-Aminzade 23 January 2007 Designing Applications that See http://cs377s.stanford.edu Reminders Assignment #1 due now! Assignment #2


slide-1
SLIDE 1

stanford hci group / cs377s

Designing Applications that See Lecture 4: Matlab Tutorial

Designing Applications that See http://cs377s.stanford.edu

Dan Maynes-Aminzade 23 January 2007

slide-2
SLIDE 2

Reminders

Assignment #1 due now! Assignment #2 released today, due in one week All the readings are now available, linked All the readings are now available, linked from course calendar

23 January 2007 2 Lecture 4: Matlab Tutorial

slide-3
SLIDE 3

Today’s Goals

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

23 January 2007 3 Lecture 4: Matlab Tutorial

slide-4
SLIDE 4

Image Processing in Matlab

23 January 2007 4 Lecture 4: Matlab Tutorial

slide-5
SLIDE 5

Image Conversion

rgb2gray

23 January 2007 5 Lecture 4: Matlab Tutorial

im2bw

slide-6
SLIDE 6

Dilation and Erosion

imerode

23 January 2007 6 Lecture 4: Matlab Tutorial

imdilate

slide-7
SLIDE 7

Connected Components

23 January 2007 7 Lecture 4: Matlab Tutorial

bwfill, bwselect

slide-8
SLIDE 8

Linear Filtering

  • *
  • =

23 January 2007 8 Lecture 4: Matlab Tutorial

imfilter, filter2

  • *
  • kernel
slide-9
SLIDE 9

Gaussian Kernel

23 January 2007 9 Lecture 4: Matlab Tutorial

fspecial(‘gaussian’,...)

slide-10
SLIDE 10

Sobel Edge Detection

  • 23 January 2007

10 Lecture 4: Matlab Tutorial

  • edge(I, ‘sobel’)
slide-11
SLIDE 11

Canny Edge Detection

23 January 2007 11 Lecture 4: Matlab Tutorial

edge(I, ‘canny’)

slide-12
SLIDE 12

Hough Transform

23 January 2007 12 Lecture 4: Matlab Tutorial

houghlines(BW,theta, rho, peaks)

slide-13
SLIDE 13

Outline

Matlab fundamentals* Walkthrough of developing a computer vision 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

(MathWorks, Inc.)

23 January 2007 13 Lecture 4: Matlab Tutorial

slide-14
SLIDE 14

What is Matlab?

A high-level language for matrix calculations, numerical analysis, & scientific computing Language features

No variable declarations No variable declarations Automatic memory management (but preallocation helps) Variable argument lists control function behavior Vectorized: Can use for loops, but largely unnecessary (and less efficient)

23 January 2007 14 Lecture 4: Matlab Tutorial

slide-15
SLIDE 15

Need Matlab Help?

In Matlab

Highlight a term, right-click, and select “help” Type “help” to get a listing of topics “help <topic>” gets help for that topic “help <topic>” gets help for that topic

On the web

CS377S Resources page has links In particular, the MathWorks help desk:

23 January 2007 15 Lecture 4: Matlab Tutorial

www.mathworks.com/access/helpdesk/help/helpdesk.shtml

slide-16
SLIDE 16

Entering Variables

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 you’re loading images!) Comma to separate statements on same line size: Number of rows, columns

23 January 2007 16 Lecture 4: Matlab Tutorial

slide-17
SLIDE 17

Constructing Matrices

Basic built-ins:

All zeroes, ones: zeros, ones Identity: eye Random: rand (uniform), randn (unit normal) Random: rand (uniform), randn (unit normal)

Ranges: m:n, m:i:n (i is step size) Composing big matrices out of small matrix blocks repmat(A, m, n): “Tile” a big matrix with m x n copies of A

23 January 2007 17 Lecture 4: Matlab Tutorial

slide-18
SLIDE 18

Multiplications & Calculations

Transpose (‘), inverse (inv) Matrix arithmetic: +, -, *, /, ^ Elementwise arithmetic: .*, ./, .^ Functions Functions

Vectorized sin, cos, etc.

23 January 2007 18 Lecture 4: Matlab Tutorial

slide-19
SLIDE 19

Deconstructing Matrices

Indexing individual entries by row, col: A(1, 1) is upper-left entry Ranges: e.g., A(1:10, 3), A(:, 1) Matrix to vector and vice versa by column: Matrix to vector and vice versa by column: B = A(:), A(:) = B

Transpose to use row order

find: Indices of non-zero elements

23 January 2007 19 Lecture 4: Matlab Tutorial

slide-20
SLIDE 20

Matrix Analysis

Basics (by column) norm max, min sum More advanced

Linear systems: A\b solves A*x = b QR decomposition: qr Singular value decomposition: svd Eigenvalues: eig

Etc.

23 January 2007 20 Lecture 4: Matlab Tutorial

slide-21
SLIDE 21

Control Structures

Expressions, relations (==, >, |, &, functions, etc.) if/while expression statements end

Use comma to separate expression from statements if

  • n same line

if a == b & isprime(n), M = inv(K); if a == b & isprime(n), M = inv(K); else M = K; end

for variable = expression statements end

for i=1:2:100, s = s / 10; end

23 January 2007 21 Lecture 4: Matlab Tutorial

slide-22
SLIDE 22

The M-Files

Any text file ending in “.m” Use path or addpath 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). First line defines:

function y = foo(A) function [x, y] = foo2(a, M, N)

Comment: Start line with %

23 January 2007 22 Lecture 4: Matlab Tutorial

slide-23
SLIDE 23

Plotting

2-D vectors: plot(x, y)

plot(0:0.01:2*pi, sin(0:0.01:2*pi))

3-D: plot3(x, y, z)(space curve) Surfaces

meshgrid makes surface from axes, mesh plots it meshgrid makes surface from axes, mesh plots it [X,Y] = meshgrid(-2:.2:2, -2:.2:2); Z = X .* exp(-X.^2 - Y.^2); mesh(Z)

surf: Solid version of mesh Saving figures, plots: print –depsc2 filename

23 January 2007 23 Lecture 4: Matlab Tutorial

slide-24
SLIDE 24

Image Processing Toolbox

Loading, displaying images: I=imread(‘im1.jpg’), imshow(I) Saving images: imwrite(I, ‘newim.jpg’) imwrite(I, ‘newim.jpg’) Image representation

Grayscale: Matrix of uint8 Color: Stack of 3 matrices for R, G, and B

Conversion: I2 = double(I1)

23 January 2007 24 Lecture 4: Matlab Tutorial

slide-25
SLIDE 25

Building an Example Application

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 Try to follow along!

23 January 2007 25 Lecture 4: Matlab Tutorial

slide-26
SLIDE 26

Matlab Workflow

23 January 2007 26 Lecture 4: Matlab Tutorial

slide-27
SLIDE 27

Image Processing Toolbox

Image visualization Image pre- and post-processing Image analysis Spatial transformations Spatial transformations Color processing

23 January 2007 27 Lecture 4: Matlab Tutorial

slide-28
SLIDE 28

Traditional Image Processing Tasks

23 January 2007 28 Lecture 4: Matlab Tutorial

slide-29
SLIDE 29

Image Acquisition Toolbox

Stream video and images into Matlab Supports a wide variety of frame grabbers and digital cameras Configure device Configure device properties Live video previewing Background image acquisition

23 January 2007 29 Lecture 4: Matlab Tutorial

slide-30
SLIDE 30

Designing a GUI with GUIDE

Design and edit GUI Add buttons, pull-down menus, etc. Generate Matlab code Finish the code yourself Finish the code yourself

23 January 2007 30 Lecture 4: Matlab Tutorial

slide-31
SLIDE 31

Nice Things about Matlab

Unified environment Quick iteration through different algorithms Interactive graphics and visualizations High level language High level language Lots of built-in routines, useful Toolbox functions, and code available on the web

23 January 2007 31 Lecture 4: Matlab Tutorial

slide-32
SLIDE 32

To Learn More…

Digital Image Processing Using Matlab by Gonzalez, Woods, and Eddins

23 January 2007 32 Lecture 4: Matlab Tutorial

slide-33
SLIDE 33

Tutorial Files

Download the tutorial files: Copy them to your Matlab working directory (probably C:\MATLAB701\work)

http://cs377s.stanford.edu/code/matlab-tutorial.zip

directory (probably C:\MATLAB701\work)

23 January 2007 33 Lecture 4: Matlab Tutorial