2017-09-29 39 views
0

我試圖找到/計算下一個圖像中的輪廓區域:查找區域()(Python的,OpenCV的)

contour

目標是刪除所有的點你可以在圖像中看到,所以斑點的面積小於我給出的值。

removal

如何設置呢?

這是代碼我用...

import cv2 

im = cv2.imread('source.png') 
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
ret,thresh = cv2.threshold(imgray,127,255,0) 
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

img = cv2.drawContours(im, contours, -1, (0,255,0), 1) 

cv2.imshow('contour',img) 
cv2.waitKey(0) 
cv2.imwrite('contour.png',img) 

...這是圖像:

origin

謝謝

回答

1

您可以使用cv2.contourArea()來決定哪些畫:

for contour in contours: 
    if cv2.contourArea(contour) < 80: 
     cv2.drawContours(im, contour, -1, (255, 255, 255), 3) 
+0

但是這繪製輪廓還是填充它們?我認爲是第二個......對嗎? – Link

+1

使用'-1'作爲寬度來填充它們。也許可以嘗試兩次,即填寫一個,用'3'填寫一個。您也可以嘗試在閾值之前模糊圖像。 –

1

有是多種方式來做到這一點。一種方法是找到邊界矩形區域。

for contour in contours: 
    rect = cv2.boundingRect(contour) 
    area = rect[2] * rect[3] 
+0

感謝。但如何刪除它們?我想在for循環中需要一個「if」..我的意思是,我不知道指令。 我看到[這裏](http://answers.opencv.org/question/31433/remove-contoursbounding-rect-with-an-area-n/)存在一個'contours.erase'指令。但在Python? – Link

+1

@Link當然需要'if'來移除異常值。你應該不時地做一些自己的研究。 :) – zindarod

+0

正在尋找填充與白色斑點但答案:) – Link