1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*M///////////////////////////////////////////////////////////////////////////////////////
2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  By downloading, copying, installing or using the software you agree to this license.
6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  If you do not agree to this license, do not download, install,
7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  copy or use the software.
8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//                           License Agreement
11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//                For Open Source Computer Vision Library
12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Third party copyrights are property of their respective owners.
16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Redistribution and use in source and binary forms, with or without modification,
18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// are permitted provided that the following conditions are met:
19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * Redistribution's of source code must retain the above copyright notice,
21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     this list of conditions and the following disclaimer.
22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * Redistribution's in binary form must reproduce the above copyright notice,
24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     this list of conditions and the following disclaimer in the documentation
25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     and/or other materials provided with the distribution.
26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * The name of the copyright holders may not be used to endorse or promote products
28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     derived from this software without specific prior written permission.
29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
30793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// This software is provided by the copyright holders and contributors "as is" and
31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// any express or implied warranties, including, but not limited to, the implied
32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// warranties of merchantability and fitness for a particular purpose are disclaimed.
33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// In no event shall the Intel Corporation or contributors be liable for any direct,
34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// indirect, incidental, special, exemplary, or consequential damages
35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// (including, but not limited to, procurement of substitute goods or services;
36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// loss of use, data, or profits; or business interruption) however caused
37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// and on any theory of liability, whether in contract, strict liability,
38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// or tort (including negligence or otherwise) arising in any way out of
39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// the use of this software, even if advised of the possibility of such damage.
40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//M*/
42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "test_precomp.hpp"
44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include <time.h>
45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
46793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define IMGPROC_BOUNDINGRECT_ERROR_DIFF 1
47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define MESSAGE_ERROR_DIFF "Bounding rectangle found by boundingRect function is incorrect."
49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerusing namespace cv;
51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerusing namespace std;
52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerclass CV_BoundingRectTest: public cvtest::ArrayTest
54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerpublic:
56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    CV_BoundingRectTest();
57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ~CV_BoundingRectTest();
58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerprotected:
60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    void run (int);
61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerprivate:
63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    template <typename T> void generate_src_points(vector <Point_<T> >& src, int n);
64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    template <typename T> cv::Rect get_bounding_rect(const vector <Point_<T> > src);
65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    template <typename T> bool checking_function_work(vector <Point_<T> >& src, int type);
66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
68793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerCV_BoundingRectTest::CV_BoundingRectTest() {}
69793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerCV_BoundingRectTest::~CV_BoundingRectTest() {}
70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
71793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertemplate <typename T> void CV_BoundingRectTest::generate_src_points(vector <Point_<T> >& src, int n)
72793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
73793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    src.clear();
74793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    for (int i = 0; i < n; ++i)
75793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        src.push_back(Point_<T>(cv::randu<T>(), cv::randu<T>()));
76793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
77793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
78793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertemplate <typename T> cv::Rect CV_BoundingRectTest::get_bounding_rect(const vector <Point_<T> > src)
79793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
80793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int n = (int)src.size();
81793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    T min_w = std::numeric_limits<T>::max(), max_w = std::numeric_limits<T>::min();
82793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    T min_h = min_w, max_h = max_w;
83793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
84793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    for (int i = 0; i < n; ++i)
85793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
86793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        min_w = std::min<T>(src.at(i).x, min_w);
87793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        max_w = std::max<T>(src.at(i).x, max_w);
88793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        min_h = std::min<T>(src.at(i).y, min_h);
89793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        max_h = std::max<T>(src.at(i).y, max_h);
90793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
91793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
92793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    return Rect((int)min_w, (int)min_h, (int)max_w-(int)min_w + 1, (int)max_h-(int)min_h + 1);
93793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
94793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
95793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertemplate <typename T> bool CV_BoundingRectTest::checking_function_work(vector <Point_<T> >& src, int type)
96793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
97793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const int MAX_COUNT_OF_POINTS = 1000;
98793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const int N = 10000;
99793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
100793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    for (int k = 0; k < N; ++k)
101793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
102793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
103793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        RNG& rng = ts->get_rng();
104793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
105793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        int n = rng.next()%MAX_COUNT_OF_POINTS + 1;
106793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
107793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        generate_src_points <T> (src, n);
108793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
109793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cv::Rect right = get_bounding_rect <T> (src);
110793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
111793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cv::Rect rect[2] = { boundingRect(src), boundingRect(Mat(src)) };
112793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
113793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        for (int i = 0; i < 2; ++i) if (rect[i] != right)
114793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
115793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            cout << endl; cout << "Checking for the work of boundingRect function..." << endl;
116793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            cout << "Type of src points: ";
117793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            switch (type)
118793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
119793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            case 0: {cout << "INT"; break;}
120793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            case 1: {cout << "FLOAT"; break;}
121793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            default: break;
122793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
123793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            cout << endl;
124793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            cout << "Src points are stored as "; if (i == 0) cout << "VECTOR" << endl; else cout << "MAT" << endl;
125793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            cout << "Number of points: " << n << endl;
126793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            cout << "Right rect (x, y, w, h): [" << right.x << ", " << right.y << ", " << right.width << ", " << right.height << "]" << endl;
127793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            cout << "Result rect (x, y, w, h): [" << rect[i].x << ", " << rect[i].y << ", " << rect[i].width << ", " << rect[i].height << "]" << endl;
128793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            cout << endl;
129793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            CV_Error(IMGPROC_BOUNDINGRECT_ERROR_DIFF, MESSAGE_ERROR_DIFF);
130793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            return false;
131793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
132793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
133793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
134793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
135793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    return true;
136793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
137793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
138793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid CV_BoundingRectTest::run(int)
139793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
140793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    vector <Point> src_veci; if (!checking_function_work(src_veci, 0)) return;
141793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    vector <Point2f> src_vecf; checking_function_work(src_vecf, 1);
142793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
143793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
144793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerTEST (Imgproc_BoundingRect, accuracy) { CV_BoundingRectTest test; test.safe_run(); }
145