如何找到形状边界的直线近似值?

时间:2017-12-22 06:18:42

标签: opencv graphics

< p>这是图片:< / p> < p>< a href =“https://i.stack.imgur.com/a59fc.png"rel =”nofollow noreferrer“>< img src =”https://i.stack.imgur。 com / a59fc.png“alt =”在此处输入图像说明“>< / a>< / p> < p>我需要找到该图像边界的直线近似值。< / p>

1 个答案:

答案 0 :(得分:1)

  1. 阈值
  2. findContours
  3. approxPolyDP
  4. #findContours
    contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
    
    canvas = img.copy()
    
    ## draw approx contours
    
    for cnt in contours:
        arclen = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, arclen*0.005, True)
        #drawContours
        cv2.drawContours(canvas, [approx], -1, (0,0,255), 1, cv2.LINE_AA)
    
    cv2.imwrite("result.png", canvas)
    

    enter image description here

相关问题