Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

An edge (French: contour) in an image is the frontier that delimits two objects. Therefore, edge detection is useful for identifying or measuring objects, or segmenting the image.

The advantage of using the derivatives

Edges are characterized by a rapid variation in the intensity of the pixels. Figure 1 represents the brightness profile along a horizontal line in the image. One clearly sees that the outline of the industrial piece shows a sudden decrease in the brightness of the pixels.

An image and the luminosity profile on it.

Figure 1:An image and the luminosity profile on it.

From this example, it appears that derivation is an efficient tool for highlighting the edges: an edge can be detected by analyzing the first derivative of the intensity profile, taken perpendicular to the edge. Similarly, an edge can be detected by determining the zero crossing of the second derivative.

The profile of  and its derivatives.

Figure 2:The profile of Figure 1 and its derivatives.

Because an image depends on two dimensions, the derivatives must be calculated according to the two axes. For example, the first derivative of an image ff consists of the two terms ∂f(x,y)∂x\frac{\partial f(x,y)}{\partial x} and ∂f(x,y)∂y\frac{\partial f(x,y)}{\partial y}. In addition, the derivatives are discrete differences in the case of digital images.

Thus the first derivative, also called “gradient” (French: gradient), is defined by:

∇f=(f(x+1,y)−f(x,y)f(x,y+1)−f(x,y))\nabla f = \begin{pmatrix} f(x+1,y) - f(x,y) \\ f(x,y+1) - f(x,y) \end{pmatrix}

In the same way, the second derivative, called “Laplacian” (French: laplacien), is defined by:

Δf=(f(x+1,y)−2f(x,y)+f(x−1,y)f(x,y+1)−2f(x,y)+f(x,y−1))\Delta f = \begin{pmatrix} f(x+1,y) - 2f(x,y) + f(x-1,y) \\ f(x,y+1) - 2f(x,y) + f(x,y-1) \end{pmatrix}

Gradient operators

Gradient operators are very simple methods for detecting edges. They use the first derivative and can be calculated using a convolution. Indeed, the first derivative along the xx axis of an image ff can be written as a convolution product:

f(x+1,y)−f(x,y)=∑m∑nhx(m,n)f(x−m,y−n)f(x+1,y) - f(x,y) = \sum_m \sum_n h_x(m,n) f(x-m,y-n)

where hxh_x is a convolution kernel such that:

Thus, the kernel hxh_x is:

hx=(+1−100)h_x = \begin{pmatrix} +1 & -1 \\ 0 & 0 \\ \end{pmatrix}

Similarly, the first derivative along the yy axis is written as the convolution of ff with the kernel hyh_y:

hy=(+10−10)h_y = \begin{pmatrix} +1 & 0 \\ -1 & 0 \\ \end{pmatrix}

Note that the row of 0 in hxh_x and the column of 0 in hyh_y allows having kernels of the same size. These two kernels are the very basic gradient operators and, in practice, variants of them are used.

Roberts operators

The Roberts operators are along the diagonals [Roberts 1965]:

hx=(+100−1)hy=(0+1−10)h_x= \begin{pmatrix} +1 & 0 \\ 0 & -1 \\ \end{pmatrix} \quad h_y= \begin{pmatrix} 0 & +1 \\ -1 & 0 \\ \end{pmatrix}

Prewitt operators

Another variant are the Prewitt operators [Prewitt 1970] which deal with a kernel with an odd size:

hx=(+10−1+10−1+10−1)hy=(+1+1+1000−1−1−1)h_x= \begin{pmatrix} +1 & 0 & -1 \\ +1 & 0 & -1 \\ +1 & 0 & -1 \\ \end{pmatrix} \quad h_y= \begin{pmatrix} +1 & +1 & +1 \\ 0 & 0 & 0 \\ -1 & -1 & -1 \\ \end{pmatrix}

Sobel operators

Finally, the Sobel operators [Sobel 1968] are a smoothed version of the Prewitt operators. Indeed, the coefficients reproduce a convolution by a Gaussian filter, which tends to play the role of a mean filter to attenuate noise:

hx=(+10−1+20−2+10−1)hy=(+1+2+1000−1−2−1)h_x= \begin{pmatrix} +1 & 0 & -1 \\ +2 & 0 & -2 \\ +1 & 0 & -1 \\ \end{pmatrix} \quad h_y= \begin{pmatrix} +1 & +2 & +1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \\ \end{pmatrix}

Note that the sum of the elements is zero, as the Roberts and Prewitt operators.

In Figure 3, one can see that hxh_x detects horizontal edges. For example, the top and bottom of the clock are clearly detected. The top edge is white because it corresponds to an edge from black to white. On the contrary, the bottom edge is black. On the other side, hyh_y detects vertical edges.

Horizontal and vertical Sobel operators.

Figure 3:Horizontal and vertical Sobel operators.

Magnitude and angle

From the operators above, we also define:

Magnitude and angle of the Sobel operator.

Figure 4:Magnitude and angle of the Sobel operator.

Thresholding

In some cases, it would be useful to detect the most important edges. To do that, one can threshold the magnitude to keep only the large values of the gradients. As you can see in Figure 5, the threshold makes the second hand disappear. Indeed, the magnitude is lower than other parts of the image because the second hand is gray and not black.

