Edge detection with Sobel
Outlines are where brightness changes fastest. How do you measure 'change' in a grid of numbers?
About 35 min · 3 goals · Checked by automated tests
1.Why should I care?
Edges outline objects, mark lane lines and show where a part ends. Edge detectors are some of the oldest and most reliable tools in robot vision, and they are a direct use of a derivative on sampled data.
- ◇ Explain an edge as a large brightness gradient and how Sobel kernels estimate it.
- ◇ Combine x and y responses into a gradient magnitude.
- ◇ Explain why blurring before edge detection helps with noise.
2.Watch
→ Watch how the two Sobel results are combined into one edge strength, and why the result is noisy without a blur first.
Shows how two small kernels measure brightness change across and down an image, and how they combine into an edge map.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
3.Predict
Sobel x is applied to a perfectly flat grey region. What is the output?
Guess first. Being wrong here is how the surprise works.
4.Discover
Choose Kernel then Sobel x on the Bright square: which edges light up? Switch to Sobel y. Then try both on the Gradient and on the Noisy bar. Click a pixel on an edge and one inside the square and compare the sums.
Loading the interactive…
Can't use the interactive? Read a description instead
With Sobel x on the Bright square, the output is large at the square's left and right sides (positive on the left edge, where brightness rises to the right, and negative on the right edge) and 0 inside and outside. Sobel y responds to the top and bottom edges instead. On the Gradient, Sobel x gives a constant 240 (8 × the slope of 30 per column) except at the borders.
5.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Gx = 30 and Gy = 40 at a pixel. What is the gradient magnitude?
6.Understand
Edges are gradients
Along a row, a sharp jump in brightness is a large difference between neighbouring pixels: a discrete derivative. Sobel x [[−1, 0, 1], [−2, 0, 2], [−1, 0, 1]] subtracts the left column from the right, giving extra weight to the middle row. Large positive or negative values mark vertical edges; Sobel y does the same for horizontal edges.
Strength and direction
Combine the two responses into the gradient magnitude √(Gx² + Gy²): how strong the edge is, whatever its direction. The angle atan2(Gy, Gx) gives the direction brightness increases. Threshold the magnitude to get an edge map.
Noise is the enemy
Differences amplify noise: two noisy neighbours differ even on a flat surface. Sobel's three-row design already averages a little; blurring first (often with a Gaussian) helps more. It is the image version of the warning that differentiating noisy data makes it noisier.
Explain it another way
Run your finger across a page in the dark: edges are where the height suddenly changes. Sobel feels for sudden brightness changes.
Sobel = (smoothing [1, 2, 1]ᵀ) ⊗ (central difference [−1, 0, 1]). Gradient magnitude |∇I| ≈ √(Gx² + Gy²); the Canny detector adds blur, thinning and two-level thresholding on top.
For a linear ramp I(x) = a·x, Sobel x gives (1 + 2 + 1) × (a·(x + 1) − a·(x − 1)) = 8a: proportional to the slope a.
Lane-keeping assistants have used edge detection on road images to find painted lines; rain, glare and worn paint are exactly the noise and contrast problems this lesson describes.
Worked example 1
At a pixel, Gx = 120 and Gy = −50. Find the edge strength.
√(120² + 50²) = √(14400 + 2500) = √16900 = 130.
Common mix-ups
- Edge detectors find objects.
✓ They find brightness changes: shadows, reflections and texture produce edges too. Deciding which edges belong to objects is a later step. - A negative Sobel value means no edge.
✓ The sign gives the direction of the change; the size gives its strength.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
Apply the box blur (all nine weights 1/9) kernel to the centre pixel of this 3×3 patch by multiplying matching positions and adding. Patch rows: [210, 50, 40] [210, 230, 0] [250, 10, 70]. What is the output value?
Practice 3
Sobel x on a ramp that rises by 10 per column (each row the same). What is the output?
Practice 4
Sobel y responds strongly but Sobel x is near zero. The edge is…
Practice 5
Edge detection on a noisy image finds edges everywhere. What should you try first?
8.Apply it
Run Sobel x and y on a photo of a doorway, compute the magnitude, threshold it at three values and report how many edge pixels each leaves. Repeat after a 3×3 blur and explain the difference.
9.Reflect
Concepts: Convolution and image filters · Derivatives and rates of change