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));
/// 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?
我明白了。但是,如果我需要使用drawContours,即使这些孔将被填充正确吗? – Mzk 2013-02-26 01:17:06
参数CV_FILLED表示轮廓将被填充。你可以改变这个值是不是你想要的。 – 2013-02-26 04:35:14
好的。我明白你的观点。谢谢@Radford。 – Mzk 2013-02-26 06:32:19