Repository: opencv/opencv
File: modules/imgproc/test/test_fitellipse.cpp (C++)

Code before:
        sum_algebraic_dists_sqr += pt_algebraic_dist * pt_algebraic_dist;
    }
    return sqrt(sum_algebraic_dists_sqr / pts.size());
}

static bool fit_and_check_ellipse(const vector<Point2f>& pts, const double eps) {
    const RotatedRect ellipse = fitEllipseDirect(pts); // fitEllipseAMS() also works fine
    return rms_algebraic_dist(pts, ellipse) < eps;
}

TEST(Imgproc_FitEllipse_Issue_4515, accuracy) {
    vector<Point2f> pts;
    pts.push_back(Point2f(327, 317));
    pts.push_back(Point2f(328, 316));
    pts.push_back(Point2f(329, 315));
    pts.push_back(Point2f(330, 314));
    pts.push_back(Point2f(331, 314));
    pts.push_back(Point2f(332, 314));
    pts.push_back(Point2f(333, 315));
    pts.push_back(Point2f(333, 316));
    pts.push_back(Point2f(333, 317));
    pts.push_back(Point2f(333, 318));
    pts.push_back(Point2f(333, 319));
    pts.push_back(Point2f(333, 320));

    EXPECT_TRUE(fit_and_check_ellipse(pts, 1e-1));
}

TEST(Imgproc_FitEllipse_Issue_6544, accuracy) {
    vector<Point2f> pts;
    pts.push_back(Point2f(924.784f, 764.160f));
    pts.push_back(Point2f(928.388f, 615.903f));
    pts.push_back(Point2f(847.4f,   888.014f));
    pts.push_back(Point2f(929.406f, 741.675f));
    pts.push_back(Point2f(904.564f, 825.605f));
    pts.push_back(Point2f(926.742f, 760.746f));
    pts.push_back(Point2f(863.479f, 873.406f));
    pts.push_back(Point2f(910.987f, 808.863f));
    pts.push_back(Point2f(929.145f, 744.976f));
    pts.push_back(Point2f(917.474f, 791.823f));

    EXPECT_TRUE(fit_and_check_ellipse(pts, 5e-2));
}

TEST(Imgproc_FitEllipse_Issue_10270, accuracy) {
    vector<Point2f> pts;
    float scale = 1;
    Point2f shift(0, 0);
    pts.push_back(Point2f(0, 1)*scale+shift);
    pts.push_back(Point2f(0, 2)*scale+shift);
    pts.push_back(Point2f(0, 3)*scale+shift);

Review comment (bug):
I propose to remove fit_and_check_ellipse and just use `EXPECT_LE` with `rms_algebraic_dist` inside. In case of test failure GTest prints both values to console.

Code after:
        sum_algebraic_dists_sqr += pt_algebraic_dist * pt_algebraic_dist;
    }
    return sqrt(sum_algebraic_dists_sqr / pts.size());
}

TEST(Imgproc_FitEllipse_Issue_4515, accuracy) {
    vector<Point2f> pts;
    pts.push_back(Point2f(327, 317));
    pts.push_back(Point2f(328, 316));
    pts.push_back(Point2f(329, 315));
    pts.push_back(Point2f(330, 314));
    pts.push_back(Point2f(331, 314));
    pts.push_back(Point2f(332, 314));
    pts.push_back(Point2f(333, 315));
    pts.push_back(Point2f(333, 316));
    pts.push_back(Point2f(333, 317));
    pts.push_back(Point2f(333, 318));
    pts.push_back(Point2f(333, 319));
    pts.push_back(Point2f(333, 320));

    const RotatedRect ellipse = fitEllipseDirect(pts); // fitEllipseAMS() also works fine
    EXPECT_LT(rms_algebraic_dist(pts, ellipse), 1e-1);
}

TEST(Imgproc_FitEllipse_Issue_6544, accuracy) {
    vector<Point2f> pts;
    pts.push_back(Point2f(924.784f, 764.160f));
    pts.push_back(Point2f(928.388f, 615.903f));
    pts.push_back(Point2f(847.4f,   888.014f));
    pts.push_back(Point2f(929.406f, 741.675f));
    pts.push_back(Point2f(904.564f, 825.605f));
    pts.push_back(Point2f(926.742f, 760.746f));
    pts.push_back(Point2f(863.479f, 873.406f));
    pts.push_back(Point2f(910.987f, 808.863f));
    pts.push_back(Point2f(929.145f, 744.976f));
    pts.push_back(Point2f(917.474f, 791.823f));

    const RotatedRect ellipse = fitEllipseDirect(pts); // fitEllipseAMS() also works fine
    EXPECT_LT(rms_algebraic_dist(pts, ellipse), 5e-2);
}

TEST(Imgproc_FitEllipse_Issue_10270, accuracy) {
    vector<Point2f> pts;
    float scale = 1;
    Point2f shift(0, 0);
    pts.push_back(Point2f(0, 1)*scale+shift);
    pts.push_back(Point2f(0, 2)*scale+shift);
    pts.push_back(Point2f(0, 3)*scale+shift);
    pts.push_back(Point2f(2, 3)*scale+shift);
    pts.push_back(Point2f(0, 4)*scale+shift);