CV Lab 10 · Module 3 — Edge Detection

Gradients: Sobel & Scharr

An edge isn't just "bright vs dark" — it has a direction. Sobel and Scharr compute two numbers per pixel, Gx and Gy, that together form an arrow: how steeply intensity changes, and which way it's climbing. Magnitude tells you "is this an edge?" Angle tells you "which way does it run?"

Finding the gradient This patch has a hard edge running diagonally. The needle searches, wobbles, then locks onto the one direction the intensity is changing fastest — perpendicular to the edge itself. That lock is exactly what Gx and Gy compute, at every single pixel.
Ready.
Arrow density
22px
Arrow scale
0.70×
Min magnitude
15

The arrow field breathes gently while idle — that's just for legibility, the maths underneath is static per frame. Click anywhere to zoom into the exact 3×3 computation for that one pixel.

Gradient at clicked pixel
Gx =
Gy =
|G| =
θ = °

Two numbers, one arrow

Gx and Gy are just convolution

Sobel's Gx kernel looks left-vs-right; Gy looks top-vs-bottom. Each is the same sliding-window multiply-sum from Lab 06 — just with a kernel specifically shaped to detect "does the left side differ from the right" or "top vs bottom." Two convolutions per pixel, not one.

Magnitude vs direction

Treat (Gx, Gy) as the coordinates of a 2D vector. Its length — √(Gx²+Gy²) — is the magnitude: how strong the edge is. Its angle — atan2(Gy, Gx) — is the direction the intensity is climbing, always perpendicular to the edge line itself.

Why Scharr exists

Sobel's kernel weights (1,2,1) are a rough approximation of a smooth derivative — accurate for perfectly horizontal or vertical edges, slightly biased for diagonals. Scharr's weights (3,10,3) were specifically optimised to make the operator respond almost identically no matter which way the edge runs — true rotational symmetry.

Predict, then verify

You're building a system that must measure edge strength consistently, regardless of whether objects in the scene are tilted 0°, 30°, or 60°. Which operator is the safer choice?