Edges with Sobel gradients

Intermediate

Introduction

Look at a cartoon: a few lines are enough to recognise a face. Those lines are edges, places where the brightness changes suddenly, such as the border between an object and its background. Edges carry much of the information in an image and are far smaller than the image itself, so many vision methods start by finding them. This lesson measures brightness change with the Sobel kernels and turns it into an edge map.

How it works

Derivatives on a grid. On a smooth curve, the slope is the derivative. On a grid of pixels, the simplest slope is a difference: right neighbour minus left neighbour. Differences are very sensitive to noise, so the Sobel operator also averages a little in the other direction, with weights 1, 2, 1:

gx kernel        gy kernel
-1  0  1         -1 -2 -1
-2  0  2          0  0  0
-1  0  1          1  2  1

gx is large where the image gets brighter to the right (a vertical edge); gy is large where it gets brighter downwards (a horizontal edge). Both are negative where brightness decreases. Flat areas give 0.

Magnitude and direction. The two numbers form the gradient vector. Its length

magnitude = √(gx² + gy²)

measures how strong the edge is, whatever its orientation. A cheaper approximation is |gx| + |gy|. Its angle, atan2(gy, gx), points the way brightness increases, which is across the edge: a vertical edge has direction 0° (pointing right) and a horizontal edge 90° (pointing down, because y grows downwards in images).

From gradients to edges. Thresholding the magnitude gives an edge map, but Sobel edges are usually two or three pixels thick and noisy. The Canny edge detector (1986) fixes both:

  1. Blur with a Gaussian to reduce noise.
  2. Compute Sobel gradients, magnitude and direction.
  3. Non-maximum suppression: round the direction to 0°, 45°, 90° or 135°, and keep a pixel only if its magnitude is at least as large as both neighbours along that direction. Thick ridges shrink to thin lines.
  4. Hysteresis: keep pixels above a high threshold, plus pixels above a low threshold that connect to them. Weak but continuous edges survive; isolated noise does not.

Example

import math

img = [
    [10, 10, 10, 10, 10, 10],
    [10, 10, 10, 10, 10, 10],
    [10, 10, 10, 90, 90, 90],
    [10, 10, 10, 90, 90, 90],
    [10, 10, 10, 90, 90, 90],
    [10, 10, 10, 90, 90, 90],
]


def sobel(p, r, c):
    gx = (p[r - 1][c + 1] + 2 * p[r][c + 1] + p[r + 1][c + 1]) - (p[r - 1][c - 1] + 2 * p[r][c - 1] + p[r + 1][c - 1])
    gy = (p[r + 1][c - 1] + 2 * p[r + 1][c] + p[r + 1][c + 1]) - (p[r - 1][c - 1] + 2 * p[r - 1][c] + p[r - 1][c + 1])
    return gx, gy


grads = [[sobel(img, r, c) for c in range(1, 5)] for r in range(1, 5)]
print("gx (change to the right):")
for row in grads:
    print("  ", [gx for gx, gy in row])
print("gy (change downwards):")
for row in grads:
    print("  ", [gy for gx, gy in row])
mag = [[int(math.sqrt(gx * gx + gy * gy) + 0.5) for gx, gy in row] for row in grads]
print("magnitude:")
for row in mag:
    print("  ", row)
print("edges (magnitude over 150):")
for row in mag:
    print("  ", "".join(["#" if m > 150 else "." for m in row]))
for r, c in [(3, 2), (1, 4), (2, 3)]:
    gx, gy = grads[r - 1][c - 1]
    angle = round(math.degrees(math.atan2(gy, gx)), 12)
    print(f"direction at row {r}, column {c}: {angle:.0f} degrees")
Open in compiler

Line by line

  1. The 6 × 6 image is dark (10) with a bright square (90) in the bottom-right, so it has a vertical edge, a horizontal edge and a corner.
  2. sobel applies both kernels at one pixel, written out as sums of the right column minus the left column (gx) and the bottom row minus the top row (gy).
  3. Gradients are computed for the 4 × 4 inner pixels, where the 3 × 3 window fits.
  4. gx is 320 in the two columns either side of the vertical edge and 0 elsewhere; it is smaller (80, 240) near the top, where the window only partly covers the square.
  5. gy is large only in the two rows on either side of the horizontal edge.
  6. The magnitude combines them; at the corner both are large, giving 339.
  7. Thresholding at 150 draws an L-shaped edge two pixels thick, which non-maximum suppression would thin.
  8. The direction is 0° across the vertical edge, 90° across the horizontal edge and 45° at the corner, where brightness increases to the right and down.

Expected result

gx (change to the right):
   [0, 80, 80, 0]
   [0, 240, 240, 0]
   [0, 320, 320, 0]
   [0, 320, 320, 0]
gy (change downwards):
   [0, 80, 240, 320]
   [0, 80, 240, 320]
   [0, 0, 0, 0]
   [0, 0, 0, 0]
magnitude:
   [0, 113, 253, 320]
   [0, 253, 339, 320]
   [0, 320, 320, 0]
   [0, 320, 320, 0]
edges (magnitude over 150):
   ..##
   .###
   .##.
   .##.
direction at row 3, column 2: 0 degrees
direction at row 1, column 4: 90 degrees
direction at row 2, column 3: 45 degrees

In the real world

Lane-keeping systems find lane markings from edges, document scanners find the page outline, and industrial inspection checks the edges of parts against their drawings. Canny is still used everywhere, and the first layers of trained neural networks learn kernels that look very much like Sobel's. In OpenCV (shown as text; it does not run on this site):

import cv2

gx = cv2.Sobel(grey, cv2.CV_64F, 1, 0, ksize=3)
gy = cv2.Sobel(grey, cv2.CV_64F, 0, 1, ksize=3)
magnitude = cv2.magnitude(gx, gy)
edges = cv2.Canny(grey, 100, 200)   # low and high hysteresis thresholds

The CV_64F output type keeps negative gradients; with an 8-bit output they would be cut to 0.

Common mistakes

  • Storing gradients in 8-bit images, which loses negative values and everything above 255.
  • Skipping the blur, so noise produces thousands of tiny edges.
  • Mixing up gx and gy: gx responds to vertical edges, gy to horizontal ones.
  • Forgetting that y points down, so a direction of 90° points down the image.
  • Choosing one fixed threshold for every image; lighting changes the gradient sizes.

Best practices

  • Blur first, then differentiate.
  • Use the magnitude for "how strong" and the direction for "which way", and keep both as numbers, not pixels.
  • Pick thresholds from the image (for example, a fraction of the largest magnitude) rather than fixed numbers.
  • Visualise the magnitude before thresholding to see what the detector sees.

Summary

An edge is where brightness changes fast. The Sobel kernels estimate the change to the right (gx) and downwards (gy), with a little smoothing. Their magnitude √(gx² + gy²) measures edge strength and atan2(gy, gx) gives the direction across the edge. Canny blurs, computes Sobel gradients, thins edges with non-maximum suppression and links them with two thresholds.

Quick check

1. A pixel has gx = 30 and gy = 40. What is its gradient magnitude?
2. Which kind of edge gives a large gx and a gy near zero?
3. What does non-maximum suppression do in Canny?
4. Why should gradients not be stored as 8-bit unsigned values?

Key takeaways

  • An edge is a place where the gradient magnitude is large.
  • Sobel's gx kernel is (−1, 0, 1) across, weighted 1, 2, 1 down; gy is the same turned on its side.
  • The gradient points across the edge, from dark to bright; Canny thins edges along that direction and links them with two thresholds.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.