Perception · Theory · P2
P2-T5 — Edges, Contours and Simple Geometric Features
Edges mark where brightness changes. Shape measurements turn those boundaries into numbers a robot can use.
Most of the information in an image is in its changes: where a bright object ends and a dark floor begins. An edge detector finds those places by measuring how fast brightness changes from pixel to pixel. Contours follow the boundary of a region, and a few measurements on a region (area, centre, aspect ratio, circularity) already tell a robot whether something is a round part, a bar, or a speck. This lesson covers the gradient, the Sobel operator and why it amplifies noise, the Canny detector’s steps, and the shape measures, with a worked example.
Learning objectives
After this lesson, you should be able to:
- Compute the image gradient with the Sobel operator and state its magnitude and direction.
- Explain why gradients amplify noise and describe how smoothing and thresholds with hysteresis address it.
- Describe the stages of the Canny detector and what a contour is.
- Compute area, centroid, bounding-box aspect ratio and circularity of a region and say what each can and cannot distinguish.
The idea: look at the change, not the level
Walk along a row of pixels across the edge of a bright block. The values are low, then jump to high. A uniform surface, whatever its brightness, gives nearly constant values. What identifies the boundary is the change. Taking differences of neighbouring pixels converts “where is the boundary?” into “where is the difference large?”.
A derivative responds to change, and noise is change. That is why every edge detector smooths first.
Edges are useful for several reasons. They are less sensitive to a uniform change of lighting than raw brightness (an edge between two surfaces stays an edge when the light dims, though its strength falls). They mark the outlines of objects. And they are the starting point of many geometric methods: lines, corners (Module 4), outlines for shape measurement.
From edges to contours to numbers
- Smooth to suppress noise (Lesson P2-T3).
- Differentiate to get the gradient: its magnitude says how strong the change is; its direction says which way the intensity increases.
- Threshold the magnitude to keep strong edges, ideally with thin, connected results (Canny).
- Trace the boundaries to get contours: ordered lists of boundary pixels around a region.
- Measure the region: area, centroid, bounding box, perimeter, and ratios of these.
The mathematics: gradient, Sobel, Canny and shape measures
The gradient
The gradient of the image intensity I is the vector of its partial derivatives. Its magnitude and direction are
- Gx, Gy
- the rate of change of intensity along the columns and along the rows, in grey levels per pixel.
- |G|
- the edge strength at the pixel: large where brightness changes quickly.
- θ
- the gradient direction: perpendicular to the edge, pointing from dark to bright.
The Sobel operator
The derivative is approximated by a difference of neighbouring values, and the Sobel kernels combine that with a small amount of smoothing in the other direction:
Applying Kx gives Gx (the difference between the right and left columns, with the middle row counted twice), and Ky gives Gy. For a vertical step edge from level a to level b, Gx at the edge is (1 + 2 + 1)(b − a) = 4(b − a) and Gy = 0. The factor of 4 is the sum of the weights on one side; some libraries divide it out.
Noise amplification
If each pixel has independent noise of standard deviation σn, the difference of two neighbours has standard deviation √2 σn. The Sobel response sums eight weighted pixels, so its noise standard deviation is σn√(1 + 4 + 1 + 1 + 4 + 1) = σn√12 ≈ 3.5 σn, compared with a signal of 4(b − a) at a step. The signal-to-noise ratio of an edge is therefore about 4(b − a)/(3.5 σn) ≈ 1.15 (b − a)/σn: weak edges, with a step not much larger than the noise, cannot be separated from it by a threshold. Impulse noise is worse: one pixel with an extra 185 grey levels contributes up to 2 × 185 = 370 to the response of its neighbours, more than a genuine step of 90.
The Canny detector
The Canny detector (1986) is the standard pipeline built on the gradient. In outline:
- Smooth with a Gaussian to reduce noise.
- Compute the gradient magnitude and direction.
- Non-maximum suppression: keep only pixels that are a local maximum of |G| along the gradient direction, which thins edges to one pixel.
- Hysteresis thresholding: use two thresholds. Pixels above the high threshold are certain edges; pixels between the two are kept only if they connect to a certain edge. This keeps weak but connected edge segments and discards isolated noise.
Contours and shape measures
A contour is the boundary of a region, as an ordered list of pixels. For a binary region (a mask component) the quantities commonly used are:
- A
- the area in pixels (multiply by the pixel footprint squared for a physical area).
- centroid
- the mean position of the region’s pixels, a robust position for the object.
- aspect ratio
- a measure of elongation: near 1 for squares and discs, large for bars.
- P
- the perimeter. In a pixel image it depends strongly on how it is measured: counting pixel edges makes every diagonal boundary a staircase and overestimates its length, so treat circularity as approximate.
- C
- circularity: 1 for a perfect disc and smaller for other shapes. A square gives π/4 ≈ 0.785.
These are cheap, interpretable and invariant to translation; area and perimeter are not invariant to scale (the same object nearer is bigger), but aspect ratio and circularity are. Hough transforms extend the idea to find lines and circles from edge points by voting, and are a standard tool where the shape is known.
A worked example: Sobel on a step and shapes from a mask
Take a 3 × 3 patch across a vertical step from 60 on the left to 180 on the right:
Gx = (180 + 2·180 + 180) − (60 + 2·60 + 60) = 720 − 240 = 480. Gy = (60 + 2·60 + 60) − (60 + 2·60 + 60) = 0. So |G| = 480 and θ = 0° (the intensity increases to the right). With an edge threshold of 220 as in Lab P2-L3, this pixel is an edge; a hot pixel of 255 on a floor of 70 in a smooth region gives Gx up to 2 × 185 = 370 at its horizontal neighbours, which is also above 220 and produces a false edge. A median filter removes the hot pixel and the false edge before the gradient is taken.
Telling shapes apart with three numbers
Consider three regions found in a mask: a 6 × 6 square, a 3 × 12 bar, and a disc of diameter 7 pixels (the pixels whose centres lie within 3.5 pixels of the centre). The perimeter is counted as the number of pixel edges between the region and the background, and the bounding box gives the aspect ratio:
| Region | Area A | Perimeter P (pixel edges) | Aspect ratio | Circularity 4πA/P² |
|---|---|---|---|---|
| Square 6 × 6 | 36 | 24 | 1.0 | 0.785 |
| Bar 3 × 12 | 36 | 30 | 4.0 | 0.503 |
| Disc ⌀ 7 | 37 | 28 | 1.0 | 0.593 |
The areas of the square and the bar are equal (36), so area alone cannot tell them apart; aspect ratio does (1.0 against 4.0). The surprising entry is the disc: its circularity (0.59) is lower than the square’s (0.785), although a true disc has circularity 1 and a true square π/4 ≈ 0.785. The reason is the staircase: a curved boundary on a pixel grid is made of many small steps. Counting pixel edges overestimates the length of a boundary by 41% along a 45° run and by about 27% on average over all directions (4/π, the mean of |cos θ| + |sin θ|), while a boundary aligned with the grid is measured exactly. A disc is penalised on every part of its outline and a grid-aligned square on none, so the disc’s ideal circularity of 1 becomes about 1/1.27² ≈ 0.62 (measured: 0.59), while the square keeps its ideal 0.785. At this size, circularity from pixel counts is not a reliable shape test; it needs regions that are tens of pixels across and a perimeter measured along a smoothed contour.
The lesson for design: choose a measure that separates the classes you actually have. For a robot sorting bars from cubes, aspect ratio is enough. For round against square parts it takes more pixels, or the corners found in Module 4.
Engineering implications and limitations
Smooth, then differentiate. The order in the pipeline is not optional. Choose the smoothing for the noise (median for impulses, Gaussian for fine grain), and scale it to the size of the edges you want.
Use hysteresis rather than a single threshold. A high threshold alone breaks real edges into pieces; a low threshold alone keeps noise. Two thresholds with connectivity give long edges with few false ones. The thresholds are in units of gradient magnitude and depend on the contrast and noise of your images.
Remember edges are not objects. Shadows, textures, reflections, printed patterns and the seams of a floor all produce edges. Edges tell you where brightness changes, not where objects end. They are a cue to combine with others.
Measure shapes in physical units when you can. Convert areas and lengths with the known pixel footprint (or the camera model) and compare against the real dimensions of the objects. A rule such as “a part has an area of 20 to 30 cm²” survives a change of zoom or distance; “area between 300 and 450 pixels” does not.
Know the regime of validity. Shape measures such as circularity break for regions of a few pixels, for regions cut by the image border, and for partially occluded objects. A shape-based classifier needs an estimate of how many pixels the object has and must refuse to decide when there are too few.
Edge strength falls with the light. A threshold on |G| tuned on a bright image misses edges in a dim one for the same reason an intensity threshold does (Lab P2-L2). Normalise by local contrast, or tune on the darkest condition you need.
Common misconceptions
“An edge is where an object ends.”
An edge is where brightness (or colour) changes quickly. It occurs at object boundaries, and also at shadows, textures, specular highlights and printed patterns, and an object boundary with no brightness change produces no edge.
“A derivative filter is a sharpening filter, so it improves the image.”
It emphasises change, including noise. Without smoothing it makes noisy images worse, which is why detectors smooth first.
“Circularity of 1 means the region is a circle.”
It is 1 for an ideal disc in continuous geometry. On a pixel grid the perimeter is miscounted and small regions can score above 1 for any shape. It is a comparison between regions of similar size, not an absolute test.
“A larger edge threshold always gives cleaner edges.”
It removes noise and also weak real edges, and breaks long edges into pieces. Hysteresis does better.
Engineering takeaways
- Edges are where brightness changes quickly; the gradient measures it: |G| = √(Gx² + Gy²), θ = atan2(Gy, Gx).
- Sobel gives Gx = 4(b − a) at an ideal step; it amplifies noise about 3.5×, and impulse noise far more, so smooth first.
- Canny = Gaussian smoothing, gradient, non-maximum suppression and hysteresis thresholds; it yields thin, connected edges.
- Contours and shape measures (area, centroid, aspect ratio, circularity) turn regions into numbers; pick measures that separate your classes and use enough pixels.
- Edges mark brightness changes, not objects; combine them with other cues and tune thresholds for the darkest condition.
References and further reading
Classical sources; the tables were computed for this lesson from the stated regions.
- Canny, J. (1986). A computational approach to edge detection. IEEE Transactions on Pattern Analysis and Machine Intelligence 8(6): the Canny detector.
- Sobel, I. and Feldman, G. (1968). A 3×3 isotropic gradient operator for image processing. Presented at the Stanford Artificial Intelligence Project: the operator, as commonly cited.
- Gonzalez, R. C. and Woods, R. E. (2018). Digital Image Processing, 4th ed. Pearson: edge detection, boundary description and region descriptors.
- Duda, R. O. and Hart, P. E. (1972). Use of the Hough transformation to detect lines and curves in pictures. Communications of the ACM 15(1).
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A vertical step has levels 40 on the left and 100 on the right. What are Gx, Gy and |G| from the Sobel kernels at the edge, and in which direction does the gradient point?
Explanation
Gx = 4 × (100 − 40) = 240, Gy = 0, so |G| = 240, and the gradient points to the right (θ = 0°), perpendicular to the edge and towards the brighter side.
Question 2
Why can Canny’s hysteresis keep a weak edge segment that a single threshold would drop, without keeping isolated noise?
Explanation
It accepts weak pixels only when they are connected to a strong edge pixel. A weak segment of a real edge usually touches a strong part and is kept; isolated noise pixels do not connect to a strong edge and are discarded.
Question 3
A bar of area 36 and a square of area 36 appear in your mask. Which of the shape measures in this lesson separates them with the least effort, and what do you need to check before trusting it?
Explanation
The bounding-box aspect ratio: about 4 for the 3 × 12 bar and 1 for the 6 × 6 square. Check that each region has enough pixels (tens across), that it is not cut by the image border, and that it is not partly occluded, since any of these changes the box.
What’s next
Next: when it all breaks: the lighting, exposure, noise, shadow and glare failures that defeat image preprocessing, and how to diagnose them.
Related content
Nothing below is required to finish this lesson.
- BeforeP2-T4: Thresholding and Segmentation
Segment an image with fixed and adaptive thresholds and connected components.
- LabP2-L3: Diagnose a Failing Image-Processing Pipeline
Find which stage of a filter-threshold-contour pipeline fails and repair it.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.