Anda di halaman 1dari 61

MATLAB for Image Processing

Yancan Huang

Outline
Introduction to MATLAB
Basics E Examples l

Image Processing with MATLAB g g


Basics Examples

What is MATLAB?
MATLAB = M t i L b t Matrix Laboratory MATLAB is a high-level lang age and high le el language interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++ and Fortran. (www.mathworks.com) (www mathworks com) MATLAB is an interactive, interpreted language interactive that is designed for fast numerical matrix calculations

MATLAB Basics
Where to get help?
After MATLABs prompt, type:
h l helpwin, helpdesk, l kf d help, h l i h l d k lookfor, demos

O the Web On h W b
http://www.mathworks.com/support

The MATLAB Environment


MATLAB h th has the following basic window components:
Work-space
Displays all the defined variables

Command Window
To execute commands in the i th MATLAB environment

Command History
Displays record of the commands used

Command Window
Cut and paste operations ease the repetition of tasks y Use up-arrow key to repeat commands (alternative to command history)

Launch Pad
The launch window allows you to quickly select among various MATLAB components and toolboxes

MATLAB Workspace
Workspace of MATLAB shows all the defined variables along with their order, memory space they occupy, and the class to which they belong

Current Directory
Provides quick access to all files available in your path

Different Views

The MATLAB environment could be viewed in various ways This could be accomplished by the VIEW pull-down menu as shown in the slide above

MATLAB Help
MATLAB Help is an H l i extremely powerful assistance to learn MATLAB The Help not only contains th theoretical t i the th ti l background but also s o s e demos o shows the de os for implementation The MATLAB Help could be b opened b using th d by i the HELP pull-down menu

MATLAB Help (Contd ) (Contd..)


Any command description can be found by typing the command in the search field As shown above the command to take square root (sqrt) is searched We can also utilize the MATLAB help from the command window as shown in the left

More about Workspace


who, whos current variables in p workspace save save workspace variables to *.mat file load load variables from *.mat file clear all clear workspace variables

Matrices in MATLAB
Matrix is a main MATLABs data type How to build a matrix?
A=[1 2 3; 4 5 6; 7 8 9]; C t matrix A with size 3*3 Creates ti ith i

Special matrices: p
zeros(n,m), ones(n,m), eye(n,m)

Basic Operations on Matrices


All the operators in MATLAB defined on , , , , , q , , , matrices: +, -, *, /, ^, sqrt, sin, cos, etc. Element wise operators defined with preceding dot: .*, ./, .^ * / ^ size(A) size vector sum(A) columns sums vector s m(s m(A)) s m of all the elements sum(sum(A)) sum

Logical Conditions
==, < > (not equal)~=, (not)~ <, >, equal) (not) find(condition) Return indexes of As e e e ts that sat s es t e co d t o elements t at satisfies the condition

Logical Conditions (cont ) (cont.)


Example: >>A=[1 2;3 4] I=find(A<4) >>A [1 4], I find(A<4)
A=
12 34

I I=
1 2 3

Flow Control
MATLAB has five flow control constructs:
if statements switch statements for loops while loops break statements

if
IF statement condition
The general form of the IF statement is g
IF expression
statements

ELSEIF expression
statements

ELSE
statements

END

if (cont.) (cont )
Example:
if I==J
A(I,J)=2

elseif abs(I-J)==1
A(I,J)=-1

else
A(I,J)=0

end

switch
SWITCH S it h among several cases b Switch l based d on expression Th general f The l form of SWITCH statement is: f t t ti
SWITCH switch_expr
CASE case_expr, case e pr
statement, , statement

CASE {case_expr1, case_expr2, case_expr3, }


statement, , statement

OTHERWISE
statement, , statement

END

switch (cont.) (cont )


Note:
Only the statements between the matching y g CASE and the next CASE, OTHERWISE, or END are executed Unlike C, the SWITCH statement does not fall through (so BREAKs are unnecessary)

for
FOR Repeat statements a specific number of times Th general f The l form of a FOR statement is: f t t ti
FOR variable=expr p
Statement

END

for (cont.) (cont )


Example:
FOR I=1:N
FOR J=1:N
A(I,J)=1/(I+J-1) ( ) ( )

END

END

while
WHILE Repeat statements an indefinite number of times The general form of a WHILE statement is:
WHILE expression i
statements

END

while (cont ) (cont.)


Example:
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;

end

Scripts and Functions


