我正在做一个计算机视觉项目,我想问你一些问题。我使用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)]发布于 2019-05-05 11:24:35
尝试使用内置函数sorted,如下所示:
sorted(contur_list, key=lambda approx: approx[0,0,0])在这里,key参数指定了在进行比较之前要在每个列表元素上调用的函数。如果需要已排序列表的反转版本,则可以调用reversed函数。
https://stackoverflow.com/questions/55988479
复制相似问题