Python:联盟的交叉点

时间:2018-03-17 14:46:54

标签: python numpy scipy

我有以下问题。我尝试计算联合交叉,这是两个组件的重叠除以两个组件的联合。让我们假设component1是一个矩阵,其中第一个对象是1,component2是第二个对象所在的矩阵。 Overlap我可以用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)

2 个答案:

答案 0 :(得分:4)

如果您只处理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

答案 1 :(得分:1)

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)

此外,他对此进行了很好的解释。看看上面的链接。