Edge detection

Formally, an edge can be thought of as an area with rapid change in intensity in an image.

Gradient operator

For continuous domains, we have the gradient operator

It gives the direction of change (a sort of 2D slope vector). This would be good for detecting edges, as by graphing out a gradient, we can identify the areas where there are sudden spikes, thus representing an edge.

However, images are not continuous functions, and therefore we will have to use an approximation of the gradient operator by taking differences.
For calculating, the x-component, we will need two adjacent pixels with the same ordinate.
For calculating, the y-component, we will need two adjacent pixels with the same abscissa.
So, we need 4 adjacent pixels, for computing the discrete gradient.

In simpler words, find the difference along the x direction and take their mean for partial derivated wrt x. Same for y, but find differences alon y direction. here is the distance between the centers of adjacent pixels. Since, this is a constant for all of the pixels in an image, it can be safely ignored.

The good thing with this discrete gradient computation, is that it can be made into a filter which can be convolved with the image. Now, we dont have to always choose pixels that are exactly adjacent. And depending on how we choose to calculate the discrete gradient, we have these methods of edge detection as discussed here.

Roberts Operator

The Roberts operator, computes the gradient in diagonal directions instead of x,y axes like we discussed above.

Now, since it looks at immediately adjacent pixels, it has good localization, but is also not resilient to noise.

Prewitt Operator

The Prewitt operator computes the gradient for a 3x3 block of pixels, while "skipping" a line in between. It calculates gradient in x and y direction.

Sobel operator

One shortcoming of the Prewitt operator, was that it gives equal weight to the pixels further away from the center. Sobel improves on Prewitt, by giving more weight to pixels closer to the center.

Laplacian operator

The Laplacian operator is a second order gradient. It is given by

For discrete signals, we calculate the difference of differences, that is the difference of gradients. So the smallest we can go is 3x3.

The standard laplacian filter is . But it doesn't take into account diagonal edges. So, this modified filter fixes that, is widely used .

Since the laplacian is essentially detecting zero crossing in the gradient (which is where the edges are), we will subtract 128 from (an 8bit) the image, to map .

With the laplacian however, we cannot figure out the orientation and magnitude of the edge. Because we lose that information when we take the dervative a second time.

Canny edge detection

So far, we understand that

  • The gradient operator gives us the orientation, magnitude and location (poorly compared to Laplacian) of the edge.
  • The laplacian operator gives us just a more accurate edge location and nothing else.
    Canny, basically takes the best from both of these. It uses the gradient operator to find the orientation of the edge (basically a vector that is orthogonal to the edge) and then applies the laplacian operator along that direction. So, it is less bothered by pixels farther away, and we get really accurate edges.