【问题标题】:Intersection area between a numpy array and a shapely polygonnumpy 数组和形状多边形之间的交集区域
【发布时间】:2021-11-02 02:45:32
【问题描述】:

我想计算一个匀称的多边形和一个掩码(numpy.array)的交集面积:

plt.plot(*building.exterior.xy, color='brown', linewidth=4)
plt.plot(*parcelle.exterior.xy, color='orange', linewidth=4)
plt.imshow(thr, origin='upper', extent=[x_left, x_right, y_bottom, y_top], cmap='Greys')

我希望能够计算下面的绿色区域:

为此,我尝试使用此处描述的方法:How to transform contours obtained from OpenCV to SHP file polygons?

mask_to_polygons(np.flip(thr, 0))

plt.plot(*parcelle.exterior.xy)
plt.plot(*building.exterior.xy)

mask_pol = translate(mask_to_polygons(np.flip(thr, 0)), x_left, y_bottom)
for geo in mask_pol:
    plt.plot(*geo.exterior.xy, color = 'green')

但是,结果并不令人满意:

确实,MultiPolygon mask_pol 的边界不是 thr 数组的维度:

> mask_pol.bounds
(0.0, 0.0, 511.0, 511.0)
> thr.shape
(256, 256)

所以,我想知道从掩码切换到多项式的选项是否正确。我也可以尝试从匀称多项式切换到掩码。有没有更好的方法来完成这项任务?

【问题讨论】:

    标签: python numpy opencv matplotlib shapely


    【解决方案1】:

    经过几次尝试,我得出的结论是,最简单的方法是从匀称的多边形创建一个蒙版,使用rasterio。然后需要进行米-像素转换以获得总面积。

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2020-10-24
      • 2015-02-18
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      相关资源
      最近更新 更多