Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_boundingrect.cpp
16354 views
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
43
#include "test_precomp.hpp"
44
45
namespace opencv_test { namespace {
46
47
#define IMGPROC_BOUNDINGRECT_ERROR_DIFF 1
48
49
#define MESSAGE_ERROR_DIFF "Bounding rectangle found by boundingRect function is incorrect."
50
51
class CV_BoundingRectTest: public cvtest::ArrayTest
52
{
53
public:
54
CV_BoundingRectTest();
55
~CV_BoundingRectTest();
56
57
protected:
58
void run (int);
59
60
private:
61
template <typename T> void generate_src_points(vector <Point_<T> >& src, int n);
62
template <typename T> cv::Rect get_bounding_rect(const vector <Point_<T> > src);
63
template <typename T> bool checking_function_work(vector <Point_<T> >& src, int type);
64
};
65
66
CV_BoundingRectTest::CV_BoundingRectTest() {}
67
CV_BoundingRectTest::~CV_BoundingRectTest() {}
68
69
template <typename T> void CV_BoundingRectTest::generate_src_points(vector <Point_<T> >& src, int n)
70
{
71
src.clear();
72
for (int i = 0; i < n; ++i)
73
src.push_back(Point_<T>(cv::randu<T>(), cv::randu<T>()));
74
}
75
76
template <typename T> cv::Rect CV_BoundingRectTest::get_bounding_rect(const vector <Point_<T> > src)
77
{
78
int n = (int)src.size();
79
T min_w = std::numeric_limits<T>::max(), max_w = std::numeric_limits<T>::min();
80
T min_h = min_w, max_h = max_w;
81
82
for (int i = 0; i < n; ++i)
83
{
84
min_w = std::min<T>(src.at(i).x, min_w);
85
max_w = std::max<T>(src.at(i).x, max_w);
86
min_h = std::min<T>(src.at(i).y, min_h);
87
max_h = std::max<T>(src.at(i).y, max_h);
88
}
89
90
return Rect((int)min_w, (int)min_h, (int)max_w-(int)min_w + 1, (int)max_h-(int)min_h + 1);
91
}
92
93
template <typename T> bool CV_BoundingRectTest::checking_function_work(vector <Point_<T> >& src, int type)
94
{
95
const int MAX_COUNT_OF_POINTS = 1000;
96
const int N = 10000;
97
98
for (int k = 0; k < N; ++k)
99
{
100
101
RNG& rng = ts->get_rng();
102
103
int n = rng.next()%MAX_COUNT_OF_POINTS + 1;
104
105
generate_src_points <T> (src, n);
106
107
cv::Rect right = get_bounding_rect <T> (src);
108
109
cv::Rect rect[2] = { boundingRect(src), boundingRect(Mat(src)) };
110
111
for (int i = 0; i < 2; ++i) if (rect[i] != right)
112
{
113
cout << endl; cout << "Checking for the work of boundingRect function..." << endl;
114
cout << "Type of src points: ";
115
switch (type)
116
{
117
case 0: {cout << "INT"; break;}
118
case 1: {cout << "FLOAT"; break;}
119
default: break;
120
}
121
cout << endl;
122
cout << "Src points are stored as "; if (i == 0) cout << "VECTOR" << endl; else cout << "MAT" << endl;
123
cout << "Number of points: " << n << endl;
124
cout << "Right rect (x, y, w, h): [" << right.x << ", " << right.y << ", " << right.width << ", " << right.height << "]" << endl;
125
cout << "Result rect (x, y, w, h): [" << rect[i].x << ", " << rect[i].y << ", " << rect[i].width << ", " << rect[i].height << "]" << endl;
126
cout << endl;
127
CV_Error(IMGPROC_BOUNDINGRECT_ERROR_DIFF, MESSAGE_ERROR_DIFF);
128
}
129
130
}
131
132
return true;
133
}
134
135
void CV_BoundingRectTest::run(int)
136
{
137
vector <Point> src_veci; if (!checking_function_work(src_veci, 0)) return;
138
vector <Point2f> src_vecf; checking_function_work(src_vecf, 1);
139
}
140
141
TEST (Imgproc_BoundingRect, accuracy) { CV_BoundingRectTest test; test.safe_run(); }
142
143
}} // namespace
144
145