Loading
0x50Lesson 6 of 16

Find edges with gradients

Measure how fast brightness changes, combine directions, and outline objects.

20 min 6-question quiz 1 code exercise
By the end of this lesson you can
  • Compute horizontal and vertical gradients with Sobel kernels
  • Combine them into an edge strength
  • Describe the steps of the Canny edge detector

An edge is where brightness changes quickly: the outline of a face, the side of a road, the border of a printed letter. Edges carry most of the shape information in an image - you recognize a cartoon from its outlines alone - so finding them was one of computer vision’s first problems.

Gradients in two directions

The gradient measures how fast brightness changes. The Sobel kernels estimate it in each direction:

  • Sobel x ([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) responds to left-right changes - vertical edges;
  • Sobel y ([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) responds to up-down changes - horizontal edges.

Combine them into one edge strength with Pythagoras: magnitude = √(gx² + gy²). The direction of the gradient, atan2(gy, gx), tells you which way the edge runs.

Try it

Edges in each direction

Switch between Vertical edges and Horizontal edges. Each finds different sides of the square - and the diagonal line shows up in both. Click a pixel on the diagonal and compare its two outputs.

Input (click a pixel in the output)
Kernel
Edit any weight to make your own filter.
Output (orange = negative)

Output at row 6, column 6 = sum of (pixel × weight) over the highlighted 3×3 window (pixels outside the image count as 0):

220×-1 + 220×0 + 220×1 + 20×-2 + 20×0 + 20×2 + 20×-1 + 20×0 + 220×1 = 200

magnitude.py
import math
gx, gy = 30, 40
print(math.hypot(gx, gy))
Output
50.0

The Canny edge detector

Raw gradients are noisy and thick. The classic Canny detector (1986) turns them into thin, clean outlines in four steps: blur to reduce noise, compute gradient magnitude and direction, keep only the local maxima across each edge (thinning), then keep strong edges plus weak edges connected to them (“hysteresis” with two thresholds).

Key takeaways

  • Edges are fast changes in brightness; the gradient measures them.

  • Sobel x/y measure change in each direction; √(gx² + gy²) gives the edge strength.

  • Canny = blur → gradients → thin → two-threshold hysteresis.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: apply computer vision with Python

Use small pixel arrays to explore vision concepts, run your code against sample images, and connect each result to the larger computer vision idea.

Exercise 1

Measure edge strength

+25 XP

Read a JSON 3×3 grayscale patch. Apply Sobel x and Sobel y to it (one output each) and print gx gy magnitude, with the magnitude rounded to 1 decimal.

  • Vertical edge
  • Diagonal
  • Flat
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: