MATLAB Tutorial
MATLAB Tutorial
Outline
Introduction to MATLAB
Basics & Examples
What is MATLAB?
MATLAB = Matrix Laboratory MATLAB is a high-level language and 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)
MATLAB is an interactive, interpreted language that is designed for fast numerical matrix calculations
Command Window
> To execute commands in the MATLAB environment
Command History
> Displays record of the commands used
MATLAB Help
MATLAB Help is an extremely powerful assistance to learning MATLAB
Help not only contains the theoretical background, but also shows demos for implementation
MATLAB Help can be opened by using the HELP pull-down menu
Matrices in MATLAB
Matrix is the main MATLAB data type How to build a matrix?
A=[1 2 3; 4 5 6; 7 8 9]; Creates matrix A of size 3 x 3
Special matrices:
zeros(n,m), ones(n,m), eye(n,m), rand(), randn()
Logical Operators
==, <, >, (not equal) ~=, (not) ~ find(condition) Returns indexes of As elements that satisfy the condition
Idx=
3 4 6
Flow Control
MATLAB has five flow control constructs:
if statement switch statement for loop while loop break statement
if
IF statement condition
The general form of the IF statement is
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
CODE
switch
SWITCH Switch among several cases based on expression The general form of SWITCH statement is:
SWITCH switch_expr
CASE case_expr,
statement, , statement
OTHERWISE
statement, , statement
END
switch (cont.)
Note:
Only the statements between the matching CASE and the next CASE, OTHERWISE, or END are executed Unlike C, the SWITCH statement does not fall through (so BREAKs are unnecessary)
CODE
for
FOR repeats statements a specific number of times The general form of a FOR statement is:
FOR variable=expr
statements
END
CODE
while
WHILE repeats statements an indefinite number of times The general form of a WHILE statement is:
WHILE expression
statements
END
CODE
CODE
Outline
Introduction to MATLAB
Basics & Examples
Images in MATLAB
MATLAB can import/export 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) raw-data and other types of image data
Images in MATLAB
Binary images : {0,1} Intensity images : [0,1] or uint8, double etc. RGB images : m n 3 Multidimensional images: m n p (p is the number of layers)
row = 256; col = 256; img = zeros(row, col); img(100:105, :) = 0.5; img(:, 100:105) = 1; figure; imshow(img);
row = 256; col = 256; img = rand(row, col); img = round(img); figure; imshow(img);
Image Display
image - create and display image object 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 - intensity image to index image im2bw - image to binary im2double - image to double precision 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
Image Operations
RGB image to gray image Image resize Image crop Image rotate Image histogram Image histogram equalization Image DCT/IDCT Convolution
- CODE
Outline
Introduction to MATLAB
Basics & Examples
Video
Performance Issues
The idea: MATLAB is
very fast on vector and matrix operations Correspondingly slow with loops
Vectorize Loops
Example Given image matrices, A and B, of the same size (540*380), blend these two images apple = imread(apple.jpg'); orange = imread(orange.jpg); Poor Style
% measure performance using stopwatch timer tic for i = 1 : size(apple, 1) for j = 1 : size(apple, 2) for k = 1 : size(apple, 3) output(i, j, k) = (apple(i, j, k) + orange(i, j, k))/2; end end end toc
Better Style
tic % measure performance using stopwatch timer Output = (apple + orange)/2; toc
Computation is faster!
THE END
Thanks for your attention! Questions?