130 likes | 278 Views
6.S093: Visual Recognition through Machine Learning Competition. MATLAB tutorial. Getting Started. To get MATLAB student version, go to http://ist.mit.edu/matlab/all/student (or google MIT MATLAB) On Athena (athena.dialup.mit.edu), » add matlab » matlab &. MATLAB. Very easy to learn.
E N D
6.S093: Visual Recognition through Machine Learning Competition MATLAB tutorial
Getting Started • To get MATLAB student version, go tohttp://ist.mit.edu/matlab/all/student(or google MIT MATLAB) • On Athena (athena.dialup.mit.edu), » add matlab » matlab &
MATLAB • Very easy to learn. • Takes some careful attention for a fast code. • Lots of great documentation • Confused? • » help command • Go to mathworks documentation websitehttp://www.mathworks.com/help/matlab/
First Exercise • Print [Hello World] • Multiple solutions… • Easy way» disp ‘Hello World’; • Traditional way (formatting is supported)» fprintf(‘Hello World\n’);
Variable types and assignment • By default, • Number» tmp1 = 3; % 8-bytes double • Char» tmp2 = ‘a’; % 2-bytes char • You need to specify when it is not double. » tmp3 = single(3); % 4-bytes float » tmp4 = int16(3); % 2-bytes int16 » tmp5 = false; % 1-byte logical » whos % lists defined variables
Matrix • MATLAB uses Matrix (rather than array). • Matrix uses (row, column) » a = [1 2; 3 4]; » whos Name Size Bytes Class Attributes a 2x2 32 double » a(2, 1:2)ans = 3 4
Vectorization • How do you compute a dot product? • a = 1:1e8; • b = cos(1:1e8); % a vector of cos of 1:1e8 • c = 0; • for i=1:length(a) % for-loop from 1 through length(a) • c = c + a(i) * b(i); • end
Vectorization • How do you compute a dot product? • MATLAB is optimized for matrix/vector operations • First solution takes: 2.02s • Second solution takes: 0.16s • a = 1:1e8; • b = cos(1:1e8); % a vector of cos of 1:1e8 • c = 0; • for i=1:length(a) % for-loop from 1 through length(a) • c = c + a(i) * b(i); • end • d = a*b’; % matrix multiplication
Image Functions • Imagesc • imread • rgb2gray • edge • figure • imresize • imrotate • imfilter
Other useful functions • Operation: repmat, bsxfun, find • Data structure: cell array, structure • Matrix operation: inv, eig, svd • Statistical function: hist, rand, randn • Function: nargin, nargout • Debug: keyboard, dbstop if error, dbstack • Performance: tic/toc, profile
Image comparison • Compare these 3 images using the average pixel value difference • Step1: resize image (imresize) • Step2: subtract im1 from im2 and im3 • Step3: take an average of absolute difference • http://viscomp.csail.mit.edu/resource/matlab/image_compare.m
Exercise: Image blurring • Optimize the provided blurring code • Our blurring algorithm: for each pixel, assign the average value of neighborhood pixels. • http://viscomp.csail.mit.edu/resource/matlab/image_blur.m
Summary • Great documentation • Vectorization! = AVOID for-loop! • Matrix is (row, column) rather than (x, y). • Image has [height] x [width]! • [1..n] rather than [0..n-1] • LOTS of optimized functions available