Shapley多边形为每个像素阵列,而不仅仅是边界



使用可以很容易地将形状多边形的形状转换为点阵列

x,y = polygon.exterior.xy

但是,这只返回实际的点。

如何将一个形状优美的多边形转换为数组,形状外部的每个像素都有0,形状内部的每个像素也有1?

因此,例如,宽度为100、高度为100的多边形应产生(100100(数组。

我可以通过获取外部点,然后遍历每个像素,看看它是在形状的内部/外部。但我认为应该有一个更简单的方法?

我不知道你的确切要求,但最快得到一般结果的应该是:

width = int(shape.bounds[2] - shape.bounds[0])
height = int(shape.bounds[3] - shape.bounds[1])
points = MultiPoint( [(x,y) for x in range(width) for y in range(height)] )
zeroes = points.difference( shape )
ones = points.intersection( shape )

这可以回答我的问题,但我想知道是否有更快的方法。

data = []
width = int(shape.bounds[2] - shape.bounds[0])
height = int(shape.bounds[3] - shape.bounds[1])
for y in range(0,height):
row = []
for x in range(0,width):
val = 1 if shape.convex_hull.contains(Point(x,y)) else 0
row.append(val)
data.append(row)

相关内容

最新更新