UWA logo

 
School of Computer Science
& Software Engineering
Computer Vision CITS4240

Laboratory 6

Aim

The aim of this laboratory session is to experiment with edge detection and implement the Hough transform for finding lines in images.


Edge Detection

The basic gradient based edge detection process is to calculate image derivatives in the vertical and horizontal directions, square and add the results, and then take the square root to obtain a gradient strength image. Prior to taking the derivatives the image is usually smoothed with a Gaussian filter to reduce the influence of noise. The degree of smoothing controls the scale of analysis. With heavy smoothing you obtain the large scale edges, with little or no smoothing you obtain the fine scale edges.

Convert your image to greyscale and smooth it with a Gaussian filter (as you did in Lab4). To find the gradient in the vertical and horizontal directions convolve the image with simple differencing filters. The horizontal differencing filter is
-1 0 1
Display the raw derivative images, so you should see an interesting embossed effect. The white areas in the derivative images are where the derivative is large and positive, the black areas are where the derivative is large and negative. Generate the overall gradient strength image, hopefully you will get a reasonable 'sketch' of yourself.

It is quite possible that your gradient strength image will be very dark. This is most likely caused by a few values in the image being very large. When the image is rescaled to the range 1-255 for display, most parts of the image end up being very dark. To compensate for this you can use the function adjgamma to adjust the gamma of the image and enhance the dark regions of the image (e.g., try a gamma value of 2).

Generate the gradient strength image for three very different levels of Gaussian smoothing and observe the differences.

To get to an edge image the gradient strength image needs to undergo non-maximal suppression and hysteresis thresholding. Rather than implement this one can simply use MATLAB's edge function which implements a variety of edge detection methods, and performs non-maximal suppression and thresholding (see the help page for edge).

Use the edge function, with the 'canny' option, to generate an edge map of yourself. You will need to experiment with the hysteresis threshold values to get a reasonable result.


Finding Lines using the Hough Transform

The Hough transform can be used to find sets of straight lines of edge pixels in an edge image.

Write a function, hough that meets the following specification

% HOUGH
%
% Function takes a grey scale image, constructs an edge map by applying
% the Canny detector, and then constructs a Hough transform for finding 
% lines in the image.
%
% Usage:  h = hough(im, Thresh, nrho, ntheta)
%
% arguments:
%            im     - The grey scale image to be transformed
%            Thresh - A 2 -vector giving the upper and lower
%                     hysteresis threshold values
%            nrho   - Number of quantised levels of rho to use
%            ntheta - Number of quantised levels of theta to use
%
% returns;
%            h      - The Hough transform

function h = hough(im, Thresh, nrho, ntheta)
The steps you should follow:

The fiddly bit of this code is getting the conversion between rho and theta and the corresponding row and column number in the accumulator array correct. Here is some code to save you some grief!

rhomax = sqrt(rows^2 + cols^2);    % The maximum possible value of rho.
drho =  2*rhomax/(nrho-1);         % The increment in rho between successive 
                                   % entries in the accumulator matrix. 
                                   % Remember we go between +-rhomax.

dtheta = pi/ntheta;                % The increment in theta between entries.
theta = [0:dtheta:(pi-dtheta)];    % Array of theta values across the accumulator matrix.

% To convert a value of rho or theta to its appropriate index in the array use:
rhoindex = round(rho/drho + nrho/2);
thetaindex = round(theta/dtheta + 1);

Your Hough Transform should look something like this

 
A typical Hough Transform (left) and a gamma enhanced version (right)

Having calculated the Hough Transform we now want to find what the dominant lines are and overlay them in the image. Write a function with the following specification

% HOUGHLINES
%
% Function takes an image and its Hough transform, finds the
% significant lines and draws them over the image
%
% Usage:   houghlines(im, h, thresh)
%
% arguments:
%             im     - The original image
%             h      - Its Hough Transform
%             thresh - The threshold level to use in the Hough Transform to
%                      decide whether an edge is significant

function  houghlines(im, h, thresh)
What this code has to do is find the local peaks within the Hough transform.
Hints:

An alternative (and probably better) way of picking out the peaks in the Hough transform would be to use the function nonmaxsuppts.

This function was originally written for non-maximal suppression of corner strength images. What this code does is a greyscale dilation of the image (within the structuring element area every pixel is replaced with the maximum pixel value within the structuring element area). Where the greyscale dilated image matches the original image will correspond to local maxima.

In using this function you would pass your Hough transform in as the 'corner image'. The size of the structuring element controls how close together you are prepared to have local maxima.


There is plenty of room for refinements. The Hough Transform 'surface' tends to be very rough. This is partly caused by the primitive way in which we 'draw' the sinusoidal votes into it. Note that if for some increment in theta the rho value changes by two or more we should be 'drawing in' extra points to ensure that we do not skip the intermediate rho values.

One can get some improvement by smoothing the transform very slightly. Some morphological processing may also be useful after the thresholding process.


Here is the warning again! In all of the above beware of getting confused between x and y, and row and column number.


Portfolio Requirements

The following should be included in your portfolio:

Your Matlab functions/scripts will be run and tested in the marking. So, do not forget to include all other Matlab functions (excluding the Matlab library functions but including Dr Peter Kovesi's) that are needed by your Matlab functions/scripts.

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.


Return to Computer Vision 412