简化从cv2.findContours获取坐标

时间:2018-09-06 14:29:39

标签: python opencv for-loop

我目前正在使用opencv 3.4,并从二进制图像获取轮廓作为坐标列表。我的代码如下所示:

_, contours, _ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE
list_of_contours= []
for contour in contours:         
    list_of_contours.append(list(map(tuple, (coord[0] for coord in contour))))

它按预期工作,但我很好奇是否可以避免list(map(tuple, (c[0] for c in contour)))中的循环和/或这样做是否合理?

1 个答案:

答案 0 :(得分:1)

尝试一下:

cnts = cv2.findContours(gray,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
xcnts = np.vstack((x.reshape(-1,2) for x in cnts))
print(xcnts.shape)

它为此图像(从opencv - plot contours in an image返回(698, 2)

enter image description here