Introduction to Matlab for Computer Vision

By David Lowe, Jan. 2014

To start Matlab in Unix use one of the following:

matlab &            % This starts the standard matlab desktop
                    % The optional "&" spins it off as separate process.

matlab -nodesktop   % Command line interface, useful for remote access
                    %   or running inside Emacs
quit                % Quits command-line interface
Warning: expect Matlab to take some time to start as its a large system.

Matlab's getting started tutorial

If you are a beginner with Matlab, first start Matlab (the desktop version), and select "MATLAB help" under the Help menu. Go through all the sections under "Getting started." Try typing and executing the examples in Matlab as you read to see how they work. This will take several hours, but provides an excellent first introduction. You can skip some of the details, such as details of plotting, but you should know where to go back for help if you need it later.

You can also read the same documentation from the Matlab documentation site but it is much better to read it while you can execute the examples.

Other ways to get online help:

   help function    % Gives help on function
   doc function     % Jumps to help documentation on function
   type function    % Shows source code for function

Quick review

Once you have finished the Matlab tutorial, the following can be cut-and-pasted into Matlab to remind you of their function:
% Entering matrices
A = [ 1, 2, 3 ]         % produces a row vector
C = [ 1 2 3 ]' 	        % commas are redundant, "'" - operator gives transpose
1:10			% a:b is a short form for [ a, a + 1, ..., b ]
1:0.2:3			% a:i:b specifies use of an increment i

% Some special matrix building functions:
zeros(2, 3)		% Matrix of zeros
ones(4, 2)		% Ones
eye(5)			% Identity matrix
rand(10,2)		% 10x2 matrix of random numbers in range [0,1]

% Indexing matrix elements
D(3, 2)			% single elements are obtained in the obvious way
D(:, 2)			% colon gives an entire column...
D(1, :)			% or row
D(:)                    % appends all columns into a single vector (useful!)
D(2:4, 6:9)		% vectors can be used to index sub-matrices
D(1:2:end, :)           % select every second row ("end" is last element)

% Matrix operations
A = [1 2 3]; B = [1 0 -1];
A + B                   % Vector addition
A + 100			% Scalars can be added to each element
A * B'                  % Dot product
A' * B                  % Outer product is a matrix
A .* B                  % Use the "." to specify pointwise operations
[1:10] .^ 2		% Square each number from 1 to 10
D = rand(5,3)           % Create array of random numbers
sum(D)                  % Sum columns of D
sum(D(:))               % Sum all the elements in D
help elfun		% List of all elementary functions (exp,round,etc...)

% Plotting and Printing
X = 1:10;
Y = X .^ 2;
plot(X, Y);		% "plot" can plot 2-D data in many convenient ways
figure;                 % Start a new figure (otherwise previous is replaced)
plot(X, Y, 'r+');	% Uses red "+"s as markers
X = [-6:0.1:6]';        
Y = [sin(X), 2 * cos(X), 3 * abs(cos(X))];   % Plot 3 functions at once
plot(Y);
doc plot		% Look here for details

% Writing your own functions
% Using your favorite text editor, place the following 2 lines into a file
%   called test.m in current directory:
function r = test(m)
    r = [m m];         % Append two copies of the matrix m
% Now you can call the function from Matlab (it can use any *.m files in
%   the current directory or other directories you place in its path)
test([1 2; 3 4])

% If you edit a file using your own editor, Matlab may not notice the
%    change unless you enter the following command to reinitialize.
clear all;

Images in Matlab

We will use functions from the image-processing toolbox in Matlab as it provides simpler routines for displaying images, but all of the following could also be done in basic Matlab using other routines.

The "imread" function reads an image from a file. The file location is specified relative to current directory or by giving its full path. Matlab can read most common image file formats, such as tif, jpg, and ppm. The imread function returns the image in an 8-bit format. Most other Matlab commands require floats, so the first step is usually to convert to double.

im = imread('cameraman.tif');  % This image is already available in Matlab
im2 = im2double(im);           % Convert to double for processing
   im2 = rgb2gray(im2);        % Optional for color image: convert to grayscale
imshow(im2);                   % Display the image

im3 = im2 + 0.2;               % Make image brighter
imshow(im3);
imwrite(im3, 'bright.jpg');    % Writes to file (file extension gives format)

box = ones(5,5);               % Create simple 5x5 box filter
box = box / sum(box(:));       % Normalize filter elements to sum to 1
im4 = conv2(im2, box, 'same'); % Convolve with image and keep size 'same'
imshow(im4);                   % View blurred image