【问题标题】:Shapley polygon to array for each pixel, not just boundaries为每个像素排列整齐的多边形,而不仅仅是边界
【发布时间】:2021-01-02 03:37:00
【问题描述】:

一个匀称多边形的形状可以很容易地通过使用转换为一个点数组

x,y = polygon.exterior.xy

但是,这只会返回实际点数。

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

因此,例如,宽度 = 100、高度 = 100 的多边形应生成 (100,100) 数组。

我可以通过获取外部点来做到这一点,然后遍历每个像素并查看它是在形状内部/形状上还是外部。不过我觉得应该有更简单的方法吧?

【问题讨论】:

  • 这能回答你的问题吗? SciPy Create 2D Polygon Mask。这与 Shapely 并没有真正的关系。但是,如果您有多边形顶点的坐标,您可以尝试链接提供的解决方案。我的猜测是其中一些会比您下面的解决方案更快。
  • 为什么需要一个包含 1 和 0 的数组?

标签: python shapely


【解决方案1】:

我不知道你的具体要求,但最快得到一般结果的应该是这样的:

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 )

【讨论】:

    【解决方案2】:

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

    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)
    

    【讨论】:

    猜你喜欢
    • 2020-11-23
    • 2018-07-21
    • 1970-01-01
    • 2021-06-12
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    相关资源
    最近更新 更多