OpenCV how to smooth contour, reducing noise

前端 未结 2 1607
小鲜肉
小鲜肉 2021-01-20 03:07

I extracted the contours of an image, that you can see here:

However, it has some noise. How can I smooth the noise? I did a close up to make clearer what I want to

2条回答
  •  囚心锁ツ
    2021-01-20 03:55

    Try an upgraded to OpenCV 3.1.0. After some code adaptations for the new version as shown below, I tried it out with OpenCV version 3.1.0 and did not see any of the effects you are describing.

    import cv2
    import numpy as np
    
    print cv2.__version__
    
    rMaskgray = cv2.imread('5evOn.jpg', 0)
    (thresh, binRed) = cv2.threshold(rMaskgray, 50, 255, cv2.THRESH_BINARY)
    
    _, Rcontours, hier_r = cv2.findContours(binRed,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
    r_areas = [cv2.contourArea(c) for c in Rcontours]
    max_rarea = np.max(r_areas)
    CntExternalMask = np.ones(binRed.shape[:2], dtype="uint8") * 255
    
    for c in Rcontours:
        if(( cv2.contourArea(c) > max_rarea * 0.70) and (cv2.contourArea(c)< max_rarea)):
            cv2.drawContours(CntExternalMask,[c],-1,0,1)
    
    cv2.imwrite('contour1.jpg', CntExternalMask)
    

提交回复
热议问题