Edge Detection

Image analysis deals with techniques for extracting information from images. The first step is generally to segment an image. Segmentation divides an image into its constituent parts or objects. For example in a military air-to-ground target acquisition application one may want to identify tanks on a road. The first step is to segment the road from the rest of the image and then to segment objects on the road for possible identification. Segmentation algorithms are usually based on one of two properties of gray-level values: discontinuity and similarity. For discontinuity, the approach is to partition an image based on abrupt changes in gray level. Objects of interest are isolated points, lines, and edges.

Definition of Edges

An edge is the boundary between two regions with relatively distinct gray-level properties. The idea underlying most edge-detection techniques is the computation of a relative derivative operator. The figure below illustrates this concept. The picture on the right shows an image of a light stripe on a dark background, the gray-level profile of a horizontal scan line, and the first and second derivatives of the profile. The first derivative is positive when the image changes from dark to light and zero where the image is constant. The second derivative is positive for the part of the transition associated with the dark side of the edge and negative for the transition associated with the light side of the edge. Thus the magnitude of the first derivative can be used to detect the presence of an edge in the image and the sign of the second derivative can be used to detect whether a pixel lies on the light or dark side of the edge.

Spatial Masks

We will use two dimensional spatial masks to find lines of different directions in an image. The mask is similar to the filters used in part 2 of this lab except that it operates in two dimensions. The mask is centered upon each pixel in the image and the following weighted calculation is performed (x is the original image pixels, y is the output):

Mask:

 w_1 w_2 w_3
w_4 w_5 w_6
w_7 w_8 w_9

The masks shown below respond more strongly to lines of different directions in an image. The first mask would respond very strongly to horizontal lines with a thickness of one pixel. The maximum response would occur when line passed through the middle row of the mask. The direction of a mask can be established by noting that the preferred direction is weighted with a larger coefficient that the other possible directions.

 

 (a)

 -1

-1

-1

2

2

2

-1

-1

-1

 (b)

 -1

-1

2

-1

2

-1

2

-1

-1

 (c)

 -1

2

-1

-1

2

-1

-1

2

-1

 (d)

2

-1

-1

-1

2

-1

-1

-1

2

Exercises Directional Edge Detection

  1. Load the image cicada.256. Using the matlab files spfilt.m and spfilter.m (downloaded here).
  2. Filter the image using filters (a), (c),) above. Describe your results for each case. Which edges were highlighted in each case? The matlab commands for the first case are given below.
>wa=[-1,-1,-1,2,2,2;-1,-1,-1]; 
>y_a=spfilt(cicada,wa); 
>y_a=round(y_a); 
>imagesc(y_a);colormap('gray(256)');
  1. Display your results by plotting the two output images together. Comment on your results.

Gradient Operators (this section is extra credit)

The gradient of an image is the vector, del(f) shown below. The gradient vector points to the direction of maximum rate of change of f at (x,y). In edge detection the important quantity is the magnitude of this vector which is usually referred to as the "gradient." The gradient is the maximum rate of increase of f(x,y) per unit distance in the direction of the gradient vector. This vector is commonly approximated with absolute values as shown below.

Derivatives may be implemented in digital form in many different ways. The Sobel operators given below provide a differentiating and smoothing effect. Computation of the gradient at the location at the center of the masks using the equations given below gives the value of the gradient. To get the next value the mask is moved to the next pixel location and the procedure is repeated.

 

 Gx

 -1

-2

-1

0

0

0

1

2

1

 Gy

 -1

0

1

-2

0

2

-1

0

1

Exercises: Gradient Operators

  1. Use the image ucsd.256 , apply the mask G_x using the matlab files spfilt.m and spfilter.m . Save the result.
  2. Apply the mask G_y
  3. Calculate the Gradient image using the approximation given above (the sum of the absolute values of the gradients.
  4. Plot the original image along with the gradient images on the same plot. Comment on the results for each mask, what types of edges gave a good response to reach filter, what happens when the results for Gx and Gy are combined in the gradient image? Compare the results with the edges from the previous section.
>G_x=[-1,-2,-1;0,0,0;1,2,1]; 
>f_x=spfilt(f,G_x);%gradient in the x direction 
>G_y=[-1,0,1;-2,0,2;-1,0,1]; 
>f_y=spfilt(f,G_y);%gradient in the y direction 
>grad=uint8(round(abs(double(f_x)+abs(double(f_y))));% calculates the gradient 
>imagesc([f,f_x;f_y,grad]);%displays all four images together
  1. Load the image ucsdn.256. Calculate the gradient of the image using the masks G_x and G_y. What happens to the edge detection results when the image is noisy? Would this technique be effective for noisy images?

The material in this lab is based on section 7.1 of the book Digital Image Processing, by R. C. Gonzalez and R. E. Woods, Addison-Wesley:Reading, MA, 1992.