UWA logo

 
School of Computer Science
& Software Engineering
Computer Vision CITS4240

Laboratory 2

The aim of this laboratory session is to introduce you to MATLAB and some of the image processing tools available from its Image Processing Toolbox. By the end of this session, you should be able to perform some simple binary analysis on an image.


Separating and Labeling Binary Objects

Load up one of the 'lego' images (download it from here if needed) and display it.

   im = imread('lego1.png');
   imshow(im);
This image is in colour and is stored as a 3D array. (Do a size(im)). im(:,:,1) is the red image, im(:,:,2) is the green image and im(:,:,3) is the blue component. You can display these if you like.

Convert the image to greyscale using

  g = rgb2gray(im);
Choose a threshold and convert it into a binary image. It will not be possible to find an ideal threshold that separates all the components out - experiment! The thresholded image will have the objects in black on a white background. We want the reverse, white objects on black. This can be achieved with a logical negation of the image. Assuming BW is the thresholded image consisting of 1s and 0s the following command will negate the image.
  BW = ~BW;   % ~ is MATLAB's logical negation operator

MATLAB provides a function that performs connected components analysis on a binary image and assigns a unique value to the pixels in each distinct object. That is, each pixel in the first object found in the binary image will be assigned a value of one, then each pixel in the second object found is given a value of two, and so on.

  BWL = bwlabel(BW);   % BWL contains the labeled image
To view the image BWL, I suggest you use the MATLAB function imagesc. This function takes the values in the image (or arbitrary matrix) and rescales the values to lie within the range 0-255. (Note that imshow assumes the values already lie in the range 0-255 and does not do any rescaling.) Alternatively, use the show function that is provided by Dr Peter Kovesi.
  imagesc(BWL), colormap(gray), axis image  % You also typically need to
                                            % specify a colormap to use
                                            % and the axis display.

  show(BWL)       % The much simpler alternative!
You should now see that each distinct object in the image will have its own grey value.

To extract individual objects from this image one can exploit MATLAB's matrix logical operators. To generate an image that just consists of 'object 3' in the image, that is the object with all its pixels set to the value of 3, we can do the following:

  ob3 = BWL==3;
  imshow(ob3);
The image ob3 will be a matrix of 0's and 1's corresponding to points in image BWL where the boolean condition that the pixel value == 3 is true.

To find out what values image objects have been labeled set

  pixval on
As you move the mouse over the image the (x,y) position and the pixel value corresponding to the mouse position will be displayed in a black bar at the bottom of the figure.

When you extract image objects and display them there may be occasions where there seems to be nothing. However, if you look closely you will eventually find an 'object' that consist of only a few pixels. Next week we will cover ways of automatically coping with this.

Calculating Moments

Performing binary moments analysis on images is very easy in MATLAB. However, there are some tricks that one can use to exploit MATLAB's syntax more fully.

The obvious way to find the x coordinate of the centroid of an object in a binary image im is to do the following:

   [rows,cols] = size(im);

   area = sum(sum(im));
   meanx = 0;

   for r = 1:rows
     for c = 1:cols
       meanx = meanx + im(r,c)*c;    
     end
   end

   meanx = meanx/area;
This code is not as concise as it could be and will run slowly as MATLAB is interpreted and this loop will have to be executed many times.

An alternative approach is to do the following:

   [rows,cols] = size(im);
   x = ones(rows,1)*[1:cols];    % Matrix with each pixel set to its x coordinate
   y = [1:rows]'*ones(1,cols);   %   "     "     "    "    "  "   "  y    "

   area = sum(sum(im));
   meanx = sum(sum(double(im).*x))/area;
How does this work?
x is a matrix that has the same size as image im with every element set to its x coordinate. For example:
   >> rows = 6; cols = 4;
   >> ones(rows,1)     % ones is a MATLAB function that produces a matrix of 1's with the specified size
   ans =
        1
        1
        1
        1
        1
        1

   >> [1:cols]         % note that the square brackets are optional
   ans =
        1     2     3     4

   >> x = ones(rows,1)*(1:cols)
   x =
        1     2     3     4
        1     2     3     4
        1     2     3     4
        1     2     3     4
        1     2     3     4
        1     2     3     4