Thresholding the magnitude of the Sobel operator makes the second hands disappear!

Figure 5:Thresholding the magnitude of the Sobel operator makes the second hands disappear!

The impact of noise

Noise in an image essentially brings great variations in brightness between pixels. Therefore, the gradient operators is based on the derivative are very sensitive to the noise, as seen in Figure 6. Then it may be useful to denoise the image before edge detection. In figure Figure 7, a small mean filter is used before the Sobel filter: the result is much cleaner than without the use of the mean filter.

Consequence of noise on the brightness profile and its derivatives
(orange: non-noisy profiles, blue: noisy profiles).

Figure 6:Consequence of noise on the brightness profile and its derivatives (orange: non-noisy profiles, blue: noisy profiles).

Left: noisy image. Center: Sobel operator (magnitude). Right: Sobel operator applied on the result of a 3\times3 mean filter.

Figure 7:Left: noisy image. Center: Sobel operator (magnitude). Right: Sobel operator applied on the result of a 3×33\times3 mean filter.

Advanced methods

Following the gradient operators, new methods have been proposed to improve edge detection by taking into account the noise and the nature of the edges:

Marr-Hildreth Detector

The Marr-Hildreth detector consists of:

  1. apply a Gaussian filter gg on the image ff to reduce noise (this is similar to a mean filter),

  2. compute the Laplacian (second derivative) ℓ\ell on the softened image (this is implemented with a convolution),

  3. determine the zero crossings of the result.

As ℓ∗(g∗f)=(ℓ∗g)∗f\ell*(g*f) = (\ell*g)*f, the first two steps are merged into a single convolution by ℓ∗g\ell*g. Hence, the filter ℓ∗g\ell*g is the second derivative of a Gaussian:

(ℓ∗g)(x,y)=−[x2+y2−2σ2σ4]exp⁡(−x2+y22σ2).(\ell*g)(x,y) = - \left[\frac{x^2+y^2-2\sigma^2}{\sigma^4}\right] \exp\left(-\frac{x^2+y^2}{2\sigma^2}\right).

The filter ℓ∗g\ell*g is represented Figure 8. It is also called “LoG” for Laplacian of Gaussian (no French equivalent) or Mexican hat (for the resemblance to a sombrero).

Laplacian of Gaussian (left: as an image, right: profile along an axis).

Figure 8:Laplacian of Gaussian (left: as an image, right: profile along an axis).

The zero-crossings in the image resulting from the LoG convolution are given by searching for changes of sign in the intensity of two pixels. Figure 9 gives an example.

Marr-Hildreth Detector.
Left: original image,
center: result of the LoG filter,
right: detection of the zeros crossings.

Figure 9:Marr-Hildreth Detector. Left: original image, center: result of the LoG filter, right: detection of the zeros crossings.

Canny Detector

According to Canny, a good detector should serve the following purposes:

Canny expressed these goals in mathematical form and came up with optimal solutions verifying these goals. The Canny detector algorithm follows the four steps detailed below.

  1. The image ff is first smoothed with a Gaussian filter to reduce noise. This is done by using a convolution with a Gaussian kernel gg to obtain an image z=f∗gz = f*g.

  2. The gradient of the image is calculated (amplitude and angle):

    M=(hx∗z)2+(hy∗z)2andA=arctan⁡(hy∗zhx∗z)M = \sqrt{ (h_x*z)^2 + (h_y*z)^2 } \quad\text{and}\quad A = \arctan \left( \frac{h_y*z}{h_x*z} \right)
  3. Non-maxima are removed from the amplitude. This means that excessively large outlines in MM are replaced by thinner outlines. For this, we apply the algorithm below:

    1. For each pixel (x,y)(x,y) in MM:

      1. Choose the direction (vertical, horizontal or one of the two diagonals) the closest to A(x,y)A(x,y)

      2. If M(x,y)M(x,y) is lower than one of its neighbors in the chosen direction then cancel the gradient: M(x,y)=0M(x,y)=0

  4. The last step consists of thresholding by hysteresis for the bad edges. Two thresholds are therefore defined (Thigh>TlowT_\text{high} > T_\text{low}) and the algorithm below is applied:

    1. For each pixel (x,y)(x,y) in MM:

      1. If M(x,y)>ThighM(x,y) > T_\text{high} then (x,y)(x,y) is an edge

      2. If Tlow<M(x,y)<ThighT_\text{low} < M(x,y) < T_\text{high} then (x,y)(x,y) is an edge if and only if it is neighbor of an edge pixel

      3. If M(x,y)<TlowM(x,y) < T_\text{low} then (x,y)(x,y) is not an edge

Figure 11 gives an example of Canny edge detection.

Canny Detector.

Figure 11:Canny Detector.

Comparison

Figure 12 shows the results of the Sobel, Marr-Hildreth and Canny detectors on an image.

Comparison of Sobel, Marr-Hildreth and Canny detectors.

Figure 12:Comparison of Sobel, Marr-Hildreth and Canny detectors.

In Figure 13, which compares Marr-Hildreth and Canny detectors, one can see that the edges detected with Canny detector are better localized.

Comparison between Marr-Hildreth and Canny detectors (zoom).
The edges are shown in green.

Figure 13:Comparison between Marr-Hildreth and Canny detectors (zoom). The edges are shown in green.