【问题标题】:Creating your own contour in opencv using python使用python在opencv中创建自己的轮廓
【发布时间】:2012-12-19 03:19:56
【问题描述】:

我有一组对象的边界点。

我想用opencv作为轮廓画出来。

我不知道如何将我的点转换为轮廓表示。

到通过以下调用获得的相同轮廓表示

contours,_ = cv2.findContours(image,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

有什么想法吗?

谢谢

【问题讨论】:

    标签: python numpy opencv image-processing contour


    【解决方案1】:

    通过查看轮廓的格式,我认为这样的内容就足够了:

    contours = [numpy.array([[1,1],[10,50],[50,50]], dtype=numpy.int32) , numpy.array([[99,99],[99,60],[60,99]], dtype=numpy.int32)]
    

    这个小程序给出了一个运行示例:

    import numpy
    import cv2
    
    contours = [numpy.array([[1,1],[10,50],[50,50]], dtype=numpy.int32) , numpy.array([[99,99],[99,60],[60,99]], dtype=numpy.int32)]
    
    drawing = numpy.zeros([100, 100],numpy.uint8)
    for cnt in contours:
        cv2.drawContours(drawing,[cnt],0,(255,255,255),2)
    
    cv2.imshow('output',drawing)
    cv2.waitKey(0)
    

    【讨论】:

    • 以这种方式创建轮廓适用于 cv2.drawContours,但在使用 cv2.pointPolygonTest 时似乎不起作用。有没有办法实际返回轮廓对象?
    【解决方案2】:

    从点 L 的 Python 列表创建自己的轮廓

    L=[[x1,y1],[x2,y2],[x3,y3],[x4,y4],[x5,y5],[x6,y6],[x7,y7],[x8,y8],[x9,y9],...[xn,yn]]
    

    从 L 创建一个 numpy 数组 ctr,对其进行整形并强制其类型

    ctr = numpy.array(L).reshape((-1,1,2)).astype(numpy.int32)
    

    ctr 是我们的新计数,让我们在现有的图像

    上绘制它
    cv2.drawContours(image,[ctr],0,(255,255,255),1)
    

    【讨论】:

    • 很好的答案,尊重 opencv 的所有约束
    【解决方案3】:

    轮廓只是连接所有连续点的曲线,因此要创建自己的轮廓,您可以使用(x,y) 点按顺时针顺序

    创建一个np.array()
    points = np.array([[25,25], [70,10], [150,50], [250,250], [100,350]])
    

    就是这样!


    根据您的需要,有两种方法可以在图像上绘制轮廓:

    轮廓轮廓

    如果只需要轮廓轮廓,使用cv2.drawContours()

    cv2.drawContours(image,[points],0,(0,0,0),2)
    

    填充轮廓

    要获得填充轮廓,您可以使用cv2.fillPoly()cv2.drawContours()thickness=-1

    cv2.fillPoly(image, [points], [0,0,0]) # OR
    # cv2.drawContours(image,[points],0,(0,0,0),-1)
    

    完整的示例代码

    import cv2
    import numpy as np
    
    # Create blank white image
    image = np.ones((400,400), dtype=np.uint8) * 255
    
    # List of (x,y) points in clockwise order
    points = np.array([[25,25], [70,10], [150,50], [250,250], [100,350]])
    
    # Draw points onto image
    cv2.drawContours(image,[points],0,(0,0,0),2)
    
    # Fill points onto image
    # cv2.fillPoly(image, [points], [0,0,0])
    
    cv2.imshow('image', image)
    cv2.waitKey()
    

    【讨论】:

    • 轮廓的形状比点列表更复杂,遗憾的是
    【解决方案4】:

    要添加到 Cherif KAOUA 的答案,我发现我必须转换为列表并压缩我的 numpy 数组。从文本文件中读取点数组:

      contour = []
      with open(array_of_points,'r') as f:
          next(f) // line one of my file gives the number of points
          for l in f:
              row = l.split()
              numbers = [int(n) for n in row]
              contour.append(numbers)
    
      ctr = np.array(contour).reshape((-1,1,2)).astype(np.int32)
      ctr = ctr.tolist()
      ctr = zip(*[iter(ctr)]*len(contour))
    

    【讨论】:

      猜你喜欢
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2013-10-13
      • 2018-09-09
      相关资源
      最近更新 更多