There are two kinds of M-files:
Scripts, which do not accept input arguments p , p p g or return output arguments. They operate on data in the workspace p Functions which can accept input arguments Functions, and return output arguments. Internal variables are local to the function

Functions in MATLAB
FUNCTION Add new function New functions may be added to MATLABs MATLAB vocabulary if th are b l they expressed in terms of other existing functions

Functions in MATLAB (cont ) (cont.)


E Example: l
A file called STAT.M:
function [mean, stdev]=stat(x) %STAT Interesting statistics. n=length(x); mean=sum(x)/n; stdev=sqrt(sum((x-mean).^2)/n); td t( (( ) ^2)/ )

Defines a new function called STAT that calculates th mean and standard d i ti of l l t the d t d d deviation f a vector

Visualization and Graphics


plot(x,y),plot(x,sin(x)) plot 1-D function g g ( ) p g figure, figure(k) open a new figure hold on, hold off refreshing mesh(x_ax,y_ax,z_mat) mesh(x ax y ax z mat) view surface contour(z_mat) view z as topo map subplot(3,1,2) locate several plots in figure axis([xmin xmax ymin ymax]) change axes title(figure titile) add title to figure

Outline
Introduction to MATLAB
Basics E Examples l

Image Processing with MATLAB g g


Basics Examples

Objects in MATLAB
N = 5 % a scalar v = [1 0 0] % a row vector v = [1;2;3] % a column vector v = v' % transpose a vector (row to column or column to row) v = [1:.5:3] % a vector in a specified range v = pi*[-4:4]/4 % [start:end] or [start:stepsize:end] v = [] % empty vector m = [1 2 3; 4 5 6] % a matrix [ ; ] m = zeros(2,3) % a matrix of zeros, (ROWS, COLS ) v = ones(1 3) % a matrix of ones ones(1,3) m = eye(3) % identity matrix v = rand(3,1) % random matrix with values in [0,1] (see also randn)

Objects in MATLAB (cont.) (cont )


