test_remap.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "opencv2/core/matx.hpp"
  44. #include "nppdefs.h"
  45. #ifdef HAVE_CUDA
  46. namespace opencv_test { namespace {
  47. ///////////////////////////////////////////////////////////////////
  48. // Gold implementation
  49. namespace
  50. {
  51. template <typename T, template <typename> class Interpolator> void remapImpl(const cv::Mat& src, const cv::Mat& xmap, const cv::Mat& ymap, cv::Mat& dst, int borderType, cv::Scalar borderVal)
  52. {
  53. const int cn = src.channels();
  54. cv::Size dsize = xmap.size();
  55. dst.create(dsize, src.type());
  56. for (int y = 0; y < dsize.height; ++y)
  57. {
  58. for (int x = 0; x < dsize.width; ++x)
  59. {
  60. for (int c = 0; c < cn; ++c)
  61. dst.at<T>(y, x * cn + c) = Interpolator<T>::getValue(src, ymap.at<float>(y, x), xmap.at<float>(y, x), c, borderType, borderVal);
  62. }
  63. }
  64. }
  65. void remapGold(const cv::Mat& src, const cv::Mat& xmap, const cv::Mat& ymap, cv::Mat& dst, int interpolation, int borderType, cv::Scalar borderVal)
  66. {
  67. typedef void (*func_t)(const cv::Mat& src, const cv::Mat& xmap, const cv::Mat& ymap, cv::Mat& dst, int borderType, cv::Scalar borderVal);
  68. static const func_t nearest_funcs[] =
  69. {
  70. remapImpl<unsigned char, NearestInterpolator>,
  71. remapImpl<signed char, NearestInterpolator>,
  72. remapImpl<unsigned short, NearestInterpolator>,
  73. remapImpl<short, NearestInterpolator>,
  74. remapImpl<int, NearestInterpolator>,
  75. remapImpl<float, NearestInterpolator>
  76. };
  77. static const func_t linear_funcs[] =
  78. {
  79. remapImpl<unsigned char, LinearInterpolator>,
  80. remapImpl<signed char, LinearInterpolator>,
  81. remapImpl<unsigned short, LinearInterpolator>,
  82. remapImpl<short, LinearInterpolator>,
  83. remapImpl<int, LinearInterpolator>,
  84. remapImpl<float, LinearInterpolator>
  85. };
  86. static const func_t cubic_funcs[] =
  87. {
  88. remapImpl<unsigned char, CubicInterpolator>,
  89. remapImpl<signed char, CubicInterpolator>,
  90. remapImpl<unsigned short, CubicInterpolator>,
  91. remapImpl<short, CubicInterpolator>,
  92. remapImpl<int, CubicInterpolator>,
  93. remapImpl<float, CubicInterpolator>
  94. };
  95. static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};
  96. funcs[interpolation][src.depth()](src, xmap, ymap, dst, borderType, borderVal);
  97. }
  98. }
  99. ///////////////////////////////////////////////////////////////////
  100. // Test
  101. PARAM_TEST_CASE(Remap, cv::cuda::DeviceInfo, cv::Size, MatType, Interpolation, BorderType, UseRoi)
  102. {
  103. cv::cuda::DeviceInfo devInfo;
  104. cv::Size size;
  105. int type;
  106. int interpolation;
  107. int borderType;
  108. bool useRoi;
  109. cv::Mat xmap;
  110. cv::Mat ymap;
  111. virtual void SetUp()
  112. {
  113. devInfo = GET_PARAM(0);
  114. size = GET_PARAM(1);
  115. type = GET_PARAM(2);
  116. interpolation = GET_PARAM(3);
  117. borderType = GET_PARAM(4);
  118. useRoi = GET_PARAM(5);
  119. cv::cuda::setDevice(devInfo.deviceID());
  120. // rotation matrix
  121. const double aplha = CV_PI / 4;
  122. static double M[2][3] = { {std::cos(aplha), -std::sin(aplha), size.width / 2.0},
  123. {std::sin(aplha), std::cos(aplha), 0.0}};
  124. xmap.create(size, CV_32FC1);
  125. ymap.create(size, CV_32FC1);
  126. for (int y = 0; y < size.height; ++y)
  127. {
  128. for (int x = 0; x < size.width; ++x)
  129. {
  130. xmap.at<float>(y, x) = static_cast<float>(M[0][0] * x + M[0][1] * y + M[0][2]);
  131. ymap.at<float>(y, x) = static_cast<float>(M[1][0] * x + M[1][1] * y + M[1][2]);
  132. }
  133. }
  134. }
  135. };
  136. CUDA_TEST_P(Remap, Accuracy)
  137. {
  138. cv::Mat src = randomMat(size, type);
  139. cv::Scalar val = randomScalar(0.0, 255.0);
  140. cv::cuda::GpuMat dst = createMat(xmap.size(), type, useRoi);
  141. cv::cuda::remap(loadMat(src, useRoi), dst, loadMat(xmap, useRoi), loadMat(ymap, useRoi), interpolation, borderType, val);
  142. cv::Mat dst_gold;
  143. remapGold(src, xmap, ymap, dst_gold, interpolation, borderType, val);
  144. EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-3 : 1.0);
  145. }
  146. INSTANTIATE_TEST_CASE_P(CUDA_Warping, Remap, testing::Combine(
  147. ALL_DEVICES,
  148. DIFFERENT_SIZES,
  149. testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
  150. testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)),
  151. testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_CONSTANT), BorderType(cv::BORDER_REFLECT), BorderType(cv::BORDER_WRAP)),
  152. WHOLE_SUBMAT));
  153. class RemapOutOfScope : public Remap {};
  154. CUDA_TEST_P(RemapOutOfScope, Regression_18224)
  155. {
  156. cv::Mat src = randomMat(size, type);
  157. cv::cuda::GpuMat dst = createMat(xmap.size(), type, useRoi);
  158. randu(xmap, NPP_MAX_32S, NPP_MAXABS_32F);
  159. randu(ymap, NPP_MAX_32S, NPP_MAXABS_32F);
  160. cv::cuda::remap(loadMat(src, useRoi), dst, loadMat(xmap, useRoi), loadMat(ymap, useRoi), interpolation, borderType, 0.);
  161. cv::Mat dst_gold;
  162. remapGold(src, xmap, ymap, dst_gold, interpolation, borderType, 0.);
  163. EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-3 : 1.0);
  164. }
  165. INSTANTIATE_TEST_CASE_P(CUDA_Warping, RemapOutOfScope, testing::Combine(
  166. ALL_DEVICES,
  167. DIFFERENT_SIZES,
  168. testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
  169. testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR)),
  170. testing::Values(BorderType(cv::BORDER_CONSTANT)),
  171. WHOLE_SUBMAT));
  172. }} // namespace
  173. #endif // HAVE_CUDA