Guangdong Juyao Intelligent Technology Co., Ltd. · Software & Algorithm Engineering
23°07′N 113°15′E · GUANGZHOU · PURE STATIC MONOGRAPH
All open source studies

opencv/opencv · 4.10.0 · Apache-2.0 · C++17

Which contour points does OpenCV keep?

Trace five points through approxPolyDP and see why raising the tolerance from 0.8 to 2.0 leaves just the endpoints.

How many vertices does a contour need?

Tracing a stroke in an image editor can produce so many samples that the path becomes difficult to edit by hand. OpenCV is an open source computer vision library. Its imgproc module provides cv::approxPolyDP, which takes ordered points and a tolerance in coordinate units and returns selected input vertices. We use release 4.10.0 and an open path with distinct endpoints.

Official OpenCV repository · 4.10.0 · Apache-2.0 license

Follow the farthest point

The grey path visits five points, green keeps the peak, and the dashed orange line connects the endpoints.
The orange line is offset for visibility. Both endpoints have y = 0 in the calculation.

Start with cv::approxPolyDP. Our vector<cv::Point> selects the integer-coordinate branch. Setting closed=false preserves the endpoints of this open path. The internal approxPolyDP_ keeps pending ranges on an explicit stack and finds the sample farthest from the line through each range’s endpoints.

The first line joins (0,0) and (4,0). The peak (2,2) is exactly 2 units away. At ε = 0.8, the algorithm keeps the peak and splits the range. The distance from (1,0) to the left line y = x is 1/√2 ≈ 0.707. The other side is symmetric, so both samples fall within tolerance. At ε = 2 the first comparison accepts equality and the endpoints suffice. The code squares the cross product and compares it with ε² times the squared endpoint distance, avoiding a square root.

A pass costs time proportional to the range length. Persistently uneven splits can take O(n²) overall. Accepting the first range takes O(n). The implementation also cleans up some nearly collinear vertices and uses a different initialization for closed paths. Read the stack, threshold comparison, cleanup and public entry point.

Build and run locally

From a new directory on macOS or Linux, use curl, tar, CMake and a C/C++ toolchain with C++17 support. The example links directly against core and imgproc in the build tree. We ran it on Apple Silicon macOS.

Download the complete C++17 example · CMakeLists.txt · Build commands as plain text

Read the example source
#include <opencv2/core/utility.hpp>
#include <opencv2/imgproc.hpp>

#include <iomanip>
#include <iostream>
#include <vector>

int main() {
    const std::vector<cv::Point> contour{{0, 0}, {1, 0}, {2, 2}, {3, 0}, {4, 0}};
    std::cout << "OpenCV " << cv::getVersionString() << '\n';

    for (const double epsilon : {0.8, 2.0}) {
        std::vector<cv::Point> simplified;
        cv::approxPolyDP(contour, simplified, epsilon, false);
        std::cout << "epsilon=" << std::fixed << std::setprecision(1) << epsilon;
        for (const auto& point : simplified) {
            std::cout << " (" << point.x << ',' << point.y << ')';
        }
        std::cout << '\n';
    }
}
mkdir opencv-study && cd opencv-study
curl -fsSL https://codeload.github.com/opencv/opencv/tar.gz/refs/tags/4.10.0 -o opencv.tgz
tar -xzf opencv.tgz
curl -fsSLO https://www.juyaoai.com/examples/opencv-contour-simplification/demo.cpp
curl -fsSLO https://www.juyaoai.com/examples/opencv-contour-simplification/CMakeLists.txt
cmake -S opencv-4.10.0 -B opencv-build \
  -DCMAKE_BUILD_TYPE=Release -DBUILD_LIST=core,imgproc \
  -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_EXAMPLES=OFF \
  -DBUILD_opencv_apps=OFF -DBUILD_JAVA=OFF \
  -DWITH_IPP=OFF -DWITH_ITT=OFF -DWITH_OPENCL=OFF \
  -DWITH_ADE=OFF -DWITH_LAPACK=OFF -DWITH_EIGEN=OFF \
  -DWITH_OBSENSOR=OFF -DWITH_OPENJPEG=OFF -DWITH_OPENEXR=OFF \
  -DWITH_JPEG=OFF -DWITH_PNG=OFF -DWITH_TIFF=OFF -DWITH_WEBP=OFF
cmake --build opencv-build --target opencv_imgproc --parallel 4

cmake -S . -B demo-build -DOpenCV_DIR="$PWD/opencv-build" -DCMAKE_BUILD_TYPE=Release
cmake --build demo-build
./demo-build/approx_poly_dp

Results and a follow-up experiment

OpenCV 4.10.0
epsilon=0.8 (0,0) (2,2) (4,0)
epsilon=2.0 (0,0) (4,0)

Change the first tolerance to 0.6 and rerun. Both side points now exceed the tolerance for their respective lines. For a second experiment, multiply every coordinate by ten and compare tolerances 0.8 and 8.0. The tolerance is measured in coordinate units, so a real image requires a choice of pixel scale and a definition of which details matter.