Anyone who has converted a raster floor plan or mechanical drawing into DXF knows the frustration of clicking a straight wall in AutoCAD. Visually, the drawing looks crisp at full-page zoom. Once you select an exterior wall to offset a partition or trim a doorway, dozens of grip points light up along a straight run, or only a three-millimeter sliver highlights while the rest stays unselected.
This fragmentation happens because raster images live on a discrete pixel grid. When a bitmap tracer walks a black stroke, slight scanner rotation, JPEG compression ringing, and anti-aliasing fringes cause the traced path to step by one pixel every few millimeters. Left untreated, those staircase steps turn clean geometry into thousands of redundant LWPOLYLINE vertices that bog down OSNAP, break OFFSET operations, and inflate file sizes.
Why Raw Pixel Tracing Produces Staircase Vertices
Consider a straight horizontal wall scanned on an A3 sheet at 200 DPI with a slight 0.8 degree paper skew on the flatbed glass. Across an 800 px horizontal run, that tiny skew shifts the vertical position by 800 * tan(0.8 deg) = 11.2 pixels. Because pixel grids only store integer samples, the binarized stroke drops by one pixel roughly every 71 pixels.
A naive contour or skeleton tracer faithfully records two corner vertices at each one-pixel step, plus extra micro-deflections wherever JPEG 8x8 DCT blocks introduce luminance noise along stroke edges. In real production tests on a standard residential floor plan image, raw tracing generated 3,712 vertices across the drawing. On a hand-drawn renovation survey that had already undergone AI line straightening, raw vector tracing still emitted 3,428 vertices across 178 polylines. Selecting a closet wall in CAD surfaced 15 separate collinear segments where a drafter expects one continuous span.
Perpendicular Segment Distance and the 1.5-Pixel Sweet Spot
Removing staircase noise while preserving legitimate architectural corners requires geometric polyline simplification governed by perpendicular point-to-chord distance. Given a candidate chord from start point A(x1, y1) to end point B(x2, y2), the shortest Euclidean distance from an intermediate vertex P(px, py) to the finite segment AB is computed by projecting AP onto AB:
dx = x2 - x1
dy = y2 - y1
length_sq = dx * dx + dy * dy
t = clamp(((px - x1) * dx + (py - y1) * dy) / length_sq, 0, 1)
proj_x = x1 + t * dx
proj_y = y1 + t * dy
dist = hypot(px - proj_x, py - proj_y)
Using Ramer-Douglas-Peucker (RDP) recursion, we find the intermediate vertex P_max with the largest perpendicular distance dist_max from chord AB:
- If
dist_max <= tolerance, every intermediate point deviates from a true straight line by no more than pixel quantization noise. All intermediate vertices are discarded, collapsing the entire chain into the two-point segment[A, B]. - If
dist_max > tolerance,P_maxrepresents a genuine structural bend or corner. The chain splits atP_maxand simplifies[A ... P_max]and[P_max ... B]recursively.
Why choose 1.5 pixels as the default collinear tolerance? Let us walk through a concrete numerical test:
-
Pixel jitter suppression (
dist <= 1.5 px): Suppose a straight wall runs fromA(120.0, 300.0)toB(520.0, 300.0), and scanner anti-aliasing nudges an intermediate vertex toP1(295.0, 301.2). Heret = 175 / 400 = 0.4375, anddist = 1.2 px. Even a diagonal one-pixel step (sqrt(1^2 + 1^2) = 1.414 px) falls inside1.5 px, soP1is cleanly merged into a single400 pxstraight segment. -
Structural step preservation (
dist > 1.5 px): Suppose the same wall contains a deliberate4 pxcolumn pilaster or door frame reveal atP2(295.0, 304.0). Nowdist = 4.0 px > 1.5 px. RDP immediately locksP2as a hard corner vertex and preserves the crisp architectural reveal.
Setting tolerance too low (0.4 px) leaves 1 px raster steps (0.707 px to 1.414 px) intact. Setting tolerance too high (4.0 px or 5.0 px) rounds off door jambs, window mullions, and 45-degree chamfers. A 1.5 px threshold sits above the sqrt(2) diagonal quantization ceiling of a bitmap grid while preserving intentional drafting features.
Solving the Closed-Ring Seam Problem in Room Contours
Open polylines have natural start and end terminals, making standard RDP straightforward. Architectural floor plans, however, are dominated by closed rings: interior room boundaries, structural columns, plumbing shafts, and exterior building footprints (LWPolylineFlags.Closed).
Running standard RDP directly on a closed ring [V0, V1, ..., Vn-1, V0] creates a classic geometric artifact. Because V0 and Vn-1 coincide, chord length is zero unless the ring is split, and wherever the tracer happened to start scanning the pixel loop becomes a permanently pinned vertex. If V0 sits halfway along a straight north wall, that wall retains an artificial mid-span grip point forever.
A robust closed-ring simplifier eliminates seam pinning in three deterministic steps:
-
Canonical anchor rotation: Strip duplicate closing points, then rotate the vertex array so index
0sits at the extreme top-left corner minimizingx + y(breaking ties by minimumx). Extreme bounding corners on rectilinear plans correspond to genuine geometric turns. -
Antipodal split: Scan the rotated ring for the vertex
R_farat maximum Euclidean distance fromR_0. Split the loop into two open chains (R_0 -> R_farandR_far -> R_0) and run1.5 pxRDP on each half. -
Iterative modulo-N seam sweep: Rejoin the halves and check every triplet
(prev, curr, next)moduloN. WheneverdistanceToSegment(curr, prev, next) <= 1.5 px(whileN > 3), removecurrand repeat until convergence.
In benchmark runs on real conversion outputs, applying this open-and-closed 1.5 px simplification pass cut floor plan vertex counts from 3,712 down to 2,468 (a 33.5% reduction) and reduced straightened hand-sketch vertices from 3,428 down to 2,227 (a 35.0% reduction), turning jagged wall chains into long, selectable straight lines without losing closed room topology.
Putting Clean JPG-to-DXF Geometry to Work
When converting compressed JPG scans or exported plan snapshots, image cleanup and collinear simplification have to work in tandem. JPEG compression introduces mosquito noise around dark lines; running image line cleanup before vectorization prevents artifacts from spawning stray islands, and the 1.5 px collinear pass merges residual pixel steps before DXF serialization. You can test this two-stage pipeline directly on the ImgToCAD image to CAD converter, which exports simplified LWPOLYLINE DXF and SVG files with optional two-point real-world unit calibration (mm, cm, or m).
Practical Checks After Importing Into AutoCAD
Converted raster files store 2D geometry (LWPOLYLINE entities on a flat layer), leaving semantic layer assignment and parametric annotations to your CAD editor. Before starting detailed design revisions:
- Click two or three primary exterior walls to confirm long continuous grip spans and clean corner intersections.
- Keep the original JPG on a faded background reference layer while spot-checking three critical dimension callouts, since numerals in the image arrive as traced linework.
- Assign structural walls, partition lines, and fixture symbols onto dedicated CAD layers as you build out the construction sheet.

Top comments (0)