2013-02-25 51 views
1

問題有效的方式使用輪廓

有很多斑點的圖像重繪對象。我需要刪除不符合要求的斑點。但是,滿足要求的斑點在內部有洞。我需要重新繪製成功的blob。以下是我使用的一些代碼。希望有人能指出如何處理它。

/// Find contours 
vector<vector<Point> > contours; 
vector<Vec4i> hierarchy; 
cv::findContours(srcImg, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0,0)); 

更多信息(http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=moments#findcontours

/// Start to iterate to each contour found 

vector<vector<Point> >::iterator itc = contours.begin(); 
vector<Rect> rects; 

Mat dstImg = Mat::zeros(srcImg.size(), CV_8UC1); 

    //Remove patch that are no inside limits.  
    while(itc != contours.end()) { 
    /// eliminating blobs here 
    } 

/// To redraw the contours. Error here since some blobs already been removed 
int idx = 0; 
for(; idx >= 0; idx = hierarchy[idx][0]) 
{ 
Scalar color(255, 255, 255); 
drawContours(dstImg, contours, idx, color, CV_FILLED, 8, hierarchy); 
} 

/// To redraw the contours but the holes also filled 
for(unsigned int i = 0; i < rects.size(); i++) { 
Scalar color = Scalar(255,255,255); 
drawContours(dstImg, contours, i, color, CV_FILLED, 8, noArray(), 0, Point()); 
} 

我必須再次使用findContours?

回答

0

我想也許這裏有兩個問題。首先,你想刪除輪廓中找到的輪廓?爲此,請使用CV_RETR_EXTERNAL而不是CV_RETR_CCOMP。其次,你只想繪製未被刪除的輪廓?這更多取決於你如何去除其他輪廓。解決這個問題的一種簡單而快速的方法是創建一個新的vector>和push_back輪廓,這些輪廓不會在while循環中丟掉。

+0

我明白了。但是,如果我需要使用drawContours,即使這些孔將被填充正確嗎? – Mzk 2013-02-26 01:17:06

+0

參數CV_FILLED表示輪廓將被填充。你可以改變這個值是不是你想要的。 – 2013-02-26 04:35:14

+0

好的。我明白你的觀點。謝謝@Radford。 – Mzk 2013-02-26 06:32:19