Scan Line Fill Algorithm in Python / Numpy

I have thousands of polygons with four angular coordinates (quadrilaterals) and would like to convert them to a raster representation as a numpy 2d array. There are many grid algorithms, such as the popular scan graphics. (see http://www.cs.rit.edu/~icss571/filling/how_to.html or http://cs.uvm.edu/~rsnapp/teaching/cs274/lectures/scanlinefill.pdf )

Octave implements this in a poly2mask function (e.g. http://octave.sourceforge.net/image/function/poly2mask.html ).

Is there a similar feature in Numpy? I still do not understand how these algorithms work in detail, and thus, I would be very grateful if you could give me some tips on how to effectively implement it in Python / Numpy.

Or would it be better to code it in CPython (which I am not familiar with either) for speed reasons?

+3
source share
2 answers

A lean ecosystem (in order) has several different functions:

1) The most widely available option is to use matplotlib points_inside_poly. However, it is very suboptimal for filling in a regular grid (ie. This is an explicit point in the test of the polygon, and not the "scanline" approach).

2) mahotas fill_polygon, : http://mahotas.readthedocs.org/en/latest/polygon.html#drawing

3) skimage (scikits-image) draw.polygon, , : http://scikit-image.org/docs/dev/api/skimage.draw.html#skimage.draw.polygon

4) , PIL numpy. ImageDraw: http://effbot.org/imagingbook/imagedraw.htm

, skimage . . , scikits - , .

+6

OpenCV : cv2.fillPoly

+1

All Articles