OpenCV Contour Approximation: What Epsilon Keeps
2026-11-09

OpenCV Contour Approximation: What Epsilon Keeps

OpenCV contour approximation replaces a sampled outline with a smaller list of vertices. The function approxPolyDP uses a tolerance named epsilon to decide when a straight span can represent part of the input. The retained vertices can preserve a prominent turn while removing intermediate samples along a nearly straight section. The tolerance belongs to the coordinate units of the supplied contour.

The input is an ordered chain

An outline is more than a collection of points. The order connects each vertex to the next. For an open chain with different endpoints, the OpenCV 4.12 implementation begins with a candidate span joining the first and last vertices. It then tests intermediate vertices from that ordered range. An unordered point cloud does not provide the same sequence of segments.

The algorithm therefore works downstream of finding or supplying the contour. It can reduce an outline already represented as points. It does not decide which pixels constitute an edge in the original image. That separation is helpful when reading a vision pipeline in which edge detection, contour finding and contour approximation appear as different steps.

The largest deviation determines whether a span is split

For a candidate span, the implementation compares each intermediate point's cross-product magnitude against a threshold derived from epsilon and the endpoint difference. In a simple drawing where the perpendicular falls within the span, that comparison has an ordinary geometric interpretation. It asks whether the farthest point's perpendicular deviation is greater than the tolerance. The code uses squared quantities for this decision.

When the largest deviation is within tolerance, the candidate span can represent that part of the chain. When it exceeds the tolerance, the point responsible becomes a split point and the two ranges are examined separately. The endpoint line can therefore retain a strong turn while letting nearby points on either side be represented by longer spans.

Five points form an open chain, with a horizontal first candidate and a vertical distance at the center point.
With epsilon one, the center point causes a split. Source: Zhilianhai editorial desk, original AI-assisted concept; no real product, event or measured data.

A small example shows how epsilon changes the result

Consider five input vertices at (0,0), (2,1), (4,4), (6,1) and (8,0). The first candidate is the horizontal line between the two ends. The center point is four units from that line. With epsilon equal to one, the center point causes the first split.

After that split, the other two intermediate points are each about 0.71 units from the line through the corresponding new endpoints. Those deviations fit within one unit. In the example, the approximation keeps the two ends and the center. With epsilon equal to four, the first candidate already meets the tolerance and the two ends suffice.

Epsilon one retains three vertices and epsilon four retains only the end vertices.
A larger tolerance can allow the original endpoint span. Source: Zhilianhai editorial desk, original AI-assisted concept; no real product, event or measured data.

Closure changes how the chain is interpreted

The OpenCV tutorial specifies the third argument as the choice of a closed or open curve. A closed contour has a boundary that returns to its start. The implementation uses a different initial division for a closed input from the distinct-endpoint open case. A vision record can preserve the closure choice with the coordinates so a later reader can tell which outline was approximated.

The implementation also treats an open flag with identical first and last input coordinates as a special case when setting up its internal ranges. That is another reason to preserve both the flag and the actual point list. The simple example above has distinct endpoints. Its initial span does not depend on the rule used to set up a loop.

The tutorial illustrates epsilon values chosen as fractions of the contour's arc length. This is a way to select a distance tolerance based on the dimensions of a contour. The function itself receives the resulting numeric tolerance. Doubling all coordinates while using the same epsilon makes the threshold smaller relative to the resized outline.

In the five-point example, doubling the coordinates would put the center eight units from the first candidate line. A tolerance of four would then cause a split. Scaling both the coordinates and the tolerance by the same factor preserves the distance comparison in this example. This matters when a contour measured in image pixels is later represented in another coordinate scale.

Retain the tolerance and the intended use

A shape record can preserve the input point sequence, the coordinate scale, epsilon and whether the output is treated as closed. A downstream stage that fits a corner or counts vertices depends on the simplified outline it actually received. Knowing the tolerance makes it possible to interpret the absence of a small bend as a property of the approximation rather than a feature that was never present in the input.

The OpenCV 4.12 contour features tutorial shows how epsilon and closure are passed. The OpenCV 4.12 approximation implementation shows the split and threshold comparisons. Related material appears in the robot vision equipment section and the technical article directory.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

咨询 在线留言 电话:18028484626
微信 微信扫码添加我