形狀多邊形的形狀可以通過使用輕松轉換為點數組x,y = polygon.exterior.xy但是,這僅返回實際點。如何將形狀多邊形轉換為數組,其中形狀外部的每個像素為 0,形狀內部的每個像素為 1?因此,舉例來說,寬度=100、高度=100 的多邊形應生成 (100,100) 數組。我可以通過獲取外部點,然后循環遍歷每個像素并查看它是在形狀內部/形狀上還是外部來做到這一點。但我認為應該有更簡單的方法?
2 回答

天涯盡頭無女友
TA貢獻1831條經驗 獲得超9個贊
我不知道你的具體要求,但最快獲得一般結果應該是這樣的:
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 )

FFIVE
TA貢獻1797條經驗 獲得超6個贊
這可以回答我的問題,但我想知道是否有更快的方法。
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)
添加回答
舉報
0/150
提交
取消