Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Matlab and Image Processing Toolbox: Overview and Data Types - Prof. Shawn Newsam, Lab Reports of Computer Science

A lecture note from a fall 2008 computer science engineering (cse) course, cse185, focusing on matlab and the image processing toolbox. An overview of matlab, including data types, converting between data types, and vectors. It also discusses indexing and selecting blocks of elements in vectors and matrices, as well as logical indexing and using the find function.

Typology: Lab Reports

Pre 2010

Uploaded on 08/16/2009

koofers-user-pym
koofers-user-pym 🇺🇸

5

(1)

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSE185
Fall 2008
Lecture 5
Matlab and the Image Processing
Toolbox (continued)
Today
Matlab and the Image Processing Toolbox
continued
Assignments
Lab #1 due Wed. 9/24 by midnight
Matlab overview
intro.m
Matlab overview – data types
Data types
www.imageprocessingbook.com
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins
Digital Image Processing Using MATLAB®
Digital Image Processing Using MATLAB®
Matlab overview – data types
Converting between data types
Ex: a = uint8(b)
Double->uint8
Values < 0 mapped to 0
Values > 255 mapped to 255
Fractions discarded
Be aware of this!
pf3
pf4
pf5
pf8

Partial preview of the text

Download Matlab and Image Processing Toolbox: Overview and Data Types - Prof. Shawn Newsam and more Lab Reports Computer Science in PDF only on Docsity!

CSE

Fall 2008

Lecture 5

Matlab and the Image Processing

Toolbox (continued)

Today

• Matlab and the Image Processing Toolbox

continued

Assignments

• Lab #1 due Wed. 9/24 by midnight

Matlab overview

• intro.m

Matlab overview – data types

• Data types

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com

Digital Image Processing Using MATLABDigital Image Processing Using MATLAB®®

Matlab overview – data types

• Converting between data types

– Ex: a = uint8(b)

• Double->uint

– Values < 0 mapped to 0

– Values > 255 mapped to 255

– Fractions discarded

• Be aware of this!

Matlab overview - vectors

• 1xN dimensional array is called a row vector

• Index of first element is 1 (not 0)

• Initialize using square brackets

v = [1 3 5 7 9] v = 1 3 5 7 9

v(2) ans = 3

Matlab overview - vectors

• Convert row vector to column vector and

vice versa using transpose operator (‘)

w = v'

w =

1 3 5 7 9

Matlab overview - vectors

• To access blocks of elements, use colon (:)

notation

v(1:3) ans =

1 3 5

v(3:end) ans =

5 7 9

Matlab overview - vectors

• Indexing not restricted to contiguous elements

v(1:2:5) ans =

1 5 9

v(5:-2:1) ans =

9 5 1

Matlab overview - vectors

• Colon by itself returns column vector

v(:)

ans =

1 3 5 7 9

Matlab overview - vectors

• Can index into a vector using a vector

v([1 4 5])

ans =

1 7 9

Matlab overview - matrices

• Can use comparison operators to index

>> A(B==0)

ans =

3 6 9

E = (B==0) E = 0 0 1 0 0 1 0 0 1 whos E Name Size Bytes Class E 3x3 72 double array (logical) Grand total is 9 elements using 72 bytes

Matlab overview - matrices

• Use find function to determine indices of

values satisfying condition

[i,j] = find(B~=0); i' ans =

1 2 3 1 2 3

j' ans =

1 1 1 2 2 2

Matlab overview - matrices

• Use ndims to find dimension

d = ndims(A)

d =

2

Matlab overview - matrices

• Use size to find sizes of dimensions

size(T2) ans =

2 3

size(T2,1) ans =

2

size(T2,2) ans =

3

Matlab overview - matrices

• Can specify dimension for many functions

sum(T2) ans = 5 7 9 sum(T2,1) ans =

5 7 9

sum(T2,2) ans = 6 15

Matlab overview - matrices

• Can recurse to compute over all dimensions

sum(sum(T2))

ans =

21

Matlab overview – standard arrays

  • zeros(M,N) -> MxN matrix of zeros of type double
  • ones(M,N) -> MxN matrix of ones of type double
  • rand(M,N) -> MxN matrix of uniformly distributed random numbers in the interval [0,1]
  • randn(M,N) -> MxN matrix of normally (Gaussian) distributed random numbers with mean 0 and variance 1

Matlab overview – standard arrays

A = 5*ones(3,3) A =

5 5 5 5 5 5 5 5 5

B = rand(2,4)

B =

0.9501 0.6068 0.8913 0. 0.2311 0.4860 0.7621 0.

Matlab overview - .m file programming

Operators

• 3 types of operators

– Arithmetic operators that perform numeric

computations

– Relational operators that compare operands

quantitatively

– Logical operators that perform the functions

AND, OR, and NOT

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com

Digital Image Processing Using MATLAB Digital Image Processing Using MATLAB®®

Arithmetic operators I

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com

Digital Image Processing Using MATLABDigital Image Processing Using MATLAB®®

Arithmetic operators II

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com

Digital Image Processing Using MATLAB Digital Image Processing Using MATLAB®®

Relational operators

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com

Digital Image Processing Using MATLABDigital Image Processing Using MATLAB®®

Flow control statements

• Preallocating arrays: use zeros() for example

Matlab overview - .m file programming

Code optimization – preallocating arrays

f ( ) x = A sin( x / 2 π ) for x = 0,1, … M − 1

f = zeros(M,1); % preallocate the array

for x=1:M f(x) = A * sin((x-1)/(2*pi)); end

for x=1:M f(x) = A * sin((x-1)/(2*pi)); end

or

• sin_no_preallocate.m

• sin_preallocate.m

Matlab overview - .m file programming

Code optimization – preallocating arrays

• Preallocating arrays

– Improves code execution time

– Helps reduce memory fragmentation

Matlab overview - .m file programming

Code optimization – preallocating arrays

• Vectorizing loops: converting for and while

loops to equivalent vector or matrix operations.

Matlab overview - .m file programming

Code optimization – vectorizing loops

f ( ) x = A sin( x / 2 π) for x = 0,1, … M − 1

for x=1:M f(x) = A * sin((x-1)/(2*pi)); end

x = 0:M-1; f = A * sin(x/(2*pi));

or

• sin_loop.m

• sin_vectorize.m

Matlab overview - .m file programming

Code optimization

  • disp(argument)

Matlab overview - .m file programming

Interactive output I

>> A = [1 2; 3 4];

disp(A); 1 2 3 4

sc = 'Digital Image Processing'; disp(sc); Digital Image Processing

  • fprintf(1,’format’,argument);

Matlab overview - .m file programming

Interactive output II

x = 5;

fprintf(1,'x = %.5f',x); x = 5.

fprintf(1,'x = %e',x); x = 5.000000e+

  • t = input(‘message’, ‘s’)

Matlab overview - .m file programming

Interactive input

t = input('Enter you data: ','s'); Enter you data: 1,2, disp(t) 1,2, class(t) ans = char size(t) ans = 1 5 n=str2num(t) n = 1 2 4 class(n) ans = double size(n) ans = 1 3

  • help plot
  • plot_example.m

Matlab overview - .m file programming

Plotting