CITS4402 Computer Vision

Week 2 Laboratory Exercises

The aim of this lab session is to introduce you to MATLAB with an emphasis on its image processing toolbox. We will go through some of the binary image analysis techniques and use their MATLAB implementations to understand their operation.

The laboratory work can be done in any operating system using Matlab. 


Our Images Database

Our shared directory for the course is /cslinux/examples/CITS4402/Images directory. In it you will find images that will be used for some of the lab exercises. You will find a variety of images here, extra images will be added from time to time during the course.

As you do not have write permissions on this directory I suggest that you create an Images directory in your own area and copy any images that you want to work on from the shared area to your own. Note that some of the images there have read permission removed as they are a bit outdated and are unlikely to be used in the future.


Getting started with MATLAB

MATLAB is a computing environment for high-performance numerical computation and visualization. MATLAB stands for matrix laboratory and its basic data structure is a matrix. Many numerical problems, especially those typical to linear algebra, can be implemented in a fraction of the time it would take under more traditional environments such as C. Since we will be using MATLAB throughout this course, it is important that you get yourself familiar with its basic syntax, matrix and vector operations, loops and conditional statements, MATLAB scripts, and input and output.

Copy a few images including the image 'shapes.png' into your own Images directory.
Invoke MATLAB (simply by typing matlab &).
It may take a little while for MATLAB to start up. So be patient! The command window prompt is >>. To enter an expression, type it after the prompt (correct any mistakes by backspacing) and press return. MATLAB prints the result back to the command window. For example, to ``evaluate'' pi type

	>> pi

MATLAB responds with
	ans =

3.1416
pi is a built-in MATLAB variable. The preceding command is actually a request for MATLAB to print the value of this variable.

Try the following examples

	>> sin(pi/4)

>> 2^(log2(4))

>> sqrt(9)

Matrices

To enter the matrix
             1 2
3 4
and store it in a variable a, do this:
       >> a = [ 1 2; 3 4 ]

Do this now: define the matrix a. Do the same with the examples below: work out each of them with matlab. Learn by doing!

To redisplay the matrix, just type its name:
       >> a

Once you know how to enter and display matrices, it is easy to compute with them. First we will square the matrix a :

       >> a * a

Wasn't that easy? Now we'll try something a little harder. First we define a matrix b:

       >> b = [ 1 2; 0 1 ]
Then we compute the product ab:
       >> a*b

Finally, we compute the product in the other order:

       >> b*a

Notice that the two products are different: matrix multiplication is noncommmutative.

Of course, we can also add matrices:

       >> a + b

Now let's store the result of this addition so that we can use it later:

       >> s = a + b
Matrices can sometimes be inverted:
       >> inv(s)

To check that this is correct, we compute the product of s and its inverse:

       >> s * inv(s)

The result is the unit, or identity matrix. We can also write the computation as

       >>  s/s

We can also write

       >>  s\s
which is the same as
       >>  inv(s) * s

To see that these operations, left and right division, are really different, we do the following:

       >> a/b

>> a\b

Not all matrices can be inverted, or used as the denominator in matrix division:

       >> c = [ 1 1; 1 1 ]
>> inv(c);

A matrix can be inverted if and only if its determinant is nonzero:

       >> det(a)
>> det(c)

On-line Help(!)

MATLAB provides on-line help for all built-in functions and commands. To browse a list of help topics type
	>> help
To get help on a specific topic, for example the cosine function, type
	>> help cos
Using help is one way to learn about the variety of built-in functions. The help command is most useful when you know the name of the function, but are unsure how to use it. The MATLAB manuals provide cross-referenced and indexed descriptions of all aspects of using MATLAB.

Suppressing Output with Semicolon

In the preceding examples it was useful to have MATLAB print the results of the calculations. This is not always the case. MATLAB will print the result of every assignment operation unless the expression on the right hand side is terminated with a semicolon. Unlike C the semicolon is not required by MATLAB syntax. Rather, it is a convenience that allows multiple statements to be executed without printing the intermediate results. For example, most lines in m-files end with semicolons because only the final results of the calculations are of interest.

To see how the semicolon works enter the following statements exactly as shown, ending each line with a carriage return. Do not enter the prompt character, >>.

	>> x = pi/3;
>> y = sin(x)/cos(x);
>> z = y - tan(x);
>> z
>> y
The last two lines do not end in semicolons so MATLAB prints the results of evaluating z and y. The the values of z and y were assigned in the y = and z = statements. The last two lines merely printed the values of the expressions z and y.
 

The Image Processing Toolbox

The Image Processing Toolbox is a collection of image processing functions, ranging from image display through to filtering, analysis, and image transforms. Most of these functions are written as M-files, that is they consist of a series of MATLAB statements that implement the function. You can view the MATLAB code by using the statement

      type function_name
Try this for the MATLAB function hist, which plots the histogram of a vector of data. If you simply type
      help function_name
MATLAB responds by typing out the first contiguous block of comments in the script. Thus, document your own scripts sensibly with a block of comments at the top in a way that conforms with MATLAB style and makes it easy for others to use your code.

To find out more about any command, say, imread type "help imread".
"help images" will give you a list of all the image related commands within MATLAB. You can also browse the MATLAB Help Desk, which provides tutorials, supports navigation and search of MATLAB functions.

MATLAB also has a built-in editor, which is useful for editing your own MATLAB functions for future Laboratory exercises. Together with the built-in debugger, the task of debugging your MATLAB functions inside the MATLAB editor becomes a less painful one. To invoke the MATLAB built-in editor, simple select [File | Open...] to open an existing MATLAB function file or select [File | New] to start a new MATLAB function file.

You can import images into MATLAB using a number of standard tools. The one you will be most interested in is imread. The function imread attempts to infer the format of the file from its contents, and reads a number of types of images, including png format. Copy the image 'lego1.png' to your MATLAB working directory. Then, the command

   im = imread('lego1.png');

will return the image in the variable im.

To determine the number of rows and columns in this image use the MATLAB function size, which returns the dimensions of the matrix im. The command size(im) without the terminating semi-colon returns the values of the function to the screen. If the image you have loaded is colour, then size(im) will return the size of a 3-dimensional array, with the last dimension having the value 3 for the red, green, and blue bands of the image.

You can now view your image with

   imshow(im);

Now generate a greyscale version of the image using

   g = rgb2gray(im);
   imshow(g);

Calculate and display the histogram of this image using

   imhist(g);

Decide on a suitable threshold to produce a binary image containing simple connected objects. You can threshold your image at this value to obtain a black and white image, tt>BW,, using MATLAB's 'greater than' logical operator with the following command:

   bw = g > 150;           % threshold the image at a grey value of 150

This creates an image of 0s or 1s depending on whether each pixel in the image is greater than 150 or not. Display the image and adjust the threshold if needed.

   imshow(bw);   
imshow(~bw); % show an inverted binary image

You can perform connected component labeling in a binary image using bwlabel method of the image processing toolbox

[L, num] = bwlabel(bw, 4);   % for 4-connected components

num indicates the number of connected components found, and L is the labeled image. Now, you can perform morphological image processing using the methods explained here: http://www.mathworks.com.au/help/images/morphology-fundamentals-dilation-and-erosion.html

The first assignment is to perform morphological image processing operations so that you get the correct count of the number of objects present in the image lego1.png. Submit a MATLAB script that reads the image 'lego1.png', binarizes it using the methods above, and then performs the necessary morphological operations to print out the correct number of objects in this image.

School of Computer Science and Software Engineering

This Page

Updated by: Ajmal Mian