[英]Get max rectangle area from numpy array
I want to get max rectangle area from numpy array that looks like that:我想从 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]
...]
For now I have:现在我有:
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
But I expect many thousands of rows or even more.但我预计会有数千行甚至更多。 Is there a more efficient way of calculating this?有没有更有效的计算方法?
You can calculate the area of the rectangles in bulk with:您可以使用以下方法批量计算矩形的面积:
areas = (predictions[:,4] - predictions[:,2]) * (predictions[:,5] - predictions[:,3])
Next you can obtain the index (row) with the largest area with:接下来,您可以获得面积最大的索引(行):
np.argmax(areas)
For your given sample data, the first rectangle is the largest:对于给定的样本数据,第一个矩形是最大的:
>>> 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.