Thursday 20 September 2018

qgis - Converting raster to vector by generating center lines?


I have a binary raster containing boundary pixels with the value 1 (white) and no-boundary pixels with the value 0 (black):


enter image description here


I would like to vectorize the raster by keeping one center line trough the boundary pixels. However, most polygonize methods I tried (e.g., gdalogr:polygonize, result in two lines around the outline of the boundary pixels (red lines) or no output (saga:gridskeletonization): enter image description here


Did I miss any method with which this can be easily done? Or do I have to vectorize the raster first to a vector layer containing two parallel lines and find a way from there to skeletonize/centerline the two lines?




Answer



Now it worked for me as @RoVo suggested before. The problem were the no-data values in my input raster. I changed these not with gdal_translate as @AndreJ suggests (in comments of answer above), but with GRASS r.mapcalc. Here the modules that I used:


Replace all no data values:


processing.runalg('grass:r.mapcalculator',
{"amap": gPb_rlayer,
"formula": "if(A>0, 1, null())",
"GRASS_REGION_PARAMETER": "%f,%f,%f,%f" % (xmin, xmax, ymin, ymax),
"GRASS_REGION_CELLSIZE_PARAMETER": 1,
"outfile": mapcalc})


Thin raster layer to thin non-null cells:


processing.runalg('grass7:r.thin',
{"input": mapcalc,
"GRASS_REGION_PARAMETER": "%f,%f,%f,%f" % (xmin, xmax, ymin, ymax),
"output": thinned})

Raster to vector conversion:


processing.runalg('grass7:r.to.vect',
{"input": thinned,
"type": 0,

"GRASS_OUTPUT_TYPE_PARAMETER": 2,
"GRASS_REGION_PARAMETER": "%f,%f,%f,%f" % (xmin, xmax, ymin, ymax),
"output": centerlines})

enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...