在OpenCV中查找椭圆的成本

drj*_*rm3 4 c++ opencv

我使用的是基于此示例的代码,想知道是否有任何方法可以找出椭圆拟合的“良好”程度。我有一些椭圆,这些椭圆通常只适合我的数据,而有些椭圆几乎是完美的,我想摆脱它们。

我想保持良好的身材,摆脱不良的身材。我该怎么做opencv

Mik*_*iki 5

您可以在文献中找到几种方法,例如:

  • 检查Dilip K. Prasad,Maylor KH Leung和赵兆阳,“基于边缘曲率和凸度的椭圆检测方法”,“模式识别”,2012年第4.2节。

  • 检查Fornaciari,Michele,Andrea Prati和Rita Cucchiara。“用于嵌入式视觉应用的快速有效的椭圆检测器。” 模式识别47.11(2014):3693-3708。3.3.1节

但是,非常简单的方法可以是同时属于轮廓和椭圆的像素数。您可以进行计算,例如,在两个单独的黑色初始化图像上绘制轮廓和椭圆,并计算相交的数量,即两个图像的逻辑与的白色像素的数量。

为了更鲁棒,您可以使用2的线宽绘制轮廓和椭圆形。这将导致估计值略有偏移,但在视觉上仍然正确。

例如,给定此输入图像:

在此处输入图片说明

您可以看到,好的椭圆形是绿色,而不好的椭圆形是红色:

在此处输入图片说明

码:

#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
    // Load image
    Mat3b img = imread("path_to_image");

    // Convert to grayscale. Binarize if needed
    Mat1b bin;
    cvtColor(img, bin, COLOR_BGR2GRAY);

    // Find contours
    vector<vector<Point>> contours;
    findContours(bin.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);

    // For each contour
    for (int i = 0; i < contours.size(); ++i)
    {
        // Find ellipse
        RotatedRect ell = fitEllipse(contours[i]);

        // Draw contour
        Mat1b maskContour(img.rows, img.cols, uchar(0));
        drawContours(maskContour, contours, i, Scalar(255), 2);

        // Draw ellips
        Mat1b maskEllipse(img.rows, img.cols, uchar(0));
        ellipse(maskEllipse, ell, Scalar(255), 2);

        // Intersect
        Mat1b intersection = maskContour & maskEllipse;

        // Count amount of intersection
        float cnz = countNonZero(intersection);
        // Count number of pixels in the drawn contour
        float n = countNonZero(maskContour);
        // Compute your measure
        float measure = cnz / n;

        // Draw, color coded: good -> gree, bad -> red
        ellipse(img, ell, Scalar(0, measure*255, 255 - measure*255), 3);

    }

    imshow("Result", img);
    waitKey();


    return 0;
}
Run Code Online (Sandbox Code Playgroud)