如何从右到左和从上到下排列对象检测边界框列表?

原文标题How to Arrange list of object detection bounding box from right to left and top to bottom?

我正在尝试从右到左和从上到下对对象检测后获得的边界框列表进行排序。以下是边界框位置

bbox = [(637, 207, 681, 207),  (679, 99, 726, 99), (747, 497, 798, 497), (829, 124, 892, 124), (1002, 131, 1059, 131),  (1010, 656, 1071, 656)] 

我根据 xmin 值对这个列表进行了排序,如下所示

sorted(bbox,key=lambda x:x[2][0])

我最终得到了如下图所示的边界框排列sample image

顺序不对,应该是从左到右,从上到下,如下图sample2

任何实现这一目标的建议或指南将不胜感激

原文链接:https://stackoverflow.com//questions/71471798/how-to-arrange-list-of-object-detection-bounding-box-from-right-to-left-and-top

回复

我来回复
  • SiP的头像
    SiP 评论

    您希望按最接近左上角的点排序?如果是这样,您需要计算距该角的距离,然后才排序:

    import numpy as np
    
    bbox = [(637, 207, 681, 207),  (679, 99, 726, 99), (747, 497, 798, 497), (829, 124, 892, 124), (1002, 131, 1059, 131),  (1010, 656, 1071, 656)]
    arr = np.array(bbox)
    r = (arr[:, 0]**2 + arr[:, 1] **2)**0.5
    print(np.argsort(r))
    

    这将打印出点的顺序。我假设每个点的前 2 个坐标是 x 和 y,并且原点在左上角。

    2年前 0条评论