Imagine a robot inspecting a washer. A box around the washer shows where it is, but not whether its hole is missing or its edge is chipped. Contour detection follows the actual outline, giving the robot a shape it can measure.
1. What is Contour Detection?
Suppose a camera looks down at stamped washers on a bright conveyor. A detector can place a rectangle around each washer, which is enough to count them. But inspection asks more detailed questions: Is the outside edge round? Is the central hole present? Is a piece chipped from the rim?
To answer those questions, the system needs the shape itself. A contour is an ordered sequence of image coordinates that follows the boundary of a connected foreground region. Imagine tracing around the washer with a pencil without lifting the tip. The recorded path is its contour.
Because the boundary points remain in order, the robot can calculate area, perimeter, centroid, orientation, circularity, convexity, and local edge defects. A bounding box cannot preserve that information; it reduces the entire outline to four sides.
The contour algorithm does not know that the shape is a washer. It only knows which pixels are foreground and which are background. A shadow, oil stain, or glare can therefore create a perfectly valid—but meaningless—contour. This is why the story begins before contour detection: lighting, thresholding, and segmentation must first produce a useful binary image.
1.1 The Complete Journey From Pixels to a Measurable Shape
Binary image → borders and hierarchy
Retrieval mode chooses topology; chain approximation chooses retained boundary detail.
The Suzuki–Abe paper starts after another method has already separated foreground from background. Its input is therefore a binary image: for the washer example, white may mean metal and black may mean conveyor or hole. Lighting correction and thresholding matter, but they happen before the border-following algorithm shown here.
The algorithm reads the image row by row, much as text is read across a page. This orderly pass is called a raster scan. When the local pixel pattern changes from background to foreground, or from foreground into a hole, the algorithm recognizes the beginning of a new border.
It then pauses the scan and walks around neighboring boundary pixels until it returns to the starting configuration. Because the points are recorded in walking order, they form a usable outline rather than an unordered collection of edge pixels. Border identifiers also record which outlines contain other outlines, preserving the relationship between a washer’s rim and its central hole.
No dataset teaches these steps. The result is determined by the input mask, connectivity rules, retrieval mode, approximation method, and geometric filters chosen by the engineer. The next section follows the scan and border walk more closely.
1.2 First, Decide Which Pixels Belong to the Object
Before contour detection begins, thresholding or segmentation has already produced a binary image. Each pixel has only two possible meanings: foreground, which is the material or object of interest, and background, which is everything else. For the washer example, white pixels might represent metal and black pixels the conveyor and the opening in the washer.
The algorithm must also know what it means for two pixels to be neighbours. Under 4-connectivity, a pixel is connected only to pixels directly above, below, left, and right. Under 8-connectivity, diagonal neighbours count too. This choice changes the objects the algorithm sees: two regions touching only at a corner remain separate with 4-connectivity but become one region with 8-connectivity.
This is not a minor implementation detail. A thin diagonal wire may break apart under 4-connectivity, while two manufactured parts that touch at one corner may merge under 8-connectivity. Once foreground and connectivity have been defined, the algorithm knows what counts as one connected region. It can now search for that region’s boundary.
1.3 Next, Find Where a Boundary Begins
A raster scan is simply the familiar reading order used to visit an image: start at the top-left pixel, move across the first row, then continue row by row toward the bottom-right. The scan is not yet tracing anything. Its job is to find a place where the image changes between background and foreground.
Suppose the scan moves from a black background pixel to the first white pixel of the washer. That foreground pixel is a candidate start of an outer border. If the scan is already inside the white washer and encounters the first black pixel of its opening, it has found the start of a hole border.
Finding a start point solves only the first problem. The algorithm now knows where one boundary begins, but not which other pixels belong to it. That leads naturally to border following.
1.4 Then, Follow the Boundary in Order
Sequential border following means walking from one boundary pixel to a neighbouring boundary pixel while keeping the foreground region on a consistent side. At each step, the algorithm examines the local neighbourhood, selects the next boundary pixel, records it, and continues. It stops when it returns to the starting pixel with the same incoming direction.
The word sequential matters because the resulting coordinates are ordered along the shape. An edge detector might return a scattered set of edge pixels. Border following instead returns a path: point 20 comes after point 19 and before point 21. That order is what later makes perimeter, orientation, curvature, and polygon approximation possible.
After one border closes, the raster scan resumes where it left off and searches for the next unprocessed border. In this way, the algorithm alternates between scanning for a start and walking an entire boundary.
1.5 Finally, Record Which Borders Contain Other Borders
Think again about the washer. Its outside rim is an outer border because it encloses foreground material. The boundary around its opening is a hole border because it encloses background inside that material.
Suzuki and Abe assign a number to every discovered border. NBD, the current border number, identifies the boundary being followed. LNBD, the last border number encountered on the current scan line, provides the context needed to choose its parent. These bookkeeping labels let the algorithm recover topology: not only which boundaries exist, but which boundaries surround others.
The paper’s first algorithm recovers every border and its surroundness relationship. Its second algorithm keeps only outermost borders. The lighter representation is enough when the goal is simply to count separate parts. The full hierarchy is needed when an internal hole carries engineering meaning, as it does for washers, rings, sockets, and drilled components.
This combination of ordered geometry and hierarchy is the central contribution of Suzuki and Abe. Once the borders and their relationships are known, the next question is how much boundary detail the application should retain.
1.6 Choose How Much Boundary Detail to Keep
The raw contour may contain one point for almost every boundary pixel. Along a straight stamped edge, hundreds of those samples may describe the same line. Keeping them all preserves every small fluctuation, but it also increases memory, noise, and downstream computation.
Boundary approximation decides what to remember. A simple collinearity method removes redundant points along straight runs while retaining their endpoints. The Teh–Chin method uses local support and curvature to identify the dominant points of a digital curve.
This choice must follow the inspection requirement. If a two-pixel chip matters, aggressive simplification can erase the defect. If the robot only needs the overall orientation of a large plate, a compact polygon is easier to process than thousands of nearly identical points.
Interactive contour approximation
Trade boundary detail for a simpler polygon
Increase the permitted geometric error. Nearly straight samples disappear first; the chipped region survives until the tolerance becomes larger than its deviation.
1.7 Turn the Ordered Boundary Into Measurements
Once the contour has been extracted and simplified appropriately, it becomes measurable geometry. For vertices ordered around a closed polygon, the signed shoelace area is
Here, is the signed area, is the number of contour vertices, indexes a vertex, and is that vertex’s image coordinate. The equality closes the polygon by returning the final edge to the first vertex.
The sign reveals traversal direction; the magnitude estimates enclosed pixel area. The perimeter is the distance travelled while walking once around the boundary:
Here, is the perimeter in pixels; the remaining symbols use the same ordered vertices and index defined above.
Area and perimeter together produce the scale-independent compactness measure called circularity:
Here, is circularity, is the unsigned enclosed area, is the perimeter, and is the circle constant.
An ideal mathematical circle has ; elongated or irregular shapes have smaller values. Real circles also score below one because pixels form stair-stepped boundaries and image noise changes the perimeter. Image moments similarly provide centroid coordinates and , where is foreground area and and are first-order moments along the horizontal and vertical axes.
At this point the robot can compare the measured washer with its specification. But the measurements are still in pixels. A contour centroid becomes a robot pick point only after camera calibration and a depth or plane model connect image coordinates to physical coordinates. Shape extraction provides the geometry; calibration places that geometry in the robot’s world.
1.8 Convert Pixel Measurements Into Millimetres
Suppose the washer contour has an equivalent diameter of 120 pixels. That number is not yet a physical measurement. If a calibrated planar workcell provides a scale of mm per pixel at the conveyor plane, the estimate is
Here, is the estimated physical diameter; 120 px is the measured image diameter, and mm/px is the calibrated scale at the measurement plane.
This simple conversion is valid only when the image plane and measurement plane are related by the calibration model. Perspective makes scale vary across an oblique surface. Lens distortion bends straight boundaries near the image edge. Part height changes the distance to the camera. A robust inspection pipeline first undistorts the image, maps the relevant plane through a homography or full camera model, and then evaluates tolerances in metric coordinates.
Uncertainty should travel through that conversion. If thresholding moves the boundary by roughly one pixel, the diameter uses opposite sides of the contour and may vary by about two pixels. At mm per pixel, that alone contributes roughly mm of measurement variation before calibration and mechanical errors. A tolerance tighter than the measurement uncertainty cannot be enforced reliably by choosing more decimal places.
1.9 Build an Inspection Decision From the Measurements
The final robot decision usually compares several shape measurements rather than trusting one number. A washer might be accepted only when its outer diameter is in range, its hole has the correct parent contour, its circularity exceeds a threshold, and its centroid lies inside the expected pick region.
Establish those limits from representative good and bad parts. Save the mask and contour for every false accept and false reject. If failures cluster around glare, improve illumination or segmentation. If the raw contour is correct but a chip disappears, reduce approximation. If measurements drift by image location, revisit calibration. This layered debugging is one of classical vision’s strongest advantages: every intermediate representation has a physical interpretation.
1.10 When Contours Are the Right Representation
| Representation | Preserves | Best question |
|---|---|---|
| Edge map | Local intensity transitions | “Where does appearance change?” |
| Contour | Ordered region boundary | “What is this outline’s geometry?” |
| Bounding box | Coarse rectangular extent | “Where is the object approximately?” |
| Segmentation mask | Every classified pixel | “Which pixels belong to the object?” |
These representations answer different questions and often appear in the same pipeline. A learned detector can first locate the washer, segmentation can separate it from the conveyor, and contour extraction can perform precise geometric inspection. Use contours when the outline carries the answer. Use a semantic model when identity or appearance matters more than boundary shape.
Contour detection has no loss function because it is not learned from examples. Its equivalent engineering objective is explicit: produce a binary mask and connectivity rule for which the recovered boundary corresponds to the physical edge the robot needs to measure.
2. How to Use Contour Detection in the Telekinesis Agentic OS
Telekinesis provides contour extraction as the Retina Skill detect_contours. The code below loads a grayscale image, selects the retrieval and approximation modes, filters contours by area, and prints each returned boundary’s shape.
from telekinesis import datatypes, retina
image = datatypes.Image.from_url(
url=(
"https://assets.telekinesis.ai/examples/v1/images/"
"nuts_scattered_filtered_gaussian.png"
)
).to_grayscale()
contours = retina.detect_contours(
image=image,
retrieval_mode="retrieve_list",
approx_method="chain_approximate_simple",
min_area=200,
max_area=100_000,
)
print(f"Detected {len(contours.points)} contours")
for index, points in enumerate(contours.points):
print(index, points.shape) # (K_i, 2), with (x, y) coordinates
The output makes the variable-length geometry visible. Check that the traced boundaries follow the physical parts—not shadows, glare, or texture—before computing measurements from them.

