IVR Practical One: Introduction to Matlab (Week 2)

The purpose of this exercise is to introduce you to Matlab which is the main programming language you will be using in this course. Matlab is an interactive language for technical computing based on array manipulation. It has many useful built in functions, and can be used to write programs and interface to various systems.

There is substantial online information about Matlab. It would be worth spending some time outside the class exploring it:
http://www.mathworks.com/support/

You can use the software outside the class times, on most Dice machines. However, note that there are a limited number of licenses for Matlab, meaning only a limited number of people can use them at the same time. So please: don't use them during other lab times; don't use them if you just want to browse the helpfiles, but read the online manuals instead; and don't stay connected to them when you have stopped using them.

Introduction to Matlab

To start Matlab: with the graphical interface, from the shell type matlab &.

NOTE: If you receive a warning that the correct command should be matlab14 then you may need to tell the computer where in its filesystem it can find the matlab executable.
In the terminal window type export PATH=$PATH:/opt/matlab-R2014a/bin/. You should now be able to run matlab using matlab &.

You should see a graphic appear for a moment on your screen, and the following will appear in the command window:
>>

You can then type commands at the prompt. We suggest you run:
>>edit intro.m


to first see the code that is going to be run, and then input:
>> intro

to see what it does. Please go through and understand each line.

For a quick explanation of other basic features. If you want to go a bit further with this after the session, try:
>> helpdesk

and work through some of the material under 'Getting started'.

Basic Image Matrix Manipulation

For now, try loading an image from a file (you can use the file 'pout.tif' which comes with Matlab, or substitute another image file name for pout.tif in the command) by typing:
>> I = imread('pout.tif'); 

Note the semicolon! This stops matlab from printing out the value assigned to I, which is a large array of numbers that will take a while to print on the screen. Try:
>> help imread

to get more detail on what file formats are supported. You can display the image using:
>> imshow(I)

If you type:
>> whos

You will see that I is an uint8 array (an array of unsigned 8-bit integers) with the number of rows and columns. You can use matlab's standard matrix operations on this array, e.g. to select the top 50x50 pixels do:
>> i50 = I(1:50,1:50)

And try displaying this with imshow. Note, mathematical operators are not defined for uint8 variables. So, you convert the images by:
>> DI=double(I);

You can easily examine simple statistics using Matlab's inbuilt functions:
size(DI)

Note that multi-dimensional data (in this case 2D) is often operated in per-dimension:
mean(DI)

So, to get the mean across the whole image collapse the image into a single row:
mean(DI(:))

Try out simple operations such as transpose and flipping:
DI_modified = DI';
imagesc(DI_modified);
DI_modified = flipud(DI);
DI_modified = fliplr(DI);
plot(sort(DI(:))

It is common to use binary masks to manipulate only certain pixels e.g. this thresholding operation:
DI_mask = (DI > 145)
imagesc(DI_mask);

This applies the mask to the original image:
I_masked = uint8(DI_mask) .* I;
figure;
imshow(I_masked)
Try Reducing the threshold to 120, what happens?

Note the dot-multiplication used when multiplying two matrices of the same size, element by element. It is common in Matlab and is optimised. Where possible avoid using for-loops.

Capturing Data (using the terminal shell)

Plug the webcam into a USB socket on the computer. You can try the following command from the shell (note the parameter is v4-el, not v4-one):
mplayer tv:// -tv device=/dev/video0:driver=v4l2

(If you get an 'invalid password' message, just click on 'OK').
This views the camera input in real-time. If you right click in the window, you get a menu that allows you to 'Grab image' in different formats. 

An alternative way to capture an image directly is to use (note the parameter is v4-el, not v4-one):
mplayer tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0 -frames 5 -vo jpeg

This will capture 5 frames, but will then hang. Use CTRL-C to kill the process. The captured frames will be in the files 0000000X.jpg, where X=1/2/3/4/5. Check these (using e.g. xv or gimp) and use one of the good images.

Once you have captured an image, try:
xv <filename.extension>

(using whatever was the name and format of your captured image, e.g. xv foobar.ppm). This is an image viewing and conversion program. You can right click on the image to get the menu 'xv controls'. Try saving the image in pgm ascii format: Set 'Format:' to PBM/PGM/...*ascii) , set 'Colors:' to Greyscale and set 'filename extension' to pgm. A new file appears in the xv controls windows -  double click on this filename to see the greyscale image. In the shell, if you now type :
more foobar.pgm

 
You can see the file format, looking something like this:

P2
# CREATOR: XV Version 3.10a Rev: 12/29/94 (PNG patch 1.2)
320 240
255
41 41 41 40 40 39 40 40 39 39 38 38 38 38 38 38 37 37 37 36 36 35

etc.

Line 1 = file type ('man pgm' tells you all about this)
Line 2 = Image comment tag
Line 3 = Width and Height (X * Y)
Line 4 = maximum grey value; here 8 bit color 255 = white, 0 = black
Line 5 onwards = actual pixel values!!

Capturing Data (from within Matlab)

You can run unix commands from the matlab interface using the unix function. So for example, if you are connected to the webcam (see below) you could do the  following from within matlab to capture a picture and load it into your workspace (note the parameter is v4-el, not v4-one):
>> unix('mplayer tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0 -frames 5 -vo jpeg
')
>> I = imread('0000000X.jpg');

This will capture 5 frames, but will then hang. The captured frames will be in the files 0000000X.jpg, where X=1/2/3/4/5. Check these (using e.g. xv or gimp) and use one of the good images (usually image 4 or 5 is good).


Main IVR Page