CS425 Lab: Edge Detection and Hough Transform

1. Edge Detection

See chapter 7 up to section 7.5 in your textbook

In the field of Image Processing, the extraction of geometric features from images is very common problem. Over the years, several different approaches have been devised to extract these features.

These different approaches can be characterized and classified in several different ways.

Some of the techniques involve global examination of the image, while others only involve local examination of each pixel in the image.

1.1 What is Edge Detection?

Typically, the first step in the process is to perform some form of edge detection on the image, and to generate a new Binary edge image that provides the necessary segmentation of the original image.

Edge detection algorithms operate on the premise that each pixel in a grayscale digital image has a first derivative, with regard to the change in intensity at that point, if a significant change occurs at a given pixel in the image, then a white pixel is placed in the binary image, otherwise, a black pixel is placed there instead.

In general, the gradient is calculated for each pixel that gives the degree of change at that point in the image. The question basically amounts to how much change in the intensity should be required in order to constitute an edge feature in the binary image.

A threshold value, T, is often used to classify edge points.

Some edge finding techniques calculate the second derivative to more accurately find points that correspond to a local maximum or minimum in the first derivative. This technique is often referred to as a Zero Crossing because local maxima and minima are the places where the second derivative equal zero, and its left and right neighbors are non-zero with opposite signs.

1.2 Edge Detection With the edge Function in MATLAB

The Image Processing Toolbox's edge function provides several derivative estimators based on the criteria just you learned above.

For some of these estimators, it is possible to specify whether the edge detector is sensitive to horizontal or vertical edges or to both. The general syntax for the edge function is:

[g, t]= edge(f, 'method', parameters ... , options ... );
where f is the input image. The most popularly used approaches are listed in the following table. Additional approaces and control parameters are discussed in MATLAB's documentation. Your lab instructor will give you a tour of some of the details of the three approaches below in the lab lecture.

In the output, g is a logical array with 1's at the locations where edge points were detected in f and 0's elsewhere. Parameter t is optional, it gives the threshold used by edge to determine which gradient values are strong enough to be called edge points.

Edge Detector Basic Properties
Sobel Finds edges using the Sobel approximation to the derivatives.
Canny Finds edges by looking for local maxima of the gradient of f(x,y). The gradient is calculated using the derivative of a Gaussian filter. The method uses two thresholds to detect strong and weak edges, and includes the weak edges in the output only if they are connnected to strong edges. Therefore, this method is more likely to detect true weak edges.
Zero Crossing

Finds edges by looking for zero crossings after filtering f(x,y) with a user defined filter.

This method is often combined with the Laplacian of a Gaussian filter, but any filter that approximates the second derivative of the image's data will do. If you don't provide your own filter, Zero Crossing uses the equivalent of this filter:

H = FSPECIAL('log', 13, 2);

See also the log section of help FSPECIAL.

Now let us try those different methods and parameters to see what is the difference between them:

[g1, t1]=edge(f, 'sobel', 'vertical');
[g3, t3]=edge(f, 'canny', [0.04 0.10], sigma);


2. Hough Transform

See chapter 9 up to section 9.3 in your textbook

The Hough Transform (HT) is a robust method for finding lines in images that was developed by Paul Hough.

The main idea for the HT is as follows:

To implement this algorithm we quantize the parameter space by using a 2D array of counters, where the array coordinates represent the parameters of the line. This is commonly known as an accumulator array.

The HT method for finding lines in images generally consists of the following three stages:

2.1 Line detection using Hough Transform in MATLAB

The MATLAB has a function called hough that computes the Hough Transform.

In the following example, we will illustrate the use of function hough on a simple binary image.

Type in the following commands. After typing each imshow, explain to yourself why you are seeing the new curve on the Hough transform.


The last imshow should looks like this:

Hough Space

Let's try a more complex shape, the edge detected square from the examples above:

%generate the picture

%find edges
[g3, t3]=edge(f, 'canny', [0.04 0.10], sigma);

