UWA logo

 
School of Computer Science
& Software Engineering
Computer Vision CITS4240

Laboratory 3

The aim of this laboratory session is to familiarize you with MATLAB's morphological operations erode and dilate.

Dilation and Erosion

The MATLAB function dilate dilates a binary image by adding extra pixels to the boundary of objects in the image. The function erode removes pixels from the boundary. These functions are called as follows:
  BW2 = imdilate(BW, SE);
  BW3 = imerode(BW, SE);
Where BW is a binary image and SE is a matrix of 1's and 0's defining the shape of the structuring element. A square structuring element can be simply constructed using the MATLAB function ones

For your convenience a small MATLAB function, circularstruct.m, is provided by Dr Peter Kovesi for generating circular structuring elements which you can download.

Dilate your face...

Download your image from the ClassOf2007 directory. (If you do not have an image yet grab someone's image from the ClassOf2003, ClassOf2004, or ClassOf2005 directory temporarily and arrange a time with me to have your picture taken). This image will be in colour, if you call MATLAB's size function on your image you will see that it is a 3D array; three 2D images, one for each of the red, green and blue components. At this stage we do not want to be concerned with the complexities of colour. Convert the colour image to greyscale using
>> gim = rgb2gray(im);
Threshold the image at a level that gives the most pleasing result. Try a dilation followed by erosion to perform a closing operation. Note MATLAB also provides the functions imclose and imopen. Try an open-close noise removal operation to 'clean up' your image. Try a close-open operation and observe the difference.

Experiment with different sized and shaped structuring elements, see if you can create some interesting effects.

Locating Landmarks on a Multiple Choice Answer Sheet

This year every unit on campus will be surveyed using the Student Unit Reflective Feedback (SURF) form. The forms will be filled in by students, collected, scanned and then processed to automatically determine the responses. Some examples of scanned forms can be found in the Images directory. Note that these scanned images are quite large. When you display them under MATLAB they will be considerably reduced in size. You can use the magnify tool at the top of the figure window to zoom in and see features in more detail.

A key step in the processing of these images is the location of the 'landmark' squares at the corners. Once the centres of these have been found a coordinate system can be defined that allow one to readily locate the response squares and determine what responses have been selected.

Your Task...

Write a MATLAB function to the following specification:
% LOCATELANDMARKS - locates landmarks on SURF form
%
% Usage:  [tl, tr, bl, br] = locatelandmarks(im)
%
% Argument: im - Image to be processed, assumed binary.
%
% Returns:  tl, tr, bl, br
%              - Coordinates of the centroids of the top-left, top-right,
%                bottom-left and bottom-right landmarks respectively.  
%                These coordinates are returned as column vectors in the
%                form [row; col] for each landmark.
%
% The function should also display the image with the centroids of the 
% landmarks overlayed.

There are a number of ways one could do this. Here are some suggested steps.

  1. Invert the image to make the writing white on black (remember objects are 'white'). This can be done as follows:
         bw = ~bw;   % The ~ operator performs a logical not operation.
                     % Image values of 0 become 1 and vice-versa.
    

  2. Some of the scanned images have some missed lines passing through some of the landmarks. There may also be some spurious marks on the form. These need to be corrected in order to reliably locate the landmarks. You should experiment with open-close or close-open operations with various structuring elements to achieve a good image. Note you may find that a square structuring element is better suited to these images.

  3. Use bwlabel to label all the blobs. Note that this function can also return the total number of blobs.

  4. To locate the landmarks (the corner blobs), search through all the labeled blobs and find the ones closest to the corners of the image.

    You can use the find function to return the row and column coordinates of all the pixels in an object. If you have two arrays, r and c, containing the row and column coordinates of the pixels in a blob then the pixel with the minimum value of (r+c) will be the one that is closest to the top left corner. Thus the top left blob will be the one with the smallest of all the minimum values of (r+c). Similarly the top-right pixel will be the one with the smallest value of (r-c). By using different linear combinations of the row and column coordinates you can test for distances with respect to any direction.

    Note that unfortunately the Institutional Research Unit has written their name across the bottom left of the form complicating the identification of the bottom-left landmark. - You can have a think about this!

  5. Obviously the operations outlined above are time consuming. To improve the processing speed you could work with small sections of the image.

  6. Consider how to make your code automatically determine whether the form is upside down or right way up.

  7. Consider also how you might make your code invariant to the resolution that the document was scanned at. For example, you can make the size of the structuring element a function of the overall size of the image, and so on. Achieving invariance to image variations is always a goal to strive for.

There are several images of SURF forms in the Images directory, note that surf01 and surf02 are slightly smaller in scale than the others, see if your code can cope with them all.

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.


Return to Computer Vision 412