Lulumoy Studio

Math guide

SVG vectorization

Turns the image into black-and-white vector paths: first it decides which pixels belong to the shapes (thresholding), then it follows their outlines, and finally it simplifies and smooths them. The steps, in the order they are applied, are as follows.

1. Working resolution and blur

The image is converted to 8-bit grayscale and enlarged s times (s = 1, 2 or 4) with bicubic interpolation. Working at a higher resolution places edges with sub-pixel accuracy, which greatly improves small images. If W·H·s² would exceed 4·107 pixels, s is reduced automatically. Then a Gaussian blur of σ·s is applied, if σ > 0. All parameters in pixels refer to pixels of the original image; internally they are multiplied by s.

2. Thresholding

F={p:I(p)≤T(p)}
F is the set of pixels that are traced (the dark shapes). With "Trace light shapes" its complement is used.

Fixed: T is the same for the whole image, the chosen level (0–255).

T∗=arg maxTω0(T)ω1(T)[μ0(T)−μ1(T)]2
Automatic (Otsu): chooses the T that maximises the between-class variance of the histogram (background and shapes). ω0, ω1 are the fractions of pixels on each side of T and μ0, μ1 their means. It works well when the histogram has two clear peaks.
T(p)=∑q∈B(p)g(q−p)I(q)−C
Adaptive: T varies from point to point; it is the local mean weighted by a b×b Gaussian window g, minus the constant C. It corrects uneven lighting and scan shadows. A pixel is traced if it is at least C levels darker than its surroundings.

3. Edge cleaning

F′=(F∘D)•D
Morphological opening and then closing with a disc D of diameter 2r + 1 pixels: opening removes specks and protrusions with radius below r, and closing fills holes and gaps of that size.

4. Outlines

cv::findContours follows the border of every shape and every hole (Suzuki and Abe algorithm). OpenCV returns the centres of the border pixels, so each outline is shifted half a pixel towards the background to run along the actual pixel edges. Without this step shapes would be one pixel thinner and 1-px lines would vanish.

A=12|∑i=1n(xiyi+1−xi+1yi)|
Outline area (shoelace formula). Shapes and holes with A below "Ignore specks" are discarded; if a shape is discarded, so are its holes.

5. Simplification

d(P)=|(B−A)×(P−A)|‖B−A‖
Ramer–Douglas–Peucker algorithm (cv::approxPolyDP): between two vertices A and B, if the intermediate point farthest from segment AB is at a distance d > ε, it is kept and the process repeats on each side; otherwise the whole stretch is replaced by the segment. No part of the simplified outline is farther than ε from the original. If a thin shape would end up with fewer than 3 vertices, ε is halved until it does not.

6. Curves and corners

θi=arccos(Vi−Vi−1)·(Vi+1−Vi)‖Vi−Vi−1‖‖Vi+1−Vi‖
Turning angle at each vertex Vi of the simplified polygon: 0° if the outline goes straight on, 180° if it doubles back.
B(t)=(1−t)2Mi−1+2(1−t)tVi+t2Mi,Mi=Vi+Vi+12,t∈[0,1]
With "Smooth curves", the path passes through the midpoints Mi of the sides and each vertex Vi with θi ≤ θcorner becomes the control point of a quadratic Bézier curve. At Mi the two neighbouring curves share the same tangent (the direction of the side), so the joint is smooth. Vertices with θi > θcorner are kept as sharp corners.

7. SVG file

All outlines form a single black path with the even-odd fill rule (fill-rule="evenodd"): a point is filled if a ray starting from it crosses the path an odd number of times, which is why holes are transparent. Coordinates are in pixels of the original image, with pixel (i, j) covering the square [i, i+1]×[j, j+1], written with two decimals. The "nodes" shown in the dialog are the segments (lines and curves) of the path.

Parameters

ParameterMeaning
Threshold methodHow T is computed (step 2).
Threshold level (fixed method)T for the fixed method, from 0 to 255.
Block size in px (adaptive method)b, window side for the adaptive method (odd). It must be larger than the strokes: about 2–5 times their width.
Offset C (adaptive method)C, in gray levels. Higher = only pixels clearly darker than their surroundings are traced (less noise).
Pre-blur (sigma, px)σ of the pre-blur (step 1). Reduces noise and rounds jagged edges.
Trace light shapes (invert)Traces light areas instead of dark ones.
Working resolutions (step 1).
Edge cleaning (px)r in step 3; 0 = no cleaning.
Ignore specks smaller than (px²)Minimum area in px² (step 4).
Simplification (px)ε in step 5, in pixels. 0 = no simplification: the SVG reproduces the thresholded image pixel by pixel.
OutlinePolygons or Bézier curves (step 6).
Keep corners sharper than (degrees)θcorner in degrees (step 6). 0 = all corners (polygon); 180 = all curves.
White background in the SVGAdds a white rectangle behind the path; otherwise the background is transparent.

See also: Morphology, Interpolation and borders

References

  1. OpenCV 4.x documentation — Miscellaneous Image Transformations (threshold, adaptiveThreshold). docs.opencv.org/4.x/d7/d1b/group__imgproc__misc.html
  2. N. Otsu. "A threshold selection method from gray-level histograms." IEEE Transactions on Systems, Man, and Cybernetics, 9(1):62–66, 1979.
  3. OpenCV 4.x documentation — Structural Analysis and Shape Descriptors (findContours, approxPolyDP). docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html
  4. S. Suzuki, K. Abe. "Topological structural analysis of digitized binary images by border following." Computer Vision, Graphics, and Image Processing, 30(1):32–46, 1985.
  5. D. H. Douglas, T. K. Peucker. "Algorithms for the reduction of the number of points required to represent a digitized line or its caricature." Cartographica, 10(2):112–122, 1973.
  6. W3C. Scalable Vector Graphics (SVG) 1.1 (Second Edition), 2011 — paths and the fill-rule property. www.w3.org/TR/SVG11/