%Do the Hough transform
[H t r] = hough(g3); %r and c store column labels...

%Display the transform in such a way that we can draw points on it later...
imshow(H, [], 'XData', t, 'YData', r );

%Add axis labels to the picture
xlabel('\theta'), ylabel('\rho');
axis on, axis normal;

You result should look like this:

Hough Transform With Peaks Highlighted

2.2 Hough Transform Peak Detection

Now that you know how to do a Hough transform, the next step is to find line candidates by doing peak detection.

MATLAB provides us a function named houghpeaks to do this. The syntax is:

PEAKS = houghpeaks(H, numpeaks);  
where H is the Hough Transfrom matrix, and numpeaks is the maximum number of peak locations to look for. Be aware that peaks suppresses other peaks that are close to a peak just detected, and that if peaks are too weak, they will not be detected. These behaviours can be controlled with parameters. Consult the help file for houghpeaks for details.

Continuing the sample from above:

%detect four peaks
P  = houghpeaks(H,4);

%draw peaks over Hough transform
%don't replace the picture when we start to draw
hold on; 
plot( t( P(:,2) ), r( P(:,1) ), 's', 'color', 'green'); 

You result should look like this:

Hough Transform With Peaks Highlighted

Study help houghpeaks if you want to see a more complex example.

2.3 Hough Transform Line Linking

Once a set of candidate peaks has been identified in the Hough transform, it remains to be determined if there are line segments associated with those peaks, as well as start and ending points.

For each peak, the first step is to find the location of all nonzero pixels in the image that contributed to that peak and construct line segments based on those pixels. The houghlines function can do this.

Learning the details of this function from help houghlines is left to you as an exercise.


3. Global Threshoding

See section 10.3 in Digital Image Processing Using Matlab

Image thresholding plays a very important role in image segmentation.

In this section we discuss one way of choosing the threshold automatically.

An algorithm like the following can be used to choose a threshold, T automatically:

  1. Select an initial estimate for T. A possible initial value is the midpoint between the minimum and maximun intensity values in the image.
  2. Segment the image using T. This will produce two groups of pixels:
    • G1 consisting of all pixels with intensity values > T
    • G2, consisting of pixels with values < T.
  3. Compute the average intensity values x1 and x2 for the pixels in regions G1 and G2.
  4. Compute a new threshod value: T=1/2(x1+x2)
  5. Repeat steps 2 through 4 until the difference in T in successive iterations is smaller than a predifined parameter T0.

The above iterative method can be implemented as follows:

done = false;
while ~done
  g = f >= T;
  Tnext = 0.5*(mean(f(g))+mean(f(~g)));
  done = abs (T-Tnext) < 0.5;
  T = Tnext;

where g is a binary mask, f(g) is a subset of f defined by g.

You can also use IPT's function graythresh to reach the same goal.

T = graythresh(f)

4. Exercise

Part 1: Edge Maps

  1. Download the following image "building.jpg" and store it in MATLAB's "Current Directory".
  2. Identify which of the following is the result of zerocrossing or Canny detector
    Image 1
    Image 2
    Building Edges 1 Building Edges 2
  3. Reproduce the results for input image building.jpg. I encourage you to try different T and Sigma to get the best result.

Part 2: Automatical Threshoding

  1. Download the following image "text.jpg" and store it in MATLAB's "Current Directory".
  2. Reproduce the results to get the image looks like the following enhance_text.gif.
    Enhanced Text

Part 3: The Hough Transform

  1. Perform a hough transform on the Canny edge results from Part 1.
  2. Find a reasonable number of peaks in your Hough transform results using houghpeaks. You may want to fine tune your results by adjusting the threshold and neighborhood suppression parameters.
  3. Use these peaks with houghlines as documented in help houghlines to produce a line plot of the original image. The example on the help page rotates the picture before finding edges. There is no need to do that, though I won't deduct marks if you do. There is no need to highlight the longest segment either.


5. References