OpenCV - How to find rectangle contour of a rectangle with round corner?

前端 未结 3 1079
野性不改
野性不改 2020-12-30 06:29

I\'m trying to find the contour of a rectangle object with round corner in a image. I tried HoughLinesP and findContours, but did not achieve the d

3条回答
  •  旧时难觅i
    2020-12-30 06:43

    You need to find the bounding rectangle of the found contours.

    img = cv2.imread("image.png", -1)
    
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    binary = cv2.bitwise_not(gray)
    
    (_,contours,_) = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    
    for contour in contours:
        (x,y,w,h) = cv2.boundingRect(contour)
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
    

提交回复
热议问题