Skip to content Skip to sidebar Skip to footer

Skimage Polygon Function: Why Does The Last Vertice Repeats In The Polygon Documentation Example?

I'm trying to understand how the polygon function works with this example of the documentation: from skimage.draw import polygon img = np.zeros((10, 10), dtype=np.uint8) r = np.arr

Solution 1:

The function polygon consumes two sequences, namely the row and column coordinates of the vertices of a polygon. You don't need to repeat the coordinates of the first vertex at the end of both sequences as they are assumed to define closed polygonal chains.

Having a look at the source code is insighful. Under the hood skimage.draw.polygon calls skimage._draw._polygon which in turn determines whether a pixel lies inside a polygon through a call to the helper function point_in_polygon. In this function there is a for loop which iterates over the line segments that make up the polygon. It clearly emerges from the code that the polygonal chain is enforced to be closed as the first line segment is defined by the vertices of indices n_vert - 1 and 0. As a consequence polygon([1, 2, 8, 1], [1, 7, 4, 1]) returns the coordinates of the pixels that lie inside the polygon defined by the following line segments:

(1, 1) - (1, 1)
(1, 1) - (2, 7)
(2, 7) - (8, 4)
(8, 4) - (1, 1)

while polygon([1, 2, 8], [1, 7, 4]) returns the coordinates of the pixels that lie inside the polygon defined by the following line segments

(8, 4) - (1, 1)
(1, 1) - (2, 7)
(2, 7) - (8, 4)

As the length of segment (1, 1) - (1, 1) is zero, both polygons are actually the same polygon. This is why you are getting the same results.

Post a Comment for "Skimage Polygon Function: Why Does The Last Vertice Repeats In The Polygon Documentation Example?"