Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 46 ยท Filtering and Edges

Gradients and Edge Detection

Understand image gradients, compute Sobel and Prewitt magnitude and direction, use the Laplacian, and build the Canny edge detector step by step in NumPy.

An edge is a place where the image changes quickly: the outline of a coin on a table, the border between sky and roof, the stroke of a letter. Edges carry most of the structure of a scene, which is why they are the starting point for finding shapes, lines, corners and objects. The key observation is simple: an edge is where brightness changes fast, and "how fast something changes" is a derivative.

In this lesson you will compute image derivatives (gradients) by differences, see how Sobel and Prewitt kernels do it with built-in smoothing, read off the strength and direction of an edge, try the second-derivative Laplacian, and then assemble the complete Canny detector, one stage at a time, in a few lines of NumPy and SciPy. You need the sliding-window idea from Convolution and Correlation and the smoothing filters from Blurring and Denoising.

Gradients: how fast does brightness change?

For a 1D row of pixels, the change at a position is the difference between the neighbours on either side. The central difference is (right - left) / 2. For a 2D image we compute it twice: Gx is the change along the columns (left to right) and Gy is the change along the rows (top to bottom). Together they form the gradient vector (Gx, Gy) at each pixel. It has two useful properties:

  • Its magnitude, sqrt(Gx^2 + Gy^2), says how strong the edge is.
  • Its direction, atan2(Gy, Gx), points toward the brighter side, perpendicular to the edge itself.
Python Finite differences on a step edge

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The response is 40 on two neighbouring pixels, the two that touch the jump (a 2-pixel-wide "edge band" is the price of a central difference). The magnitude is large only there, and the direction of 0 degrees says "brightness increases toward the right", as it should.

Sobel and Prewitt: difference plus smoothing

A bare difference reacts to noise as strongly as to real edges. The classic fix is to smooth in the direction across the difference. The Prewitt kernel does this with a uniform average of three rows; the Sobel kernel gives the centre row double weight, which is closer to a Gaussian:

TEXT
Sobel Gx            Sobel Gy            Prewitt Gx
| -1  0  1 |        | -1 -2 -1 |        | -1  0  1 |
| -2  0  2 |        |  0  0  0 |        | -1  0  1 |
| -1  0  1 |        |  1  2  1 |        | -1  0  1 |

Each is a separable product (see the end of the convolution lesson): a smoothing column times a difference row. Apply Gx and Gy with a correlation, combine them, and you have an edge map. SciPy provides ndimage.sobel(img, axis=...) and ndimage.prewitt(...); axis=1 gives Gx and axis=0 gives Gy.

Python Edge strength and direction for four orientations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Vertical and horizontal edges give a single non-zero component, and the angle tells you which way the brightness grows: 0 degrees toward the right, 90 degrees downward (the row index grows downward) and -90 degrees upward. The diagonal edge gets equal Gx and Gy (300 each) and an angle of 45 degrees, with magnitude 424.3 (300 * sqrt(2)). Sobel responds with 400 where Prewitt gives 300 on the same edge, because its positive weights add up to 4 instead of 3 (a step of 100 gives 4 * 100 and 3 * 100). Only the relative sizes matter when you threshold, so you can divide by the kernel gain whenever you want a more readable scale.

Note: The gradient direction is perpendicular to the edge. A vertical edge (a line running up and down) has a gradient pointing horizontally. Canny uses exactly this fact in its thinning step.

An edge map of a small scene

Let us put the magnitude to work on a scene with a rectangle and a disc, and see what smoothing does when noise is present.

Python Gradient magnitude with and without smoothing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On the clean scene the background level is exactly 0.00 and only the outlines glow. Adding noise lifts the flat background to 4.47, a hum that could be mistaken for faint edges. Smoothing the noisy image with a Gaussian before differentiating brings that floor down to 1.23, at the cost of a lower peak (44.2 instead of 83.3) and slightly wider, softer edges. Every serious edge detector therefore starts with a blur.

