我有以下问题。我尝试计算Union上的交集,它是两个组件的重叠除以两个组件的单位单位。让我们假设component1是一个矩阵,其中第一个对象是1,component2是一个矩阵,第二个对象是1。我可以用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)发布于 2018-03-18 00:20:56
如果您只处理0和1,则使用布尔数组会更容易:
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发布于 2021-01-26 23:21:46
Oleksii在medium中为您的问题提供了答案。
简单地说:
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)此外,他对此做了很好的解释。看看上面的链接。
发布于 2021-09-13 09:44:56
只需使用专用的numpy函数intersect1d和union1d
intersection = np.intersect1d(a, b)
union = np.union1d(a, b)
iou = intersection.shape[0] / union.shape[0]https://stackoverflow.com/questions/49338166
复制相似问题