load matrix_data % read data from a file called matrix data matrix data matrix_data m = [1 2 3; 4 5 6] m(1,3) % access a matrix element, matrix(row#, column#) (1 3) ti l t ti ( # l #) m(2,:) % access a matrix row (2nd row) m(:,1) % access a matrix column (1st column) size(m) % size of a matrix size(m,1) % number rows size(m,2) % number of columns ( , ) m1 = zeros(size(m)) % create a new matrix with size of m who % list of variables whos % list/size/type of variables

Simple operations on vectors and matrices


Pointwise (element by element) Operations: % addition of vectors/matrices and multiplication by a scalar are done "element by element"
a = [1 2 3 4]; % vector 2 * a % scalar multiplication a / 4 % scalar multiplication b = [5 6 7 8]; % vector a + b % pointwise vector addition a - b % pointwise vector addition a .^ 2 % pointise vector squaring (note .) . a .* b % pointwise vector multiply (note .) a ./ b % pointwise vector divide (note .) log( [ 2 3 4] ) % pointwise arithmetic operation g( [1 ] p p round( [1.5 2; 2.2 3.1] ) % pointwise arithmetic operation

Vector Operations (no for loops needed) % Built in matlab functions Built-in operate on vectors, if a matrix is given, % then the function operates on each column of the matrix a = [1 4 6 3] % vector sum(a) % sum of vector elements mean(a) % mean of vector elements var(a) % variance std(a) % standard deviation max(a) % maximum a = [1 2 3 4 5 6] % matrix 3; ti a(:) % vectorized version of the matrix mean(a) % mean of each column max(a) % max of each column max(max(a)) % to obtain max of matrix ( ( )) max(a(:)) % or...

Matrix Operations:
[1 2 3] * [4 5 6]' % row vector 1x3 times column vector 3x1, [ ] [ ] , results in single number, also known as dot product or inner product [1 2 3]' * [4 5 6] % column vector 3x1 times row vector 1x3, results in 3 3 matrix, also k lt i 3x3 ti l known as outer product t d t a = rand(3,2) % 3x2 matrix b = rand(2,4) % 2x4 matrix c = a * b % 3x4 matrix

Saving your Work


save mysession % creates mysession.mat with all variables save mysession a b % save only variables a and b clear all % clear all variables clear a b % clear variables a and b load mysession % load session a b

Relations and control statements


% Example: given a vector v create a new vector with values equal v, to v if they are greater than 0, and equal to 0 if they less than or equal to 0. v = [3 5 -2 5 -1 0] % 1: FOR LOOPS 2 1 u = zeros( size(v) ); % initialize for i = 1:size(v,2) % size(v,2) is the number of columns
if( v(i) > 0 )
u(i) = v(i);

end

end u v = [3 5 -2 5 -1 0] % 2: NO FOR LOOPS u2 = zeros( size(v) ); % initialize ind = find( v>0 ) % index into >0 elements u2(ind) = v( ind )

Creating functions using m files m-files


% Functions in matlab are written in m-files. Create a file called 'thres.m' In this file put the following 4 lines: ( function res = thres( v ) u = zeros( size(v) ); % initialize ind = find( v>0 ) % index into >0 elements u(ind) = v( ind ) v = [3 5 -2 5 -1 0] thres( v ) % call from command line

Plotting
x = [0 1 2 3 4]; % basic plotting plot( x ); plot( x, 2*x ); axis( [0 8 0 8] ); x = pi*[-24:24]/24; plot( x, sin(x) ); xlabel( 'radians' ); radians ylabel( 'sin value' ); title( 'dummy' ); gtext( 'put cursor where you want text and press mouse' ); figure; % multiple functions in separate graphs subplot( 1,2,1 ); plot( x, sin(x) ); x axis square; subplot( 1,2,2 ); plot( x, 2.*cos(x) ); axis square;

Plotting (cont ) (cont.)


figure; % multiple functions in single graph plot( x,sin(x) ); hold on; % hold on tells matlab to write on top plot ( 2 * l t (x, 2.*cos(x), ''--'' ); % of the current plot ( ) ) f th t l t legend( 'sin', 'cos' ); hold off; figure; % matrices as images m = rand(64,64); ( , ); imagesc(m) colormap gray; axis image axis off;

Outline
Introduction to MATLAB
Basics E Examples l

Image Processing with MATLAB g g


Basics Examples

What is the Image Processing Toolbox?


The Image Processing Toolbox is a collection of functions that extend the capability of the MATLAB numeric computing environment The toolbox supports a environment. wide range of image processing operations, including:
Geometric operations p Neighborhood and block operations Linear filtering and filter design Transforms Image analysis and enhancement Binary image operations Region of interest operations

Images in MATLAB
MATLAB can import/export i / several image formats
BMP (Microsoft Windows Bitmap) GIF (Graphics Interchange Files) HDF (Hierarchical Data Format) JPEG (Joint Photographic Experts Group) PCX (Paintbrush) PNG (Portable Network Graphics) TIFF (Tagged Image File Format) XWD (X Window Dump) MATLAB can also load raw-data or other types of image data

DatatypesinMATLAB
Double(64bitdoubleprecision floatingpoint) Single(32bitsingleprecision floatingpoint) Int32(32bitsignedinteger) I 16 (16 bi i d i Int16(16bitsignedinteger) ) Int8(8bitsignedinteger) Uint32(32bitunsignedinteger) Uint16(16bitunsignedinteger) Uint8(8bitunsignedinteger)

Images in MATLAB
Bi Binary i images : {0 1} {0,1} Intensity images : [0,1] or uint8, double etc. RGB images : m-by-n-by-3 m by n by 3 Indexed images : m-by-3 color map Multidimensional images m-by-n-by-p (p is the number of layers)

Image import and export


Read d i images i M l b R d and write i in Matlab
>> I=imread('cells.jpg'); >> imshow(I) >> size(I) ans = 479 600 3 (RGB image) >> Igrey=rgb2gray(I); >> imshow(Igrey) >> imwrite(lgrey, 'cell_gray.tif', 'tiff')

Alternatives to imshow
>>imagesc(I) >>imtool(I) i t l(I) >>image(I)

Images and Matrices


How to build a matrix (or image)? >> A = [ 1 2 3; 4 5 6; 7 8 9 ]; A= 1 2 3 4 5 6 7 8 9 >> B = zeros(3,3) B= 0 0 0 0 0 0 0 0 0 >> C = ones(3,3) (3 3) C= 1 1 1 1 1 1 1 1 1 >>imshow(A) (imshow(A,[]) to get automatic pixel range)

Images and Matrices


Accesing image elements (row (row, column) >> A(2,1) ans = 4 : can be used to extract a whole column or row >> A(:,2) ans = 2 5 8 or a part of a column or row >> A(1:2,2) ans = 2 5
X

A= 1 2 3 4 5 6 7 8 9

Image Arithmetic
Arithmetic operations such as addition, subtraction multiplication and addition subtraction, division can be applied to images in MATLAB +, -, *, / performs matrix operations
>> A+A ans = 2 8 14 >> A*A ans = 30 66 102 4 6 10 12 16 18 36 42 81 96 126 150

A= 1 2 4 5 7 8

3 6 9

To perform an elementwise operation use . (.*, ./, .*, .^ etc) >> A.*A A *A ans : 1 4 9 16 25 36 49 64 81

Image Display
image - create and display image object g p y g imagesc - scale and display as image imshow - display image colorbar - display colorbar getimage- get image data from axes truesize - adjust display size of image zoom - zoom in and zoom out of 2D plot

Image Conversion
gray2ind - i t 2i d intensity i it image t index i to i d image im2bw - image to binary im2double - image to double precision g g g im2uint8 - image to 8-bit unsigned integers im2uint16 - image to 16-bit unsigned integers ind2gray - indexed image to intensity image mat2gray - matrix to intensity image rgb2gray - RGB image to grayscale rgb2ind - RGB image to indexed image

Outline
Introduction to MATLAB
Basics E Examples l

Image Processing with MATLAB g g


Basics Examples

Example for working with Images (1/3)


[I,map]=imread('trees.tif'); % read a TIFF image [I ] i d('t tif') d i figure, imshow(I,map) % display it as indexed image I2=ind2gray(I,map); I2=ind2gray(I map); % convert it to grayscale figure colormap( gray ) colormap('gray') % use gray colormap imagesc(I2,[0 1]) % scale data to use full colormap for values between 0 and 1 axis('image') % make displayed aspect ratio proportional to image dimensions I=imread(moon.jpg'); % read a JPEG image into 3D array figure

Example for working with Images (2/3)


imshow(I) i h (I) rect=getrect; % select rectangle I2=imcrop(I,rect); I2=imcrop(I rect); % crop I2=rgb2gray(I2); % convert cropped image to grayscale imagesc(I2) % scale data to use full colormap between min and max values in I2 colormap('gray') p( g y ) colorbar % turn on color bar pixval % display pixel values interactively truesize % display at resolution of one screen pixel per image pixel

Example for working with Images (3/3)


truesize(2*size(I2)) % display at resolution of two screen pixels per image pixel I3 i I3=imresize(I2,0.5,'bil'); % resize b 50% using bili i (I2 0 5 'bil') i by i bilinear interpolation I3=imrotate(I2 45 'bil'); % rotate 45 degrees and crop to I3=imrotate(I2,45, bil ); original size I3=double(I2); % convert from uint8 to double to allow double, math operations imagesc(I3.^2) % display squared image (pixel-wise) imagesc(I3. 2) (pixel wise) imagesc(log(I3)) % display log of image

Performance Issues
The idea: MATLAB is
very fast on vector and matrix operations y p Correspondingly slow with loops

Try to avoid loops y p Try to vectorize your code

Vectorize Loops
Example
Given image matrix A and B with the same size (600*400). Blend these two images a=imread(test1.jpg'); i d(t t1 j ') b=imread(test2.jpg);

Poor Style y
tic% measure performance using stopwatch timer for i=1:size(a,1)
for j=1:size(a,2) j 1:size(a,2)
c(i,j)=(a(i,j)+b(i,j))/2;

end

end toc % Elapsed time is 3.814800 seconds.

Vectorize Loops (cont ) (cont.)


Example
Given image matrix A and B with the same size (600*400). Blend these two images a=imread(test1.jpg'); i d(t t1 j ') b=imread(test2.jpg);

Better Style
tic% measure performance using stopwatch timer c=(a+b)/2; toc % Elapsed time is 0.023389 seconds.

Computation time is almost 160 times faster

Another Example
T k Read i an RGB i Task: R d in image, convert t G t to Gray i image, improve the contrast and then save to the disk Code:
clear all; close all; clc; I=imread('campus.tif'); I=rgb2gray(I); imshow(I) () figure, imhist(I) % show the distribution of intensities I2=histeq(I) % to spread the intensity values over the full range of the image, i.e, histogram equalization figure, imshow(I2) figure, imhist(I2) imwrite(I2, 'campus2.png'); imfinfo('campus2.png'); imfinfo('campus2 png'); % return information about the image

Result of Previous Example

THE END
Thanks for Your Attention! Any Questions?

Anda mungkin juga menyukai