Roberts cross and how the operators compare

The oldest gradient operator is the Roberts cross (1963). It uses two 2x2 kernels that take the difference along the two diagonals:

TEXT
Gx = |  1   0 |      Gy = |  0   1 |
     |  0  -1 |           | -1   0 |

The magnitude is sqrt(Gx^2 + Gy^2), exactly like Sobel. Because the kernels are tiny, Roberts is extremely fast and gives thin, well-localized edges on clean images, but it has no smoothing at all, so it is very sensitive to noise, and its 2x2 window is centered between pixels rather than on one. Prewitt and Sobel use 3x3 kernels that smooth across the edge (Sobel weights the center row double), which makes them more robust.

Operator Kernel size Smoothing Noise sensitivity Typical use
Roberts cross 2x2 None High Fast, clean images, hardware pipelines
Prewitt 3x3 Equal weights Medium Simple edge maps
Sobel 3x3 Center weighted Lower General purpose, the step inside Canny
Scharr 3x3 Optimized weights Lower More accurate angles than Sobel
Laplacian 3x3 None (second order) Very high Zero crossings, blob finding after smoothing
Canny Multi-step Gaussian + hysteresis Low When thin, connected edges matter

Edge detectors fall into two families: gradient (first-derivative) methods look for maxima of the gradient magnitude (Roberts, Prewitt, Sobel, Scharr), and second-derivative methods look for zero crossings (Laplacian, LoG). Canny is a pipeline built on the first family.

Python Roberts, Prewitt and Sobel on a noisy step

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All three find the edge, but the 3x3 kernels give a much better ratio between the edge response and the noise in the flat area (about 5 against 3 for Roberts). Prewitt and Sobel are close on this image; Sobel simply has the larger raw response because its kernel weights add up to more. Smoothing across the edge is what helps, which is the practical reason the 3x3 operators, not Roberts, are used as the first step of Canny, and why you smooth before differentiating whenever noise is present.

The Laplacian: second derivatives

The gradient finds the slope of brightness. The Laplacian measures the curvature: the second derivative, summed over both directions. A common 3x3 kernel is:

TEXT
|  0   1   0 |
|  1  -4   1 |
|  0   1   0 |

The weights sum to 0, so a flat region gives 0. At a step edge the Laplacian produces a positive lobe on one side and a negative lobe on the other, and the edge sits at the zero crossing between them. The Laplacian does not give a direction, only a signed response, and it is strongly sensitive to noise because it differentiates twice. The compromise is to smooth first, which gives the Laplacian of Gaussian (LoG, ndimage.gaussian_laplace), the base of the blob detector you will meet in the scale-space lesson.

Python The Laplacian, zero crossings and noise

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The raw second difference gives +100 and -100 on the two sides of the step; a positive and a negative value meeting at the edge is the signature of a zero crossing. On pure noise with a standard deviation of 5.0 the Laplacian produces 22.2, a factor of about 4.4 (the square root of its squared weights, 1 + 1 + 1 + 1 + 16 = 20), while Sobel Gx gives 17.4 and a Gaussian smoothing pass cuts the Laplacian result to 1.7. That is why you smooth first.

Canny, step by step

In 1986 John Canny set out three goals for an edge detector: find real edges (few misses), place them where they truly are (good localisation), and report each edge once (thin, single-pixel lines). The algorithm he designed meets them with five stages:

flowchart LR
    A["1. Gaussian blur<br/>reduce noise"] --> B["2. Sobel gradient<br/>magnitude and angle"]
    B --> C["3. Non-maximum<br/>suppression: thin to 1 pixel"]
    C --> D["4. Double threshold<br/>strong and weak edges"]
    D --> E["5. Hysteresis:<br/>keep weak edges touching strong ones"]

