gpt4 book ai didi

c++ - 在轮廓/OpenCV c++ 中搜索轮廓

转载 作者:行者123 更新时间:2023-11-28 00:28:43 29 4
gpt4 key购买 nike

我正在尝试跟踪图像中的自定义圆形标记,我需要检查一个圆是否包含最少数量的其他圆/对象。我查找圈子的代码如下:

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 个以上的小圆圈 || 最小的圆圈是 [颜色] )我可以采用什么方法 -> 做些什么?

最佳答案

看看 documentation对于

findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())

您会看到有一个可选的hierarchy 输出 vector ,它应该对您的问题很方便。

hierarchy – Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i] , the elements hierarchy[i][0] , hiearchyi , hiearchyi , and hiearchyi are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.

当使用 CV_RETR_TREE 调用 findCountours 时,您将获得找到的每个轮廓的完整层次结构。

doc很好地解释了 hierarchy 格式。

关于c++ - 在轮廓/OpenCV c++ 中搜索轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23824940/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com