我正在尝试绘制这样的多边形:
In [1]: canvas = numpy.zeros((12, 12), dtype=int) In [2]: mahotas.polygon.fill_polygon( ...: [(1, 1), (1, 10), (10, 10), (10, 1)], ...: canvas) In [3]: canvas Out[3]: array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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]])
我希望得到以下输出:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
为什么[(10,2):(10:10)]仍然是零?还有另一种方法将填充的多边形绘制到数组吗?
[(10,2):(10:10)]
这是一个奇怪的结果。我发现,如果您颠倒了点的顺序,则会绘制出完整的图形。换一种说法:
# this is broken pts = [(1, 1), (1, 10), (10, 10), (10, 1)] # this works pts = [(1, 1), (10, 1), (10, 10), (1, 10)]
这是一个测试程序:
import numpy import mahotas.polygon def run(n, reverse=0): canvas = numpy.zeros((n, n), dtype=int) lim = n-2 print '\n%d x %d, lim=%d reverse=%d' % (n, n, lim, reverse) pts = [(1, 1), (1, lim), (lim, lim), (lim, 1), (1, 1)] if reverse: pts.reverse() mahotas.polygon.fill_polygon(pts, canvas) return canvas for rev in (0, 1): for n in range(3, 14): print run(n, rev)
例子:
6 x 6, lim=4 reverse=0 [[0 0 0 0 0 0] [0 1 0 0 1 0] [0 1 1 1 1 0] [0 1 1 1 1 0] [0 1 0 0 0 0] [0 0 0 0 0 0]] 6 x 6, lim=4 reverse=1 [[0 0 0 0 0 0] [0 1 1 1 1 0] [0 1 1 1 1 0] [0 1 1 1 1 0] [0 1 1 1 1 0] [0 0 0 0 0 0]]