从numpy数组中获取最大矩形面积

4
我想从类似于numpy数组的数据中获取最大矩形面积:
.. class   conf xmin   ymin   xmax   ymax
[[ 19.     0.78 102.79  98.85 258.17 282.53]
 [  7.     0.66 245.61 211.98 270.66 234.76]
 [  6.     0.56  -6.51 143.64  39.31 286.06]
 [  6.     0.5  103.77  94.07 256.6  278.14]
...]

目前我有:

def chooseBiggest(predictions):
    max = 0;
    index = 0;
    for i in range(len(predictions)):
        pred = predictions[i]
        area = (pred[4] - pred[2])*(pred[5] - pred[3])
        if area > max:
            max = area
            index = i

    return index

但我预计会有成千上万行甚至更多。是否有更有效的计算方法?


1
((predictions[:,4] - predictions[:,2])*(predictions[:,5] - predictions[:,3])).argmax()。 - Andras Deak -- Слава Україні
1个回答

4

您可以使用以下方法批量计算矩形的面积:

areas = (predictions[:,4] - predictions[:,2]) * (predictions[:,5] - predictions[:,3])

接下来,您可以使用以下方法获取具有最大面积的索引(行):

np.<b>argmax(</b>areas<b>)</b>

根据给出的样本数据,第一个矩形是最大的:
>>> predictions = np.array([[19., 0.78, 102.79, 98.85, 258.17, 282.53],
... [ 7., 0.66, 245.61, 211.98, 270.66, 234.76],
... [ 6., 0.56, -6.51, 143.64, 39.31, 286.06],
... [ 6., 0.5, 103.77, 94.07, 256.6, 278.14]])
>>> areas = (predictions[:,4] - predictions[:,2]) * (predictions[:,5] - predictions[:,3])
>>> areas
array([28540.1984,   570.639 ,  6525.6844, 28131.4181])
>>> np.argmax(areas)
0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接