Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_fitellipse.cpp
16339 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
//
5
// Copyright (C) 2016, Itseez, Inc, all rights reserved.
6
7
#include "test_precomp.hpp"
8
9
namespace opencv_test { namespace {
10
11
// return true if point lies inside ellipse
12
static bool check_pt_in_ellipse(const Point2f& pt, const RotatedRect& el) {
13
Point2f to_pt = pt - el.center;
14
double pt_angle = atan2(to_pt.y, to_pt.x);
15
double el_angle = el.angle * CV_PI / 180;
16
double x_dist = 0.5 * el.size.width * cos(pt_angle + el_angle);
17
double y_dist = 0.5 * el.size.height * sin(pt_angle + el_angle);
18
double el_dist = sqrt(x_dist * x_dist + y_dist * y_dist);
19
return cv::norm(to_pt) < el_dist;
20
}
21
22
// Return true if mass center of fitted points lies inside ellipse
23
static bool fit_and_check_ellipse(const vector<Point2f>& pts) {
24
RotatedRect ellipse = fitEllipseDirect(pts); // fitEllipseAMS() also works fine
25
26
Point2f mass_center;
27
for (size_t i = 0; i < pts.size(); i++) {
28
mass_center += pts[i];
29
}
30
mass_center /= (float)pts.size();
31
32
return check_pt_in_ellipse(mass_center, ellipse);
33
}
34
35
TEST(Imgproc_FitEllipse_Issue_4515, accuracy) {
36
vector<Point2f> pts;
37
pts.push_back(Point2f(327, 317));
38
pts.push_back(Point2f(328, 316));
39
pts.push_back(Point2f(329, 315));
40
pts.push_back(Point2f(330, 314));
41
pts.push_back(Point2f(331, 314));
42
pts.push_back(Point2f(332, 314));
43
pts.push_back(Point2f(333, 315));
44
pts.push_back(Point2f(333, 316));
45
pts.push_back(Point2f(333, 317));
46
pts.push_back(Point2f(333, 318));
47
pts.push_back(Point2f(333, 319));
48
pts.push_back(Point2f(333, 320));
49
50
EXPECT_TRUE(fit_and_check_ellipse(pts));
51
}
52
53
TEST(Imgproc_FitEllipse_Issue_6544, accuracy) {
54
vector<Point2f> pts;
55
pts.push_back(Point2f(924.784f, 764.160f));
56
pts.push_back(Point2f(928.388f, 615.903f));
57
pts.push_back(Point2f(847.4f, 888.014f));
58
pts.push_back(Point2f(929.406f, 741.675f));
59
pts.push_back(Point2f(904.564f, 825.605f));
60
pts.push_back(Point2f(926.742f, 760.746f));
61
pts.push_back(Point2f(863.479f, 873.406f));
62
pts.push_back(Point2f(910.987f, 808.863f));
63
pts.push_back(Point2f(929.145f, 744.976f));
64
pts.push_back(Point2f(917.474f, 791.823f));
65
66
EXPECT_TRUE(fit_and_check_ellipse(pts));
67
}
68
69
}} // namespace
70
71