简体   繁体   English

简化从 cv2.findContours 获取坐标

[英]simplify getting coordinates from cv2.findContours

I'm currently working with opencv 3.4 and getting the contours as a list of coordinates from a binary image.我目前正在使用 opencv 3.4 并将轮廓作为来自二进制图像的坐标列表。 My code looks like the following:我的代码如下所示:

_, 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))))

It is working as intended but I'm curious if the loop in list(map(tuple, (c[0] for c in contour))) could be avoided and/or is it even reasonable to do so?它按预期工作,但我很好奇list(map(tuple, (c[0] for c in contour)))的循环是否可以避免和/或这样做是否合理?

Try this:尝试这个:

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)

It return (698, 2) for this image (from opencv - plot contours in an image ):它为此图像返回(698, 2) (来自opencv - 图像中的绘制轮廓):

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM