1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "test_precomp.hpp"
51 void __wrap_printf_func(const char* fmt, ...)
56 vsprintf (buffer, fmt, args);
57 cvtest::TS::ptr()->printf(cvtest::TS::SUMMARY, buffer);
61 #define PRINT_TO_LOG __wrap_printf_func
65 ////////////////////////////////////////////////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////////////////////////////////////////////////
69 class CV_ImageWarpBaseTest :
70 public cvtest::BaseTest
78 CV_ImageWarpBaseTest();
80 virtual void run(int);
82 virtual ~CV_ImageWarpBaseTest();
85 virtual void generate_test_data();
87 virtual void run_func() = 0;
88 virtual void run_reference_func() = 0;
89 virtual void validate_results() const = 0;
91 Size randSize(RNG& rng) const;
93 const char* interpolation_to_string(int inter_type) const;
100 CV_ImageWarpBaseTest::CV_ImageWarpBaseTest() :
101 BaseTest(), interpolation(-1),
104 test_case_count = 40;
105 ts->set_failed_test_info(cvtest::TS::OK);
108 CV_ImageWarpBaseTest::~CV_ImageWarpBaseTest()
112 const char* CV_ImageWarpBaseTest::interpolation_to_string(int inter) const
114 if (inter == INTER_NEAREST)
115 return "INTER_NEAREST";
116 if (inter == INTER_LINEAR)
117 return "INTER_LINEAR";
118 if (inter == INTER_AREA)
120 if (inter == INTER_CUBIC)
121 return "INTER_CUBIC";
122 if (inter == INTER_LANCZOS4)
123 return "INTER_LANCZOS4";
124 if (inter == INTER_LANCZOS4 + 1)
125 return "INTER_AREA_FAST";
126 return "Unsupported/Unkown interpolation type";
129 void interpolateLinear(float x, float* coeffs)
135 void interpolateCubic(float x, float* coeffs)
137 const float A = -0.75f;
139 coeffs[0] = ((A*(x + 1) - 5*A)*(x + 1) + 8*A)*(x + 1) - 4*A;
140 coeffs[1] = ((A + 2)*x - (A + 3))*x*x + 1;
141 coeffs[2] = ((A + 2)*(1 - x) - (A + 3))*(1 - x)*(1 - x) + 1;
142 coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2];
145 void interpolateLanczos4(float x, float* coeffs)
147 static const double s45 = 0.70710678118654752440084436210485;
148 static const double cs[][2]=
149 {{1, 0}, {-s45, -s45}, {0, 1}, {s45, -s45}, {-1, 0}, {s45, s45}, {0, -1}, {-s45, s45}};
151 if( x < FLT_EPSILON )
153 for( int i = 0; i < 8; i++ )
160 double y0=-(x+3)*CV_PI*0.25, s0 = sin(y0), c0=cos(y0);
161 for(int i = 0; i < 8; i++ )
163 double y = -(x+3-i)*CV_PI*0.25;
164 coeffs[i] = (float)((cs[i][0]*s0 + cs[i][1]*c0)/(y*y));
169 for(int i = 0; i < 8; i++ )
173 typedef void (*interpolate_method)(float x, float* coeffs);
174 interpolate_method inter_array[] = { &interpolateLinear, &interpolateCubic, &interpolateLanczos4 };
176 Size CV_ImageWarpBaseTest::randSize(RNG& rng) const
179 size.width = saturate_cast<int>(std::exp(rng.uniform(0.0, 7.0)));
180 size.height = saturate_cast<int>(std::exp(rng.uniform(0.0, 7.0)));
185 void CV_ImageWarpBaseTest::generate_test_data()
187 RNG& rng = ts->get_rng();
189 Size ssize = randSize(rng);
192 while (depth == CV_8S || depth == CV_32S)
193 depth = rng.uniform(0, CV_64F);
195 int cn = rng.uniform(1, 4);
197 cn = rng.uniform(1, 4);
198 interpolation = rng.uniform(0, CV_INTER_LANCZOS4 + 1);
199 interpolation = INTER_NEAREST;
201 src.create(ssize, CV_MAKE_TYPE(depth, cn));
203 // generating the src matrix
205 if (cvtest::randInt(rng) % 2)
207 for (y = 0; y < ssize.height; y += cell_size)
208 for (x = 0; x < ssize.width; x += cell_size)
209 rectangle(src, Point(x, y), Point(x + std::min<int>(cell_size, ssize.width - x), y +
210 std::min<int>(cell_size, ssize.height - y)), Scalar::all((x + y) % 2 ? 255: 0), CV_FILLED);
214 src = Scalar::all(255);
215 for (y = cell_size; y < src.rows; y += cell_size)
216 line(src, Point2i(0, y), Point2i(src.cols, y), Scalar::all(0), 1);
217 for (x = cell_size; x < src.cols; x += cell_size)
218 line(src, Point2i(x, 0), Point2i(x, src.rows), Scalar::all(0), 1);
222 void CV_ImageWarpBaseTest::run(int)
224 for (int i = 0; i < test_case_count; ++i)
226 generate_test_data();
228 run_reference_func();
229 if (ts->get_err_code() < 0)
232 if (ts->get_err_code() < 0)
234 ts->update_context(this, i, true);
236 ts->set_gtest_status();
239 ////////////////////////////////////////////////////////////////////////////////////////////////////////
241 ////////////////////////////////////////////////////////////////////////////////////////////////////////
243 class CV_Resize_Test :
244 public CV_ImageWarpBaseTest
248 virtual ~CV_Resize_Test();
251 virtual void generate_test_data();
253 virtual void run_func();
254 virtual void run_reference_func();
255 virtual void validate_results() const;
263 void resize_generic();
265 double getWeight(double a, double b, int x);
267 typedef std::vector<std::pair<int, double> > dim;
268 void generate_buffer(double scale, dim& _dim);
269 void resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim);
272 CV_Resize_Test::CV_Resize_Test() :
273 CV_ImageWarpBaseTest(), scale_x(), scale_y(),
274 area_fast(false), reference_dst()
278 CV_Resize_Test::~CV_Resize_Test()
282 void CV_Resize_Test::generate_test_data()
284 CV_ImageWarpBaseTest::generate_test_data();
285 RNG& rng = ts->get_rng();
286 Size dsize, ssize = src.size();
288 if (interpolation == INTER_AREA)
290 area_fast = rng.uniform(0., 1.) > 0.5;
293 scale_x = rng.uniform(2, 5);
294 scale_y = rng.uniform(2, 5);
298 scale_x = rng.uniform(1.0, 3.0);
299 scale_y = rng.uniform(1.0, 3.0);
304 scale_x = rng.uniform(0.4, 4.0);
305 scale_y = rng.uniform(0.4, 4.0);
307 dsize.width = saturate_cast<int>((ssize.width + scale_x - 1) / scale_x);
308 dsize.height = saturate_cast<int>((ssize.height + scale_y - 1) / scale_y);
310 dst = Mat::zeros(dsize, src.type());
311 reference_dst = Mat::zeros(dst.size(), CV_MAKE_TYPE(CV_32F, dst.channels()));
313 scale_x = src.cols / static_cast<double>(dst.cols);
314 scale_y = src.rows / static_cast<double>(dst.rows);
316 if (interpolation == INTER_AREA && (scale_x < 1.0 || scale_y < 1.0))
317 interpolation = INTER_LINEAR;
319 area_fast = interpolation == INTER_AREA &&
320 fabs(scale_x - cvRound(scale_x)) < FLT_EPSILON &&
321 fabs(scale_y - cvRound(scale_y)) < FLT_EPSILON;
324 scale_x = cvRound(scale_x);
325 scale_y = cvRound(scale_y);
329 void CV_Resize_Test::run_func()
331 cv::resize(src, dst, dst.size(), 0, 0, interpolation);
334 void CV_Resize_Test::run_reference_func()
336 if (src.depth() != CV_32F)
339 src.convertTo(tmp, CV_32F);
342 if (interpolation == INTER_AREA)
348 double CV_Resize_Test::getWeight(double a, double b, int x)
350 float w = std::min(x + 1., b) - std::max(x + 0., a);
355 void CV_Resize_Test::resize_area()
357 Size ssize = src.size(), dsize = reference_dst.size();
358 CV_Assert(ssize.area() > 0 && dsize.area() > 0);
359 int cn = src.channels();
361 CV_Assert(scale_x >= 1.0 && scale_y >= 1.0);
363 double fsy0 = 0, fsy1 = scale_y;
364 for (int dy = 0; dy < dsize.height; ++dy)
366 float* yD = reference_dst.ptr<float>(dy);
367 int isy0 = cvFloor(fsy0), isy1 = std::min(cvFloor(fsy1), ssize.height - 1);
368 CV_Assert(isy1 <= ssize.height && isy0 < ssize.height);
370 float fsx0 = 0, fsx1 = scale_x;
372 for (int dx = 0; dx < dsize.width; ++dx)
374 float* xyD = yD + cn * dx;
375 int isx0 = cvFloor(fsx0), isx1 = std::min(ssize.width - 1, cvFloor(fsx1));
377 CV_Assert(isx1 <= ssize.width);
378 CV_Assert(isx0 < ssize.width);
380 // for each pixel of dst
381 for (int r = 0; r < cn; ++r)
385 for (int sy = isy0; sy <= isy1; ++sy)
387 const float* yS = src.ptr<float>(sy);
388 for (int sx = isx0; sx <= isx1; ++sx)
390 double wy = getWeight(fsy0, fsy1, sy);
391 double wx = getWeight(fsx0, fsx1, sx);
393 xyD[r] += yS[sx * cn + r] * w;
398 CV_Assert(area != 0);
402 fsx1 = std::min<double>((fsx0 = fsx1) + scale_x, ssize.width);
404 fsy1 = std::min<double>((fsy0 = fsy1) + scale_y, ssize.height);
408 // for interpolation type : INTER_LINEAR, INTER_LINEAR, INTER_CUBIC, INTER_LANCZOS4
409 void CV_Resize_Test::resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim)
411 Size dsize = _dst.size();
412 int cn = _dst.channels();
413 float* yD = _dst.ptr<float>(dy);
415 if (interpolation == INTER_NEAREST)
417 const float* yS = _src.ptr<float>(dy);
418 for (int dx = 0; dx < dsize.width; ++dx)
420 int isx = _dim[dx].first;
421 const float* xyS = yS + isx * cn;
422 float* xyD = yD + dx * cn;
424 for (int r = 0; r < cn; ++r)
428 else if (interpolation == INTER_LINEAR || interpolation == INTER_CUBIC || interpolation == INTER_LANCZOS4)
430 interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : 1)];
431 int elemsize = _src.elemSize();
433 int ofs = 0, ksize = 2;
434 if (interpolation == INTER_CUBIC)
436 else if (interpolation == INTER_LANCZOS4)
438 cv::AutoBuffer<float> _w(ksize);
441 Mat _extended_src_row(1, _src.cols + ksize * 2, _src.type());
442 uchar* srow = _src.data + dy * _src.step;
443 memcpy(_extended_src_row.data + elemsize * ksize, srow, _src.step);
444 for (int k = 0; k < ksize; ++k)
446 memcpy(_extended_src_row.data + k * elemsize, srow, elemsize);
447 memcpy(_extended_src_row.data + (ksize + k) * elemsize + _src.step, srow + _src.step - elemsize, elemsize);
450 for (int dx = 0; dx < dsize.width; ++dx)
452 int isx = _dim[dx].first;
453 double fsx = _dim[dx].second;
455 float *xyD = yD + dx * cn;
456 const float* xyS = _extended_src_row.ptr<float>(0) + (isx + ksize - ofs) * cn;
460 for (int r = 0; r < cn; ++r)
463 for (int k = 0; k < ksize; ++k)
464 xyD[r] += w[k] * xyS[k * cn + r];
473 void CV_Resize_Test::generate_buffer(double scale, dim& _dim)
475 int length = _dim.size();
476 for (int dx = 0; dx < length; ++dx)
478 double fsx = scale * (dx + 0.5f) - 0.5f;
479 int isx = cvFloor(fsx);
480 _dim[dx] = std::make_pair(isx, fsx - isx);
484 void CV_Resize_Test::resize_generic()
486 Size dsize = reference_dst.size(), ssize = src.size();
487 CV_Assert(dsize.area() > 0 && ssize.area() > 0);
489 dim dims[] = { dim(dsize.width), dim(dsize.height) };
490 if (interpolation == INTER_NEAREST)
492 for (int dx = 0; dx < dsize.width; ++dx)
493 dims[0][dx].first = std::min(cvFloor(dx * scale_x), ssize.width - 1);
494 for (int dy = 0; dy < dsize.height; ++dy)
495 dims[1][dy].first = std::min(cvFloor(dy * scale_y), ssize.height - 1);
499 generate_buffer(scale_x, dims[0]);
500 generate_buffer(scale_y, dims[1]);
503 Mat tmp(ssize.height, dsize.width, reference_dst.type());
504 for (int dy = 0; dy < tmp.rows; ++dy)
505 resize_1d(src, tmp, dy, dims[0]);
508 transpose(reference_dst, reference_dst);
510 for (int dy = 0; dy < tmp.rows; ++dy)
511 resize_1d(tmp, reference_dst, dy, dims[1]);
512 transpose(reference_dst, reference_dst);
515 void CV_Resize_Test::validate_results() const
518 if (dst.depth() != reference_dst.depth())
521 dst.convertTo(tmp, reference_dst.depth());
525 Size dsize = dst.size();
526 int cn = _dst.channels();
529 if (interpolation == INTER_CUBIC)
531 else if (interpolation == INTER_LANCZOS4)
533 else if (interpolation == INTER_NEAREST)
535 else if (interpolation == INTER_AREA)
538 for (int dy = 0; dy < dsize.height; ++dy)
540 const float* rD = reference_dst.ptr<float>(dy);
541 const float* D = _dst.ptr<float>(dy);
543 for (int dx = 0; dx < dsize.width; ++dx)
544 if (fabs(rD[dx] - D[dx]) > t)
546 PRINT_TO_LOG("\nNorm of the difference: %lf\n", norm(reference_dst, _dst, NORM_INF));
547 PRINT_TO_LOG("Error in (dx, dy): (%d, %d)\n", dx / cn + 1, dy + 1);
548 PRINT_TO_LOG("Tuple (rD, D): (%f, %f)\n", rD[dx], D[dx]);
549 PRINT_TO_LOG("Dsize: (%d, %d)\n", dsize.width / cn, dsize.height);
550 PRINT_TO_LOG("Ssize: (%d, %d)\n", src.cols, src.rows);
551 PRINT_TO_LOG("Interpolation: %s\n",
552 interpolation_to_string(area_fast ? INTER_LANCZOS4 + 1 : interpolation));
553 PRINT_TO_LOG("Scale (x, y): (%lf, %lf)\n", scale_x, scale_y);
554 PRINT_TO_LOG("Elemsize: %d\n", src.elemSize());
555 PRINT_TO_LOG("Channels: %d\n", cn);
558 const std::string w1("Resize (run func)"), w2("Reference Resize"), w3("Src image"), w4("Diff");
559 namedWindow(w1, CV_WINDOW_KEEPRATIO);
560 namedWindow(w2, CV_WINDOW_KEEPRATIO);
561 namedWindow(w3, CV_WINDOW_KEEPRATIO);
562 namedWindow(w4, CV_WINDOW_KEEPRATIO);
565 absdiff(reference_dst, _dst, diff);
568 imshow(w2, reference_dst);
575 const int radius = 3;
576 int rmin = MAX(dy - radius, 0), rmax = MIN(dy + radius, dsize.height);
577 int cmin = MAX(dx - radius, 0), cmax = MIN(dx + radius, dsize.width);
579 cout << "opencv result:\n" << dst(Range(rmin, rmax), Range(cmin, cmax)) << endl;
580 cout << "reference result:\n" << reference_dst(Range(rmin, rmax), Range(cmin, cmax)) << endl;
583 ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
589 ////////////////////////////////////////////////////////////////////////////////////////////////////////
591 ////////////////////////////////////////////////////////////////////////////////////////////////////////
593 class CV_Remap_Test :
594 public CV_ImageWarpBaseTest
599 virtual ~CV_Remap_Test();
602 typedef void (CV_Remap_Test::*remap_func)(const Mat&, Mat&);
605 virtual void generate_test_data();
606 virtual void prepare_test_data_for_reference_func();
608 virtual void run_func();
609 virtual void run_reference_func();
610 virtual void validate_results() const;
624 void remap_nearest(const Mat&, Mat&);
625 void remap_generic(const Mat&, Mat&);
628 const char* borderType_to_string() const;
631 CV_Remap_Test::CV_Remap_Test() :
632 CV_ImageWarpBaseTest(), mapx(), mapy(),
633 borderType(), borderValue(), dilate_src(), erode_src(),
634 dilate_dst(), erode_dst()
636 funcs[0] = &CV_Remap_Test::remap_nearest;
637 funcs[1] = &CV_Remap_Test::remap_generic;
640 CV_Remap_Test::~CV_Remap_Test()
644 const char* CV_Remap_Test::borderType_to_string() const
646 if (borderType == BORDER_CONSTANT)
647 return "BORDER_CONSTANT";
648 if (borderType == BORDER_REPLICATE)
649 return "BORDER_REPLICATE";
650 if (borderType == BORDER_REFLECT)
651 return "BORDER_REFLECT";
652 return "Unsupported/Unkown border type";
655 void CV_Remap_Test::generate_test_data()
657 CV_ImageWarpBaseTest::generate_test_data();
659 RNG& rng = ts->get_rng();
660 borderType = rng.uniform(1, BORDER_WRAP);
661 borderValue = Scalar::all(rng.uniform(0, 255));
663 Size dsize = randSize(rng);
664 dst.create(dsize, src.type());
666 // generating the mapx, mapy matrix
667 static const int mapx_types[] = { CV_16SC2, CV_32FC1, CV_32FC2 };
668 mapx.create(dst.size(), mapx_types[rng.uniform(0, sizeof(mapx_types) / sizeof(int))]);
671 const int n = std::min(std::min(src.cols, src.rows) / 10 + 1, 2);
672 float _n = 0; //static_cast<float>(-n);
675 // mapx.create(dst.size(), CV_32FC2);
676 // for (int y = 0; y < dsize.height; ++y)
678 // float* yM = mapx.ptr<float>(y);
679 // for (int x = 0; x < dsize.width; ++x)
681 // float* xyM = yM + (x << 1);
692 MatIterator_<Vec2s> begin_x = mapx.begin<Vec2s>(), end_x = mapx.end<Vec2s>();
693 for ( ; begin_x != end_x; ++begin_x)
695 begin_x[0] = rng.uniform(static_cast<int>(_n), std::max(src.cols + n - 1, 0));
696 begin_x[1] = rng.uniform(static_cast<int>(_n), std::max(src.rows + n - 1, 0));
699 if (interpolation != INTER_NEAREST)
701 static const int mapy_types[] = { CV_16UC1, CV_16SC1 };
702 mapy.create(dst.size(), mapy_types[rng.uniform(0, sizeof(mapy_types) / sizeof(int))]);
708 MatIterator_<ushort> begin_y = mapy.begin<ushort>(), end_y = mapy.end<ushort>();
709 for ( ; begin_y != end_y; ++begin_y)
710 begin_y[0] = (ushort)rng.uniform(0, 1024);
716 MatIterator_<short> begin_y = mapy.begin<short>(), end_y = mapy.end<short>();
717 for ( ; begin_y != end_y; ++begin_y)
718 begin_y[0] = (short)rng.uniform(0, 1024);
728 mapy.create(dst.size(), CV_32FC1);
729 float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
730 fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
731 MatIterator_<float> begin_x = mapx.begin<float>(), end_x = mapx.end<float>();
732 MatIterator_<float> begin_y = mapy.begin<float>();
733 for ( ; begin_x != end_x; ++begin_x, ++begin_y)
735 begin_x[0] = rng.uniform(_n, fscols);
736 begin_y[0] = rng.uniform(_n, fsrows);
743 MatIterator_<Vec2f> begin_x = mapx.begin<Vec2f>(), end_x = mapx.end<Vec2f>();
744 float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
745 fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
746 for ( ; begin_x != end_x; ++begin_x)
748 begin_x[0] = rng.uniform(_n, fscols);
749 begin_x[1] = rng.uniform(_n, fsrows);
756 void CV_Remap_Test::run_func()
758 remap(src, dst, mapx, mapy, interpolation, borderType, borderValue);
761 void CV_Remap_Test::convert_maps()
763 if (mapx.type() == mapy.type() && mapx.type() == CV_32FC1)
765 Mat maps[] = { mapx, mapy };
767 merge(maps, sizeof(maps) / sizeof(Mat), dst_map);
770 else if (interpolation == INTER_NEAREST && mapx.type() == CV_16SC2)
773 mapx.convertTo(tmp_mapx, CV_32F);
778 else if (mapx.type() != CV_32FC2)
781 convertMaps(mapx.clone(), mapy, mapx, out_mapy, CV_32FC2, interpolation == INTER_NEAREST);
787 void CV_Remap_Test::prepare_test_data_for_reference_func()
791 if (src.depth() != CV_32F)
794 src.convertTo(_src, CV_32F);
800 Mat kernel = getStructuringElement(CV_MOP_ERODE, Size(ksize, ksize));
801 Mat mask(src.size(), CV_8UC1, Scalar::all(255)), dst_mask;
802 cv::erode(src, erode_src, kernel);
803 cv::erode(mask, dst_mask, kernel, Point(-1, -1), 1, BORDER_CONSTANT, Scalar::all(0));
804 bitwise_not(dst_mask, mask);
805 src.copyTo(erode_src, mask);
808 mask = Scalar::all(0);
809 kernel = getStructuringElement(CV_MOP_DILATE, kernel.size());
810 cv::dilate(src, dilate_src, kernel);
811 cv::dilate(mask, dst_mask, kernel, Point(-1, -1), 1, BORDER_CONSTANT, Scalar::all(255));
812 src.copyTo(dilate_src, dst_mask);
819 dilate_dst = Mat::zeros(dst.size(), dilate_src.type());
820 erode_dst = Mat::zeros(dst.size(), erode_src.type());
823 void CV_Remap_Test::run_reference_func()
825 prepare_test_data_for_reference_func();
827 if (interpolation == INTER_AREA)
828 interpolation = INTER_LINEAR;
829 CV_Assert(interpolation != INTER_AREA);
831 int index = interpolation == INTER_NEAREST ? 0 : 1;
832 (this->*funcs[index])(erode_src, erode_dst);
833 (this->*funcs[index])(dilate_src, dilate_dst);
836 void CV_Remap_Test::remap_nearest(const Mat& _src, Mat& _dst)
838 CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
839 CV_Assert(mapx.type() == CV_32FC2);
841 Size ssize = _src.size(), dsize = _dst.size();
842 CV_Assert(ssize.area() > 0 && dsize.area() > 0);
843 int cn = _src.channels();
845 for (int dy = 0; dy < dsize.height; ++dy)
847 const float* yM = mapx.ptr<float>(dy);
848 float* yD = _dst.ptr<float>(dy);
850 for (int dx = 0; dx < dsize.width; ++dx)
852 float* xyD = yD + cn * dx;
853 int sx = cvRound(yM[dx * 2]), sy = cvRound(yM[dx * 2 + 1]);
855 if (sx >= 0 && sx < ssize.width && sy >= 0 && sy < ssize.height)
857 const float *S = _src.ptr<float>(sy) + sx * cn;
859 for (int r = 0; r < cn; ++r)
862 else if (borderType != BORDER_TRANSPARENT)
864 if (borderType == BORDER_CONSTANT)
865 for (int r = 0; r < cn; ++r)
866 xyD[r] = (float)borderValue[r];
869 sx = borderInterpolate(sx, ssize.width, borderType);
870 sy = borderInterpolate(sy, ssize.height, borderType);
871 CV_Assert(sx >= 0 && sy >= 0 && sx < ssize.width && sy < ssize.height);
873 const float *S = _src.ptr<float>(sy) + sx * cn;
875 for (int r = 0; r < cn; ++r)
883 void CV_Remap_Test::remap_generic(const Mat& _src, Mat& _dst)
886 if (interpolation == INTER_CUBIC)
888 else if (interpolation == INTER_LANCZOS4)
890 int ofs = (ksize / 2) - 1;
892 CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
893 CV_Assert(mapx.type() == CV_32FC2);
895 Size ssize = _src.size(), dsize = _dst.size();
896 int cn = _src.channels(), width1 = std::max(ssize.width - ksize / 2, 0), height1 = std::max(ssize.height - ksize / 2, 0);
899 interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : 1)];
901 for (int dy = 0; dy < dsize.height; ++dy)
903 const float* yM = mapx.ptr<float>(dy);
904 float* yD = _dst.ptr<float>(dy);
906 for (int dx = 0; dx < dsize.width; ++dx)
908 float* xyD = yD + dx * cn;
909 float sx = yM[dx * 2], sy = yM[dx * 2 + 1];
910 int isx = cvFloor(sx), isy = cvFloor(sy);
912 float fsx = sx - isx, fsy = sy - isy;
914 inter_func(fsy, w + ksize);
916 if (isx >= ofs && isx < width1 && isy >= ofs && isy < height1)
918 for (int r = 0; r < cn; ++r)
920 for (int y = 0; y < ksize; ++y)
922 const float* xyS = _src.ptr<float>(isy + y - ofs) + isx * cn;
925 for (int i = 0; i < ksize; ++i)
926 ix[y] += w[i] * xyS[i * cn + r];
930 for (int i = 0; i < ksize; ++i)
931 xyD[r] += w[ksize + i] * ix[i];
934 else if (borderType != BORDER_TRANSPARENT)
936 if (borderType == BORDER_CONSTANT &&
937 (isx >= ssize.width || isx + ksize <= 0 ||
938 isy >= ssize.height || isy + ksize <= 0))
939 for (int r = 0; r < cn; ++r)
940 xyD[r] = (float)borderValue[r];
943 int ar_x[8], ar_y[8];
945 for(int k = 0; k < ksize; k++ )
947 ar_x[k] = borderInterpolate(isx + k - ofs, ssize.width, borderType) * cn;
948 ar_y[k] = borderInterpolate(isy + k - ofs, ssize.height, borderType);
950 CV_Assert(ar_x[k] < ssize.width * cn && ar_y[k] < ssize.height);
953 for (int r = 0; r < cn; r++)
955 // if (interpolation == INTER_LINEAR)
958 for (int i = 0; i < ksize; ++i)
963 const float* yS = _src.ptr<float>(ar_y[i]);
964 for (int j = 0; j < ksize; ++j)
967 CV_Assert(ar_x[j] < ssize.width * cn);
968 ix[i] += yS[ar_x[j] + r] * w[j];
971 ix[i] += borderValue[r] * w[j];
974 for (int j = 0; j < ksize; ++j)
975 ix[i] += borderValue[r] * w[j];
977 for (int i = 0; i < ksize; ++i)
978 xyD[r] += w[ksize + i] * ix[i];
983 // if (src.elemSize() == 4)
986 // float cv = borderValue[r], sum = cv * ONE;
987 // for (int i = 0; i < ksize; ++i)
992 // const float* S1 = _src.ptr<float>(ar_y[i]);
993 // for (int j = 0; j < ksize; ++j)
994 // if( ar_x[j] >= 0 )
995 // sum += (S1[ar_x[j] + r] - cv) * w[j];
1006 void CV_Remap_Test::validate_results() const
1009 dst.convertTo(_dst, CV_32F);
1011 Size dsize = _dst.size(), ssize = src.size();
1012 dsize.width *= _dst.channels();
1014 CV_Assert(_dst.size() == erode_dst.size() && dilate_dst.size() == _dst.size());
1015 CV_Assert(dilate_dst.type() == _dst.type() && _dst.type() == erode_dst.type());
1017 for (int y = 0; y < dsize.height; ++y)
1019 const float* D = _dst.ptr<float>(y);
1020 const float* dD = dilate_dst.ptr<float>(y);
1021 const float* eD = erode_dst.ptr<float>(y);
1025 for (int x = 0; x < dsize.width; ++x)
1026 if ( !(eD[x] - t <= D[x] && D[x] <= dD[x] + t) )
1028 PRINT_TO_LOG("\nnorm(erode_dst, dst): %lf\n", norm(erode_dst, _dst, NORM_INF));
1029 PRINT_TO_LOG("norm(dst, dilate_dst): %lf\n", norm(_dst, dilate_dst, NORM_INF));
1030 PRINT_TO_LOG("(dx, dy): (%d, %d)\n", x / _dst.channels() + 1, 1 + y);
1031 PRINT_TO_LOG("Values = (%f, %f, %f)\n", eD[x], D[x], dD[x]);
1032 PRINT_TO_LOG("Interpolation: %s\n",
1033 interpolation_to_string((interpolation | CV_WARP_INVERSE_MAP) ^ CV_WARP_INVERSE_MAP));
1034 PRINT_TO_LOG("Ssize: (%d, %d)\n", ssize.width, ssize.height);
1035 PRINT_TO_LOG("Dsize: (%d, %d)\n", _dst.cols, dsize.height);
1036 PRINT_TO_LOG("BorderType: %s\n", borderType_to_string());
1037 PRINT_TO_LOG("BorderValue: (%f, %f, %f, %f)\n",
1038 borderValue[0], borderValue[1], borderValue[2], borderValue[3]);
1042 std::string w1("Dst"), w2("Erode dst"), w3("Dilate dst"), w4("Diff erode"), w5("Diff dilate");
1044 cv::namedWindow(w1, CV_WINDOW_AUTOSIZE);
1045 cv::namedWindow(w2, CV_WINDOW_AUTOSIZE);
1046 cv::namedWindow(w3, CV_WINDOW_AUTOSIZE);
1047 cv::namedWindow(w4, CV_WINDOW_AUTOSIZE);
1048 cv::namedWindow(w5, CV_WINDOW_AUTOSIZE);
1050 Mat diff_dilate, diff_erode;
1051 absdiff(_dst, erode_dst, diff_erode);
1052 absdiff(_dst, dilate_dst, diff_dilate);
1054 cv::imshow(w1, dst / 255.);
1055 cv::imshow(w2, erode_dst / 255.);
1056 cv::imshow(w3, dilate_dst / 255.);
1057 cv::imshow(w4, erode_dst / 255.);
1058 cv::imshow(w5, dilate_dst / 255.);
1065 const int radius = 3;
1066 int rmin = MAX(y - radius, 0), rmax = MIN(y + radius, dsize.height);
1067 int cmin = MAX(x - radius, 0), cmax = MIN(x + radius, dsize.width);
1069 cout << "src:\n" << src(Range(rmin, rmax), Range(cmin, cmax)) << endl;
1070 cout << "opencv result:\n" << dst(Range(rmin, rmax), Range(cmin, cmax)) << endl << std::endl;
1071 cout << "erode src:\n" << erode_src(Range(rmin, rmax), Range(cmin, cmax)) << endl;
1072 cout << "erode result:\n" << dilate_dst(Range(rmin, rmax), Range(cmin, cmax)) << endl << std::endl;
1073 cout << "dilate src:\n" << dilate_src(Range(rmin, rmax), Range(cmin, cmax)) << endl;
1074 cout << "dilate result:\n" << dilate_dst(Range(rmin, rmax), Range(cmin, cmax)) << endl << std::endl;
1077 ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
1083 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1085 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1087 class CV_WarpAffine_Test :
1088 public CV_Remap_Test
1091 CV_WarpAffine_Test();
1093 virtual ~CV_WarpAffine_Test();
1096 virtual void generate_test_data();
1098 virtual void run_func();
1099 virtual void run_reference_func();
1103 void warpAffine(const Mat&, Mat&);
1106 CV_WarpAffine_Test::CV_WarpAffine_Test() :
1111 CV_WarpAffine_Test::~CV_WarpAffine_Test()
1115 void CV_WarpAffine_Test::generate_test_data()
1117 CV_Remap_Test::generate_test_data();
1118 CV_Remap_Test::prepare_test_data_for_reference_func();
1120 if (src.depth() != CV_32F)
1123 src.convertTo(tmp, CV_32F);
1127 RNG& rng = ts->get_rng();
1129 // generating the M 2x3 matrix
1130 static const int depths[] = { CV_32FC1, CV_64FC1 };
1132 // generating 2d matrix
1133 M = getRotationMatrix2D(Point2f(src.cols / 2.f, src.rows / 2.f),
1134 rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
1135 int depth = depths[rng.uniform(0, sizeof(depths) / sizeof(depths[0]))];
1136 if (M.depth() != depth)
1139 M.convertTo(tmp, depth);
1143 // warp_matrix is inverse
1144 if (rng.uniform(0., 1.) > 0)
1145 interpolation |= CV_WARP_INVERSE_MAP;
1148 void CV_WarpAffine_Test::run_func()
1150 cv::warpAffine(src, dst, M, dst.size(), interpolation, borderType, borderValue);
1153 void CV_WarpAffine_Test::run_reference_func()
1155 CV_Remap_Test::prepare_test_data_for_reference_func();
1157 warpAffine(erode_src, erode_dst);
1158 warpAffine(dilate_src, dilate_dst);
1161 void CV_WarpAffine_Test::warpAffine(const Mat& _src, Mat& _dst)
1163 Size dsize = _dst.size();
1165 CV_Assert(_src.size().area() > 0 && dsize.area() > 0);
1166 CV_Assert(_src.type() == _dst.type());
1169 M.convertTo(tM, CV_64F);
1171 int inter = interpolation & INTER_MAX;
1172 if (inter == INTER_AREA)
1173 inter = INTER_LINEAR;
1175 mapx.create(dsize, CV_16SC2);
1176 if (inter != INTER_NEAREST)
1177 mapy.create(dsize, CV_16SC1);
1179 if (!(interpolation & CV_WARP_INVERSE_MAP))
1180 invertAffineTransform(tM.clone(), tM);
1182 const int AB_BITS = MAX(10, (int)INTER_BITS);
1183 const int AB_SCALE = 1 << AB_BITS;
1184 int round_delta = (inter == INTER_NEAREST) ? AB_SCALE / 2 : (AB_SCALE / INTER_TAB_SIZE / 2);
1186 const double* data_tM = tM.ptr<double>(0);
1187 for (int dy = 0; dy < dsize.height; ++dy)
1189 short* yM = mapx.ptr<short>(dy);
1190 for (int dx = 0; dx < dsize.width; ++dx, yM += 2)
1192 int v1 = saturate_cast<int>(saturate_cast<int>(data_tM[0] * dx * AB_SCALE) +
1193 saturate_cast<int>((data_tM[1] * dy + data_tM[2]) * AB_SCALE) + round_delta),
1194 v2 = saturate_cast<int>(saturate_cast<int>(data_tM[3] * dx * AB_SCALE) +
1195 saturate_cast<int>((data_tM[4] * dy + data_tM[5]) * AB_SCALE) + round_delta);
1196 v1 >>= AB_BITS - INTER_BITS;
1197 v2 >>= AB_BITS - INTER_BITS;
1199 yM[0] = saturate_cast<short>(v1 >> INTER_BITS);
1200 yM[1] = saturate_cast<short>(v2 >> INTER_BITS);
1202 if (inter != INTER_NEAREST)
1203 mapy.ptr<short>(dy)[dx] = ((v2 & (INTER_TAB_SIZE - 1)) * INTER_TAB_SIZE + (v1 & (INTER_TAB_SIZE - 1)));
1207 cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
1210 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1212 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1214 class CV_WarpPerspective_Test :
1215 public CV_WarpAffine_Test
1218 CV_WarpPerspective_Test();
1220 virtual ~CV_WarpPerspective_Test();
1223 virtual void generate_test_data();
1225 virtual void run_func();
1226 virtual void run_reference_func();
1229 void warpPerspective(const Mat&, Mat&);
1232 CV_WarpPerspective_Test::CV_WarpPerspective_Test() :
1233 CV_WarpAffine_Test()
1237 CV_WarpPerspective_Test::~CV_WarpPerspective_Test()
1241 void CV_WarpPerspective_Test::generate_test_data()
1243 CV_Remap_Test::generate_test_data();
1245 // generating the M 3x3 matrix
1246 RNG& rng = ts->get_rng();
1248 Point2f sp[] = { Point(0, 0), Point(src.cols, 0), Point(0, src.rows), Point(src.cols, src.rows) };
1249 Point2f dp[] = { Point(rng.uniform(0, src.cols), rng.uniform(0, src.rows)),
1250 Point(rng.uniform(0, src.cols), rng.uniform(0, src.rows)),
1251 Point(rng.uniform(0, src.cols), rng.uniform(0, src.rows)),
1252 Point(rng.uniform(0, src.cols), rng.uniform(0, src.rows)) };
1253 M = getPerspectiveTransform(sp, dp);
1255 static const int depths[] = { CV_32F, CV_64F };
1256 int depth = depths[rng.uniform(0, 2)];
1257 M.clone().convertTo(M, depth);
1260 void CV_WarpPerspective_Test::run_func()
1262 cv::warpPerspective(src, dst, M, dst.size(), interpolation, borderType, borderValue);
1265 void CV_WarpPerspective_Test::run_reference_func()
1267 CV_Remap_Test::prepare_test_data_for_reference_func();
1269 warpPerspective(erode_src, erode_dst);
1270 warpPerspective(dilate_src, dilate_dst);
1273 void CV_WarpPerspective_Test::warpPerspective(const Mat& _src, Mat& _dst)
1275 Size ssize = _src.size(), dsize = _dst.size();
1277 CV_Assert(ssize.area() > 0 && dsize.area() > 0);
1278 CV_Assert(_src.type() == _dst.type());
1280 if (M.depth() != CV_64F)
1283 M.convertTo(tmp, CV_64F);
1287 if (!(interpolation & CV_WARP_INVERSE_MAP))
1294 int inter = interpolation & INTER_MAX;
1295 if (inter == INTER_AREA)
1296 inter = INTER_LINEAR;
1298 mapx.create(dsize, CV_16SC2);
1299 if (inter != INTER_NEAREST)
1300 mapy.create(dsize, CV_16SC1);
1302 double* tM = M.ptr<double>(0);
1303 for (int dy = 0; dy < dsize.height; ++dy)
1305 short* yMx = mapx.ptr<short>(dy);
1307 for (int dx = 0; dx < dsize.width; ++dx, yMx += 2)
1309 double den = tM[6] * dx + tM[7] * dy + tM[8];
1310 den = den ? 1.0 / den : 0.0;
1312 if (inter == INTER_NEAREST)
1314 yMx[0] = saturate_cast<short>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
1315 yMx[1] = saturate_cast<short>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
1319 den *= INTER_TAB_SIZE;
1320 int v0 = saturate_cast<int>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
1321 int v1 = saturate_cast<int>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
1323 yMx[0] = saturate_cast<short>(v0 >> INTER_BITS);
1324 yMx[1] = saturate_cast<short>(v1 >> INTER_BITS);
1325 mapy.ptr<short>(dy)[dx] = saturate_cast<short>((v1 & (INTER_TAB_SIZE - 1)) *
1326 INTER_TAB_SIZE + (v0 & (INTER_TAB_SIZE - 1)));
1330 cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
1333 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1335 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1337 TEST(Imgproc_Resize_Test, accuracy) { CV_Resize_Test test; test.safe_run(); }
1338 // TEST(Imgproc_Remap_Test, accuracy) { CV_Remap_Test test; test.safe_run(); }
1339 TEST(Imgproc_WarpAffine_Test, accuracy) { CV_WarpAffine_Test test; test.safe_run(); }
1340 TEST(Imgproc_WarpPerspective_Test, accuracy) { CV_WarpPerspective_Test test; test.safe_run(); }