在numpy数组中绘制多边形 [英] Drawing polygons in numpy arrays
本文介绍了在numpy数组中绘制多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试绘制这样的多边形:
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), (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]])
不过,我希望得到以下输出:
I'd expect the following output though:
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)]
仍为零?还有另一种方法可以将填充的多边形绘制到数组上?
Why are [(10,2):(10:10)]
still zeros? Is there another way to draw a filled polygon to an array?
推荐答案
这是一个奇怪的结果.我发现,如果您颠倒了点的顺序,则会绘制出完整的图形.换句话说:
It is a strange result. I found that if you reverse the order of the points it draws the complete figure. In other words:
# 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]]
这篇关于在numpy数组中绘制多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文