Python 简化从cv2.findContours获取坐标的过程

Python 简化从cv2.findContours获取坐标的过程,python,opencv,for-loop,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 conto

我目前正在使用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))))
它按预期工作,但我很好奇是否可以避免
列表(map(tuple,(c[0]表示等高线中的c))
中的循环,或者这样做是否合理?

尝试以下方法:

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)
它返回此图像的
(698,2)
(从):