gpt4 book ai didi

python - 从 numpy 数组中获取最大矩形区域

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:58 27 4
gpt4 key购买 nike

我想从 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

但我预计会有数千行甚至更多。有没有更有效的计算方法?

最佳答案

您可以批量计算矩形的面积:

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

关于python - 从 numpy 数组中获取最大矩形区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892045/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com