The output follows the visible part boundaries. Whether an outline is useful still depends on the quality and meaning of the foreground image.
3. Benchmarking
3.1 Evidence Reported in the Original Research
The 1985 Suzuki–Abe paper predates COCO-style benchmark tables. It establishes algorithmic behavior and demonstrates component counting, shrinking, and topology extraction rather than reporting a portable AP or FPS score. A faithful summary is therefore qualitative:
| Paper evaluation dimension | Reported capability |
|---|---|
| Input | Digitized binary images |
| Output | Outer borders, hole borders, and surroundness relations |
| Algorithm style | Sequential border following during raster scan |
| Demonstrated uses | Component counting, shrinking, topological analysis |
| Important limitation | Result quality inherits the binary foreground definition |
For a robot workcell, benchmark boundary precision/recall against annotated masks, centroid and area error against metrology, false accept/reject rate, and end-to-end latency at the deployed resolution.
4. Where to Go Next?
5. References
- Suzuki, S., and Abe, K. “Topological Structural Analysis of Digitized Binary Images by Border Following”, Computer Vision, Graphics, and Image Processing, 1985.
- Teh, C.-H., and Chin, R. T. “On the Detection of Dominant Points on Digital Curves”, IEEE Transactions on Pattern Analysis and Machine Intelligence, 1989.
- Ramer, U. “An Iterative Procedure for the Polygonal Approximation of Plane Curves”, Computer Graphics and Image Processing, 1972.
- Douglas, D. H., and Peucker, T. K. “Algorithms for the Reduction of the Number of Points Required to Represent a Digitized Line or Its Caricature”, Cartographica, 1973.


