【问题标题】:Sorting a list which contains numpy.ndarray elements PYTHON对包含 numpy.ndarray 元素 PYTHON 的列表进行排序
【发布时间】:2019-05-05 03:01:45
【问题描述】:

我正在从事一个计算机视觉项目,我想问你一些事情。我使用cv2.findContours() 方法,然后使用approxPolyDP(),每个形状都有 4-4 个检测到的边缘。图片上有三个相邻的矩形。问题是,我想根据第一个 x,y 坐标对列表进行排序。从左到右。

谢谢!

contours, _ = cv2.findContours(raw_image2, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

contour_list = []
for contour in contours:
    approx= cv2.approxPolyDP(contour,0.1*cv2.arcLength(contour,True),True)
    contur_list.append(approx)


   [array([[[383,  22]],

   [[384, 127]],

   [[492, 127]],

   [[491,  20]]], dtype=int32), array([[[ 54,  16]],

   [[ 52, 123]],

   [[160, 124]],

   [[160,  17]]], dtype=int32), array([[[222,  14]],

   [[220, 124]],

   [[328, 125]],

   [[328,  15]]], dtype=int32)]

这是未排序的输出,但我希望成为这个:

   [array([[[ 54,  16]],

   [[ 52, 123]],

   [[160, 124]],

   [[160,  17]]],dtype=int32), array([[[222,  14]],

   [[220, 124]],

   [[328, 125]],

   [[328,  15]]], dtype=int32), array([[[383,  22]],

   [[384, 127]],

   [[492, 127]],

   [[491,  20]]], dtype=int32)]

【问题讨论】:

    标签: python arrays sorting opencv


    【解决方案1】:

    像这样尝试内置函数sorted

    sorted(contur_list, key=lambda approx: approx[0,0,0])
    

    这里的key 参数指定了在进行比较之前对每个列表元素调用的函数。如果您想要排序列表的反向版本,可以调用 reversed 函数。

    【讨论】:

      猜你喜欢
      • 2018-08-17
      • 1970-01-01
      • 2010-10-29
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      相关资源
      最近更新 更多