1

我有一个从一些算法计算出来的二进制图像。图像中有一个洞,我想在这个洞中最适合一个圆圈。我尝试使用bestminEnclosingCircle函数,但它没有给出最好的结果。

这是二进制图像

在此处输入图像描述

这是我从这个函数中得到的

在此处输入图像描述

这是预期的

在此处输入图像描述

我想排除这部分

在此处输入图像描述

这是我查找轮廓的代码

    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;


    findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
4

1 回答 1

0

检查下面的代码

在此处输入图像描述

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;


int main(int, char** argv)
{
    Mat src, src_gray;

    /// Load source image and convert it to gray
    src = imread("e:/test/ifFz9.png");
    resize(src, src, Size(), 0.25, 0.25);

    /// Convert image to gray and blur it
    cvtColor(src, src_gray, COLOR_BGR2GRAY);
    blur(src_gray, src_gray, Size(3, 3));

    imshow("Source", src);

    Mat threshold_output;
    vector<vector<Point> > contours;

    /// Detect edges using Threshold
    threshold(src_gray, threshold_output, 127, 255, THRESH_BINARY);
    /// Find contours
    findContours(threshold_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);

    for (size_t i = 0; i < contours.size(); i++)
    {
        if (contours[i].size() > 50)
        {
            RotatedRect minEllipse = fitEllipse(contours[i]);

            int size = min(minEllipse.size.width, minEllipse.size.height) / 2;
            // ellipse
            if (size < src.rows / 2)
                ellipse(src, minEllipse.center, Size(size, size), 0, 360, 0, Scalar(0, 0, 0255), 2, 8);
        }
    }

    imshow("Contours", src);
    waitKey(0);

    return 0;
}
于 2017-09-13T21:37:21.867 回答