Edge detection and contour extraction: A complete guide to Canny operator, Hough transform, and contour analysis
Introduction
Have you ever wondered: How do self-driving cars “see” lane lines? Why can the mobile app measure the diameter of coins with one click? ** Behind these scenes, there are two basic and hard-core computer vision technologies - edge detection and contour extraction.
A simple analogy:
- Edge: Just like the hook line in a pencil sketch, it is the place where the brightness or color changes the most in the image, usually corresponding to the boundary, shadow outline or texture change of the object.
- Contour: It is a closed curve obtained by connecting the points of these hook lines, which can directly circle the shape of the object we are interested in.
These two technologies are the "stepping stone" for traditional computer vision and are also the preliminary steps for many advanced vision tasks. By mastering them, you can quickly realize practical functions such as shape recognition, size measurement, and defect detection.
📂 Learning stage: Stage 1 — Cornerstone of image processing (traditional CV) 🔗 Related chapters: 图像增强与滤波 · 特征匹配实战
1. Basic concepts of edge detection
1.1 What is an edge?
In layman's terms, edges are those pixels in the image that have "obvious jumps in color or brightness." They often appear in:
- The intersection of object and background
- Transition areas between different materials or textures
- Shadow boundaries produced by lighting
- Location of color mutation
In these places, the brightness value of the image will change drastically, just like the "high frequency component" in the signal, so the edges are also regarded as the "high frequency signal" of the image.
1.2 Gradient: a tool for measuring change
The core idea of edge detection is to find the most intensely changing points on the image. We use a quantity called "gradient" to describe this change.
- Gradient Strength: How strong the change is (the larger the value, the more likely it is an edge)
- Gradient direction: In which direction the brightness changes (the direction of the edge is perpendicular to this direction)
The following example demonstrates the use of the Sobel operator to calculate gradient strength:
In actual projects, such a simple gradient map is not directly used as the final edge, because the place with strong gradient is not necessarily a clean single-pixel edge. The Canny operator introduced next is a complete optimization process.
2. Detailed explanation of Canny edge detection
The Canny operator is an optimal edge detection algorithm proposed by John F. Canny in 1986. Decades later, it is still the "gold standard" in the industry. A set of Canny operations can produce thin, accurate, and less noisy edge images.
2.1 Four key steps
Canny's process can be divided into four major steps:
- Gaussian filter: First smooth the image and suppress the noise. Noise can easily be misjudged as edges, and this step is equivalent to "skin grinding".
- Calculate gradient: Use the Sobel (or Scharr) operator to calculate the gradient strength and direction of each pixel.
- Non-maximum suppression (NMS): Remove those points that are "not local maximum" in the gradient map, retain only the centermost pixels on the edge, and make the thick edges thinner.
- Hysteresis Threshold: Set two thresholds, high and low. Points whose intensity is higher than the high threshold are directly confirmed as "true edges"; points whose intensity is lower than the low threshold are directly discarded; points in between will only be retained when they are connected to true edges, so that intermittent edges can be connected.
2.2 Code implementation and parameter tuning
When actually using Canny, the most troublesome thing is how to set the two thresholds. A very classic technique is to automatically calculate based on the median value of the image gradient, which can be adapted to different images:
- Gaussian kernel size: commonly used (3,3), (5,5) or (7,7). The larger the kernel, the stronger the smoothing effect, retaining only the most obvious large edges.
- Double Threshold Ratio: If set manually, it is recommended that High Threshold: Low Threshold be between 2:1 and 3:1, so that strong edges and weak edges can be better distinguished. :::
3. Detailed explanation of Hough transform
The edge map obtained by Canny only has pixel lines, but what we want is the semantics of "this is a straight line" and "this is a circle". Hough Transform is a feature extraction technology that specializes in detecting regular geometric shapes (straight lines, circles, ellipses, etc.) in images.
3.1 Core idea (taking a straight line as an example)
The Hough transform plays a "spatial voting" game:
- In the image space, there are countless straight lines passing through a point, which is difficult to find.
- But if you change the parameter space, each straight line can be described by a set of parameters (such as distance and angle in polar coordinates).
- A point in the image space will become a curve in the parameter space; multiple points on a straight line, the curves in the parameter space will converge to the same position (i.e. intersection point).
- So, as long as you look for the "busiest intersections" in the parameter space, you will find the most straight-line-like places in the image space.
In order to avoid the trouble of infinite slope of vertical lines, polar coordinate representation is always used in actual use: the distance ρ from the origin to the straight line and the angle θ between the normal and the x-axis are used to describe the straight line. After this transformation, we only need to vote in the ρ-θ grid.
3.2 Practical combat: Probabilistic Hough line detection
In actual engineering, we more commonly use the probabilistic Hough transform (HoughLinesP), because it is not only fast, but also directly returns the coordinates of the two endpoints of the line segment, which can be used to draw lines.
3.3 Hough circle detection
The parameters of a circle have one more radius than a straight line, turning it into a three-dimensional voting, and the calculation amount suddenly increases. Therefore, before detecting circles, it is strongly recommended to use Gaussian blur to denoise first.
:::info Advantages and Disadvantages of Hough Transform ✅ Advantages: Insensitive to local breaks of straight lines or circles, good noise resistance, and strong interpretability. ❌ Disadvantages: It is very sensitive to parameters, requires a large amount of calculation (especially circles), and is powerless for irregular shapes.
4. Contour extraction and analysis
Contour can be regarded as an "upgraded version" of edge detection - it can not only find boundary points, but also string these points into closed curves to directly obtain the outer frame of the object.
4.1 Basics of contour extraction
To extract contours, you must first turn the image into a binary image (black and white, the background is black, and the foreground is white). OpenCVfindContoursThe function returns a list of contours, each contour is a sequence of point coordinates.
4.2 Analysis of contour geometric features
With the contour, we can calculate many useful geometric features for shape recognition or size measurement.
These features can be combined into simple rules to determine shapes, such as:
- Rectangle: If the aspect ratio is close to 1, it is a square, otherwise it is an ordinary rectangle.
- Circle: If the roundness is greater than a certain threshold (such as 0.8), it can be judged as a circle.
5. Practical project: simple shape detector
Combined with the previous contour analysis and contour approximation (approxPolyDP), we can quickly build a simple shape detector to automatically identify common shapes such as triangles, rectangles, squares, circles, etc.
The logic of this detector is very simple, but it can handle a large number of standard shape recognition tasks. You can use it to test different pictures and feel the parameters (such as approximate accuracy0.04) on the results.
6. Summary
Edge detection and contour extraction are very core skills in traditional computer vision systems. The relationship and applicable scenarios between the three are summarized as follows:
It is strongly recommended that you use your hands to adjust parameters and observe with your own eyes the impact of parameter changes on the results. It is equally important to understand the limitations of each method: for example, the Hough transform is sensitive to noise and parameters, and contour extraction relies heavily on clear binary images. In actual projects, it is usually necessary to combine the steps of preprocessing (filtering, binarization) → edge detection → contour analysis to form a reliable processing pipeline.
🔗 Extended reading

