Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_intersection.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) 2008-2011, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// @Authors
18
// Nghia Ho, [email protected]
19
//
20
// Redistribution and use in source and binary forms, with or without modification,
21
// are permitted provided that the following conditions are met:
22
//
23
// * Redistribution's of source code must retain the above copyright notice,
24
// this list of conditions and the following disclaimer.
25
//
26
// * Redistribution's in binary form must reproduce the above copyright notice,
27
// this list of conditions and the following disclaimer in the documentation
28
// and/or other materials provided with the distribution.
29
//
30
// * The name of OpenCV Foundation may not be used to endorse or promote products
31
// derived from this software without specific prior written permission.
32
//
33
// This software is provided by the copyright holders and contributors "as is" and
34
// any express or implied warranties, including, but not limited to, the implied
35
// warranties of merchantability and fitness for a particular purpose are disclaimed.
36
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
37
// indirect, incidental, special, exemplary, or consequential damages
38
// (including, but not limited to, procurement of substitute goods or services;
39
// loss of use, data, or profits; or business interruption) however caused
40
// and on any theory of liability, whether in contract, strict liability,
41
// or tort (including negligence or otherwise) arising in any way out of
42
// the use of this software, even if advised of the possibility of such damage.
43
//
44
//M*/
45
46
#include "test_precomp.hpp"
47
48
namespace opencv_test { namespace {
49
50
#define ACCURACY 0.00001
51
52
// See pics/intersection.png for the scenarios we are testing
53
54
// Test the following scenarios:
55
// 1 - no intersection
56
// 2 - partial intersection, rectangle translated
57
// 3 - partial intersection, rectangle rotated 45 degree on the corner, forms a triangle intersection
58
// 4 - full intersection, rectangles of same size directly on top of each other
59
// 5 - partial intersection, rectangle on top rotated 45 degrees
60
// 6 - partial intersection, rectangle on top of different size
61
// 7 - full intersection, rectangle fully enclosed in the other
62
// 8 - partial intersection, rectangle corner just touching. point contact
63
// 9 - partial intersetion. rectangle side by side, line contact
64
65
static void compare(const std::vector<Point2f>& test, const std::vector<Point2f>& target)
66
{
67
ASSERT_EQ(test.size(), target.size());
68
ASSERT_TRUE(test.size() < 4 || isContourConvex(test));
69
ASSERT_TRUE(target.size() < 4 || isContourConvex(target));
70
for( size_t i = 0; i < test.size(); i++ )
71
{
72
double r = sqrt(normL2Sqr<double>(test[i] - target[i]));
73
ASSERT_LT(r, ACCURACY);
74
}
75
}
76
77
TEST(Imgproc_RotatedRectangleIntersection, accuracy_1)
78
{
79
// no intersection
80
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 12.0f);
81
RotatedRect rect2(Point2f(10, 10), Size2f(2, 2), 34.0f);
82
83
vector<Point2f> vertices;
84
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
85
86
CV_Assert(ret == INTERSECT_NONE);
87
CV_Assert(vertices.empty());
88
}
89
90
TEST(Imgproc_RotatedRectangleIntersection, accuracy_2)
91
{
92
// partial intersection, rectangles translated
93
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
94
RotatedRect rect2(Point2f(1, 1), Size2f(2, 2), 0.0f);
95
96
vector<Point2f> vertices;
97
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
98
99
CV_Assert(ret == INTERSECT_PARTIAL);
100
101
vector<Point2f> targetVertices(4);
102
targetVertices[0] = Point2f(1.0f, 0.0f);
103
targetVertices[1] = Point2f(1.0f, 1.0f);
104
targetVertices[2] = Point2f(0.0f, 1.0f);
105
targetVertices[3] = Point2f(0.0f, 0.0f);
106
compare(vertices, targetVertices);
107
}
108
109
TEST(Imgproc_RotatedRectangleIntersection, accuracy_3)
110
{
111
// partial intersection, rectangles rotated 45 degree on the corner, forms a triangle intersection
112
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
113
RotatedRect rect2(Point2f(1, 1), Size2f(sqrt(2.0f), 20), 45.0f);
114
115
vector<Point2f> vertices;
116
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
117
118
CV_Assert(ret == INTERSECT_PARTIAL);
119
120
vector<Point2f> targetVertices(3);
121
targetVertices[0] = Point2f(1.0f, 0.0f);
122
targetVertices[1] = Point2f(1.0f, 1.0f);
123
targetVertices[2] = Point2f(0.0f, 1.0f);
124
compare(vertices, targetVertices);
125
}
126
127
TEST(Imgproc_RotatedRectangleIntersection, accuracy_4)
128
{
129
// full intersection, rectangles of same size directly on top of each other
130
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
131
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);
132
133
vector<Point2f> vertices;
134
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
135
136
CV_Assert(ret == INTERSECT_FULL);
137
138
vector<Point2f> targetVertices(4);
139
targetVertices[0] = Point2f(-1.0f, 1.0f);
140
targetVertices[1] = Point2f(-1.0f, -1.0f);
141
targetVertices[2] = Point2f(1.0f, -1.0f);
142
targetVertices[3] = Point2f(1.0f, 1.0f);
143
compare(vertices, targetVertices);
144
}
145
146
TEST(Imgproc_RotatedRectangleIntersection, accuracy_5)
147
{
148
// partial intersection, rectangle on top rotated 45 degrees
149
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
150
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 45.0f);
151
152
vector<Point2f> vertices;
153
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
154
155
CV_Assert(ret == INTERSECT_PARTIAL);
156
157
vector<Point2f> targetVertices(8);
158
targetVertices[0] = Point2f(-1.0f, -0.414214f);
159
targetVertices[1] = Point2f(-0.414214f, -1.0f);
160
targetVertices[2] = Point2f(0.414214f, -1.0f);
161
targetVertices[3] = Point2f(1.0f, -0.414214f);
162
targetVertices[4] = Point2f(1.0f, 0.414214f);
163
targetVertices[5] = Point2f(0.414214f, 1.0f);
164
targetVertices[6] = Point2f(-0.414214f, 1.0f);
165
targetVertices[7] = Point2f(-1.0f, 0.414214f);
166
compare(vertices, targetVertices);
167
}
168
169
TEST(Imgproc_RotatedRectangleIntersection, accuracy_6)
170
{
171
// 6 - partial intersection, rectangle on top of different size
172
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
173
RotatedRect rect2(Point2f(0, 0), Size2f(2, 10), 0.0f);
174
175
vector<Point2f> vertices;
176
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
177
178
CV_Assert(ret == INTERSECT_PARTIAL);
179
180
vector<Point2f> targetVertices(4);
181
targetVertices[0] = Point2f(-1.0f, -1.0f);
182
targetVertices[1] = Point2f(1.0f, -1.0f);
183
targetVertices[2] = Point2f(1.0f, 1.0f);
184
targetVertices[3] = Point2f(-1.0f, 1.0f);
185
compare(vertices, targetVertices);
186
}
187
188
TEST(Imgproc_RotatedRectangleIntersection, accuracy_7)
189
{
190
// full intersection, rectangle fully enclosed in the other
191
RotatedRect rect1(Point2f(0, 0), Size2f(12.34f, 56.78f), 0.0f);
192
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);
193
194
vector<Point2f> vertices;
195
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
196
197
CV_Assert(ret == INTERSECT_FULL);
198
199
vector<Point2f> targetVertices(4);
200
targetVertices[0] = Point2f(-1.0f, 1.0f);
201
targetVertices[1] = Point2f(-1.0f, -1.0f);
202
targetVertices[2] = Point2f(1.0f, -1.0f);
203
targetVertices[3] = Point2f(1.0f, 1.0f);
204
compare(vertices, targetVertices);
205
}
206
207
TEST(Imgproc_RotatedRectangleIntersection, accuracy_8)
208
{
209
// intersection by a single vertex
210
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
211
RotatedRect rect2(Point2f(2, 2), Size2f(2, 2), 0.0f);
212
213
vector<Point2f> vertices;
214
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
215
216
CV_Assert(ret == INTERSECT_PARTIAL);
217
compare(vertices, vector<Point2f>(1, Point2f(1.0f, 1.0f)));
218
}
219
220
TEST(Imgproc_RotatedRectangleIntersection, accuracy_9)
221
{
222
// full intersection, rectangle fully enclosed in the other
223
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
224
RotatedRect rect2(Point2f(2, 0), Size2f(2, 123.45f), 0.0f);
225
226
vector<Point2f> vertices;
227
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
228
229
CV_Assert(ret == INTERSECT_PARTIAL);
230
231
vector<Point2f> targetVertices(2);
232
targetVertices[0] = Point2f(1.0f, -1.0f);
233
targetVertices[1] = Point2f(1.0f, 1.0f);
234
compare(vertices, targetVertices);
235
}
236
237
TEST(Imgproc_RotatedRectangleIntersection, accuracy_10)
238
{
239
// three points of rect2 are inside rect1.
240
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
241
RotatedRect rect2(Point2f(0, 0.5), Size2f(1, 1), 45.0f);
242
243
vector<Point2f> vertices;
244
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
245
246
CV_Assert(ret == INTERSECT_PARTIAL);
247
248
vector<Point2f> targetVertices(5);
249
targetVertices[0] = Point2f(0.207107f, 1.0f);
250
targetVertices[1] = Point2f(-0.207107f, 1.0f);
251
targetVertices[2] = Point2f(-0.707107f, 0.5f);
252
targetVertices[3] = Point2f(0.0f, -0.207107f);
253
targetVertices[4] = Point2f(0.707107f, 0.5f);
254
compare(vertices, targetVertices);
255
}
256
257
TEST(Imgproc_RotatedRectangleIntersection, accuracy_11)
258
{
259
RotatedRect rect1(Point2f(0, 0), Size2f(4, 2), 0.0f);
260
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), -45.0f);
261
262
vector<Point2f> vertices;
263
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
264
265
CV_Assert(ret == INTERSECT_PARTIAL);
266
267
vector<Point2f> targetVertices(6);
268
targetVertices[0] = Point2f(-0.414214f, -1.0f);
269
targetVertices[1] = Point2f(0.414213f, -1.0f);
270
targetVertices[2] = Point2f(1.41421f, 0.0f);
271
targetVertices[3] = Point2f(0.414214f, 1.0f);
272
targetVertices[4] = Point2f(-0.414213f, 1.0f);
273
targetVertices[5] = Point2f(-1.41421f, 0.0f);
274
compare(vertices, targetVertices);
275
}
276
277
TEST(Imgproc_RotatedRectangleIntersection, accuracy_12)
278
{
279
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
280
RotatedRect rect2(Point2f(0, 1), Size2f(1, 1), 0.0f);
281
282
vector<Point2f> vertices;
283
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
284
285
CV_Assert(ret == INTERSECT_PARTIAL);
286
287
vector<Point2f> targetVertices(4);
288
targetVertices[0] = Point2f(-0.5f, 1.0f);
289
targetVertices[1] = Point2f(-0.5f, 0.5f);
290
targetVertices[2] = Point2f(0.5f, 0.5f);
291
targetVertices[3] = Point2f(0.5f, 1.0f);
292
compare(vertices, targetVertices);
293
}
294
295
TEST(Imgproc_RotatedRectangleIntersection, accuracy_13)
296
{
297
RotatedRect rect1(Point2f(0, 0), Size2f(1, 3), 0.0f);
298
RotatedRect rect2(Point2f(0, 1), Size2f(3, 1), 0.0f);
299
300
vector<Point2f> vertices;
301
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
302
303
CV_Assert(ret == INTERSECT_PARTIAL);
304
305
vector<Point2f> targetVertices(4);
306
targetVertices[0] = Point2f(-0.5f, 0.5f);
307
targetVertices[1] = Point2f(0.5f, 0.5f);
308
targetVertices[2] = Point2f(0.5f, 1.5f);
309
targetVertices[3] = Point2f(-0.5f, 1.5f);
310
compare(vertices, targetVertices);
311
}
312
313
TEST(Imgproc_RotatedRectangleIntersection, accuracy_14)
314
{
315
const int kNumTests = 100;
316
const float kWidth = 5;
317
const float kHeight = 5;
318
RotatedRect rects[2];
319
std::vector<Point2f> inter;
320
cv::RNG& rng = cv::theRNG();
321
for (int i = 0; i < kNumTests; ++i)
322
{
323
for (int j = 0; j < 2; ++j)
324
{
325
rects[j].center = Point2f(rng.uniform(0.0f, kWidth), rng.uniform(0.0f, kHeight));
326
rects[j].size = Size2f(rng.uniform(1.0f, kWidth), rng.uniform(1.0f, kHeight));
327
rects[j].angle = rng.uniform(0.0f, 360.0f);
328
}
329
int res = rotatedRectangleIntersection(rects[0], rects[1], inter);
330
EXPECT_TRUE(res == INTERSECT_NONE || res == INTERSECT_PARTIAL || res == INTERSECT_FULL) << res;
331
ASSERT_TRUE(inter.size() < 4 || isContourConvex(inter)) << inter;
332
}
333
}
334
335
TEST(Imgproc_RotatedRectangleIntersection, regression_12221_1)
336
{
337
RotatedRect r1(
338
Point2f(259.65081787109375, 51.58895492553711),
339
Size2f(5487.8779296875, 233.8921661376953),
340
-29.488616943359375);
341
RotatedRect r2(
342
Point2f(293.70465087890625, 112.10154724121094),
343
Size2f(5487.8896484375, 234.87368774414062),
344
-31.27001953125);
345
346
std::vector<Point2f> intersections;
347
int interType = cv::rotatedRectangleIntersection(r1, r2, intersections);
348
EXPECT_EQ(INTERSECT_PARTIAL, interType);
349
EXPECT_LE(intersections.size(), (size_t)8);
350
}
351
352
TEST(Imgproc_RotatedRectangleIntersection, regression_12221_2)
353
{
354
RotatedRect r1(
355
Point2f(239.78500366210938, 515.72021484375),
356
Size2f(70.23420715332031, 39.74684524536133),
357
-42.86162567138672);
358
RotatedRect r2(
359
Point2f(242.4205322265625, 510.1195373535156),
360
Size2f(66.85948944091797, 61.46455383300781),
361
-9.840961456298828);
362
363
std::vector<Point2f> intersections;
364
int interType = cv::rotatedRectangleIntersection(r1, r2, intersections);
365
EXPECT_EQ(INTERSECT_PARTIAL, interType);
366
EXPECT_LE(intersections.size(), (size_t)8);
367
}
368
369
}} // namespace
370
371