#include "test_precomp.hpp"\r
#include <time.h>\r
+#include <iostream>\r
+\r
+#define IMGPROC_BOUNDINGRECT_ERROR_DIFF 1\r
+\r
+#define MESSAGE_ERROR_DIFF "Bounding rectangle found by boundingRect function is incorrect."\r
\r
using namespace cv;\r
using namespace std;\r
void run (int);\r
\r
private:\r
-\r
+ template <class T> void generate_src_points(vector <Point_<T> >& src, int n);\r
+ template <class T> cv::Rect get_bounding_rect(const vector <Point_<T> > src);\r
+ template <class T> bool checking_function_work(vector <Point_<T> >& src, int type);\r
};\r
\r
CV_BoundingRectTest::CV_BoundingRectTest() {}\r
CV_BoundingRectTest::~CV_BoundingRectTest() {}\r
\r
-void CV_BoundingRectTest::run(int)\r
+template <class T> void CV_BoundingRectTest::generate_src_points(vector <Point_<T> >& src, int n)\r
+{\r
+ src.clear();\r
+ for (size_t i = 0; i < n; ++i) \r
+ src.push_back(Point_<T>(cv::randu<T>(), cv::randu<T>()));\r
+}\r
+\r
+template <class T> cv::Rect CV_BoundingRectTest::get_bounding_rect(const vector <Point_<T> > src)\r
{\r
- const int MAX_WIDTH = 100;\r
- const int MAX_HEIGHT = 100;\r
- const int N = 100;\r
+ int n = src.size();\r
+ T min_w = std::numeric_limits<T>::max(), max_w = std::numeric_limits<T>::min();\r
+ T min_h = min_w, max_h = max_w;\r
+\r
+ for (size_t i = 0; i < n; ++i) \r
+ {\r
+ min_w = std::min<T>(src.at(i).x, min_w);\r
+ max_w = std::max<T>(src.at(i).x, max_w);\r
+ min_h = std::min<T>(src.at(i).y, min_h);\r
+ max_h = std::max<T>(src.at(i).y, max_h);\r
+ }\r
+\r
+ return Rect((int)min_w, (int)min_h, (int)(floor(1.0*(max_w-min_w)) + 1), (int)(floor(1.0*(max_h-min_h)) + 1));\r
+}\r
\r
- RNG& rng = ts->get_rng();\r
+template <class T> bool CV_BoundingRectTest::checking_function_work(vector <Point_<T> >& src, int type)\r
+{\r
+ const int MAX_COUNT_OF_POINTS = 1000;\r
+ const int N = 10000;\r
\r
- for (size_t i = 0; i < N; ++i)\r
+ for (int k = 0; k < N; ++k)\r
{\r
- int w = rng.next()%MAX_WIDTH + 1, h = rng.next()%MAX_HEIGHT + 1;\r
- cv::Mat src(h, w, CV_8U); cv::randu(src, Scalar_<bool>::all(false), Scalar_<bool>::all(true));\r
+\r
+ RNG& rng = ts->get_rng();\r
+\r
+ int n = rng.next()%MAX_COUNT_OF_POINTS + 1;\r
+\r
+ generate_src_points <T> (src, n);\r
+\r
+ cv::Rect right = get_bounding_rect <T> (src);\r
+\r
+ cv::Rect rect[2] = { boundingRect(src), boundingRect(Mat(src)) };\r
+ \r
+ for (int i = 0; i < 2; ++i) if (rect[i] != right)\r
+ {\r
+ cout << endl; cout << "Checking for the work of boundingRect function..." << endl;\r
+ cout << "Type of src points: "; \r
+ switch (type)\r
+ {\r
+ case 0: {cout << "INT"; break;}\r
+ case 1: {cout << "FLOAT"; break;}\r
+ case 2: {cout << "DOUBLE"; break;}\r
+ default: break;\r
+ }\r
+ cout << endl;\r
+ cout << "Src points are stored as "; if (i == 0) cout << "VECTOR" << endl; else cout << "MAT" << endl;\r
+ cout << "Number of points: " << n << endl;\r
+ cout << "Right rect (x, y, w, h): [" << right.x << ", " << right.y << ", " << right.width << ", " << right.height << "]" << endl;\r
+ cout << "Result rect (x, y, w, h): [" << rect[i].x << ", " << rect[i].y << ", " << rect[i].width << ", " << rect[i].height << "]" << endl;\r
+ cout << endl;\r
+ CV_Error(IMGPROC_BOUNDINGRECT_ERROR_DIFF, MESSAGE_ERROR_DIFF);\r
+ return false;\r
+ }\r
+\r
}\r
+\r
+ return true;\r
+}\r
+\r
+void CV_BoundingRectTest::run(int)\r
+{\r
+ vector <Point> src_veci; if (!checking_function_work(src_veci, 0)) return;\r
+ vector <Point2f> src_vecf; checking_function_work(src_vecf, 1);\r
}\r
\r
TEST (Imgproc_BoundingRect, accuracy) { CV_BoundingRectTest test; test.safe_run(); }
\ No newline at end of file