Contour in Geometry for Programmers

In the context of image vectorization and geometry processing, a contour is a crucial concept used to delineate different regions of an image or a shape based on a threshold value. This wiki page explores the process of building, refining, and smoothing contours using Signed Distance Functions (SDF) and other techniques.

Building Contours on a Grid

The initial step in contour creation involves defining a grid over the SDF. Cells where the SDF at their central points are negative are identified, and a contour is constructed by creating segments along the edges of cells where neighboring cells have SDFs of different signs. This results in a contour made of axis-oriented segments, as illustrated in Figure 11.25.

Figure 11.25 Start from a contour that segregates the negative-valued cells from the positive-valued cells. Figure 11.25 Start from a contour that segregates the negative-valued cells from the positive-valued cells.

Attracting Segments to the Expected Curve

The initial contour obtained from the grid is often inaccurate. To improve its accuracy, each vertex of the contour is shifted by its own error value toward the direction inverse to the SDF's gradient. This adjustment helps the contour align more closely with the true border between negative and positive areas of the SDF, as shown in Figure 11.26.

Figure 11.26 Pulling the points from the original contour by function gradients and values makes the contour fit the 0-isoline better. Figure 11.26 Pulling the points from the original contour by function gradients and values makes the contour fit the 0-isoline better.

Implementing Contours

The implementation of contours involves several steps:

  1. Defining a Grid: Establish a grid over the SDF.
  2. Identifying Contour Segments: Detect segments based on changes in the SDF sign.
  3. Adjusting Vertices: Use the SDF gradient to adjust vertices for better accuracy.
  4. Converting Segments: Transform segments into parametric cubic curves.

Python code can be used to demonstrate these steps, including solving cubic equations and refining the contour into smooth segments.

Smoothing Contours

Smoothing a contour involves converting it into a smooth curve composed of Bezier pieces. This process uses the gradient of the interpolated image to find tangent values, which are then corrected to align with the segments. Each segment is described by a cubic polynomial piece. However, exaggerated gradients can lead to issues such as loops in the contour, as depicted in Figure 12.22.

Figure 12.22 Exaggerated tangent vectors spoil the contour. It’s still technically smooth and continuous, though. Figure 12.22 Exaggerated tangent vectors spoil the contour. It’s still technically smooth and continuous, though.

To mitigate this, tangent vectors are normalized to ensure they are proportionate to the pixel size, which helps maintain a smooth contour without unwanted loops, as shown in Figure 12.23.

Figure 12.23 By normalizing tangent vectors, we keep the contour smooth, but the unwanted loops are gone. Figure 12.23 By normalizing tangent vectors, we keep the contour smooth, but the unwanted loops are gone.

FAQ (Frequently asked questions)

What mathematical representation is used to smooth contours?