Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_imgwarp_strict.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
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "test_precomp.hpp"
43
44
namespace opencv_test { namespace {
45
46
void __wrap_printf_func(const char* fmt, ...)
47
{
48
va_list args;
49
va_start(args, fmt);
50
char buffer[256];
51
vsprintf (buffer, fmt, args);
52
cvtest::TS::ptr()->printf(cvtest::TS::SUMMARY, buffer);
53
va_end(args);
54
}
55
56
#define PRINT_TO_LOG __wrap_printf_func
57
58
#define SHOW_IMAGE
59
#undef SHOW_IMAGE
60
61
////////////////////////////////////////////////////////////////////////////////////////////////////////
62
// ImageWarpBaseTest
63
////////////////////////////////////////////////////////////////////////////////////////////////////////
64
65
class CV_ImageWarpBaseTest :
66
public cvtest::BaseTest
67
{
68
public:
69
enum { cell_size = 10 };
70
71
CV_ImageWarpBaseTest();
72
virtual ~CV_ImageWarpBaseTest();
73
74
virtual void run(int);
75
protected:
76
virtual void generate_test_data();
77
78
virtual void run_func() = 0;
79
virtual void run_reference_func() = 0;
80
virtual float get_success_error_level(int _interpolation, int _depth) const;
81
virtual void validate_results() const;
82
virtual void prepare_test_data_for_reference_func();
83
84
Size randSize(RNG& rng) const;
85
86
String interpolation_to_string(int inter_type) const;
87
88
int interpolation;
89
Mat src;
90
Mat dst;
91
Mat reference_dst;
92
};
93
94
CV_ImageWarpBaseTest::CV_ImageWarpBaseTest() :
95
BaseTest(), interpolation(-1),
96
src(), dst(), reference_dst()
97
{
98
test_case_count = 40;
99
ts->set_failed_test_info(cvtest::TS::OK);
100
}
101
102
CV_ImageWarpBaseTest::~CV_ImageWarpBaseTest()
103
{
104
}
105
106
String CV_ImageWarpBaseTest::interpolation_to_string(int inter) const
107
{
108
bool inverse = (inter & WARP_INVERSE_MAP) != 0;
109
inter &= ~WARP_INVERSE_MAP;
110
String str;
111
112
if (inter == INTER_NEAREST)
113
str = "INTER_NEAREST";
114
else if (inter == INTER_LINEAR)
115
str = "INTER_LINEAR";
116
else if (inter == INTER_LINEAR_EXACT)
117
str = "INTER_LINEAR_EXACT";
118
else if (inter == INTER_AREA)
119
str = "INTER_AREA";
120
else if (inter == INTER_CUBIC)
121
str = "INTER_CUBIC";
122
else if (inter == INTER_LANCZOS4)
123
str = "INTER_LANCZOS4";
124
else if (inter == INTER_LANCZOS4 + 1)
125
str = "INTER_AREA_FAST";
126
127
if (inverse)
128
str += " | WARP_INVERSE_MAP";
129
130
return str.empty() ? "Unsupported/Unknown interpolation type" : str;
131
}
132
133
Size CV_ImageWarpBaseTest::randSize(RNG& rng) const
134
{
135
Size size;
136
size.width = static_cast<int>(std::exp(rng.uniform(1.0f, 7.0f)));
137
size.height = static_cast<int>(std::exp(rng.uniform(1.0f, 7.0f)));
138
139
return size;
140
}
141
142
void CV_ImageWarpBaseTest::generate_test_data()
143
{
144
RNG& rng = ts->get_rng();
145
146
// generating the src matrix structure
147
Size ssize = randSize(rng), dsize;
148
149
int depth = rng.uniform(0, CV_64F);
150
while (depth == CV_8S || depth == CV_32S)
151
depth = rng.uniform(0, CV_64F);
152
153
int cn = rng.uniform(1, 4);
154
while (cn == 2)
155
cn = rng.uniform(1, 4);
156
157
src.create(ssize, CV_MAKE_TYPE(depth, cn));
158
159
// generating the src matrix
160
int x, y;
161
if (cvtest::randInt(rng) % 2)
162
{
163
for (y = 0; y < ssize.height; y += cell_size)
164
for (x = 0; x < ssize.width; x += cell_size)
165
rectangle(src, Point(x, y), Point(x + std::min<int>(cell_size, ssize.width - x), y +
166
std::min<int>(cell_size, ssize.height - y)), Scalar::all((x + y) % 2 ? 255: 0), CV_FILLED);
167
}
168
else
169
{
170
src = Scalar::all(255);
171
for (y = cell_size; y < src.rows; y += cell_size)
172
line(src, Point2i(0, y), Point2i(src.cols, y), Scalar::all(0), 1);
173
for (x = cell_size; x < src.cols; x += cell_size)
174
line(src, Point2i(x, 0), Point2i(x, src.rows), Scalar::all(0), 1);
175
}
176
177
// generating an interpolation type
178
interpolation = rng.uniform(0, CV_INTER_LANCZOS4 + 1);
179
180
// generating the dst matrix structure
181
double scale_x, scale_y;
182
if (interpolation == INTER_AREA)
183
{
184
bool area_fast = rng.uniform(0., 1.) > 0.5;
185
if (area_fast)
186
{
187
scale_x = rng.uniform(2, 5);
188
scale_y = rng.uniform(2, 5);
189
}
190
else
191
{
192
scale_x = rng.uniform(1.0, 3.0);
193
scale_y = rng.uniform(1.0, 3.0);
194
}
195
}
196
else
197
{
198
scale_x = rng.uniform(0.4, 4.0);
199
scale_y = rng.uniform(0.4, 4.0);
200
}
201
CV_Assert(scale_x > 0.0f && scale_y > 0.0f);
202
203
dsize.width = saturate_cast<int>((ssize.width + scale_x - 1) / scale_x);
204
dsize.height = saturate_cast<int>((ssize.height + scale_y - 1) / scale_y);
205
206
dst = Mat::zeros(dsize, src.type());
207
reference_dst = Mat::zeros(dst.size(), CV_MAKE_TYPE(CV_32F, dst.channels()));
208
209
scale_x = src.cols / static_cast<double>(dst.cols);
210
scale_y = src.rows / static_cast<double>(dst.rows);
211
212
if (interpolation == INTER_AREA && (scale_x < 1.0 || scale_y < 1.0))
213
interpolation = INTER_LINEAR;
214
}
215
216
void CV_ImageWarpBaseTest::run(int)
217
{
218
for (int i = 0; i < test_case_count; ++i)
219
{
220
generate_test_data();
221
run_func();
222
run_reference_func();
223
if (ts->get_err_code() < 0)
224
break;
225
validate_results();
226
if (ts->get_err_code() < 0)
227
break;
228
ts->update_context(this, i, true);
229
}
230
ts->set_gtest_status();
231
}
232
233
float CV_ImageWarpBaseTest::get_success_error_level(int _interpolation, int) const
234
{
235
if (_interpolation == INTER_CUBIC)
236
return 1.0f;
237
else if (_interpolation == INTER_LANCZOS4)
238
return 1.0f;
239
else if (_interpolation == INTER_NEAREST)
240
return 1.0f;
241
else if (_interpolation == INTER_AREA)
242
return 2.0f;
243
else
244
return 1.0f;
245
}
246
247
void CV_ImageWarpBaseTest::validate_results() const
248
{
249
Mat _dst;
250
dst.convertTo(_dst, reference_dst.depth());
251
252
Size dsize = dst.size(), ssize = src.size();
253
int cn = _dst.channels();
254
dsize.width *= cn;
255
float t = get_success_error_level(interpolation & INTER_MAX, dst.depth());
256
257
for (int dy = 0; dy < dsize.height; ++dy)
258
{
259
const float* rD = reference_dst.ptr<float>(dy);
260
const float* D = _dst.ptr<float>(dy);
261
262
for (int dx = 0; dx < dsize.width; ++dx)
263
if (fabs(rD[dx] - D[dx]) > t &&
264
// fabs(rD[dx] - D[dx]) < 250.0f &&
265
rD[dx] <= 255.0f && D[dx] <= 255.0f && rD[dx] >= 0.0f && D[dx] >= 0.0f)
266
{
267
PRINT_TO_LOG("\nNorm of the difference: %lf\n", cvtest::norm(reference_dst, _dst, NORM_INF));
268
PRINT_TO_LOG("Error in (dx, dy): (%d, %d)\n", dx / cn + 1, dy + 1);
269
PRINT_TO_LOG("Tuple (rD, D): (%f, %f)\n", rD[dx], D[dx]);
270
PRINT_TO_LOG("Dsize: (%d, %d)\n", dsize.width / cn, dsize.height);
271
PRINT_TO_LOG("Ssize: (%d, %d)\n", src.cols, src.rows);
272
273
double scale_x = static_cast<double>(ssize.width) / dsize.width;
274
double scale_y = static_cast<double>(ssize.height) / dsize.height;
275
bool area_fast = interpolation == INTER_AREA &&
276
fabs(scale_x - cvRound(scale_x)) < FLT_EPSILON &&
277
fabs(scale_y - cvRound(scale_y)) < FLT_EPSILON;
278
if (area_fast)
279
{
280
scale_y = cvRound(scale_y);
281
scale_x = cvRound(scale_x);
282
}
283
284
PRINT_TO_LOG("Interpolation: %s\n", interpolation_to_string(area_fast ? INTER_LANCZOS4 + 1 : interpolation).c_str());
285
PRINT_TO_LOG("Scale (x, y): (%lf, %lf)\n", scale_x, scale_y);
286
PRINT_TO_LOG("Elemsize: %d\n", src.elemSize1());
287
PRINT_TO_LOG("Channels: %d\n", cn);
288
289
#ifdef SHOW_IMAGE
290
const std::string w1("OpenCV impl (run func)"), w2("Reference func"), w3("Src image"), w4("Diff");
291
namedWindow(w1, CV_WINDOW_KEEPRATIO);
292
namedWindow(w2, CV_WINDOW_KEEPRATIO);
293
namedWindow(w3, CV_WINDOW_KEEPRATIO);
294
namedWindow(w4, CV_WINDOW_KEEPRATIO);
295
296
Mat diff;
297
absdiff(reference_dst, _dst, diff);
298
299
imshow(w1, dst);
300
imshow(w2, reference_dst);
301
imshow(w3, src);
302
imshow(w4, diff);
303
304
waitKey();
305
#endif
306
307
const int radius = 3;
308
int rmin = MAX(dy - radius, 0), rmax = MIN(dy + radius, dsize.height);
309
int cmin = MAX(dx / cn - radius, 0), cmax = MIN(dx / cn + radius, dsize.width);
310
311
std::cout << "opencv result:\n" << dst(Range(rmin, rmax), Range(cmin, cmax)) << std::endl;
312
std::cout << "reference result:\n" << reference_dst(Range(rmin, rmax), Range(cmin, cmax)) << std::endl;
313
314
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
315
return;
316
}
317
}
318
}
319
320
void CV_ImageWarpBaseTest::prepare_test_data_for_reference_func()
321
{
322
if (src.depth() != CV_32F)
323
{
324
Mat tmp;
325
src.convertTo(tmp, CV_32F);
326
src = tmp;
327
}
328
}
329
330
////////////////////////////////////////////////////////////////////////////////////////////////////////
331
// Resize
332
////////////////////////////////////////////////////////////////////////////////////////////////////////
333
334
class CV_Resize_Test :
335
public CV_ImageWarpBaseTest
336
{
337
public:
338
CV_Resize_Test();
339
virtual ~CV_Resize_Test();
340
341
protected:
342
virtual void generate_test_data();
343
344
virtual void run_func();
345
virtual void run_reference_func();
346
347
private:
348
double scale_x;
349
double scale_y;
350
bool area_fast;
351
352
void resize_generic();
353
void resize_area();
354
double getWeight(double a, double b, int x);
355
356
typedef std::vector<std::pair<int, double> > dim;
357
void generate_buffer(double scale, dim& _dim);
358
void resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim);
359
};
360
361
CV_Resize_Test::CV_Resize_Test() :
362
CV_ImageWarpBaseTest(), scale_x(),
363
scale_y(), area_fast(false)
364
{
365
}
366
367
CV_Resize_Test::~CV_Resize_Test()
368
{
369
}
370
371
namespace
372
{
373
void interpolateLinear(float x, float* coeffs)
374
{
375
coeffs[0] = 1.f - x;
376
coeffs[1] = x;
377
}
378
379
void interpolateCubic(float x, float* coeffs)
380
{
381
const float A = -0.75f;
382
383
coeffs[0] = ((A*(x + 1) - 5*A)*(x + 1) + 8*A)*(x + 1) - 4*A;
384
coeffs[1] = ((A + 2)*x - (A + 3))*x*x + 1;
385
coeffs[2] = ((A + 2)*(1 - x) - (A + 3))*(1 - x)*(1 - x) + 1;
386
coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2];
387
}
388
389
void interpolateLanczos4(float x, float* coeffs)
390
{
391
static const double s45 = 0.70710678118654752440084436210485;
392
static const double cs[][2]=
393
{{1, 0}, {-s45, -s45}, {0, 1}, {s45, -s45}, {-1, 0}, {s45, s45}, {0, -1}, {-s45, s45}};
394
395
if( x < FLT_EPSILON )
396
{
397
for( int i = 0; i < 8; i++ )
398
coeffs[i] = 0;
399
coeffs[3] = 1;
400
return;
401
}
402
403
float sum = 0;
404
double y0=-(x+3)*CV_PI*0.25, s0 = sin(y0), c0=cos(y0);
405
for(int i = 0; i < 8; i++ )
406
{
407
double y = -(x+3-i)*CV_PI*0.25;
408
coeffs[i] = (float)((cs[i][0]*s0 + cs[i][1]*c0)/(y*y));
409
sum += coeffs[i];
410
}
411
412
sum = 1.f/sum;
413
for(int i = 0; i < 8; i++ )
414
coeffs[i] *= sum;
415
}
416
417
typedef void (*interpolate_method)(float x, float* coeffs);
418
interpolate_method inter_array[] = { &interpolateLinear, &interpolateCubic, &interpolateLanczos4 };
419
}
420
421
void CV_Resize_Test::generate_test_data()
422
{
423
RNG& rng = ts->get_rng();
424
425
// generating the src matrix structure
426
Size ssize = randSize(rng), dsize;
427
428
int depth = rng.uniform(0, CV_64F);
429
while (depth == CV_8S || depth == CV_32S)
430
depth = rng.uniform(0, CV_64F);
431
432
int cn = rng.uniform(1, 4);
433
while (cn == 2)
434
cn = rng.uniform(1, 4);
435
436
src.create(ssize, CV_MAKE_TYPE(depth, cn));
437
438
// generating the src matrix
439
int x, y;
440
if (cvtest::randInt(rng) % 2)
441
{
442
for (y = 0; y < ssize.height; y += cell_size)
443
for (x = 0; x < ssize.width; x += cell_size)
444
rectangle(src, Point(x, y), Point(x + std::min<int>(cell_size, ssize.width - x), y +
445
std::min<int>(cell_size, ssize.height - y)), Scalar::all((x + y) % 2 ? 255: 0), CV_FILLED);
446
}
447
else
448
{
449
src = Scalar::all(255);
450
for (y = cell_size; y < src.rows; y += cell_size)
451
line(src, Point2i(0, y), Point2i(src.cols, y), Scalar::all(0), 1);
452
for (x = cell_size; x < src.cols; x += cell_size)
453
line(src, Point2i(x, 0), Point2i(x, src.rows), Scalar::all(0), 1);
454
}
455
456
// generating an interpolation type
457
interpolation = rng.uniform(0, cv::INTER_MAX - 1);
458
459
// generating the dst matrix structure
460
if (interpolation == INTER_AREA)
461
{
462
area_fast = rng.uniform(0., 1.) > 0.5;
463
if (area_fast)
464
{
465
scale_x = rng.uniform(2, 5);
466
scale_y = rng.uniform(2, 5);
467
}
468
else
469
{
470
scale_x = rng.uniform(1.0, 3.0);
471
scale_y = rng.uniform(1.0, 3.0);
472
}
473
}
474
else
475
{
476
scale_x = rng.uniform(0.4, 4.0);
477
scale_y = rng.uniform(0.4, 4.0);
478
}
479
CV_Assert(scale_x > 0.0f && scale_y > 0.0f);
480
481
dsize.width = saturate_cast<int>((ssize.width + scale_x - 1) / scale_x);
482
dsize.height = saturate_cast<int>((ssize.height + scale_y - 1) / scale_y);
483
484
dst = Mat::zeros(dsize, src.type());
485
reference_dst = Mat::zeros(dst.size(), CV_MAKE_TYPE(CV_32F, dst.channels()));
486
487
scale_x = src.cols / static_cast<double>(dst.cols);
488
scale_y = src.rows / static_cast<double>(dst.rows);
489
490
if (interpolation == INTER_AREA && (scale_x < 1.0 || scale_y < 1.0))
491
interpolation = INTER_LINEAR_EXACT;
492
if (interpolation == INTER_LINEAR_EXACT && (depth == CV_32F || depth == CV_64F))
493
interpolation = INTER_LINEAR;
494
495
area_fast = interpolation == INTER_AREA &&
496
fabs(scale_x - cvRound(scale_x)) < FLT_EPSILON &&
497
fabs(scale_y - cvRound(scale_y)) < FLT_EPSILON;
498
if (area_fast)
499
{
500
scale_x = cvRound(scale_x);
501
scale_y = cvRound(scale_y);
502
}
503
}
504
505
void CV_Resize_Test::run_func()
506
{
507
cv::resize(src, dst, dst.size(), 0, 0, interpolation);
508
}
509
510
void CV_Resize_Test::run_reference_func()
511
{
512
CV_ImageWarpBaseTest::prepare_test_data_for_reference_func();
513
514
if (interpolation == INTER_AREA)
515
resize_area();
516
else
517
resize_generic();
518
}
519
520
double CV_Resize_Test::getWeight(double a, double b, int x)
521
{
522
double w = std::min(static_cast<double>(x + 1), b) - std::max(static_cast<double>(x), a);
523
CV_Assert(w >= 0);
524
return w;
525
}
526
527
void CV_Resize_Test::resize_area()
528
{
529
Size ssize = src.size(), dsize = reference_dst.size();
530
CV_Assert(!ssize.empty() && !dsize.empty());
531
int cn = src.channels();
532
533
CV_Assert(scale_x >= 1.0 && scale_y >= 1.0);
534
535
double fsy0 = 0, fsy1 = scale_y;
536
for (int dy = 0; dy < dsize.height; ++dy)
537
{
538
float* yD = reference_dst.ptr<float>(dy);
539
int isy0 = cvFloor(fsy0), isy1 = std::min(cvFloor(fsy1), ssize.height - 1);
540
CV_Assert(isy1 <= ssize.height && isy0 < ssize.height);
541
542
double fsx0 = 0, fsx1 = scale_x;
543
544
for (int dx = 0; dx < dsize.width; ++dx)
545
{
546
float* xyD = yD + cn * dx;
547
int isx0 = cvFloor(fsx0), isx1 = std::min(ssize.width - 1, cvFloor(fsx1));
548
549
CV_Assert(isx1 <= ssize.width);
550
CV_Assert(isx0 < ssize.width);
551
552
// for each pixel of dst
553
for (int r = 0; r < cn; ++r)
554
{
555
xyD[r] = 0.0f;
556
double area = 0.0;
557
for (int sy = isy0; sy <= isy1; ++sy)
558
{
559
const float* yS = src.ptr<float>(sy);
560
for (int sx = isx0; sx <= isx1; ++sx)
561
{
562
double wy = getWeight(fsy0, fsy1, sy);
563
double wx = getWeight(fsx0, fsx1, sx);
564
double w = wx * wy;
565
xyD[r] += static_cast<float>(yS[sx * cn + r] * w);
566
area += w;
567
}
568
}
569
570
CV_Assert(area != 0);
571
// norming pixel
572
xyD[r] = static_cast<float>(xyD[r] / area);
573
}
574
fsx1 = std::min((fsx0 = fsx1) + scale_x, static_cast<double>(ssize.width));
575
}
576
fsy1 = std::min((fsy0 = fsy1) + scale_y, static_cast<double>(ssize.height));
577
}
578
}
579
580
// for interpolation type : INTER_LINEAR, INTER_LINEAR_EXACT, INTER_CUBIC, INTER_LANCZOS4
581
void CV_Resize_Test::resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim)
582
{
583
Size dsize = _dst.size();
584
int cn = _dst.channels();
585
float* yD = _dst.ptr<float>(dy);
586
587
if (interpolation == INTER_NEAREST)
588
{
589
const float* yS = _src.ptr<float>(dy);
590
for (int dx = 0; dx < dsize.width; ++dx)
591
{
592
int isx = _dim[dx].first;
593
const float* xyS = yS + isx * cn;
594
float* xyD = yD + dx * cn;
595
596
for (int r = 0; r < cn; ++r)
597
xyD[r] = xyS[r];
598
}
599
}
600
else if (interpolation == INTER_LINEAR || interpolation == INTER_LINEAR_EXACT || interpolation == INTER_CUBIC || interpolation == INTER_LANCZOS4)
601
{
602
interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : interpolation == INTER_LINEAR_EXACT ? 5 : 1)];
603
size_t elemsize = _src.elemSize();
604
605
int ofs = 0, ksize = 2;
606
if (interpolation == INTER_CUBIC)
607
ofs = 1, ksize = 4;
608
else if (interpolation == INTER_LANCZOS4)
609
ofs = 3, ksize = 8;
610
611
Mat _extended_src_row(1, _src.cols + ksize * 2, _src.type());
612
const uchar* srow = _src.ptr(dy);
613
memcpy(_extended_src_row.ptr() + elemsize * ksize, srow, _src.step);
614
for (int k = 0; k < ksize; ++k)
615
{
616
memcpy(_extended_src_row.ptr() + k * elemsize, srow, elemsize);
617
memcpy(_extended_src_row.ptr() + (ksize + k) * elemsize + _src.step, srow + _src.step - elemsize, elemsize);
618
}
619
620
for (int dx = 0; dx < dsize.width; ++dx)
621
{
622
int isx = _dim[dx].first;
623
double fsx = _dim[dx].second;
624
625
float *xyD = yD + dx * cn;
626
const float* xyS = _extended_src_row.ptr<float>(0) + (isx + ksize - ofs) * cn;
627
628
float w[8];
629
inter_func(static_cast<float>(fsx), w);
630
631
for (int r = 0; r < cn; ++r)
632
{
633
xyD[r] = 0;
634
for (int k = 0; k < ksize; ++k)
635
xyD[r] += w[k] * xyS[k * cn + r];
636
}
637
}
638
}
639
else
640
CV_Assert(0);
641
}
642
643
void CV_Resize_Test::generate_buffer(double scale, dim& _dim)
644
{
645
size_t length = _dim.size();
646
for (size_t dx = 0; dx < length; ++dx)
647
{
648
double fsx = scale * (dx + 0.5) - 0.5;
649
int isx = cvFloor(fsx);
650
_dim[dx] = std::make_pair(isx, fsx - isx);
651
}
652
}
653
654
void CV_Resize_Test::resize_generic()
655
{
656
Size dsize = reference_dst.size(), ssize = src.size();
657
CV_Assert(!dsize.empty() && !ssize.empty());
658
659
dim dims[] = { dim(dsize.width), dim(dsize.height) };
660
if (interpolation == INTER_NEAREST)
661
{
662
for (int dx = 0; dx < dsize.width; ++dx)
663
dims[0][dx].first = std::min(cvFloor(dx * scale_x), ssize.width - 1);
664
for (int dy = 0; dy < dsize.height; ++dy)
665
dims[1][dy].first = std::min(cvFloor(dy * scale_y), ssize.height - 1);
666
}
667
else
668
{
669
generate_buffer(scale_x, dims[0]);
670
generate_buffer(scale_y, dims[1]);
671
}
672
673
Mat tmp(ssize.height, dsize.width, reference_dst.type());
674
for (int dy = 0; dy < tmp.rows; ++dy)
675
resize_1d(src, tmp, dy, dims[0]);
676
677
cv::Mat tmp_t(tmp.cols, tmp.rows, tmp.type());
678
cvtest::transpose(tmp, tmp_t);
679
cv::Mat reference_dst_t(reference_dst.cols, reference_dst.rows, reference_dst.type());
680
cvtest::transpose(reference_dst, reference_dst_t);
681
682
for (int dy = 0; dy < tmp_t.rows; ++dy)
683
resize_1d(tmp_t, reference_dst_t, dy, dims[1]);
684
685
cvtest::transpose(reference_dst_t, reference_dst);
686
}
687
688
////////////////////////////////////////////////////////////////////////////////////////////////////////
689
// remap
690
////////////////////////////////////////////////////////////////////////////////////////////////////////
691
692
class CV_Remap_Test :
693
public CV_ImageWarpBaseTest
694
{
695
public:
696
CV_Remap_Test();
697
698
virtual ~CV_Remap_Test();
699
700
private:
701
typedef void (CV_Remap_Test::*remap_func)(const Mat&, Mat&);
702
703
protected:
704
virtual void generate_test_data();
705
virtual void prepare_test_data_for_reference_func();
706
707
virtual void run_func();
708
virtual void run_reference_func();
709
710
Mat mapx, mapy;
711
int borderType;
712
Scalar borderValue;
713
714
remap_func funcs[2];
715
716
private:
717
void remap_nearest(const Mat&, Mat&);
718
void remap_generic(const Mat&, Mat&);
719
720
void convert_maps();
721
const char* borderType_to_string() const;
722
virtual void validate_results() const;
723
};
724
725
CV_Remap_Test::CV_Remap_Test() :
726
CV_ImageWarpBaseTest(), borderType(-1)
727
{
728
funcs[0] = &CV_Remap_Test::remap_nearest;
729
funcs[1] = &CV_Remap_Test::remap_generic;
730
}
731
732
CV_Remap_Test::~CV_Remap_Test()
733
{
734
}
735
736
void CV_Remap_Test::generate_test_data()
737
{
738
CV_ImageWarpBaseTest::generate_test_data();
739
740
RNG& rng = ts->get_rng();
741
borderType = rng.uniform(1, BORDER_WRAP);
742
borderValue = Scalar::all(rng.uniform(0, 255));
743
744
// generating the mapx, mapy matrices
745
static const int mapx_types[] = { CV_16SC2, CV_32FC1, CV_32FC2 };
746
mapx.create(dst.size(), mapx_types[rng.uniform(0, sizeof(mapx_types) / sizeof(int))]);
747
mapy.release();
748
749
const int n = std::min(std::min(src.cols, src.rows) / 10 + 1, 2);
750
float _n = 0; //static_cast<float>(-n);
751
752
switch (mapx.type())
753
{
754
case CV_16SC2:
755
{
756
MatIterator_<Vec2s> begin_x = mapx.begin<Vec2s>(), end_x = mapx.end<Vec2s>();
757
for ( ; begin_x != end_x; ++begin_x)
758
{
759
(*begin_x)[0] = static_cast<short>(rng.uniform(static_cast<int>(_n), std::max(src.cols + n - 1, 0)));
760
(*begin_x)[1] = static_cast<short>(rng.uniform(static_cast<int>(_n), std::max(src.rows + n - 1, 0)));
761
}
762
763
if (interpolation != INTER_NEAREST)
764
{
765
static const int mapy_types[] = { CV_16UC1, CV_16SC1 };
766
mapy.create(dst.size(), mapy_types[rng.uniform(0, sizeof(mapy_types) / sizeof(int))]);
767
768
switch (mapy.type())
769
{
770
case CV_16UC1:
771
{
772
MatIterator_<ushort> begin_y = mapy.begin<ushort>(), end_y = mapy.end<ushort>();
773
for ( ; begin_y != end_y; ++begin_y)
774
*begin_y = static_cast<ushort>(rng.uniform(0, 1024));
775
}
776
break;
777
778
case CV_16SC1:
779
{
780
MatIterator_<short> begin_y = mapy.begin<short>(), end_y = mapy.end<short>();
781
for ( ; begin_y != end_y; ++begin_y)
782
*begin_y = static_cast<short>(rng.uniform(0, 1024));
783
}
784
break;
785
}
786
}
787
}
788
break;
789
790
case CV_32FC1:
791
{
792
mapy.create(dst.size(), CV_32FC1);
793
float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
794
fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
795
MatIterator_<float> begin_x = mapx.begin<float>(), end_x = mapx.end<float>();
796
MatIterator_<float> begin_y = mapy.begin<float>();
797
for ( ; begin_x != end_x; ++begin_x, ++begin_y)
798
{
799
*begin_x = rng.uniform(_n, fscols);
800
*begin_y = rng.uniform(_n, fsrows);
801
}
802
}
803
break;
804
805
case CV_32FC2:
806
{
807
float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
808
fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
809
int width = mapx.cols << 1;
810
811
for (int y = 0; y < mapx.rows; ++y)
812
{
813
float * ptr = mapx.ptr<float>(y);
814
815
for (int x = 0; x < width; x += 2)
816
{
817
ptr[x] = rng.uniform(_n, fscols);
818
ptr[x + 1] = rng.uniform(_n, fsrows);
819
}
820
}
821
}
822
break;
823
824
default:
825
CV_Assert(0);
826
break;
827
}
828
}
829
830
void CV_Remap_Test::run_func()
831
{
832
remap(src, dst, mapx, mapy, interpolation, borderType, borderValue);
833
}
834
835
void CV_Remap_Test::convert_maps()
836
{
837
if (mapx.type() != CV_16SC2)
838
convertMaps(mapx.clone(), mapy.clone(), mapx, mapy, CV_16SC2, interpolation == INTER_NEAREST);
839
else if (interpolation != INTER_NEAREST)
840
if (mapy.type() != CV_16UC1)
841
mapy.clone().convertTo(mapy, CV_16UC1);
842
843
if (interpolation == INTER_NEAREST)
844
mapy = Mat();
845
CV_Assert(((interpolation == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16UC1 ||
846
mapy.type() == CV_16SC1) && mapx.type() == CV_16SC2);
847
}
848
849
const char* CV_Remap_Test::borderType_to_string() const
850
{
851
if (borderType == BORDER_CONSTANT)
852
return "BORDER_CONSTANT";
853
if (borderType == BORDER_REPLICATE)
854
return "BORDER_REPLICATE";
855
if (borderType == BORDER_REFLECT)
856
return "BORDER_REFLECT";
857
if (borderType == BORDER_WRAP)
858
return "BORDER_WRAP";
859
if (borderType == BORDER_REFLECT_101)
860
return "BORDER_REFLECT_101";
861
return "Unsupported/Unknown border type";
862
}
863
864
void CV_Remap_Test::prepare_test_data_for_reference_func()
865
{
866
CV_ImageWarpBaseTest::prepare_test_data_for_reference_func();
867
convert_maps();
868
}
869
870
void CV_Remap_Test::run_reference_func()
871
{
872
prepare_test_data_for_reference_func();
873
874
if (interpolation == INTER_AREA)
875
interpolation = INTER_LINEAR;
876
877
int index = interpolation == INTER_NEAREST ? 0 : 1;
878
(this->*funcs[index])(src, reference_dst);
879
}
880
881
void CV_Remap_Test::remap_nearest(const Mat& _src, Mat& _dst)
882
{
883
CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
884
CV_Assert(mapx.type() == CV_16SC2 && mapy.empty());
885
886
Size ssize = _src.size(), dsize = _dst.size();
887
CV_Assert(!ssize.empty() && !dsize.empty());
888
int cn = _src.channels();
889
890
for (int dy = 0; dy < dsize.height; ++dy)
891
{
892
const short* yM = mapx.ptr<short>(dy);
893
float* yD = _dst.ptr<float>(dy);
894
895
for (int dx = 0; dx < dsize.width; ++dx)
896
{
897
float* xyD = yD + cn * dx;
898
int sx = yM[dx * 2], sy = yM[dx * 2 + 1];
899
900
if (sx >= 0 && sx < ssize.width && sy >= 0 && sy < ssize.height)
901
{
902
const float *xyS = _src.ptr<float>(sy) + sx * cn;
903
904
for (int r = 0; r < cn; ++r)
905
xyD[r] = xyS[r];
906
}
907
else if (borderType != BORDER_TRANSPARENT)
908
{
909
if (borderType == BORDER_CONSTANT)
910
for (int r = 0; r < cn; ++r)
911
xyD[r] = saturate_cast<float>(borderValue[r]);
912
else
913
{
914
sx = borderInterpolate(sx, ssize.width, borderType);
915
sy = borderInterpolate(sy, ssize.height, borderType);
916
CV_Assert(sx >= 0 && sy >= 0 && sx < ssize.width && sy < ssize.height);
917
918
const float *xyS = _src.ptr<float>(sy) + sx * cn;
919
920
for (int r = 0; r < cn; ++r)
921
xyD[r] = xyS[r];
922
}
923
}
924
}
925
}
926
}
927
928
void CV_Remap_Test::remap_generic(const Mat& _src, Mat& _dst)
929
{
930
CV_Assert(mapx.type() == CV_16SC2 && mapy.type() == CV_16UC1);
931
932
int ksize = 2;
933
if (interpolation == INTER_CUBIC)
934
ksize = 4;
935
else if (interpolation == INTER_LANCZOS4)
936
ksize = 8;
937
else if (interpolation != INTER_LINEAR)
938
assert(0);
939
int ofs = (ksize / 2) - 1;
940
941
CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
942
Size ssize = _src.size(), dsize = _dst.size();
943
int cn = _src.channels(), width1 = std::max(ssize.width - ksize + 1, 0),
944
height1 = std::max(ssize.height - ksize + 1, 0);
945
946
float ix[8], w[16];
947
interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : 1)];
948
949
for (int dy = 0; dy < dsize.height; ++dy)
950
{
951
const short* yMx = mapx.ptr<short>(dy);
952
const ushort* yMy = mapy.ptr<ushort>(dy);
953
954
float* yD = _dst.ptr<float>(dy);
955
956
for (int dx = 0; dx < dsize.width; ++dx)
957
{
958
float* xyD = yD + dx * cn;
959
float sx = yMx[dx * 2], sy = yMx[dx * 2 + 1];
960
int isx = cvFloor(sx), isy = cvFloor(sy);
961
962
inter_func((yMy[dx] & (INTER_TAB_SIZE - 1)) / static_cast<float>(INTER_TAB_SIZE), w);
963
inter_func(((yMy[dx] >> INTER_BITS) & (INTER_TAB_SIZE - 1)) / static_cast<float>(INTER_TAB_SIZE), w + ksize);
964
965
isx -= ofs;
966
isy -= ofs;
967
968
if (isx >= 0 && isx < width1 && isy >= 0 && isy < height1)
969
{
970
for (int r = 0; r < cn; ++r)
971
{
972
for (int y = 0; y < ksize; ++y)
973
{
974
const float* xyS = _src.ptr<float>(isy + y) + isx * cn;
975
976
ix[y] = 0;
977
for (int i = 0; i < ksize; ++i)
978
ix[y] += w[i] * xyS[i * cn + r];
979
}
980
xyD[r] = 0;
981
for (int i = 0; i < ksize; ++i)
982
xyD[r] += w[ksize + i] * ix[i];
983
}
984
}
985
else if (borderType != BORDER_TRANSPARENT)
986
{
987
int ar_x[8], ar_y[8];
988
989
for (int k = 0; k < ksize; k++)
990
{
991
ar_x[k] = borderInterpolate(isx + k, ssize.width, borderType) * cn;
992
ar_y[k] = borderInterpolate(isy + k, ssize.height, borderType);
993
}
994
995
for (int r = 0; r < cn; r++)
996
{
997
xyD[r] = 0;
998
for (int i = 0; i < ksize; ++i)
999
{
1000
ix[i] = 0;
1001
if (ar_y[i] >= 0)
1002
{
1003
const float* yS = _src.ptr<float>(ar_y[i]);
1004
for (int j = 0; j < ksize; ++j)
1005
ix[i] += saturate_cast<float>((ar_x[j] >= 0 ? yS[ar_x[j] + r] : borderValue[r]) * w[j]);
1006
}
1007
else
1008
for (int j = 0; j < ksize; ++j)
1009
ix[i] += saturate_cast<float>(borderValue[r] * w[j]);
1010
}
1011
for (int i = 0; i < ksize; ++i)
1012
xyD[r] += saturate_cast<float>(w[ksize + i] * ix[i]);
1013
}
1014
}
1015
}
1016
}
1017
}
1018
1019
void CV_Remap_Test::validate_results() const
1020
{
1021
CV_ImageWarpBaseTest::validate_results();
1022
if (cvtest::TS::ptr()->get_err_code() == cvtest::TS::FAIL_BAD_ACCURACY)
1023
{
1024
PRINT_TO_LOG("BorderType: %s\n", borderType_to_string());
1025
PRINT_TO_LOG("BorderValue: (%f, %f, %f, %f)\n",
1026
borderValue[0], borderValue[1], borderValue[2], borderValue[3]);
1027
}
1028
}
1029
1030
////////////////////////////////////////////////////////////////////////////////////////////////////////
1031
// warpAffine
1032
////////////////////////////////////////////////////////////////////////////////////////////////////////
1033
1034
class CV_WarpAffine_Test :
1035
public CV_Remap_Test
1036
{
1037
public:
1038
CV_WarpAffine_Test();
1039
1040
virtual ~CV_WarpAffine_Test();
1041
1042
protected:
1043
virtual void generate_test_data();
1044
virtual float get_success_error_level(int _interpolation, int _depth) const;
1045
1046
virtual void run_func();
1047
virtual void run_reference_func();
1048
1049
Mat M;
1050
private:
1051
void warpAffine(const Mat&, Mat&);
1052
};
1053
1054
CV_WarpAffine_Test::CV_WarpAffine_Test() :
1055
CV_Remap_Test()
1056
{
1057
}
1058
1059
CV_WarpAffine_Test::~CV_WarpAffine_Test()
1060
{
1061
}
1062
1063
void CV_WarpAffine_Test::generate_test_data()
1064
{
1065
CV_Remap_Test::generate_test_data();
1066
1067
RNG& rng = ts->get_rng();
1068
1069
// generating the M 2x3 matrix
1070
static const int depths[] = { CV_32FC1, CV_64FC1 };
1071
1072
// generating 2d matrix
1073
M = getRotationMatrix2D(Point2f(src.cols / 2.f, src.rows / 2.f),
1074
rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
1075
int depth = depths[rng.uniform(0, sizeof(depths) / sizeof(depths[0]))];
1076
if (M.depth() != depth)
1077
{
1078
Mat tmp;
1079
M.convertTo(tmp, depth);
1080
M = tmp;
1081
}
1082
1083
// warp_matrix is inverse
1084
if (rng.uniform(0., 1.) > 0)
1085
interpolation |= CV_WARP_INVERSE_MAP;
1086
}
1087
1088
void CV_WarpAffine_Test::run_func()
1089
{
1090
cv::warpAffine(src, dst, M, dst.size(), interpolation, borderType, borderValue);
1091
}
1092
1093
float CV_WarpAffine_Test::get_success_error_level(int _interpolation, int _depth) const
1094
{
1095
return _depth == CV_8U ? 0 : CV_ImageWarpBaseTest::get_success_error_level(_interpolation, _depth);
1096
}
1097
1098
void CV_WarpAffine_Test::run_reference_func()
1099
{
1100
Mat tmp = Mat::zeros(dst.size(), dst.type());
1101
warpAffine(src, tmp);
1102
tmp.convertTo(reference_dst, reference_dst.depth());
1103
}
1104
1105
void CV_WarpAffine_Test::warpAffine(const Mat& _src, Mat& _dst)
1106
{
1107
Size dsize = _dst.size();
1108
1109
CV_Assert(!_src.empty());
1110
CV_Assert(!dsize.empty());
1111
CV_Assert(_src.type() == _dst.type());
1112
1113
Mat tM;
1114
M.convertTo(tM, CV_64F);
1115
1116
int inter = interpolation & INTER_MAX;
1117
if (inter == INTER_AREA)
1118
inter = INTER_LINEAR;
1119
1120
mapx.create(dsize, CV_16SC2);
1121
if (inter != INTER_NEAREST)
1122
mapy.create(dsize, CV_16SC1);
1123
else
1124
mapy = Mat();
1125
1126
if (!(interpolation & CV_WARP_INVERSE_MAP))
1127
invertAffineTransform(tM.clone(), tM);
1128
1129
const int AB_BITS = MAX(10, (int)INTER_BITS);
1130
const int AB_SCALE = 1 << AB_BITS;
1131
int round_delta = (inter == INTER_NEAREST) ? AB_SCALE / 2 : (AB_SCALE / INTER_TAB_SIZE / 2);
1132
1133
const softdouble* data_tM = tM.ptr<softdouble>(0);
1134
for (int dy = 0; dy < dsize.height; ++dy)
1135
{
1136
short* yM = mapx.ptr<short>(dy);
1137
for (int dx = 0; dx < dsize.width; ++dx, yM += 2)
1138
{
1139
int v1 = saturate_cast<int>(saturate_cast<int>(data_tM[0] * dx * AB_SCALE) +
1140
saturate_cast<int>((data_tM[1] * dy + data_tM[2]) * AB_SCALE) + round_delta),
1141
v2 = saturate_cast<int>(saturate_cast<int>(data_tM[3] * dx * AB_SCALE) +
1142
saturate_cast<int>((data_tM[4] * dy + data_tM[5]) * AB_SCALE) + round_delta);
1143
v1 >>= AB_BITS - INTER_BITS;
1144
v2 >>= AB_BITS - INTER_BITS;
1145
1146
yM[0] = saturate_cast<short>(v1 >> INTER_BITS);
1147
yM[1] = saturate_cast<short>(v2 >> INTER_BITS);
1148
1149
if (inter != INTER_NEAREST)
1150
mapy.ptr<short>(dy)[dx] = ((v2 & (INTER_TAB_SIZE - 1)) * INTER_TAB_SIZE + (v1 & (INTER_TAB_SIZE - 1)));
1151
}
1152
}
1153
1154
CV_Assert(mapx.type() == CV_16SC2 && ((inter == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16SC1));
1155
cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
1156
}
1157
1158
////////////////////////////////////////////////////////////////////////////////////////////////////////
1159
// warpPerspective
1160
////////////////////////////////////////////////////////////////////////////////////////////////////////
1161
1162
class CV_WarpPerspective_Test :
1163
public CV_WarpAffine_Test
1164
{
1165
public:
1166
CV_WarpPerspective_Test();
1167
1168
virtual ~CV_WarpPerspective_Test();
1169
1170
protected:
1171
virtual void generate_test_data();
1172
virtual float get_success_error_level(int _interpolation, int _depth) const;
1173
1174
virtual void run_func();
1175
virtual void run_reference_func();
1176
1177
private:
1178
void warpPerspective(const Mat&, Mat&);
1179
};
1180
1181
CV_WarpPerspective_Test::CV_WarpPerspective_Test() :
1182
CV_WarpAffine_Test()
1183
{
1184
}
1185
1186
CV_WarpPerspective_Test::~CV_WarpPerspective_Test()
1187
{
1188
}
1189
1190
void CV_WarpPerspective_Test::generate_test_data()
1191
{
1192
CV_Remap_Test::generate_test_data();
1193
1194
// generating the M 3x3 matrix
1195
RNG& rng = ts->get_rng();
1196
1197
float cols = static_cast<float>(src.cols), rows = static_cast<float>(src.rows);
1198
Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
1199
Point2f dp[] = { Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
1200
Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
1201
Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
1202
Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)) };
1203
M = getPerspectiveTransform(sp, dp);
1204
1205
static const int depths[] = { CV_32F, CV_64F };
1206
int depth = depths[rng.uniform(0, 2)];
1207
M.clone().convertTo(M, depth);
1208
}
1209
1210
void CV_WarpPerspective_Test::run_func()
1211
{
1212
cv::warpPerspective(src, dst, M, dst.size(), interpolation, borderType, borderValue);
1213
}
1214
1215
float CV_WarpPerspective_Test::get_success_error_level(int _interpolation, int _depth) const
1216
{
1217
return CV_ImageWarpBaseTest::get_success_error_level(_interpolation, _depth);
1218
}
1219
1220
void CV_WarpPerspective_Test::run_reference_func()
1221
{
1222
Mat tmp = Mat::zeros(dst.size(), dst.type());
1223
warpPerspective(src, tmp);
1224
tmp.convertTo(reference_dst, reference_dst.depth());
1225
}
1226
1227
void CV_WarpPerspective_Test::warpPerspective(const Mat& _src, Mat& _dst)
1228
{
1229
Size ssize = _src.size(), dsize = _dst.size();
1230
1231
CV_Assert(!ssize.empty());
1232
CV_Assert(!dsize.empty());
1233
CV_Assert(_src.type() == _dst.type());
1234
1235
if (M.depth() != CV_64F)
1236
{
1237
Mat tmp;
1238
M.convertTo(tmp, CV_64F);
1239
M = tmp;
1240
}
1241
1242
if (!(interpolation & CV_WARP_INVERSE_MAP))
1243
{
1244
Mat tmp;
1245
invert(M, tmp);
1246
M = tmp;
1247
}
1248
1249
int inter = interpolation & INTER_MAX;
1250
if (inter == INTER_AREA)
1251
inter = INTER_LINEAR;
1252
1253
mapx.create(dsize, CV_16SC2);
1254
if (inter != INTER_NEAREST)
1255
mapy.create(dsize, CV_16SC1);
1256
else
1257
mapy = Mat();
1258
1259
double* tM = M.ptr<double>(0);
1260
for (int dy = 0; dy < dsize.height; ++dy)
1261
{
1262
short* yMx = mapx.ptr<short>(dy);
1263
1264
for (int dx = 0; dx < dsize.width; ++dx, yMx += 2)
1265
{
1266
double den = tM[6] * dx + tM[7] * dy + tM[8];
1267
den = den ? 1.0 / den : 0.0;
1268
1269
if (inter == INTER_NEAREST)
1270
{
1271
yMx[0] = saturate_cast<short>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
1272
yMx[1] = saturate_cast<short>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
1273
continue;
1274
}
1275
1276
den *= INTER_TAB_SIZE;
1277
int v0 = saturate_cast<int>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
1278
int v1 = saturate_cast<int>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
1279
1280
yMx[0] = saturate_cast<short>(v0 >> INTER_BITS);
1281
yMx[1] = saturate_cast<short>(v1 >> INTER_BITS);
1282
mapy.ptr<short>(dy)[dx] = saturate_cast<short>((v1 & (INTER_TAB_SIZE - 1)) *
1283
INTER_TAB_SIZE + (v0 & (INTER_TAB_SIZE - 1)));
1284
}
1285
}
1286
1287
CV_Assert(mapx.type() == CV_16SC2 && ((inter == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16SC1));
1288
cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
1289
}
1290
1291
////////////////////////////////////////////////////////////////////////////////////////////////////////
1292
// Tests
1293
////////////////////////////////////////////////////////////////////////////////////////////////////////
1294
1295
TEST(Imgproc_Resize_Test, accuracy) { CV_Resize_Test test; test.safe_run(); }
1296
TEST(Imgproc_Remap_Test, accuracy) { CV_Remap_Test test; test.safe_run(); }
1297
TEST(Imgproc_WarpAffine_Test, accuracy) { CV_WarpAffine_Test test; test.safe_run(); }
1298
TEST(Imgproc_WarpPerspective_Test, accuracy) { CV_WarpPerspective_Test test; test.safe_run(); }
1299
1300
////////////////////////////////////////////////////////////////////////////////////////////////////////
1301
1302
#ifdef OPENCV_TEST_BIGDATA
1303
1304
CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_LINEAR_EXACT, INTER_CUBIC, INTER_AREA)
1305
1306
class Imgproc_Resize :
1307
public ::testing::TestWithParam<Interpolation>
1308
{
1309
public:
1310
virtual void SetUp()
1311
{
1312
inter = GetParam();
1313
}
1314
1315
protected:
1316
int inter;
1317
};
1318
1319
TEST_P(Imgproc_Resize, BigSize)
1320
{
1321
cv::Mat src(46342, 46342, CV_8UC3, cv::Scalar::all(10)), dst;
1322
ASSERT_FALSE(src.empty());
1323
1324
ASSERT_NO_THROW(cv::resize(src, dst, cv::Size(), 0.5, 0.5, inter));
1325
}
1326
1327
INSTANTIATE_TEST_CASE_P(Imgproc, Imgproc_Resize, Interpolation::all());
1328
1329
#endif
1330
1331
}} // namespace
1332
1333