2016-05-06 2076 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