我试图在图像中跟踪自定义的圆形标记,并且需要检查一个圆圈是否包含最小数量的其他圆圈/对象。下面是我寻找圆圈的代码:
void findMarkerContours( int, void* )
{
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<Point> approx;
cv::Mat dst = src.clone();
cv::Mat src_gray;
cv::cvtColor(src, src_gray, CV_BGR2GRAY);
//Reduce noise with a 3x3 kernel
blur( src_gray, src_gray, Size(3,3));
//Convert to binary using canny
cv::Mat bw;
cv::Canny(src_gray, bw, thresh, 3*thresh, 3);
imshow("bw", bw);
findContours(bw.clone(), contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
Mat drawing = Mat::zeros( bw.size(), CV_8UC3 );
for (int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
// contour
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
//Approximate the contour with accuracy proportional to contour perimeter
cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) *0.02, true);
//Skip small or non-convex objects
if(fabs(cv::contourArea(contours[i])) < 100 || !cv::isContourConvex(approx))
continue;
if (approx.size() >= 8) //More than 6-8 vertices means its likely a circle
{
drawContours( dst, contours, i, Scalar(0,255,0), 2, 8);
}
imshow("Hopefully we should have circles! Yay!", dst);
}
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
}正如您所看到的,检测圆圈的代码运行得很好:

但是现在我需要过滤掉我不想要的标记。我的笔是最下面的。所以,一旦我找到了一个轮廓,就是一个圆,我想要检查是否有其他的圆形轮廓存在于第一个圆的区域内,最后检查最小圆的颜色。
如果(圆包含3+,小圆圈,小圆,小圆,最小圆是彩色的) ->做什么,我能用什么方法?
发布于 2014-05-23 10:18:17
请看一下文档
findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())您将看到有一个可选的hierarchy输出向量,对于您的问题应该很方便。
层次结构-可选输出向量,包含有关图像拓扑的信息。它的元素和等高线的数目一样多。对于每一个I次等高线轮廓,元素层次结构我、hiearchy我和hiearchy我分别被设置为基于0的下一个等高线和前一个等高线在同一层次、第一个子轮廓和父轮廓的轮廓中的索引。如果对于等高线i,没有下一个、前一个、父或嵌套等高线,相应的层次结构元素将为负值。
当使用findCountours调用CV_RETR_TREE时,您将得到找到的每个轮廓的完整层次结构。
这个文档很好地解释了hierarchy格式。
发布于 2014-05-23 09:01:43
你已经在寻找一定大小的圆圈了
//Skip small or non-convex objects
if(fabs(cv::contourArea(contours[i])) < 100 || !cv::isContourConvex(approx))
continue;所以你可以用它来寻找比你已经得到的更小的圆圈,而不是寻找< 100寻找contours.size
我想颜色也是一样的。
https://stackoverflow.com/questions/23824940
复制相似问题