2013-02-25 34 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