Skimage Polygon function: Why does the last vertice repeats in the polygon documentation example?

会有一股神秘感。 提交于 2021-01-27 18:56:32

问题


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.array([1, 2, 8, 1])
c = np.array([1, 7, 4, 1])
rr, cc = polygon(r, c)
img[rr, cc] = 1
img
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

I have a few questions about this:

The r variable has the row coordinates, and the c variable has the column coordinates. From what I see, it means that there are 4 vertices like this: (1,1), (2,7), (8,4) and (1,1). But when I see the img array, it looks like a triangle... Shouldn't the total of vertices be 3 instead of 4?

If I remove the last vertice, and use the polygon function I get the same results.

r = np.array([1, 2, 8])
c = np.array([1, 7, 4])
rr, cc = polygon(r, c)  

# rr2 = array([2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 7])
# cc2 = array([1, 2, 3, 4, 5, 6, 3, 4, 5, 6, 3, 4, 5, 3, 4, 5, 4, 4])

r2 = np.array([1, 2, 8, 1])
c2 = np.array([1, 7, 4, 1])
rr2, cc2 = polygon(r2, c2)  

# rr2 = array([2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 7])
# cc2 = array([1, 2, 3, 4, 5, 6, 3, 4, 5, 6, 3, 4, 5, 3, 4, 5, 4, 4])

Why I get the same results? its ignoring the last vertice (1,1)?


回答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.



来源:https://stackoverflow.com/questions/55114005/skimage-polygon-function-why-does-the-last-vertice-repeats-in-the-polygon-docum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!