Each Canny stage fixes a weakness of the one before it.

  1. Smooth with a Gaussian to suppress noise.
  2. Gradient: compute Gx, Gy, the magnitude and the angle.
  3. Non-maximum suppression (NMS): the gradient magnitude is a thick ridge across the edge. Keep a pixel only if its magnitude is at least as large as both neighbours along the gradient direction (the direction across the edge); otherwise set it to zero. The result is a one-pixel-thin line. Real implementations interpolate; the version below rounds the angle to one of four directions.
  4. Double threshold: pixels above a high threshold are strong (certainly edges). Pixels between the low and high thresholds are weak (maybe). Everything below the low threshold is discarded.
  5. Hysteresis: keep a weak pixel only if it is connected to a strong one. This follows edges that fade along their length without admitting isolated noise.

The scene below has a rectangle whose contrast fades from top to bottom (190 down to 75), so the bottom of its outline is weak, plus a dimmer disc. Single thresholding loses the faint parts; hysteresis recovers them.

Python The Canny detector from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The pipeline reduces 1,024 pixels to a thin outline in a few steps. After non-maximum suppression only 179 pixels remain non-zero, a single ridge pixel across each edge. The low threshold keeps 88, but only 68 are strong. Hysteresis then reconnects 16 of the 20 weak pixels that touch a strong one, such as the faint bottom of the rectangle, for a final 84 pixels, and it drops the 4 weak pixels that were not connected to anything. With one threshold of 20, the bottom of the rectangle and part of the disc are missing (68 pixels).

Tip: Typical thresholds are set relative to the gradient scale: high is about two to three times low. If real edges are broken, lower low; if noise specks survive, raise high. In OpenCV you would write cv2.Canny(gray, low, high) and get the same five stages in one call, but there the thresholds are in raw Sobel units, a different scale from the divide-by-8 units used here.

Where edge detection goes next

Edge maps feed many later tools: the Hough transform turns edge pixels into lines and circles; corner detection builds on the same gradients; and contour and shape measurement trace closed outlines. Before any of these, you often clean a mask with morphology. Edges are also fragile: shadows, textures and reflections create edges that do not belong to objects, and weak object boundaries can disappear. Deep networks learn more robust boundary features, but they start from the same idea, a filter that responds to local change.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. At a pixel, Gx = 3 and Gy = 4. What is the gradient magnitude?

    The magnitude is sqrt(Gx^2 + Gy^2) = sqrt(9 + 16) = 5.
  2. 2. The gradient direction at a point on a vertical edge (a line running up and down) is...

    The gradient points in the direction of fastest brightness change, which is across the edge. For a vertical edge that is horizontal.
  3. 3. Why does the Sobel kernel give double weight to the centre row (or column)?

    Each Sobel kernel is a smoothing vector [1, 2, 1] times a difference vector [-1, 0, 1], so it averages along the edge while differentiating across it.
  4. 4. What is the job of non-maximum suppression in the Canny detector?

    A gradient magnitude map is several pixels wide at each edge. NMS keeps a pixel only when it is at least as strong as its two neighbours along the gradient direction.
  5. 5. In hysteresis thresholding, which weak-edge pixels are kept?

    Weak pixels are accepted only when they are linked to a strong pixel. That recovers faint stretches of real edges and rejects isolated noise.

Summary

  • An edge is a place of fast brightness change, so detecting it means computing a derivative. The gradient (Gx, Gy) gives strength (sqrt(Gx^2 + Gy^2)) and a direction perpendicular to the edge.
  • Sobel and Prewitt kernels combine a difference with smoothing across it. In scipy.ndimage, sobel(img, axis=1) is Gx and axis=0 is Gy.
  • Differentiating amplifies noise, so smooth first. The Laplacian is a second derivative, marks edges as zero crossings and is especially noise-sensitive; the Laplacian of Gaussian fixes that.
  • Canny = Gaussian blur, Sobel gradient, non-maximum suppression, double threshold, hysteresis. The result is thin, connected edges.
  • Choose thresholds in the same units as your gradient, and judge the result visually.

Next up: clean up binary masks with Morphological Operations.