Skip to content

Vectorization

Status: Working experimental pipeline; broad drawing accuracy is not yet validated

Last reviewed: 2026-08-01

Code: Drawing Extraction Lab

Purpose

The vectorization work converts a raster engineering drawing into geometry that can be inspected, measured, and traced back to the pixels that produced it.

It is deliberately not a one-step "image to CAD" conversion. The code keeps intermediate evidence so a reviewer can see whether an error came from the ink mask, stroke graph, primitive fitting, or topology. It currently produces geometric and structural candidates; it does not yet claim complete engineering semantics or production-ready DXF reconstruction.

What The Code Does

flowchart LR
    A["Raster drawing"] --> B["Ink mask and ink likelihood"]
    B --> C["Hierarchical contours and skeleton"]
    C --> D["Stroke graph and stroke widths"]
    D --> E["Lines, arcs, and circles"]
    E --> F["Recovered weak and repeated circles"]
    F --> G["Contacts, intersections, and connectivity"]
    G --> H["Loops, repeated groups, frames, grids, and view candidates"]

The current whole-sheet pipeline performs these steps:

  1. Separate ink from paper. It converts the image to grayscale, keeps an ink-likelihood image, and creates a binary mask using either a white-tolerance threshold or Otsu thresholding.
  2. Preserve two shape representations. Hierarchical contours retain closed boundaries and holes. Skeletonization reduces strokes to one-pixel centerlines, while a distance transform records their original width.
  3. Build a stroke graph. Endpoints, junctions, cycles, and the pixel paths between them become stable graph nodes and edges.
  4. Fit local primitives. Graph edges are fitted as lines, arcs, or circles. Compatible line and curve fragments are merged, with residuals and supporting pixels kept for review.
  5. Recover geometry missed by local fitting. Short curved fragments (arclets), contour evidence, affine-aware circle fitting, concentric evidence, and repeated-pattern votes propose additional circles. A proposal is accepted only when the raster still supports it.
  6. Separate likely text loops. Small circle-like glyphs are retained as evidence but tagged as text candidates instead of silently becoming engineering circles.
  7. Build topology without rewriting geometry. Nearby endpoints, analytic intersections, local ink, and graph evidence are used to classify junctions, crossings, tangencies, and ambiguous contacts. Only accepted relationships create connected components.
  8. Propose structure. The code groups concentric and equal-radius circles, repeated geometry, collinear sets, closed loops, frames, grids, and possible drawing views. These remain auditable candidates rather than engineering meaning.

The extractor writes reviewable PNG, SVG, and JSON artifacts for the mask, skeleton, stroke graph, primitives, circle evidence, relationships, topology, structure, and the ordered review stages.

Techniques Tried And What We Learned

Technique Finding
Canny edges, Hough lines/circles, and contour heuristics Useful as a quick detector baseline, but separate detectors produced disconnected results with weak provenance. They were not a sufficient foundation for continuous vector geometry.
One binary mask followed by skeletonization Simple and auditable, and effective for strong ink. Faint, antialiased, compressed, or crossing geometry can fragment before fitting, so ink likelihood and the original pixels must remain available.
Independent line, arc, and circle fits Works for well-supported local strokes. It misses large or interrupted rings when each surviving fragment looks straight, and loose thresholds also promote text and annotation curves.
Annular pixel aggregation Produced plausible low-residual circles, but scattered pixels from neighboring rings and crossing lines could manufacture false models. Coherent graph-edge support and distributed angular coverage are stronger evidence than residual alone.
Affine-normalized circle fitting The main test raster was stretched vertically. Treating visible ellipses as affine-distorted circles recovered the concentric flange rings and removed forced isotropic circle overlays. Raster distortion must be modeled rather than hidden by wider tolerances.
Hierarchical contours and arclet center/radius voting Solved a proposal-generation gap: weak curved fragments can now propose circle centers instead of waiting for an already-known circle. This is more general than adding another disconnected Hough detector.
Concentric and repeated-pattern support Helped recover small repeated holes from weak evidence without hardcoding the number or location of holes. Pattern agreement can strengthen a proposal, but it cannot create geometry where local ink is absent.
Text-region witnesses and arc-coherence checks Reduced false circles from glyphs and rejected incoherent page-spanning arcs while preserving supported dashed and dimension arcs. Negative evidence needs to remain explicit.
Endpoint contacts followed by topology classification A nearby endpoint pair is only a hypothesis, not a connection. Clustering contacts, checking analytic intersections and local ink, and preserving ambiguous crossings avoids silently joining unrelated drawing elements.
Whole-sheet structural candidates One reviewed sheet produced a useful view candidate after an annotation-like false positive was repaired with a general geometry rule. This demonstrates the workflow, not broad accuracy.
Information-island and region-first experiments Region-first vectorization remains promising, but the tested crop methods produced too many incomplete, oversized, or merged regions. The whole-sheet vectorizer is therefore preserved as the baseline until region boundaries are reliable.

Current Findings

  • The staged pipeline is implemented and its checked behavior is deterministic and stable.
  • Every important geometric result can retain its pixel, contour, graph, fit, and relationship evidence; the browser is a reviewer, not the source of truth.
  • The strongest improvements came from generating better hypotheses, not from repeatedly loosening acceptance thresholds.
  • Affine distortion, text loops, crossing strokes, repeated geometry, and region boundaries are different problems and should not be collapsed into one confidence score.
  • Passing software tests proves stable behavior. It does not prove that drawings are interpreted accurately.
  • Broad accuracy is still unknown: the current store has only two indexed pages, there is no representative annotated topology truth set, and the required multi-drawing benchmark has not been completed.

Current Boundary

The protected whole-sheet vectorizer is the working baseline. Information Islands are being developed as a separate upstream way to find useful regions, but local region vectorization will not replace the baseline until the region proposal passes the fixed usefulness gate.

The next quality step is a representative set of clean, scanned, rotated, low-contrast, dense, and text-heavy drawings with human-reviewed primitive and topology truth. Until then, the system is implemented and verified, but not broadly validated.

Sources

  • Drawing Extraction Lab source
  • src/extraction_lab/vectorize/pipeline.py
  • src/extraction_lab/vectorize/mask.py
  • src/extraction_lab/vectorize/contours.py
  • src/extraction_lab/vectorize/skeleton.py
  • src/extraction_lab/vectorize/fit.py
  • src/extraction_lab/vectorize/topology*.py
  • Central CAD Intelligence MASTER_PLAN.md and TRACKING.md
  • Central decision DL-010 Information Regions Before View-Local Vectorization