2/12/2010 Outline • Introduction to MATLAB MATLAB for Image Processing – Basics & Examples • Image Processing with MATLAB CS638-1 TA: Tuo Wang – Basics & Examples B i & E l tuowang@cs.wisc.edu Feb 12 th , 2010 What is MATLAB? The MATLAB Environment • MATLAB window • MATLAB = Matrix Laboratory components: p Workspace • “MATLAB is a high-level language and > Displays all the defined interactive environment that enables you to variables perform computationally intensive tasks faster Command Window than with traditional programming languages > To execute commands such as C, C++ and Fortran.” in the MATLAB environment (www.mathworks.com) Command History Command History > Displays record of the commands used • MATLAB is an interactive, interpreted language File Editor Window that is designed for fast numerical matrix > Define your functions calculations 1
2/12/2010 MATLAB Help MATLAB Help (cont.) • Any command description • MATLAB Help is an can be found by typing extremely powerful y p the command in the h d i h assistance to learning search field MATLAB • Help not only contains the • As shown above, the theoretical background, command to take square but also shows demos for root ( sqrt ) is searched implementation implementation • We can also utilize • MATLAB Help can be MATLAB Help from the opened by using the command window as HELP pull-down menu shown More about the Workspace Matrices in MATLAB • who , whos – current variables in the • Matrix is the main MATLAB data type workspace k • How to build a matrix? • save – save workspace variables to *.mat – A=[1 2 3; 4 5 6; 7 8 9]; file – Creates matrix A of size 3 x 3 • load – load variables from *.mat file • Special matrices: • clear – clear workspace variables • clear workspace variables l – zeros(n,m), ones(n,m), eye(n,m), ( ) ( ) ( ) rand(), randn() - CODE 2
2/12/2010 Basic Operations on Matrices Variable Name in Matlab • All operators in MATLAB are defined on • Variable naming rules matrices matrices: +, -, *, /, ^, sqrt, + * / ^ t - must be unique in the first 63 characters sin, cos , etc. - must begin with a letter - may not contain blank spaces or other types of punctuation • Element-wise operators defined with a - may contain any combination of letters, digits, and preceding dot: .*, ./, .^ underscores • size(A) – size vector size vector - are case-sensitive size(A) - should not use Matlab keyword • sum(A) – columns sums vector • Pre-defined variable names • sum(sum(A)) – sum of all the elements • pi - CODE Logical Operators Logical Operators (cont.) • Example: • ==, <, >, (not equal) ~= , (not) ~ >>A=[7 3 5; 6 2 1], Idx=find(A<4) A= 7 3 5 • find(‘condition’) – Returns indexes 6 2 1 of A’s elements that satisfy the condition Idx= 3 4 6 3
2/12/2010 Flow Control if • MATLAB has five flow control constructs: • IF statement condition – if statement – The general form of the IF statement is – switch statement IF expression statements – for loop ELSEIF expression – while loop statements – break statement break statement ELSE ELSE statements END • CODE switch switch (cont.) • SWITCH – Switch among several cases based • Note: on expression on expression – Only the statements between the matching • The general form of SWITCH statement is: CASE and the next CASE , OTHERWISE , or END SWITCH switch_expr are executed CASE case_expr, – Unlike C, the SWITCH statement does not fall statement, …, statement CASE {case_expr1, case_expr2, case_expr3, …} through (so BREAK s are unnecessary) statement, …, statement … OTHERWISE • CODE statement, …, statement END 4
2/12/2010 for while • FOR repeats statements a specific • WHILE repeats statements an indefinite number of times b f ti number of times b f ti • The general form of a WHILE statement is: • The general form of a FOR statement is: WHILE expression statements FOR variable=expr END statements statements END • CODE • CODE Scripts and Functions Functions in MATLAB (cont.) • Example: • There are two kinds of M-files: – A file called STAT M: A file called STAT.M: function [mean, stdev]=stat(x) – Scripts, which do not accept input arguments %STAT Interesting statistics. or return output arguments. They operate on n=length(x); mean=sum(x)/n; data in the workspace stdev=sqrt(sum((x-mean).^2)/n); – Defines a new function called STAT that calculates D fi f ti ll d STAT th t l l t – Functions, which can accept input arguments the mean and standard deviation of a vector. Function name and file name should be the SAME! and return output arguments. Internal – CODE variables are local to the function 5
2/12/2010 Visualization and Graphics Saving your Work • save mysession • plot(x,y),plot(x,sin(x)) – plot 1D function % creates mysession.mat with all variables y • figure • figure, figure(k) – open a new figure figure(k) – open a new figure • save mysession a b • hold on, hold off – refreshing % save only variables a and b • axis([xmin xmax ymin ymax]) – change axes • clear all • title(‘figure titile’) – add title to figure % clear all variables • mesh(x_ax,y_ax,z_mat) – view surface • clear a b • contour(z_mat) – view z as topo map % clear variables a and b • subplot(3,1,2) – locate several plots in figure • load mysession - CODE and Debug CODE % load session Outline What is the Image Processing Toolbox? • The Image Processing Toolbox is a collection of functions that extend the capabilities of the MATLAB s functions that extend the capabilities of the MATLAB’s • Introduction to MATLAB numeric computing environment. The toolbox supports a wide range of image processing operations, including: – Basics & Examples – Geometric operations – Neighborhood and block operations • Image Processing with MATLAB – Linear filtering and filter design – Transforms – Basics & Examples B i & E l – Image analysis and enhancement – Binary image operations – Region of interest operations 6
2/12/2010 Images in MATLAB Images in MATLAB • MATLAB can import/export • Data types in MATLAB • Binary images : {0,1} several image formats: – Double (64-bit double-precision • Intensity images : [0,1] or uint8 , double etc. y g [ ] floating point) – BMP (Microsoft Windows Bitmap) • RGB images : m × n × 3 – Single (32-bit single-precision – GIF (Graphics Interchange Files) • Multidimensional images: m × n × p (p is the number of layers) floating point) – HDF (Hierarchical Data Format) – Int32 (32-bit signed integer) – JPEG (Joint Photographic – Int16 (16-bit signed integer) Experts Group) – Int8 (8-bit signed integer) – PCX (Paintbrush) – Uint32 (32-bit unsigned integer) – PNG (Portable Network Graphics) – Uint16 (16-bit unsigned integer) – TIFF (Tagged Image File Format) – Uint8 (8-bit unsigned integer) – XWD (X Window Dump) – raw-data and other types of image data Image Import and Export Images and Matrices • Read and write images in Matlab [0, 0] How to build a matrix img imread( apple.jpg ); img = imread('apple.jpg'); (or image)? dim = size(img); o Intensity Image: figure; Row 1 to 256 imshow(img); row = 256; imwrite(img, 'output.bmp', 'bmp'); col = 256; img = zeros(row, col); • Alternatives to imshow Alternatives to imshow i img(100:105, :) = 0.5; (100 105 ) 0 5 imagesc(I) img(:, 100:105) = 1; imtool(I) figure; o image(I) imshow(img); Column 1 to 256 [256, 256] 7
2/12/2010 Images and Matrices Image Display • image - create and display image object Binary Image: • imagesc - scale and display as image l d di l i i row = 256; • imshow - display image col = 256; img = rand(row, • colorbar - display colorbar col); • getimage - get image data from axes img = round(img); • truesize - adjust display size of image j p y g figure; imshow(img); • zoom - zoom in and zoom out of 2D plot Image Conversion Image Operations • gray2ind - intensity image to index image • RGB image to gray image • im2bw • im2bw - image to binary image to binary • Image resize • Image resize • im2double - image to double precision • Image crop • im2uint8 - image to 8-bit unsigned integers • Image rotate • im2uint16 - image to 16-bit unsigned integers • Image histogram • ind2gray - indexed image to intensity image • Image histogram equalization • mat2gray - matrix to intensity image t i t i t it i • Image DCT/IDCT I DCT/IDCT t2 • rgb2gray - RGB image to grayscale • Convolution • rgb2ind - RGB image to indexed image - CODE 8
2/12/2010 Examples working with Images Outline (1/3) • Introduction to MATLAB – Basics & Examples Create AVI movie with a series images • Image Processing with MATLAB Video – Basics & Examples Examples working with Images Examples working with Images (2/3) (3/3) Blending two images Sobel descriptor to detect object edge Sobel descriptor to detect object edge 9
Recommend
More recommend