test_resize.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #ifdef HAVE_CUDA
  44. namespace opencv_test { namespace {
  45. ///////////////////////////////////////////////////////////////////
  46. // Gold implementation
  47. namespace
  48. {
  49. template <typename T, template <typename> class Interpolator>
  50. void resizeImpl(const cv::Mat& src, cv::Mat& dst, double fx, double fy)
  51. {
  52. const int cn = src.channels();
  53. cv::Size dsize(cv::saturate_cast<int>(src.cols * fx), cv::saturate_cast<int>(src.rows * fy));
  54. dst.create(dsize, src.type());
  55. float ifx = static_cast<float>(1.0 / fx);
  56. float ify = static_cast<float>(1.0 / fy);
  57. for (int y = 0; y < dsize.height; ++y)
  58. {
  59. for (int x = 0; x < dsize.width; ++x)
  60. {
  61. for (int c = 0; c < cn; ++c)
  62. dst.at<T>(y, x * cn + c) = Interpolator<T>::getValue(src, y * ify, x * ifx, c, cv::BORDER_REPLICATE);
  63. }
  64. }
  65. }
  66. void resizeGold(const cv::Mat& src, cv::Mat& dst, double fx, double fy, int interpolation)
  67. {
  68. typedef void (*func_t)(const cv::Mat& src, cv::Mat& dst, double fx, double fy);
  69. static const func_t nearest_funcs[] =
  70. {
  71. resizeImpl<unsigned char, NearestInterpolator>,
  72. resizeImpl<signed char, NearestInterpolator>,
  73. resizeImpl<unsigned short, NearestInterpolator>,
  74. resizeImpl<short, NearestInterpolator>,
  75. resizeImpl<int, NearestInterpolator>,
  76. resizeImpl<float, NearestInterpolator>
  77. };
  78. static const func_t linear_funcs[] =
  79. {
  80. resizeImpl<unsigned char, LinearInterpolator>,
  81. resizeImpl<signed char, LinearInterpolator>,
  82. resizeImpl<unsigned short, LinearInterpolator>,
  83. resizeImpl<short, LinearInterpolator>,
  84. resizeImpl<int, LinearInterpolator>,
  85. resizeImpl<float, LinearInterpolator>
  86. };
  87. static const func_t cubic_funcs[] =
  88. {
  89. resizeImpl<unsigned char, CubicInterpolator>,
  90. resizeImpl<signed char, CubicInterpolator>,
  91. resizeImpl<unsigned short, CubicInterpolator>,
  92. resizeImpl<short, CubicInterpolator>,
  93. resizeImpl<int, CubicInterpolator>,
  94. resizeImpl<float, CubicInterpolator>
  95. };
  96. static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};
  97. funcs[interpolation][src.depth()](src, dst, fx, fy);
  98. }
  99. }
  100. ///////////////////////////////////////////////////////////////////
  101. // Test
  102. PARAM_TEST_CASE(Resize, cv::cuda::DeviceInfo, cv::Size, MatType, double, Interpolation, UseRoi)
  103. {
  104. cv::cuda::DeviceInfo devInfo;
  105. cv::Size size;
  106. double coeff;
  107. int interpolation;
  108. int type;
  109. bool useRoi;
  110. virtual void SetUp()
  111. {
  112. devInfo = GET_PARAM(0);
  113. size = GET_PARAM(1);
  114. type = GET_PARAM(2);
  115. coeff = GET_PARAM(3);
  116. interpolation = GET_PARAM(4);
  117. useRoi = GET_PARAM(5);
  118. cv::cuda::setDevice(devInfo.deviceID());
  119. }
  120. };
  121. CUDA_TEST_P(Resize, Accuracy)
  122. {
  123. cv::Mat src = randomMat(size, type);
  124. cv::cuda::GpuMat dst = createMat(cv::Size(cv::saturate_cast<int>(src.cols * coeff), cv::saturate_cast<int>(src.rows * coeff)), type, useRoi);
  125. cv::cuda::resize(loadMat(src, useRoi), dst, cv::Size(), coeff, coeff, interpolation);
  126. cv::Mat dst_gold;
  127. resizeGold(src, dst_gold, coeff, coeff, interpolation);
  128. EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-2 : 1.0);
  129. }
  130. INSTANTIATE_TEST_CASE_P(CUDA_Warping, Resize, testing::Combine(
  131. ALL_DEVICES,
  132. DIFFERENT_SIZES,
  133. testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
  134. testing::Values(0.3, 0.5, 1.5, 2.0),
  135. testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)),
  136. WHOLE_SUBMAT));
  137. /////////////////
  138. PARAM_TEST_CASE(ResizeSameAsHost, cv::cuda::DeviceInfo, cv::Size, MatType, double, Interpolation, UseRoi)
  139. {
  140. cv::cuda::DeviceInfo devInfo;
  141. cv::Size size;
  142. double coeff;
  143. int interpolation;
  144. int type;
  145. bool useRoi;
  146. virtual void SetUp()
  147. {
  148. devInfo = GET_PARAM(0);
  149. size = GET_PARAM(1);
  150. type = GET_PARAM(2);
  151. coeff = GET_PARAM(3);
  152. interpolation = GET_PARAM(4);
  153. useRoi = GET_PARAM(5);
  154. cv::cuda::setDevice(devInfo.deviceID());
  155. }
  156. };
  157. // downscaling only: used for classifiers
  158. CUDA_TEST_P(ResizeSameAsHost, Accuracy)
  159. {
  160. cv::Mat src = randomMat(size, type);
  161. cv::cuda::GpuMat dst = createMat(cv::Size(cv::saturate_cast<int>(src.cols * coeff), cv::saturate_cast<int>(src.rows * coeff)), type, useRoi);
  162. cv::cuda::resize(loadMat(src, useRoi), dst, cv::Size(), coeff, coeff, interpolation);
  163. cv::Mat dst_gold;
  164. cv::resize(src, dst_gold, cv::Size(), coeff, coeff, interpolation);
  165. EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-2 : 1.0);
  166. }
  167. INSTANTIATE_TEST_CASE_P(CUDA_Warping, ResizeSameAsHost, testing::Combine(
  168. ALL_DEVICES,
  169. DIFFERENT_SIZES,
  170. testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
  171. testing::Values(0.3, 0.5),
  172. testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_AREA)),
  173. WHOLE_SUBMAT));
  174. }} // namespace
  175. #endif // HAVE_CUDA