Matlab • Scientific programming language specialized in CSED441:Introduction to Computer Vision (2015S) Lecture3: Matlab Tutorial Numerical computing and optimization Matrix & vector operations • Interface with other programming languages such as C/C++ Fortran • Having powerful toolboxes such as Bohyung Han CSE, POSTECH [email protected] Image processing and computer vision Optimization Statistics Parallel programming etc. • Able to build a standalone executable file 2 Variables Vectors ≫ x = 15+0.1 x = 15.1000 • Construction Defined with assignment operator (=) Used without declaration ≫ class(x) ans = double ≫ x = true x = 1 ≫ class(x) ans = logical ≫ x = x+1 ans = 2 ≫ class(x) ans = double • Type The type of variables can be changed at any time. Implicit type conversion occurs frequently. 3 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 ≫ x = [2.0 3.1 5.4] • Representation x = Square bracket [] Maybe separated by white space ( ) or comma (,) Apostrophe: transpose operator 2.0000 3.1000 5.4000 ≫ x = [2.0 3.1 5.4]’ x = 2.1000 3.1000 5.4000 ≫ y = [1 ; 2 ; 3]; ≫ y(2:3) y = 2 3 ≫ x’*y = ans = 24.4000 • Accessing elements Parenthesis () • Accessing sub‐vector Parenthesis (indices) 4 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Matrices Built‐in Functions ≫ A = [3 4 5 ; 1 2 3] • Representation • Basic built‐in functions A = Square bracket [] Changing rows by semi‐colon (;) Maybe separated by white space ( ) or comma (,) Apostrophe: transpose operator Size, length Norm Rank Determinant Inverse matrix Eigen‐decomposition Singular value decomposition (SVD) Condition number Pseudo random number generator Many others 3 4 5 1 2 3 ≫ y = [2.0 3.1 5.4]’; ≫ z = A*y z = 45.4000 24.4000 ≫ A(1,2) ans = 4 ≫ A(1:2,[1 3]) ans = 3 5 1 3 • Accessing elements Parenthesis () • Accessing sub‐matrix Parenthesis & comma (row_indices, col_indices) 5 Size, length Norm Rank Determinant Inverse matrix Eigen‐decomposition Singular value decomposition (SVD) Condition number Pseudo random number generator Many others 12 ≫ A = [3 0 1 ; 0 5 3 ; 1 3 9]; ≫ r = rank(A) r = 3 ≫ [V, D] = eig(A) V = -0.8512 0.5122 0.1141 -0.4109 -0.7858 0.4623 0.3265 0.3466 0.8794 D = 2.6165 0 0 0 3.6767 0 0 0 10.7068 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Conditions & Loops ≫ A = [3 0 1 ; 0 5 3 ; 1 3 9]; ≫ [r, c] = size(A) r = 3 c = 3 ≫ det(A) ans = 103 ≫ inv_A = inv(A) inv_A = 0.3495 0.0291 -0.0485 0.0291 0.2524 -0.0874 -0.0485 -0.0874 0.1456 7 n = 6 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Built‐in Functions • Basic built‐in functions ≫ n = norm([3 4 5], 1) CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 • • • • ≫ ≫ ≫ ≫ ≫ if / elseif / else switch for loop while loop a = rand(100,1); b = rand(100,1); c = 0; for i=1:100, c = c+a(i)*b(i); end ≫ ≫ c = 0; ≫ cnt = 0; ≫ ≫ while cnt < 100 cnt = cnt+1; c = c+a(cnt)*b(cnt); end • Almost similar to C/C++ 8 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Help • Manual for each function Plotting & Graphic ≫ help det Function prototype Usages & examples Related functions References Link to Matlab document (very powerful and containing rich description) DET Determinant. DET(X) is the determinan t of the square matrix X. Use COND instead of DET to test for matrix singula rity. See also cond. Reference page in Help b rowser doc det 9 ≫ [X,Y] = meshgrid(-10:0.25:10,-10:0.25:10); ≫ ≫ ≫ ≫ f = sinc(sqrt((X/pi).^2+(Y/pi).^2)); surf(X,Y,f); axis([-10 10 -10 10 -0.3 1]); xlabel('{\bfx}‘); ylabel('{\bfy}'); zlabel('{\bfsinc} ({\bfR})') 10 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Image Processing Toolbox • Image I/O imread Imwrite • Image manipulation imtransform imresize imfilter • Image display imshow • Image processing ≫ img = imread(‘test.jpg’); ≫ size(img) ans = 240 320 3 ≫ imwrite(img, ‘out.png’, ‘png’); ≫ rimg = imresize(img, 0.5); ≫ size(img) ans = 120 160 3 ≫ imshow(rimg); ≫ gimg = rgb2gray(img); ≫ edge(gimg); edge imerode, imdilate: morphological operators 11 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Script and Function • Matlab source code File extension: *.m Script or function mscript1.m c = 3+4; d = 3-4; mathop.m [c,d] = function mathop(a,b) c = a+b; d = a-b; 12 ≫ mscript1 c = 7 d = -1 ≫ [p,q] = mathop(3,4) p = 7 q = -1 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Matlab and C/C++ Important Tip 1 • Matlab compatibility with C/C++ • Use loops as LEAST as possible You can call C/C++ functions from Matlab Advanced resource: http://classes.soe.ucsc.edu/ee264/Fall11/cmex.pdf hello.c #include "mex.h" /* Always include this */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mexPrintf("Hello, world!\n"); return; } ≫ mex hello.c ≫ hello Hello, world! 13 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Important Tip 2 Loops are particularly slow in Matlab. ≫ ≫ ≫ ≫ a = rand(10000000,1); b = rand(10000000,1); c1 = 0; tic; for i=1:10000000, c1 = c1+a(i)*b(i); end; toc; Elapsed time is 8.096525 seconds. ≫ tic; c2 = a’*b; toc; Elapsed time is 0.026638 seconds. 14 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Important Tip 3 • The orders and kinds of computations matter. • Do NOT to increase the size of a matrix or vector inside loop. Do matrix‐vector multiplication first. Use Gaussian elimination instead of inverse matrix. Matlab can allocate only consecutive memory. ≫ A = rand(3000,3000); ≫ B = rand(3000,3000); ≫ c = rand(3000,1); ≫ tic; A*B*c; toc; Elapsed time is 7.645052 ≫ tic; A*(B*c); toc; Elapsed time is 0.024930 ≫tic; inv(A)*c; toc; Elapsed time is 9.363578 ≫ tic; A\c; toc; Elapsed time is 2.992717 ≫ a = rand(1,100000); ≫ tic; for i=1:100000, b(i) = a(i); end; toc; Elapsed time is 15.336680 seconds. ≫ c = zeros(1,100000); ≫ tic; for i=1:100000, c(i) = a(i); end; toc; Elapsed time is 0.082945 seconds. 15 seconds. seconds. seconds. seconds. CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 16 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 Octave • Matlab‐like programming language • Most Matlab programs are portable to Octave. • Free (http://www.gnu.org/software/octave/) 17 CSED441: Introduction to Computer Vision by Prof. Bohyung Han, Spring 2015 18
© Copyright 2024