0

I have an Image (PNG) with simple polygons of different colours. I need to serialise these polygons given that the top left corner have the coordinates [0,0]. I already tried gdal_polygonize and I am almost happy with the result but there is one problem. Take a look at this triangle I would like to get the corner points and their sequence to know which of them are linked together. So the coordinates I expect are [30.0, 23.0 ], [ 30.0, 504.0 ], [ 511.0, 504.0 ], [ 30.0, 23.0 ]. I tried gdal_polygonize.py but when I try

gdal_polygonize -8 triangle.png triangle 

I get hundreds of coordinates because it counts every single pixel of the hypotenuse as a corner of the polygon. What can I do? There must be some algorithms for it but all I can find are algorithms to extract polygons from 3d objects and so on :(

I am not sure but maybe this is the solution for my problem but since I am not so good in R I would like to know, whether I can use an R-script and if so how does it have to look like?

Community
  • 1
  • 1
Selphiron
  • 897
  • 1
  • 12
  • 30

1 Answers1

1

gdal_polygonize is very exact, it follows the contour of every pixel. That makes the behavior easy and predictable. You want apparently some interpretation of your image, in this case that a pixelated diagonal is in fact a straight line, but this is not obvious so it really depends on your application.

For complex vectorization there are many options, you could look at Scikit image for example: http://freeconnection.blogspot.nl/2013/07/vectorize-image-with-python-scikit-image.html

For this simple example there are a few easier tricks, but it really depends on the actual application if they are useable.

Given your triangle image, and the result from gdal_polygonize. You can convert the output with ogr2ogr and provide a simplify value of 1.

ogr2ogr -simplify 1 output.shp input.shp

I think the value is provided in the units of the inputs, which given the lack of spatial reference system still is a 'pixel'.

Running ogrinfo on the result reveals the remaining vertices in the polygon:

ogrinfo -al triangle_sim.shp    
POLYGON ((30 23,30 504,511 504,511 503,30 23))

I'm not sure why there still is a 'single pixel' step remaining, from 511 504 to 511 503. It might be due to the nature of the simplification algorithm OGR uses. Raising the simplify tolerance doesn't seem to work. Its close to what you want though, you could try the same approach (using a simplification algorithm) with different software and see if you get the desired result.

Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97