test_boundingrect.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. #define IMGPROC_BOUNDINGRECT_ERROR_DIFF 1
  45. #define MESSAGE_ERROR_DIFF "Bounding rectangle found by boundingRect function is incorrect."
  46. class CV_BoundingRectTest: public cvtest::ArrayTest
  47. {
  48. public:
  49. CV_BoundingRectTest();
  50. ~CV_BoundingRectTest();
  51. protected:
  52. void run (int);
  53. private:
  54. template <typename T> void generate_src_points(vector <Point_<T> >& src, int n);
  55. template <typename T> cv::Rect get_bounding_rect(const vector <Point_<T> > src);
  56. template <typename T> bool checking_function_work(vector <Point_<T> >& src, int type);
  57. };
  58. CV_BoundingRectTest::CV_BoundingRectTest() {}
  59. CV_BoundingRectTest::~CV_BoundingRectTest() {}
  60. template <typename T> void CV_BoundingRectTest::generate_src_points(vector <Point_<T> >& src, int n)
  61. {
  62. src.clear();
  63. for (int i = 0; i < n; ++i)
  64. src.push_back(Point_<T>(cv::randu<T>(), cv::randu<T>()));
  65. }
  66. template <typename T> cv::Rect CV_BoundingRectTest::get_bounding_rect(const vector <Point_<T> > src)
  67. {
  68. int n = (int)src.size();
  69. T min_w = std::numeric_limits<T>::max(), max_w = std::numeric_limits<T>::min();
  70. T min_h = min_w, max_h = max_w;
  71. for (int i = 0; i < n; ++i)
  72. {
  73. min_w = std::min<T>(src.at(i).x, min_w);
  74. max_w = std::max<T>(src.at(i).x, max_w);
  75. min_h = std::min<T>(src.at(i).y, min_h);
  76. max_h = std::max<T>(src.at(i).y, max_h);
  77. }
  78. return Rect((int)min_w, (int)min_h, (int)max_w-(int)min_w + 1, (int)max_h-(int)min_h + 1);
  79. }
  80. template <typename T> bool CV_BoundingRectTest::checking_function_work(vector <Point_<T> >& src, int type)
  81. {
  82. const int MAX_COUNT_OF_POINTS = 1000;
  83. const int N = 10000;
  84. for (int k = 0; k < N; ++k)
  85. {
  86. RNG& rng = ts->get_rng();
  87. int n = rng.next()%MAX_COUNT_OF_POINTS + 1;
  88. generate_src_points <T> (src, n);
  89. cv::Rect right = get_bounding_rect <T> (src);
  90. cv::Rect rect[2] = { boundingRect(src), boundingRect(Mat(src)) };
  91. for (int i = 0; i < 2; ++i) if (rect[i] != right)
  92. {
  93. cout << endl; cout << "Checking for the work of boundingRect function..." << endl;
  94. cout << "Type of src points: ";
  95. switch (type)
  96. {
  97. case 0: {cout << "INT"; break;}
  98. case 1: {cout << "FLOAT"; break;}
  99. default: break;
  100. }
  101. cout << endl;
  102. cout << "Src points are stored as "; if (i == 0) cout << "VECTOR" << endl; else cout << "MAT" << endl;
  103. cout << "Number of points: " << n << endl;
  104. cout << "Right rect (x, y, w, h): [" << right.x << ", " << right.y << ", " << right.width << ", " << right.height << "]" << endl;
  105. cout << "Result rect (x, y, w, h): [" << rect[i].x << ", " << rect[i].y << ", " << rect[i].width << ", " << rect[i].height << "]" << endl;
  106. cout << endl;
  107. CV_Error(IMGPROC_BOUNDINGRECT_ERROR_DIFF, MESSAGE_ERROR_DIFF);
  108. }
  109. }
  110. return true;
  111. }
  112. void CV_BoundingRectTest::run(int)
  113. {
  114. vector <Point> src_veci; if (!checking_function_work(src_veci, 0)) return;
  115. vector <Point2f> src_vecf; checking_function_work(src_vecf, 1);
  116. }
  117. TEST (Imgproc_BoundingRect, accuracy) { CV_BoundingRectTest test; test.safe_run(); }
  118. }} // namespace