我想跟踪图像中的自定义圆形标记,并且我需要检查圆圈是否包含其他圆/对象的最小数量。我寻找圈子代码如下:搜索轮廓内的轮廓/ OpenCV C++
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个以上的小圆圈,最小的圆圈是[颜色]) - >做些什么?
很好的回答,帮了我很多。谢谢 – user2790954