Alternatively, you could try the MATLAB repmat function:
   >> x = repmat(1:cols, rows, 1)
   x =
        1     2     3     4
        1     2     3     4
        1     2     3     4
        1     2     3     4
        1     2     3     4
        1     2     3     4
We then do a point-wise multiplication (the .* operator) between the binary image and the matrix x. This produces a new image with each element weighted by its x coordinate. We then sum up all these values and divide by the area to get the mean x coordinate.
   meanx = sum(sum(double(im).*x))/area;
Note that the image has to be cast to double format because MATLAB does not allow multiplication of uint8 (unsigned 8 bit integer) objects - which is the typical format of images. Make sure you do not use the '.*' operator when you mean to use the '.*' operator - otherwise you will end up doing a full matrix multiplication rather than point-wise multiplication.

This approach leads to an interesting style of programming where you do not write loops but instead construct vectors or matrices of the index values that you wish to iterate over. This produces efficient and concise code (at the expense of memory usage).

Note that in some images the camera pixels are not square. Circles will appear as ellipses, squares as parallelograms, etc. This will affect your calculations of the moments of binary objects. Thus the x and y pixel coordinates need to be scaled before you perform moments analysis. The usual ratio is 4:3 width to height.

Your Task...

Write a MATLAB function called moments (in a file called moments.m) that meets the following specification:

   % MOMENTS
   %
   % Function calculates the moments of a binary image and returns the
   % area, the centroid, the angle of axis of minimum inertia, and a measure 
   % of 'roundness'.  The function assumes that there is only one object
   % in the binary image.
   %
   % function [area, centroid, theta, roundness] = moments(im)
   %
   % Argument:  im   - a binary image containing values of 0 or 1
   %
   % Returns:   area      - the area of the binary image
   %            centroid  - a 2 element vector
   %            theta     - the angle of axis of minimum inertia (radians)
   %            roundness - ratio of minimum inertia/maximum inertia.
   %
   % Note that positive x is to the right and positive y is downwards
   % thus angles are positive clockwise.
   %
   % The function also displays the image and overlays the position of
   % the centroid and the axis of minimum inertia.  The area computed
   % by the function should be displayed as the title of the image.

   function [area, centroid, theta, roundness] = moments(im)

This function can be written in about 25 lines of MATLAB. If you are new to MATLAB or feeling a bit rusty do not hesitate to call on your lab demonstrator to give you a hand in getting started. For information on drawing lines in images do a 'help line'.

When you calculate the orientation of the axis of minimum inertia do not use the asin, acos or atan functions. These functions cannot give you a unique solution for the angle, as there will always be two possible solutions. Instead, you should evaluate the value of theta using the atan2 function to obtain a unique solution.

Your code should catch the case when you encounter a perfectly circular object (say a single pixel). In this case the denominator b^2+(a-c)^2 goes to zero. The angle calculation becomes undefined and theta can be set to an arbitrary value (say 0) and roundness set to 1.

You should extract some objects from your binary version of the image and apply your moments function to them. You should also test your code for correctness. One test might be to rotate the image by a known amount (see imrotate) and then see whether your output changes accordingly. You should also construct some synthetic images for which you know the ground truth of centroid, orientation and roundness.

Portfolio Requirements

The following should be included in your portfolio:

You might find it more tidy to save your MATLAB code and input/output test image files into subdirectories away from your HTML file. Ensure that all the links in your HTML file are correct. You should also provide some explanation in your HTML file to demonstrate your understanding about the problem and the work that you carried out.

This material can be placed in a special directory in your area for subsequent incorporation into a portfolio.

You can save images from MATLAB using the imwrite command. Save images in the jpeg or png formats if you want to embed them in web pages.

Some suggestions on how to compile your portfolio are available here


Return to Computer Vision 412