在OpenCV的findContours方法中重复

时间:2017-03-01 15:23:02

标签: python opencv contour opencv-contour

我正在使用python openCV库来获取简单图像中轮廓的坐标。据我所知,轮廓中点的顺序将是findContour()方法返回的顺序。

但是findContour()返回的点集在其中有重复。如果订单被保留,那么怎么会有重复?

如何阅读输出?

这是代码

import numpy as np
import cv2
from pylab import plot,show

from PIL import Image


def get_contours(im):

  imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  ret,thresh = cv2.threshold(imgray,127,255,0)
  im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

  return im2, contours, hierarchy

def main():
    im = cv2.imread('border.jpeg')

    contour_image, contours, hierarchy = get_contours(im)

    contour_points = []
    for pt in contours[1]:
        contour_points.append((pt[0][0],pt[0][1]))
        plot(pt[0][0],pt[0][1])

    if len(contour_points)!= len(set(contour_points)):
        print "has_duplicates"
    else:
        print "no duplicates"

main()

Here is the image

2 个答案:

答案 0 :(得分:1)

您找到轮廓的对象包含大约1个像素宽和#34;瓶颈",以便相对两侧的边共享一个顶点。

最好用图片说明这一点。

想象一下4x5像素图像中的这种情况:

让我们运行一个简短的脚本来找到它的轮廓:

select distinct column1, column2
from table_name

如果我们把它画进来,那就很明显了:

答案 1 :(得分:0)

已经很久了,但是...

也许您可以使用层次结构变量中存在的父级和子级。 对于过滤相同级别的轮廓可能很有用。 您可以通过这样的索引访问形状的父编号。

hierarchy[0,cn,3]

cn是轮廓的索引。

完整信息HERE