test_fitellipse.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // return true if point lies inside ellipse
  9. static bool check_pt_in_ellipse(const Point2f& pt, const RotatedRect& el) {
  10. Point2f to_pt = pt - el.center;
  11. double pt_angle = atan2(to_pt.y, to_pt.x);
  12. double el_angle = el.angle * CV_PI / 180;
  13. double x_dist = 0.5 * el.size.width * cos(pt_angle + el_angle);
  14. double y_dist = 0.5 * el.size.height * sin(pt_angle + el_angle);
  15. double el_dist = sqrt(x_dist * x_dist + y_dist * y_dist);
  16. return cv::norm(to_pt) < el_dist;
  17. }
  18. // Return true if mass center of fitted points lies inside ellipse
  19. static bool fit_and_check_ellipse(const vector<Point2f>& pts) {
  20. RotatedRect ellipse = fitEllipseDirect(pts); // fitEllipseAMS() also works fine
  21. Point2f mass_center;
  22. for (size_t i = 0; i < pts.size(); i++) {
  23. mass_center += pts[i];
  24. }
  25. mass_center /= (float)pts.size();
  26. return check_pt_in_ellipse(mass_center, ellipse);
  27. }
  28. TEST(Imgproc_FitEllipse_Issue_4515, accuracy) {
  29. vector<Point2f> pts;
  30. pts.push_back(Point2f(327, 317));
  31. pts.push_back(Point2f(328, 316));
  32. pts.push_back(Point2f(329, 315));
  33. pts.push_back(Point2f(330, 314));
  34. pts.push_back(Point2f(331, 314));
  35. pts.push_back(Point2f(332, 314));
  36. pts.push_back(Point2f(333, 315));
  37. pts.push_back(Point2f(333, 316));
  38. pts.push_back(Point2f(333, 317));
  39. pts.push_back(Point2f(333, 318));
  40. pts.push_back(Point2f(333, 319));
  41. pts.push_back(Point2f(333, 320));
  42. EXPECT_TRUE(fit_and_check_ellipse(pts));
  43. }
  44. TEST(Imgproc_FitEllipse_Issue_6544, accuracy) {
  45. vector<Point2f> pts;
  46. pts.push_back(Point2f(924.784f, 764.160f));
  47. pts.push_back(Point2f(928.388f, 615.903f));
  48. pts.push_back(Point2f(847.4f, 888.014f));
  49. pts.push_back(Point2f(929.406f, 741.675f));
  50. pts.push_back(Point2f(904.564f, 825.605f));
  51. pts.push_back(Point2f(926.742f, 760.746f));
  52. pts.push_back(Point2f(863.479f, 873.406f));
  53. pts.push_back(Point2f(910.987f, 808.863f));
  54. pts.push_back(Point2f(929.145f, 744.976f));
  55. pts.push_back(Point2f(917.474f, 791.823f));
  56. EXPECT_TRUE(fit_and_check_ellipse(pts));
  57. }
  58. TEST(Imgproc_FitEllipse_Issue_10270, accuracy) {
  59. vector<Point2f> pts;
  60. float scale = 1;
  61. Point2f shift(0, 0);
  62. pts.push_back(Point2f(0, 1)*scale+shift);
  63. pts.push_back(Point2f(0, 2)*scale+shift);
  64. pts.push_back(Point2f(0, 3)*scale+shift);
  65. pts.push_back(Point2f(2, 3)*scale+shift);
  66. pts.push_back(Point2f(0, 4)*scale+shift);
  67. // check that we get almost vertical ellipse centered around (1, 3)
  68. RotatedRect e = fitEllipse(pts);
  69. EXPECT_LT(std::min(fabs(e.angle-180), fabs(e.angle)), 10.);
  70. EXPECT_NEAR(e.center.x, 1, 1);
  71. EXPECT_NEAR(e.center.y, 3, 1);
  72. EXPECT_LT(e.size.width*3, e.size.height);
  73. }
  74. TEST(Imgproc_FitEllipse_JavaCase, accuracy) {
  75. vector<Point2f> pts;
  76. float scale = 1;
  77. Point2f shift(0, 0);
  78. pts.push_back(Point2f(0, 0)*scale+shift);
  79. pts.push_back(Point2f(1, 1)*scale+shift);
  80. pts.push_back(Point2f(-1, 1)*scale+shift);
  81. pts.push_back(Point2f(-1, -1)*scale+shift);
  82. pts.push_back(Point2f(1, -1)*scale+shift);
  83. // check that we get almost vertical ellipse centered around (1, 3)
  84. RotatedRect e = fitEllipse(pts);
  85. EXPECT_NEAR(e.center.x, 0, 0.01);
  86. EXPECT_NEAR(e.center.y, 0, 0.01);
  87. EXPECT_NEAR(e.size.width, sqrt(2.)*2, 0.4);
  88. EXPECT_NEAR(e.size.height, sqrt(2.)*2, 0.4);
  89. }
  90. }} // namespace