Drawing polygons in numpy arrays

前端 未结 3 1186
灰色年华
灰色年华 2020-12-19 12:22

I\'m trying to draw polygons like this:

In [1]: canvas = numpy.zeros((12, 12), dtype=int)

In [2]: mahotas.polygon.fill_polygon(
   ...: [(1, 1), (1, 10), (1         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 12:39

    If the polygons are always rectangles, then we only need two points and:

    import numpy
    canvas = numpy.zeros((12, 12), dtype=int)
    points = [(1, 1), (1, 10), (10, 10), (10, 1)]
    start_pt, end_pt = min(points), max(points)
    canvas[start_pt[1]:end_pt[1]+1, start_pt[0]:end_pt[0]+1] = 1
    

提交回复
热议问题