Find the cost of an ellipse in OpenCV

I am using code based on in this example and wondering if there is a way to find out how good this ellipse is. I have a few ellipses that only fit my data very much, and I want to get rid of them, while some of the ellipses are almost perfect.

I would like to keep very good supplies and get rid of poor seizures. How to do it in opencv?

+4
source share
1 answer

There are several methods in the literature, for example:

  • . , .. Leung and Siu-Yeung Cho, " ", " ", 2012. at 4.2

  • , , . " ". 47.11 (2014): 3693-3708. 3.3.1

, , . , , - , .

, , 2. , .

, :

enter image description here

, , - :

enter image description here

:

#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;
}
+4

Source: https://habr.com/ru/post/1626798/


All Articles