This is to help you get started using Matlab for the assignments for the Computer Vision course. If you have difficulty with Matlab, please speak to the tutor or lecturer.
On PCs, the Matlab icon may appear on the toolbar and it is also on a sub-menu of the Start menu. When you click on it you will get a new window and the ">>" prompt will appear.
On the Informatics Unix teaching network, give the command "matlab" at the Unix prompt. After an introductory message you will see the Matlab prompt ">>" appear. You can then start giving commands to Matlab.
If you have your own computer, you can install the student edition of Matlab, though this is not particularly cheap. You may need to download the Image Processing Toolbox. You should check that solutions for assignments will also run correctly on the Matlab version installed at the University as you may have a more recent one.
Matlab's online documentation is very good. To begin, give the "helpwin" command in the Matlab window. This will open the help window. In the left-hand half (the Help Navigator), click MATLAB, and then in the right-hand window click Getting Started. Following through this documentation from page to page will give you a good overview of the system and introduce you to programming in Matlab.
You should spend some time looking through these documents. You clearly cannot absorb all the information at once, and you will not need to know about everything covered, but you should skim through these pages and be prepared to return to them whenever you need to.
The Getting Started pages have a section on documentation. There are two main ways to get information:
To access local programs and images, it is convenient to set Matlab's path variable to include the directory of programs for the Computer Vision course. To do this on a PC, type the following into the Matlab command window:
On the Informatics Unix machines, the equivalent is
The addpath command's documentation is available from the Matlab help window if you want to know more about it.
You can put the appropriate line above in a file called startup.m in a directory (folder) called matlab in your home directory. It will then be executed automatically when Matlab starts up.
You should use Matlab's help information to look up key functions used in the examples below, in order that you can use them in other contexts.
You can cut and paste the examples into a Matlab window in order to see what they do.
To read in an image, you would normally use the Matlab function imread, possibly followed by a call to a function to convert from e.g. a colour image to a grey-level image. These operations are packaged for you in the local function teachimage which knows where to find example images and how to convert their formats.You need to give teachimage the filename of the image as a string. In Matlab you do this with single quote marks (which actually cause a special kind of matrix to be constructed), like this
image = teachimage('moffat.bmp');
If you are not using an Informatics machine, you will need to copy the image from H:\teach\ComputerVision\images\moffat.bmp, or you may be able to download it from here. You will also have to make your own copy of teachimage, from H:\teach\ComputerVision\matlab\teachimage.m, and modify it to look in the directory where you have installed the file.
You can look at the image you have read in with this call:
You should now have a new window showing a grey-level image.
The Matlab image processing toolbox contains many powerful tools for image processing - see above for how to find out about them. However, you will learn more by using Matlab's basic operations wherever possible. In fact, these allow you to do a remarkable amount very simply.
The introductory example of image processing was taking differences between horizontally adjacent pixels to emphasis vertical edges. We can do this to our image with the following line of Matlab code:
hordiffs = image(:, 2:end) - image(:, 1:end-1);
To understand this example, you need to understand Matlab's extremely powerful colon ":" operator. This is described in the "Getting Started" online documentation (see above). It is well worth making sure that you understand exactly why the example above does what it does. It reveals that in Matlab, you can often avoid writing loops explicitly.
It is also important to note that Matlab handles the image as a matrix. That is, contrary to the usual image-processing convention, the row comes first and the column comes second. You simply have to live with this, and be aware of possible problems if you switch between the conventions.
We can now look at the results of the operation. However, because some pixels will be negative rather than positive, we need to tell imshow to use a different range from its 0 to 1 default. A simple solution for now is just to say that the range should be from the most negative value in the array to the most positive, with a call like this:
There are more sophisticated ways to display this kind of result - for example using false colour - which you can discover by looking through the documentation if you wish. But you should now have an image with vertical structure highlighted, as in the example in the lectures.
As you have seen, you can run Matlab by typing code to its top-level prompt, or by cutting and pasting it from web pages. One way to try out simple ideas is to just cut and paste from code you have typed in to an editor. Note also that the arrow keys can be used in the Matlab window to go back to previous entries and edit them.
This is clearly not enough for serious program development. For this, you need to put your code into M-files, which Matlab will read and execute. You will need to look at the Scripts and Functions section of Getting Started, and you need to know about the path variable. You should use addpath to make Matlab look in the directory where you are going to develop your program.
You should normally create small M-files, with the editor of your choice, each of which contains essentially one function. (Sometimes it is convenient to break down a function into a number of smaller functions, and they can go in the same M-file, but only one of these functions can be called from outside the M-file, so it must call the others.) Once you have made an M-file in a directory in the search path, you can call it immediately from the main Matlab window or from other M-files.
For example, you could make a function that uses the code above to get horizontal differences, so that it is packaged for future use. Create a file called hordiff.m with the following contents:
function imout = hordiff(imin) %HORDIFF Get horizontal differences % IMOUT = HORDIFF(IMIN) subtracts the value of each pixel in an image % from the value of the pixel to its left, putting each result in the % corresponding position in the output image. imout = imin(:, 2:end) - imin(:, 1:end-1);
Now in the Matlab window you can type "help hordiff" to get the synopsis, and you can call your new function with
h = hordiff(image);
Often, software development in packages like Matlab follows the route illustrated here: commands are tried out directly, then once the required behaviour has been established, they are packaged up into a neat function. You should now, as an exercise, make a vertical differences function and try it out.
When you have completed assignments, you will be asked to hand in your M-files in a specific way - see the separate assignment page for this.
You should now be able to make progress with Matlab on your own, using its documentation and exploiting the ease with which you can experiment to find out what any particular function does.