OpenCV如何平滑轮廓,减少噪音

mar*_*rco 4 python opencv smooth noise contour

我提取了图像的轮廓,您可以在这里看到: 轮廓

但是,它有一些噪音。我怎样才能消除噪音?我做了一个特写来更清楚我想表达的意思 在此处输入图片说明

我使用的原始图像: 在此处输入图片说明

代码:

rMaskgray = cv2.imread('redmask.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
(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)
Run Code Online (Sandbox Code Playgroud)

tfv*_*tfv 5

尝试升级到 OpenCV 3.1.0。如下所示对新版本进行了一些代码修改后,我用 OpenCV 3.1.0 版进行了尝试,没有看到您描述的任何效果。

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明