Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/ocl/test_warp.cpp
16344 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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16
// Third party copyrights are property of their respective owners.
17
//
18
// @Authors
19
// Niko Li, [email protected]
20
// Jia Haipeng, [email protected]
21
// Shengen Yan, [email protected]
22
// Jiang Liyuan, [email protected]
23
// Rock Li, [email protected]
24
// Wu Zailong, [email protected]
25
// Xu Pang, [email protected]
26
// Sen Liu, [email protected]
27
//
28
// Redistribution and use in source and binary forms, with or without modification,
29
// are permitted provided that the following conditions are met:
30
//
31
// * Redistribution's of source code must retain the above copyright notice,
32
// this list of conditions and the following disclaimer.
33
//
34
// * Redistribution's in binary form must reproduce the above copyright notice,
35
// this list of conditions and the following disclaimer in the documentation
36
// and/or other materials provided with the distribution.
37
//
38
// * The name of the copyright holders may not be used to endorse or promote products
39
// derived from this software without specific prior written permission.
40
//
41
// This software is provided by the copyright holders and contributors "as is" and
42
// any express or implied warranties, including, but not limited to, the implied
43
// warranties of merchantability and fitness for a particular purpose are disclaimed.
44
// In no event shall the Intel Corporation or contributors be liable for any direct,
45
// indirect, incidental, special, exemplary, or consequential damages
46
// (including, but not limited to, procurement of substitute goods or services;
47
// loss of use, data, or profits; or business interruption) however caused
48
// and on any theory of liability, whether in contract, strict liability,
49
// or tort (including negligence or otherwise) arising in any way out of
50
// the use of this software, even if advised of the possibility of such damage.
51
//
52
//M*/
53
54
#include "../test_precomp.hpp"
55
#include "opencv2/ts/ocl_test.hpp"
56
57
#ifdef HAVE_OPENCL
58
59
namespace opencv_test {
60
namespace ocl {
61
62
enum
63
{
64
noType = -1
65
};
66
67
/////////////////////////////////////////////////////////////////////////////////////////////////
68
// warpAffine & warpPerspective
69
70
PARAM_TEST_CASE(WarpTestBase, MatType, Interpolation, bool, bool)
71
{
72
int type, interpolation;
73
Size dsize;
74
bool useRoi, mapInverse;
75
int depth;
76
77
TEST_DECLARE_INPUT_PARAMETER(src);
78
TEST_DECLARE_OUTPUT_PARAMETER(dst);
79
80
virtual void SetUp()
81
{
82
type = GET_PARAM(0);
83
interpolation = GET_PARAM(1);
84
mapInverse = GET_PARAM(2);
85
useRoi = GET_PARAM(3);
86
depth = CV_MAT_DEPTH(type);
87
88
if (mapInverse)
89
interpolation |= WARP_INVERSE_MAP;
90
}
91
92
void random_roi()
93
{
94
dsize = randomSize(1, MAX_VALUE);
95
96
Size roiSize = randomSize(1, MAX_VALUE);
97
Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
98
randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
99
100
Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
101
randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
102
103
UMAT_UPLOAD_INPUT_PARAMETER(src);
104
UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
105
}
106
107
void Near(double threshold = 0.0)
108
{
109
if (depth < CV_32F)
110
EXPECT_MAT_N_DIFF(dst_roi, udst_roi, cvRound(dst_roi.total()*threshold));
111
else
112
OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);
113
}
114
};
115
116
PARAM_TEST_CASE(WarpTest_cols4_Base, MatType, Interpolation, bool, bool)
117
{
118
int type, interpolation;
119
Size dsize;
120
bool useRoi, mapInverse;
121
int depth;
122
123
TEST_DECLARE_INPUT_PARAMETER(src);
124
TEST_DECLARE_OUTPUT_PARAMETER(dst);
125
126
virtual void SetUp()
127
{
128
type = GET_PARAM(0);
129
interpolation = GET_PARAM(1);
130
mapInverse = GET_PARAM(2);
131
useRoi = GET_PARAM(3);
132
depth = CV_MAT_DEPTH(type);
133
134
if (mapInverse)
135
interpolation |= WARP_INVERSE_MAP;
136
}
137
138
void random_roi()
139
{
140
dsize = randomSize(1, MAX_VALUE);
141
dsize.width = ((dsize.width >> 2) + 1) * 4;
142
143
Size roiSize = randomSize(1, MAX_VALUE);
144
Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
145
randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
146
147
Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
148
randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
149
150
UMAT_UPLOAD_INPUT_PARAMETER(src);
151
UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
152
}
153
154
void Near(double threshold = 0.0)
155
{
156
if (depth < CV_32F)
157
EXPECT_MAT_N_DIFF(dst_roi, udst_roi, cvRound(dst_roi.total()*threshold));
158
else
159
OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);
160
}
161
};
162
163
/////warpAffine
164
165
typedef WarpTestBase WarpAffine;
166
167
/////warpAffine
168
169
typedef WarpTestBase WarpAffine;
170
171
OCL_TEST_P(WarpAffine, Mat)
172
{
173
for (int j = 0; j < test_loop_times; j++)
174
{
175
double eps = depth < CV_32F ? 0.04 : 0.06;
176
random_roi();
177
178
Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f),
179
rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
180
181
OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation));
182
OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation));
183
184
Near(eps);
185
}
186
}
187
188
typedef WarpTest_cols4_Base WarpAffine_cols4;
189
190
OCL_TEST_P(WarpAffine_cols4, Mat)
191
{
192
for (int j = 0; j < test_loop_times; j++)
193
{
194
double eps = depth < CV_32F ? 0.04 : 0.06;
195
random_roi();
196
197
Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f),
198
rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
199
200
OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation));
201
OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation));
202
203
Near(eps);
204
}
205
}
206
207
//// warpPerspective
208
209
typedef WarpTestBase WarpPerspective;
210
211
OCL_TEST_P(WarpPerspective, Mat)
212
{
213
for (int j = 0; j < test_loop_times; j++)
214
{
215
double eps = depth < CV_32F ? 0.03 : 0.06;
216
random_roi();
217
218
float cols = static_cast<float>(src_roi.cols), rows = static_cast<float>(src_roi.rows);
219
float cols2 = cols / 2.0f, rows2 = rows / 2.0f;
220
Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
221
Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)),
222
Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)),
223
Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)),
224
Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) };
225
Mat M = getPerspectiveTransform(sp, dp);
226
227
OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation));
228
OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation));
229
230
Near(eps);
231
}
232
}
233
234
typedef WarpTest_cols4_Base WarpPerspective_cols4;
235
236
OCL_TEST_P(WarpPerspective_cols4, Mat)
237
{
238
for (int j = 0; j < test_loop_times; j++)
239
{
240
double eps = depth < CV_32F ? 0.03 : 0.06;
241
random_roi();
242
243
float cols = static_cast<float>(src_roi.cols), rows = static_cast<float>(src_roi.rows);
244
float cols2 = cols / 2.0f, rows2 = rows / 2.0f;
245
Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
246
Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)),
247
Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)),
248
Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)),
249
Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) };
250
Mat M = getPerspectiveTransform(sp, dp);
251
252
OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation));
253
OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation));
254
255
Near(eps);
256
}
257
}
258
259
/////////////////////////////////////////////////////////////////////////////////////////////////
260
//// resize
261
262
PARAM_TEST_CASE(Resize, MatType, double, double, Interpolation, bool, int)
263
{
264
int type, interpolation;
265
int widthMultiple;
266
double fx, fy;
267
bool useRoi;
268
269
TEST_DECLARE_INPUT_PARAMETER(src);
270
TEST_DECLARE_OUTPUT_PARAMETER(dst);
271
272
virtual void SetUp()
273
{
274
type = GET_PARAM(0);
275
fx = GET_PARAM(1);
276
fy = GET_PARAM(2);
277
interpolation = GET_PARAM(3);
278
useRoi = GET_PARAM(4);
279
widthMultiple = GET_PARAM(5);
280
}
281
282
void random_roi()
283
{
284
CV_Assert(fx > 0 && fy > 0);
285
286
Size srcRoiSize = randomSize(10, MAX_VALUE), dstRoiSize;
287
// Make sure the width is a multiple of the requested value, and no more
288
srcRoiSize.width += widthMultiple - 1 - (srcRoiSize.width - 1) % widthMultiple;
289
dstRoiSize.width = cvRound(srcRoiSize.width * fx);
290
dstRoiSize.height = cvRound(srcRoiSize.height * fy);
291
292
if (dstRoiSize.empty())
293
{
294
random_roi();
295
return;
296
}
297
298
Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
299
randomSubMat(src, src_roi, srcRoiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
300
301
Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
302
randomSubMat(dst, dst_roi, dstRoiSize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
303
304
UMAT_UPLOAD_INPUT_PARAMETER(src);
305
UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
306
}
307
};
308
309
#if defined(__aarch64__) || defined(__arm__)
310
const int integerEps = 3;
311
#else
312
const int integerEps = 1;
313
#endif
314
OCL_TEST_P(Resize, Mat)
315
{
316
for (int j = 0; j < test_loop_times; j++)
317
{
318
int depth = CV_MAT_DEPTH(type);
319
double eps = depth <= CV_32S ? integerEps : 5e-2;
320
321
random_roi();
322
323
OCL_OFF(cv::resize(src_roi, dst_roi, Size(), fx, fy, interpolation));
324
OCL_ON(cv::resize(usrc_roi, udst_roi, Size(), fx, fy, interpolation));
325
326
OCL_EXPECT_MAT_N_DIFF(dst, eps);
327
}
328
}
329
330
/////////////////////////////////////////////////////////////////////////////////////////////////
331
// remap
332
333
PARAM_TEST_CASE(Remap, MatDepth, Channels, std::pair<MatType, MatType>, BorderType, bool)
334
{
335
int srcType, map1Type, map2Type;
336
int borderType;
337
bool useRoi;
338
339
Scalar val;
340
341
TEST_DECLARE_INPUT_PARAMETER(src);
342
TEST_DECLARE_INPUT_PARAMETER(map1);
343
TEST_DECLARE_INPUT_PARAMETER(map2);
344
TEST_DECLARE_OUTPUT_PARAMETER(dst);
345
346
virtual void SetUp()
347
{
348
srcType = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1));
349
map1Type = GET_PARAM(2).first;
350
map2Type = GET_PARAM(2).second;
351
borderType = GET_PARAM(3);
352
useRoi = GET_PARAM(4);
353
}
354
355
void random_roi()
356
{
357
val = randomScalar(-MAX_VALUE, MAX_VALUE);
358
Size srcROISize = randomSize(1, MAX_VALUE);
359
Size dstROISize = randomSize(1, MAX_VALUE);
360
361
Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
362
randomSubMat(src, src_roi, srcROISize, srcBorder, srcType, 5, 256);
363
364
Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
365
randomSubMat(dst, dst_roi, dstROISize, dstBorder, srcType, -MAX_VALUE, MAX_VALUE);
366
367
int mapMaxValue = MAX_VALUE << 2;
368
Border map1Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
369
randomSubMat(map1, map1_roi, dstROISize, map1Border, map1Type, -mapMaxValue, mapMaxValue);
370
371
Border map2Border = randomBorder(0, useRoi ? MAX_VALUE + 1 : 0);
372
if (map2Type != noType)
373
{
374
int mapMinValue = -mapMaxValue;
375
if (map2Type == CV_16UC1 || map2Type == CV_16SC1)
376
mapMinValue = 0, mapMaxValue = INTER_TAB_SIZE2;
377
randomSubMat(map2, map2_roi, dstROISize, map2Border, map2Type, mapMinValue, mapMaxValue);
378
}
379
380
UMAT_UPLOAD_INPUT_PARAMETER(src);
381
UMAT_UPLOAD_INPUT_PARAMETER(map1);
382
UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
383
if (noType != map2Type)
384
UMAT_UPLOAD_INPUT_PARAMETER(map2);
385
}
386
};
387
388
typedef Remap Remap_INTER_NEAREST;
389
390
OCL_TEST_P(Remap_INTER_NEAREST, Mat)
391
{
392
for (int j = 0; j < test_loop_times; j++)
393
{
394
random_roi();
395
396
OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_NEAREST, borderType, val));
397
OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_NEAREST, borderType, val));
398
399
OCL_EXPECT_MAT_N_DIFF(dst, 1.0);
400
}
401
}
402
403
typedef Remap Remap_INTER_LINEAR;
404
405
OCL_TEST_P(Remap_INTER_LINEAR, Mat)
406
{
407
for (int j = 0; j < test_loop_times; j++)
408
{
409
random_roi();
410
411
double eps = 2.0;
412
#ifdef __ANDROID__
413
// TODO investigate accuracy
414
if (cv::ocl::Device::getDefault().isNVidia())
415
eps = 8.0;
416
#elif defined(__arm__)
417
eps = 8.0;
418
#endif
419
420
OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_LINEAR, borderType, val));
421
OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_LINEAR, borderType, val));
422
423
OCL_EXPECT_MAT_N_DIFF(dst, eps);
424
}
425
}
426
427
/////////////////////////////////////////////////////////////////////////////////////
428
429
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine, Combine(
430
Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
431
Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
432
Bool(),
433
Bool()));
434
435
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine_cols4, Combine(
436
Values((MatType)CV_8UC1),
437
Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
438
Bool(),
439
Bool()));
440
441
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective, Combine(
442
Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
443
Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
444
Bool(),
445
Bool()));
446
447
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective_cols4, Combine(
448
Values((MatType)CV_8UC1),
449
Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
450
Bool(),
451
Bool()));
452
453
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Resize, Combine(
454
Values(CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, CV_32FC4),
455
Values(0.5, 1.5, 2.0, 0.2),
456
Values(0.5, 1.5, 2.0, 0.2),
457
Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR),
458
Bool(),
459
Values(1, 16)));
460
461
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpLinearExact, Resize, Combine(
462
Values(CV_8UC1, CV_8UC4, CV_16UC2),
463
Values(0.5, 1.5, 2.0, 0.2),
464
Values(0.5, 1.5, 2.0, 0.2),
465
Values((Interpolation)INTER_LINEAR_EXACT),
466
Bool(),
467
Values(1, 16)));
468
469
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpResizeArea, Resize, Combine(
470
Values((MatType)CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4),
471
Values(0.7, 0.4, 0.5),
472
Values(0.3, 0.6, 0.5),
473
Values((Interpolation)INTER_AREA),
474
Bool(),
475
Values(1, 16)));
476
477
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_LINEAR, Combine(
478
Values(CV_8U, CV_16U, CV_32F),
479
Values(1, 3, 4),
480
Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),
481
std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),
482
std::pair<MatType, MatType>((MatType)CV_32FC2, noType)),
483
Values((BorderType)BORDER_CONSTANT,
484
(BorderType)BORDER_REPLICATE,
485
(BorderType)BORDER_WRAP,
486
(BorderType)BORDER_REFLECT,
487
(BorderType)BORDER_REFLECT_101),
488
Bool()));
489
490
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_NEAREST, Combine(
491
Values(CV_8U, CV_16U, CV_32F),
492
Values(1, 3, 4),
493
Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),
494
std::pair<MatType, MatType>((MatType)CV_32FC2, noType),
495
std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),
496
std::pair<MatType, MatType>((MatType)CV_16SC2, noType)),
497
Values((BorderType)BORDER_CONSTANT,
498
(BorderType)BORDER_REPLICATE,
499
(BorderType)BORDER_WRAP,
500
(BorderType)BORDER_REFLECT,
501
(BorderType)BORDER_REFLECT_101),
502
Bool()));
503
504
} } // namespace opencv_test::ocl
505
506
#endif // HAVE_OPENCL
507
508