Page 71 -
P. 71
Chapter 2 ■ Edge-Detection Techniques 45
The magnitude is computed in the same manner as it was for the gradient,
whichis infactwhatis being computed.
A complete C program for a Canny edge detector is given at the end of
this chapter, but some explanation is relevant at this point. The main program
opens the image file and reads it, and also reads in the parameters, such as
s. It then calls the function canny, which does most of the actual work. First,
canny computes the Gaussian filter mask (called gau in the program) and the
derivative of a Gaussian filter mask (called dgau). Thesizeofthe mask to be
used depends on s;for small s the Gaussian will quickly become zero, resulting
in a small mask. The program determines the needed mask size automatically.
Next, the function computes the convolution as in step 4 above. The C
function separable_convolution does this, being given the input image and
the mask and returning the x and y parts of the convolution (called smx and smy
in the program; these are floating-point 2D arrays). The convolution of step 5
above is then calculated by calling the C function dxy_seperable_convolution
twice, once for x and once for y. The resulting real images (called dx and dy
in the program) are the x and y components of the image convolved with
G’. The function norm will calculate the magnitude given any pair of x and y
components.
The final step in the edge detector is a little curious at first and needs some
explanation. The value of the pixels in M is large if they are edge pixels and
smaller if not, so thresholding could be used to show the edge pixels as white
and the background as black. This does not give very good results; what must
be done is to threshold the image based partly on the direction of the gradient
at each pixel. The basic idea is that edge pixels have a direction associated
with them; the magnitude of the gradient at an edge pixel should be greater
than the magnitude of the gradient of the pixels on each side of the edge. The
final step in the Canny edge detector is a non-maximum suppression step, where
pixels that are not local maxima are removed.
Figure 2.13 attempts to shed light on this process by using geometry.
Part a of this figure shows a 3x3 region centered on an edge pixel, which
in this case is vertical. The arrows indicate the direction of the gradient at
each pixel, and the length of the arrows is proportional to the magnitude
of the gradient. Here, non-maximal suppression means that the center pixel,
the one under consideration, must have a larger gradient magnitude than
its neighbors in the gradient direction; these are the two pixels marked with
an ‘‘x’’. That is: from the center pixel, travel in the direction of the gradient
until another pixel is encountered; this is the first neighbor. Now, again
starting at the center pixel, travel in the direction opposite to that of the
gradient until another pixel is encountered; this is the second neighbor.
Moving from one of these to the other passes though the edge pixel in a
direction that crosses the edge, so the gradient magnitude should be largest at
the edge pixel.