## A Brief Introduction to MATLAB

MATLAB is a large numerical and symbolic analysis package installed on the EWS system. You can access Matlab from the GUI of Linux by clicking "Applications" drop down menu, then "EWS software" followed by "Matlab".

## Image Processing Functions

To obtain a list of image processing commands available under MATLAB, typehelp imagesat the MATLAB prompt. Images are included with the image processing toolbox (IPT). Some image's names: "clown," "trees," and "forest."

To load an image, typeload image_namewhere image_name is the name of an image. This command creates the two variablesXandmap.Xis an array of values that indexes a color map contained inmap.

To display the images typeimshow(X,map)to display imageXwith colormapmap. To load 8-bit grayscale images not included in the toolkit (e.g. tank_01.raw, fn002.raw, etc.) type the following:fid = fopen('image_name','r');The first command assigns a file pointer to the image file "image_name." The second command reads the image from a file into an array

image = fread(fid, [xsz,ysz]);

image = image' / 255;imageof sizexszbyysz. The last command takes the transpose ofimageand scales it for display purposes.

Note: you may need to typetruesizeto display the image at its normal size (MATLAB often ignores an image's dimensions and uses some default size instead).

Finally, typeimshow(image,256)to display the image. The argument "256" tells Matlab to display the image with a grayscale map of 256 different gray scales.## How to Display a Video Sequence In Matlab?

Suppose you have the frames X_i where i = 1,2,..,N . Each of these frames are images themselves and should be of the same size. The assumtion is that all pixels within these frames are between 0 and 1 (normalized) and transposed so that imshow(X_i) works for each frame. Then there are 2 ways to display these frames as a video sequence## 1st Way

for i=1:1:N,Do processing to form the frame X_iend;

X(:,:,1,i) = X_i;

mov = immovie(X*256,gray(256));

movie(mov);

## 2nd Way

for i=1:1:N,Do processing to form the frame X_iend;

image(X_i);

mov(:,i) = getframe;

movie(mov);

## MATLAB Functions and Macros

MATLAB is a high level language and lets you write functions to make programming easier. A MATLAB file must have the file extentsion ".m" (e.g., my_program.m). Let us call such a file an "m-file". To use a MATLAB function that has been saved as an m-file, type the function name at the MATLAB prompt without the extension. For example, suppose we create an ASCII text file "fun.m" that contains the following function:function[test1, test2] = fun(a, b, c)To use "fun", type:

statement;

statement;

end[var1, var2] = fun(a1, a2, a3);where a1, a2, a3 are passed to the function "fun", and var1, var2 are values returned by the function.

An m-file does not need to contain a function. It could just contain a series of MATLAB commands that is executed when it is loaded.

MATLAB takes a _very_ long time to execute "for" loops. Try to avoid "for" loops by using MATLAB's built-in matrix functions; MATLAB is optimized for array processing. Take advantage of these functions to reduce your software development time.

Lab 6 p.3 - Coding

Last edit 26-Feb-07