【问题标题】:Python: Intersection over UnionPython:联合的交集
【发布时间】:2018-03-17 14:46:53
【问题描述】:

我有以下问题。我尝试计算联合上的交集,即两个分量的重叠除以两个分量的联合。让我们假设 component1 是一个矩阵,其中包含第一个对象,而 component2 是一个矩阵,其中包含第二个对象。我可以用np.logical_and(component == 1, component2 == 1) 计算重叠。但是我怎样才能计算联盟呢?我只对连接的对象感兴趣。

import numpy as np
component1 = np.array([[0,1,1],[0,1,1],[0,1,1]])
component2 = np.array([[1,1,0],[1,1,0],[1,1,0]])
overlap = np.logical_and(component == 1, component2 == 1)
union = ?
IOU = len(overlap)/len(union)

【问题讨论】:

  • 您希望从帖子中的输入中获得什么?
  • 你的意思是我想要的输出吗?这将是:IOU = 重叠/联合
  • 抱歉,是的,想要的输出..在这一切结束时不清楚你想要什么。
  • 我想要 scoure 或 Number,它是通过联合点的数量潜水的重叠点的数量。有了这个分数,我进行后处理计算

标签: python numpy scipy


【解决方案1】:

如果您只处理01,则使用布尔数组会更容易:

import numpy as np
component1 = np.array([[0,1,1],[0,1,1],[0,1,1]], dtype=bool)
component2 = np.array([[1,1,0],[1,1,0],[1,1,0]], dtype=bool)

overlap = component1*component2 # Logical AND
union = component1 + component2 # Logical OR

IOU = overlap.sum()/float(union.sum()) # Treats "True" as 1,
                                       # sums number of Trues
                                       # in overlap and union
                                       # and divides

>>> 1*overlap
array([[0, 1, 0],
       [0, 1, 0],
       [0, 1, 0]])
>>> 1*union
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])
>>> IOU
0.3333333333333333

【讨论】:

  • 啊,将数组视为布尔值是诀窍,非常感谢!
  • 我会在这里使用 np.count_nonzero 而不是 sum,因为它更快
【解决方案2】:

Oleksiimedium 中为您的问题提供了答案。

简单地说:

intersection = numpy.logical_and(result1, result2)

union = numpy.logical_or(result1, result2)

iou_score = numpy.sum(intersection) / numpy.sum(union)

print(‘IoU is %s’ % iou_score)

另外,他给出了很好的解释。看看上面的链接。

【讨论】:

    【解决方案3】:

    只需使用专用的 numpy 函数 intersect1dunion1d

    intersection = np.intersect1d(a, b)
    union = np.union1d(a, b)
    iou = intersection.shape[0] / union.shape[0]
    

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 2010-10-28
      • 1970-01-01
      • 2021-11-26
      • 2019-01-23
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 2017-12-18
      相关资源
      最近更新 更多