Edge Detection
An edge is not a line the camera drew, it is a place where brightness changes fast, and "fast" is a number you get to choose. Drag the threshold on a real photo and watch a coherent outline dissolve into noise, or vanish into nothing.
Worth reading first: Convolution and Filters
Point at any edge in a photo and you can name it instantly: the rim of a mug, the collar of a shirt, the line where a roof meets the sky. Nobody drew that line. It is not stored anywhere in the file. It is something you compute, and the computation is smaller than it sounds.
Brightness that changes fast
Take two adjacent pixels with values 118 and 121. Neighbours, three brightness levels apart, on a scale that runs to 255. Nothing happened there. Now take two adjacent pixels with values 30 and 210. That is not a gentle slope, it is a cliff, and a cliff in brightness between neighbouring pixels is the entire definition of an edge.
An edge is not a line the camera drew. It is a place where the brightness surface has a steep slope, and steepness is measured pixel to pixel, nowhere else.
Turning a slope into a line
The slope itself is called a gradient — at every pixel it has a magnitude (how steep) and a direction (which way brightness is rising fastest). Compute it by comparing each pixel to its right neighbour and its neighbour below, and add up how much they disagree. A flat wall of paint gives you a gradient magnitude near zero at every pixel inside it. The join between wall and doorway gives you a spike.
A gradient magnitude is still just a number, though, and a number is not a yes-or-no answer. The whole trick of edge detection is one more step: pick a cutoff, and call anything above it an edge. That single decision — a threshold — is what turns a field of slopes into a clean line drawing.
The threshold is a judgement call
There is no correct threshold, only a threshold that suits what you are looking for. Set it low and you catch every real edge — and also every camera sensor’s noise, every strand of fabric texture, every visual disagreement between two pixels that happened to land on different sides of a shadow. Set it high and the noise goes away, but so does a faint, real edge: the soft border of a shadow, a low-contrast seam between two similar greys.
Drag the threshold below and watch it happen on a ten-by-ten photo of a house. At the low end the whole outline shows. Somewhere past the middle, the faintest steps in the roofline are the first to disappear — not because they stopped being edges, but because you told the threshold not to count them anymore.
A ten-by-ten photo of a house, at exactly two brightness levels. Every cell below is marked once its gradient magnitude clears the threshold you set.
threshold 90 of a possible 360
Cells marked as edges
23 / 100At threshold 0, every cell that touches a brightness change counts, and the outline is continuous. Push the threshold past 180 and the single-step edges along the roofline drop out first, leaving only the sharpest corners. Past 360 nothing clears the bar at all.
What edges cannot tell you
Run this on any photo and you get a drawing made entirely of lines — and nothing else. An edge map has no idea what is on either side of a line. It cannot tell you the shape on the left is a dog. It cannot even tell you the line closes into a shape at all; a gap of a few missing pixels along one edge is enough to turn a closed outline into an open one, and the edge detector has no way to notice.
Key takeaways
- An edge is a place where brightness changes fast between neighbouring pixels, not a line anyone drew or stored.
- The gradient gives every pixel a magnitude and a direction; thresholding that magnitude is what turns a slope into a binary edge decision.
- A low threshold catches real edges and also noise and texture; a high one misses real but faint edges, such as a soft shadow boundary.
- There is no universally correct threshold, only one suited to a particular photo and a particular purpose.
- An edge map cannot tell you what is on either side of a line, or even that the line closes into a shape — later stages have to interpret it.
Quick check
Answer these to unlock the next chapter — 3 of 4 to pass. You can retake it anytime.
Answer every question to check.
Make a free account to read on
Every chapter is free — an account is how your progress, XP, and streak follow you from your laptop to your phone, and how you show up on the leaderboard. No payment, no trial.