Extracting line vectors from an image

Viewed 743

FP image Vectorized Example

I have a high-resolution image(Floor plan image), its pixel size is 9000 x 6000.

The image is very clean(have no noise) and all the lines' thickness is 1 pixel.

I need to convert this raster image to vector format such as dxf or geojson.

I found several tools to do this job like wintopo, potrace, and Qgis raster to vector function, but these are not an API(wintopo) or convert only to polygon, not line segments.

is there any way to extract line vectors from an image or, at least, find the line pixel coordinate? (I used OpenCV also but no function tracks pixels, only trace the boundary of the pixel set.)

1 Answers

There exist some learning-based approaches like these:

One python module is pypotrace. I've tried it with your picture:

# Require imagemagick
# require potrace 
# sudo apt-get install imagemagick potrace
import os
import argparse
import glob

parser = argparse.ArgumentParser()
parser.add_argument(
    "files", type=str, help="png2svg", nargs="*"
)

parser.add_argument(
    "-o", "--output", type=str, help="folder", default="./svgs/"
)


args = parser.parse_args()

for f in args.files:
    print f
    pnmname =  f.split(".")[0] + ".pnm"
    svgname =  os.path.basename(f).split(".")[0] + ".svg"
    os.system("convert %s %s" % (f, pnmname))
    if not os.path.exists(args.output):
        os.mkdir(args.output)
    svgname = os.path.join(args.output, svgname)
    os.system("potrace -s -o %s %s" % (svgname, pnmname))
    os.remove(pnmname)

Gist Source

The result is not so good, maybe you should upload one clean picture!

Related