简体   繁体   English

找到所有同心圆并使用 OpenCV C++ 填充它们

[英]Find all the concentric circles and fill them using OpenCV C++

I have an image of concentric circles.我有一个同心圆的图像。 Now I want to find some of them and fill the area between then with white color.现在我想找到其中的一些并用白色填充它们之间的区域。 I have done canny edge detection then erosion and area filter and got the below output.我已经完成了精明的边缘检测,然后进行了侵蚀和区域过滤,得到了下面的 output。 I later on tried contouring but I am getting extra concentric circles.我后来尝试了轮廓,但我得到了额外的同心圆。 Original image:原图: 在此处输入图像描述

Region to be extracted:要提取的区域:

在此处输入图像描述

This is what I have tried:这是我尝试过的:

        Mat cannyimg;
        Canny(histimg, cannyimg,35,65);
    
        Mat dilateimg;
        Mat kernel1 = getStructuringElement(MORPH_ELLIPSE, Size(2,2));
        dilate(cannyimg, dilateimg, kernel1, Point(-1, -1), 2);
    
        Mat imga = minareafilter(dilateimg, 500);
        
    
    
        vector<vector<Point> > contours;
        findContours(imga, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
        vector<vector<Point> > contours_poly(contours.size());
        vector<Rect> boundRect(contours.size());
        vector<Point2f>centers(contours.size());
        vector<float>radius(contours.size());
        for (size_t i = 0; i < contours.size(); i++)
        {
            approxPolyDP(contours[i], contours_poly[i], 3, true);
            boundRect[i] = boundingRect(contours_poly[i]);
            minEnclosingCircle(contours_poly[i], centers[i], radius[i]);
            
        }
        Mat drawing = Mat::zeros(imga.size(), CV_8UC3);
        for (size_t i = 0; i < contours.size(); i++)
        {
            Scalar color = Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
            if (radius[i] > 50 && radius[i] < 100) {
                //drawContours(drawing, contours_poly, (int)i, color);
                //rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2);
                circle(drawing, centers[i], (int)radius[i], color, 2);
            }
        }

My canny image is我的精明形象是

在此处输入图像描述

After area filter the image is:区域过滤后的图像是:

在此处输入图像描述

after contouring the image is:轮廓化后的图像是:

在此处输入图像描述

The problem seems mostly based on detecting the contours efficiently.这个问题似乎主要基于有效地检测轮廓。 I tried the code which I have coded before to find the circles with an own method.我尝试了之前编写的代码,用自己的方法找到了圆圈。 You may check and try it here .您可以在这里查看并尝试。

When I tried, I got the contours very clearly:当我尝试时,我非常清楚地得到了轮廓:

在此处输入图像描述

Then I tried to find the circles with same code and it found the circles and here is the result(I just rectangled the circles you need):然后我尝试用相同的代码找到圆圈,它找到了圆圈,结果如下(我只是将你需要的圆圈变成矩形):

在此处输入图像描述

After detecting the circles, you need to design an algorithm for the desired circles.检测出圆圈后,您需要为所需的圆圈设计一个算法。 You may number their radius and choose the order accordingly.您可以编号它们的半径并相应地选择顺序。 Here is your target output which I colorized with fillConvexPolly .这是您的目标 output ,我用fillConvexPolly对其进行了着色。 (I manually chose the circles here): (我在这里手动选择了圆圈):

在此处输入图像描述

Edit:编辑:

cv::fillConvexPoly(input, contour[outerId],cv::Scalar(0,0,255));
cv::fillConvexPoly(input,contour[innerrId],cv::Scalar(0,255,255));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM