为每个像素排列的形状多边形,而不仅仅是边界
Shapley polygon to array for each pixel, not just boundaries
使用
可以轻松地将形状多边形的形状转换为点数组
x,y = polygon.exterior.xy
不过,这returns只是实际点数。
如何将形状多边形转换为数组,形状外部的每个像素为 0,形状内部的每个像素为 1?
因此,例如,宽度为 100、高度为 100 的多边形应生成 (100,100) 数组。
我可以通过获取外部点,然后遍历每个像素并查看它是 inside/on 形状还是在外部来做到这一点。不过我觉得应该有更简单的方法吧?
这可以回答我的问题,但我想知道是否有更快的方法。
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)
我不知道你的确切要求,但最快得到一般结果应该是这样的:
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 )
使用
可以轻松地将形状多边形的形状转换为点数组x,y = polygon.exterior.xy
不过,这returns只是实际点数。
如何将形状多边形转换为数组,形状外部的每个像素为 0,形状内部的每个像素为 1?
因此,例如,宽度为 100、高度为 100 的多边形应生成 (100,100) 数组。
我可以通过获取外部点,然后遍历每个像素并查看它是 inside/on 形状还是在外部来做到这一点。不过我觉得应该有更简单的方法吧?
这可以回答我的问题,但我想知道是否有更快的方法。
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)
我不知道你的确切要求,但最快得到一般结果应该是这样的:
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 )