2016-05-06 105 views
0

我提取图像的轮廓,你可以在这里看到: contourOpenCV中如何平滑的轮廓线,降低噪音

然而,它有一些噪音。 如何平滑噪音?我做了一个特写,以更清楚我想要的意思,我用 enter image description here

原图什么: enter image description here

代码:

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) 
+0

你能发布用于创建此代码? –

+0

@MartinEvans编辑! – marco

+0

如果用cv2.CHAIN_APPROX_NONE替换cv2.CHAIN_APPROX_SIMPLE,会发生什么情况? – tfv

回答

1

尝试升级到3.1.0的OpenCV 。如下所示,在对新版本进行了一些代码修改之后,我使用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) 

enter image description here

+0

我仍然有一些问题: https://dl.dropboxusercontent.com/u/710615/externalpcb.jpg – marco

+0

您是否曾经为此找到过解决方案?对不起,一个老问题只是有同样的问题。 @marco –

+0

@Jonathan:如上所述,这些问题似乎与上面使用的OpenCV版本一样。如果不是这种情况,请发表一个例子。 – tfv