test_fitellipse.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include "test_precomp.hpp"
  7. namespace opencv_test { namespace {
  8. static double algebraic_dist(const Point2f& pt, const RotatedRect& el) {
  9. const Point2d to_pt = pt - el.center;
  10. const double el_angle = el.angle * CV_PI / 180;
  11. const Point2d to_pt_el(
  12. to_pt.x * cos(-el_angle) - to_pt.y * sin(-el_angle),
  13. to_pt.x * sin(-el_angle) + to_pt.y * cos(-el_angle));
  14. return normL2Sqr<double>(Point2d(2 * to_pt_el.x / el.size.width, 2 * to_pt_el.y / el.size.height)) - 1;
  15. }
  16. static double rms_algebraic_dist(const vector<Point2f>& pts, const RotatedRect& el) {
  17. double sum_algebraic_dists_sqr = 0;
  18. for (const auto& pt : pts) {
  19. const auto pt_algebraic_dist = algebraic_dist(pt, el);
  20. sum_algebraic_dists_sqr += pt_algebraic_dist * pt_algebraic_dist;
  21. }
  22. return sqrt(sum_algebraic_dists_sqr / pts.size());
  23. }
  24. TEST(Imgproc_FitEllipse_Issue_4515, accuracy) {
  25. vector<Point2f> pts;
  26. pts.push_back(Point2f(327, 317));
  27. pts.push_back(Point2f(328, 316));
  28. pts.push_back(Point2f(329, 315));
  29. pts.push_back(Point2f(330, 314));
  30. pts.push_back(Point2f(331, 314));
  31. pts.push_back(Point2f(332, 314));
  32. pts.push_back(Point2f(333, 315));
  33. pts.push_back(Point2f(333, 316));
  34. pts.push_back(Point2f(333, 317));
  35. pts.push_back(Point2f(333, 318));
  36. pts.push_back(Point2f(333, 319));
  37. pts.push_back(Point2f(333, 320));
  38. const RotatedRect ellipse = fitEllipseDirect(pts); // fitEllipseAMS() also works fine
  39. EXPECT_LT(rms_algebraic_dist(pts, ellipse), 1e-1);
  40. }
  41. TEST(Imgproc_FitEllipse_Issue_6544, accuracy) {
  42. vector<Point2f> pts;
  43. pts.push_back(Point2f(924.784f, 764.160f));
  44. pts.push_back(Point2f(928.388f, 615.903f));
  45. pts.push_back(Point2f(847.4f, 888.014f));
  46. pts.push_back(Point2f(929.406f, 741.675f));
  47. pts.push_back(Point2f(904.564f, 825.605f));
  48. pts.push_back(Point2f(926.742f, 760.746f));
  49. pts.push_back(Point2f(863.479f, 873.406f));
  50. pts.push_back(Point2f(910.987f, 808.863f));
  51. pts.push_back(Point2f(929.145f, 744.976f));
  52. pts.push_back(Point2f(917.474f, 791.823f));
  53. const RotatedRect ellipse = fitEllipseDirect(pts); // fitEllipseAMS() also works fine
  54. EXPECT_LT(rms_algebraic_dist(pts, ellipse), 5e-2);
  55. }
  56. TEST(Imgproc_FitEllipse_Issue_10270, accuracy) {
  57. vector<Point2f> pts;
  58. pts.push_back(Point2f(0, 1));
  59. pts.push_back(Point2f(0, 2));
  60. pts.push_back(Point2f(0, 3));
  61. pts.push_back(Point2f(2, 3));
  62. pts.push_back(Point2f(0, 4));
  63. // check that we get almost vertical ellipse centered around (1, 3)
  64. RotatedRect e = fitEllipse(pts);
  65. EXPECT_LT(std::min(fabs(e.angle-180), fabs(e.angle)), 10.);
  66. EXPECT_NEAR(e.center.x, 1, 1);
  67. EXPECT_NEAR(e.center.y, 3, 1);
  68. EXPECT_LT(e.size.width*3, e.size.height);
  69. }
  70. TEST(Imgproc_FitEllipse_JavaCase, accuracy) {
  71. vector<Point2f> pts;
  72. pts.push_back(Point2f(0, 0));
  73. pts.push_back(Point2f(1, 1));
  74. pts.push_back(Point2f(-1, 1));
  75. pts.push_back(Point2f(-1, -1));
  76. pts.push_back(Point2f(1, -1));
  77. // check that we get almost circle centered around (0, 0)
  78. RotatedRect e = fitEllipse(pts);
  79. EXPECT_NEAR(e.center.x, 0, 0.01);
  80. EXPECT_NEAR(e.center.y, 0, 0.01);
  81. EXPECT_NEAR(e.size.width, sqrt(2.)*2, 0.4);
  82. EXPECT_NEAR(e.size.height, sqrt(2.)*2, 0.4);
  83. }
  84. TEST(Imgproc_FitEllipse_HorizontalLine, accuracy) {
  85. vector<Point2f> pts({{-300, 100}, {-200, 100}, {-100, 100}, {0, 100}, {100, 100}, {200, 100}, {300, 100}});
  86. const RotatedRect el = fitEllipse(pts);
  87. EXPECT_NEAR(el.center.x, -100, 100);
  88. EXPECT_NEAR(el.center.y, 100, 1);
  89. EXPECT_NEAR(el.size.width, 1, 1);
  90. EXPECT_GE(el.size.height, 150);
  91. EXPECT_NEAR(el.angle, 90, 0.1);
  92. }
  93. template<typename T>
  94. static float get_ellipse_fitting_error(const std::vector<T>& points, const Mat& closest_points) {
  95. float mse = 0.0f;
  96. for (int i = 0; i < static_cast<int>(points.size()); i++)
  97. {
  98. Point2f pt_err = Point2f(static_cast<float>(points[i].x), static_cast<float>(points[i].y)) - closest_points.at<Point2f>(i);
  99. mse += pt_err.x*pt_err.x + pt_err.y*pt_err.y;
  100. }
  101. return mse / points.size();
  102. }
  103. TEST(Imgproc_getClosestEllipsePoints, ellipse_mse) {
  104. // https://github.com/opencv/opencv/issues/26078
  105. std::vector<Point2i> points_list;
  106. // [1434, 308], [1434, 309], [1433, 310], [1427, 310], [1427, 312], [1426, 313], [1422, 313], [1422, 314],
  107. points_list.push_back(Point2i(1434, 308));
  108. points_list.push_back(Point2i(1434, 309));
  109. points_list.push_back(Point2i(1433, 310));
  110. points_list.push_back(Point2i(1427, 310));
  111. points_list.push_back(Point2i(1427, 312));
  112. points_list.push_back(Point2i(1426, 313));
  113. points_list.push_back(Point2i(1422, 313));
  114. points_list.push_back(Point2i(1422, 314));
  115. // [1421, 315], [1415, 315], [1415, 316], [1414, 317], [1408, 317], [1408, 319], [1407, 320], [1403, 320],
  116. points_list.push_back(Point2i(1421, 315));
  117. points_list.push_back(Point2i(1415, 315));
  118. points_list.push_back(Point2i(1415, 316));
  119. points_list.push_back(Point2i(1414, 317));
  120. points_list.push_back(Point2i(1408, 317));
  121. points_list.push_back(Point2i(1408, 319));
  122. points_list.push_back(Point2i(1407, 320));
  123. points_list.push_back(Point2i(1403, 320));
  124. // [1403, 321], [1402, 322], [1396, 322], [1396, 323], [1395, 324], [1389, 324], [1389, 326], [1388, 327],
  125. points_list.push_back(Point2i(1403, 321));
  126. points_list.push_back(Point2i(1402, 322));
  127. points_list.push_back(Point2i(1396, 322));
  128. points_list.push_back(Point2i(1396, 323));
  129. points_list.push_back(Point2i(1395, 324));
  130. points_list.push_back(Point2i(1389, 324));
  131. points_list.push_back(Point2i(1389, 326));
  132. points_list.push_back(Point2i(1388, 327));
  133. // [1382, 327], [1382, 328], [1381, 329], [1376, 329], [1376, 330], [1375, 331], [1369, 331], [1369, 333],
  134. points_list.push_back(Point2i(1382, 327));
  135. points_list.push_back(Point2i(1382, 328));
  136. points_list.push_back(Point2i(1381, 329));
  137. points_list.push_back(Point2i(1376, 329));
  138. points_list.push_back(Point2i(1376, 330));
  139. points_list.push_back(Point2i(1375, 331));
  140. points_list.push_back(Point2i(1369, 331));
  141. points_list.push_back(Point2i(1369, 333));
  142. // [1368, 334], [1362, 334], [1362, 335], [1361, 336], [1359, 336], [1359, 1016], [1365, 1016], [1366, 1017],
  143. points_list.push_back(Point2i(1368, 334));
  144. points_list.push_back(Point2i(1362, 334));
  145. points_list.push_back(Point2i(1362, 335));
  146. points_list.push_back(Point2i(1361, 336));
  147. points_list.push_back(Point2i(1359, 336));
  148. points_list.push_back(Point2i(1359, 1016));
  149. points_list.push_back(Point2i(1365, 1016));
  150. points_list.push_back(Point2i(1366, 1017));
  151. // [1366, 1019], [1430, 1019], [1430, 1017], [1431, 1016], [1440, 1016], [1440, 308]
  152. points_list.push_back(Point2i(1366, 1019));
  153. points_list.push_back(Point2i(1430, 1019));
  154. points_list.push_back(Point2i(1430, 1017));
  155. points_list.push_back(Point2i(1431, 1016));
  156. points_list.push_back(Point2i(1440, 1016));
  157. points_list.push_back(Point2i(1440, 308));
  158. RotatedRect fit_ellipse_params(
  159. Point2f(1442.97900390625, 662.1879272460938),
  160. Size2f(579.5570678710938, 730.834228515625),
  161. 20.190902709960938
  162. );
  163. // Point2i
  164. {
  165. Mat pointsi(points_list);
  166. Mat closest_pts;
  167. getClosestEllipsePoints(fit_ellipse_params, pointsi, closest_pts);
  168. EXPECT_TRUE(pointsi.rows == closest_pts.rows);
  169. EXPECT_TRUE(pointsi.cols == closest_pts.cols);
  170. EXPECT_TRUE(pointsi.channels() == closest_pts.channels());
  171. float fit_ellipse_mse = get_ellipse_fitting_error(points_list, closest_pts);
  172. EXPECT_NEAR(fit_ellipse_mse, 1.61994, 1e-4);
  173. }
  174. // Point2f
  175. {
  176. Mat pointsf;
  177. Mat(points_list).convertTo(pointsf, CV_32F);
  178. Mat closest_pts;
  179. getClosestEllipsePoints(fit_ellipse_params, pointsf, closest_pts);
  180. EXPECT_TRUE(pointsf.rows == closest_pts.rows);
  181. EXPECT_TRUE(pointsf.cols == closest_pts.cols);
  182. EXPECT_TRUE(pointsf.channels() == closest_pts.channels());
  183. float fit_ellipse_mse = get_ellipse_fitting_error(points_list, closest_pts);
  184. EXPECT_NEAR(fit_ellipse_mse, 1.61994, 1e-4);
  185. }
  186. }
  187. static std::vector<Point2f> sample_ellipse_pts(const RotatedRect& ellipse_params) {
  188. // Sample N points using the ellipse parametric form
  189. float xc = ellipse_params.center.x;
  190. float yc = ellipse_params.center.y;
  191. float a = ellipse_params.size.width / 2;
  192. float b = ellipse_params.size.height / 2;
  193. float theta = static_cast<float>(ellipse_params.angle * M_PI / 180);
  194. float cos_th = std::cos(theta);
  195. float sin_th = std::sin(theta);
  196. int nb_samples = 180;
  197. std::vector<Point2f> ellipse_pts(nb_samples);
  198. for (int i = 0; i < nb_samples; i++) {
  199. float ax = a * cos_th;
  200. float ay = a * sin_th;
  201. float bx = -b * sin_th;
  202. float by = b * cos_th;
  203. float t = static_cast<float>(i / static_cast<float>(nb_samples) * 2*M_PI);
  204. float cos_t = std::cos(t);
  205. float sin_t = std::sin(t);
  206. ellipse_pts[i].x = xc + ax*cos_t + bx*sin_t;
  207. ellipse_pts[i].y = yc + ay*cos_t + by*sin_t;
  208. }
  209. return ellipse_pts;
  210. }
  211. TEST(Imgproc_getClosestEllipsePoints, ellipse_mse_2) {
  212. const float tol = 1e-3f;
  213. // bb height > width
  214. // Check correctness of the minor/major axes swapping and updated angle in getClosestEllipsePoints
  215. {
  216. RotatedRect ellipse_params(
  217. Point2f(-142.97f, -662.1878f),
  218. Size2f(539.557f, 730.83f),
  219. 27.09960938f
  220. );
  221. std::vector<Point2f> ellipse_pts = sample_ellipse_pts(ellipse_params);
  222. Mat pointsf, closest_pts;
  223. Mat(ellipse_pts).convertTo(pointsf, CV_32F);
  224. getClosestEllipsePoints(ellipse_params, pointsf, closest_pts);
  225. float ellipse_pts_mse = get_ellipse_fitting_error(ellipse_pts, closest_pts);
  226. EXPECT_NEAR(ellipse_pts_mse, 0, tol);
  227. }
  228. // bb height > width + negative angle
  229. {
  230. RotatedRect ellipse_params(
  231. Point2f(-142.97f, 562.1878f),
  232. Size2f(53.557f, 730.83f),
  233. -75.09960938f
  234. );
  235. std::vector<Point2f> ellipse_pts = sample_ellipse_pts(ellipse_params);
  236. Mat pointsf, closest_pts;
  237. Mat(ellipse_pts).convertTo(pointsf, CV_32F);
  238. getClosestEllipsePoints(ellipse_params, pointsf, closest_pts);
  239. float ellipse_pts_mse = get_ellipse_fitting_error(ellipse_pts, closest_pts);
  240. EXPECT_NEAR(ellipse_pts_mse, 0, tol);
  241. }
  242. // Negative angle
  243. {
  244. RotatedRect ellipse_params(
  245. Point2f(742.97f, -462.1878f),
  246. Size2f(535.57f, 130.83f),
  247. -75.09960938f
  248. );
  249. std::vector<Point2f> ellipse_pts = sample_ellipse_pts(ellipse_params);
  250. Mat pointsf, closest_pts;
  251. Mat(ellipse_pts).convertTo(pointsf, CV_32F);
  252. getClosestEllipsePoints(ellipse_params, pointsf, closest_pts);
  253. float ellipse_pts_mse = get_ellipse_fitting_error(ellipse_pts, closest_pts);
  254. EXPECT_NEAR(ellipse_pts_mse, 0, tol);
  255. }
  256. }
  257. }} // namespace