You are on page 1of 17

ECE847 Digital Image Processing

Homework 3 Canny Edge Detection

October 1, 2011

Submitted by Steven Hickson

Department of Electrical & Computer Engineering Clemson University

__________________________________________________________________ Homework 3 Canny Edge Detection __________________________________________________________________ Introduction


This assignment involved implementing the canny edge detection algorithm and chamfering for template matching. The canny algorithm involves four steps: Gradient estimation using the convolution of the input with Gaussian and Gaussian derivative kernels, non-maximal suppression to find strong edges, edge-linking with hysteresis thresholding, and an optional feature synthesis. Chamfering is used to exhaustively search an image based of edge distance from a templates edges.

Theory
The Canny edge detector finds step edges by convolving with a Gaussian kernel and the derivative of a Gaussian kernel. The main goal of edge detection is to detect the intense, localized edges in an image while minimizing the error rate. However, this can be complex, and noise and false positives always occur. The canny algorithm limits the amount of noise and the number of edges by first smoothing. To smooth, the image can be convolved with a twodimensional isotropic Gaussian curve shown below:

The next step is to find the gradient of an image, which is shown by the equation below:

The gradient can be found by deriving the image as shown by the above equation. Since, the equations are linear-time-invariant, convolution is associative. Therefore, the image can be

smoothed and the gradient can be found by convolving the image with the derivative of a Gaussian curve in one step. This can be simplified even further by separating the twodimensional Gaussian into two separate one-dimensional Gaussian curves as shown below:

This method of convolution is more efficient since it is just two convolutions of one dimension. To get the magnitude and phase from the gradients the following equations are used:

After the magnitude and phase are calculated, the next step is the non-maximal suppression. For each pixel, if either of the neighbor pixels in the direction of the gradient are greater than the magnitude, then that pixel value is set to zero, otherwise it is set to the magnitude. This creates edges along the local maxima of the gradient, reducing the noise level and picking up the strongest edges. To further rid the image of noise, the Canny edge detector uses hysteresis thresholding. This method thresholds the image with a high value and a low value. All of the pixels with a magnitude greater than the thresh high stay while all of the values less than the thresh low are set to 0. The values in between only stay if they are connected to another pixel with a magnitude above the high threshold. This is achieved using a modified flood fill.

The Chamfer algorithm is a method of finding the distance to the nearest true value in a binary image. For this assignment, Manhattan distance was used. The Manhattan distance D of two points P1 and P2 is shown below:

The Chamfer distance is used by the template in order to find the location of the template by checking the sum of the distances of the edges at each pixel. This is a very inefficient way to match an image. However, object detection is sometimes inherently computationally inefficient.

Algorithm

Main 1. Open image file. Decide based on arguments whether to canny or chamfer. 2. If using canny, does the equivalent of the canny function but shows intermediate results. Compute the gradients. Calculate the magnitude and phase using the gradients. Non-maximal suppression using the magnitude and phase. Double thresholding/hysteresis based off the top 10 % of the pixels and 5 * that. 3. If chamfering, calls the canny function for both the image and the template. Then it calls chamfer to implement the chamfer function using Manhattan distance. Calls the canny function to implement canny edge detection on both the image and the template. Calls the Chamfer function to find the minimum distance to an edge from every pixel. Compute the Probability map by finding the sum of the chamfered image along the template using the TemplateMatch function. Draw a rectangle at the minimum point of the probability map. 4. Display all the output.

Canny 1. 2. 3. 4. Compute the gradient in the x and y directions by calling the Gradient function. Compute the magnitude and phase of the gradient. Perform non-maximum suppression using the magnitude and direction of the gradient Use image statistics to compute the threshold values by sorting the values of the edges image and choosing the pixel value at the top 10% of all pixels as the high threshold value. Make the low threshold value 5 * high. 5. Apply the high and low thresholds and then perform double thresholding using Flood fill.

Chamfer 1. Set all pixel values to 0. Set the maximum = width * height + 1 since that is 1 greater than the maximum distance. 2. From left to right and top to bottom, find the minimum of the maximum, the value of the pixel to the left + 1, and the value of the pixel above + 1. 3. From right to left and bottom to top, find the minimum of the value of the current pixel, the value of the pixel to the right + 1, and the value of the pixel below + 1.

TemplateMatch 1. 2. 3. 4. For every pixel in the image, sum the chamfer distance of each of the edge pixels. Set this value as the current value in the probability map. If the value is less than the minimum. Make it the minimum. Return the point where the minimum occurs and the probability map.

Gradient 1. Compute the Gaussian and the Gaussian derivative one-dimensional kernels. 2. Smooth the image by convoluting with the Gaussian in the y direction and convolve the image with the Gaussian derivative in the x direction to achieve the gradient in the x direction. 3. Smooth the image by convoluting with the Gaussian in the x direction and convolve the image with the Gaussian derivative in the y direction to achieve the gradient in the y direction. 4. Return the x and y gradients.

Non-Maximal Suppression 1. For all pixels in the image, if the magnitude of the gradient is not the maximum in either direction of the phase of the gradient, set it to zero.

Results

= 0.5

= 1.5

= 2.5

X Gradient

Y Gradient

Magnitude of the Gradient(x,y)

Phase of the Gradient(x,y)

Non-Maximal Suppression

Low Threshold

High Threshold

Final Output

9 Cameraman at = 1.5

Gradient(x)

Gradient(y)

10 Magnitude Phase

Non-Maximal Suppression

Low Threshold

High Threshold

Canny output

11

Chamfer Results

Original Image

Template Image

Image Canny Output

Template Canny Output

Chamfer Distance Output

Probability Map

12

Final output

Other Results

Original Image

13

Canny Output

Canny output with post-processing isotropic filtering

14

Original Image

Canny Output

15

Canny output with post-processing isotropic filtering

Original image

Canny Output

16

Original Image

Canny Output

Conclusion

The canny algorithm is a very robust algorithm for edge detection, with good outputs and a computationally efficient algorithm. It works best on high contrast images that are un-textured. It does not work very well on images with shadows or textured or natural environments. This is shown by the elephant, bench, and mountain images. On the elephant and mountain images, an isotropic filter is shown to compare against Canny. This means that the algorithm can be improved in certain situations in order to decrease the number of false positives. Often, the Canny algorithm will work well in practice if given different sigma values and different threshold values, despite the range of noise. Using chamfer distance to detect an object works well only if the object is roughly the same size and in the same orientation as the one in the image. If size changes or orientation changes, this algorithm would most likely fail. It helps to have a smaller sigma value for the template and a larger one for the image in order to equalize them. Overall, since this algorithm is computationally inefficient and doesnt account for size, shape, or orientation change, it is not a very good algorithm, except in very specific cases.

You might also like