代码之家  ›  专栏  ›  技术社区  ›  jtlz2

Python/OpenCV:将二维点列表转换为OpenCV等高线[重复]

  •  0
  • jtlz2  · 技术社区  · 8 年前

    我有一组物体的边界点。

    我想用opencv绘制它的轮廓。

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

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

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

    有什么想法吗?

    谢谢

    0 回复  |  直到 4 年前
        1
  •  28
  •   sietschie    13 年前

    通过查看 轮廓 我认为这样做就足够了:

    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)
    
        2
  •  23
  •   Cherif KAOUA    8 年前

    从python点列表创建自己的轮廓

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

    创建一个numpy数组 中心 从L开始,重塑它并强制其类型

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

    是我们的新国家,让我们利用现有的 形象

    cv2.drawContours(image,[ctr],0,(255,255,255),1)
    
        3
  •  13
  •   nathancy    6 年前

    轮廓只是连接所有连续点的曲线,因此要创建自己的轮廓,可以创建 np.array() 用你的 (x,y) 指向 顺时针顺序

    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。等高线图() 具有 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
  •  0
  •   ladlibertine    9 年前

    为了补充Cherif KAOUA的答案,我发现我必须转换为list并压缩我的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))
    
    推荐文章