【问题标题】:simplify getting coordinates from cv2.findContours简化从 cv2.findContours 获取坐标
【发布时间】:2019-02-11 20:31:24
【问题描述】:

我目前正在使用 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))) 中的循环是否可以避免和/或这样做是否合理?

【问题讨论】:

    标签: python opencv for-loop


    【解决方案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)
    

    它为这张图片返回(698, 2)(来自opencv - plot contours in an image):

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2015-12-31
      • 2015-11-21
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      相关资源
      最近更新 更多