test_warp_affine.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. namespace
  46. {
  47. cv::Mat createTransformMatrix(cv::Size srcSize, double angle)
  48. {
  49. cv::Mat M(2, 3, CV_64FC1);
  50. M.at<double>(0, 0) = std::cos(angle); M.at<double>(0, 1) = -std::sin(angle); M.at<double>(0, 2) = srcSize.width / 2;
  51. M.at<double>(1, 0) = std::sin(angle); M.at<double>(1, 1) = std::cos(angle); M.at<double>(1, 2) = 0.0;
  52. return M;
  53. }
  54. }
  55. ///////////////////////////////////////////////////////////////////
  56. // Test buildWarpAffineMaps
  57. PARAM_TEST_CASE(BuildWarpAffineMaps, cv::cuda::DeviceInfo, cv::Size, Inverse)
  58. {
  59. cv::cuda::DeviceInfo devInfo;
  60. cv::Size size;
  61. bool inverse;
  62. virtual void SetUp()
  63. {
  64. devInfo = GET_PARAM(0);
  65. size = GET_PARAM(1);
  66. inverse = GET_PARAM(2);
  67. cv::cuda::setDevice(devInfo.deviceID());
  68. }
  69. };
  70. CUDA_TEST_P(BuildWarpAffineMaps, Accuracy)
  71. {
  72. cv::Mat M = createTransformMatrix(size, CV_PI / 4);
  73. cv::Mat src = randomMat(randomSize(200, 400), CV_8UC1);
  74. cv::cuda::GpuMat xmap, ymap;
  75. cv::cuda::buildWarpAffineMaps(M, inverse, size, xmap, ymap);
  76. int interpolation = cv::INTER_NEAREST;
  77. int borderMode = cv::BORDER_CONSTANT;
  78. int flags = interpolation;
  79. if (inverse)
  80. flags |= cv::WARP_INVERSE_MAP;
  81. cv::Mat dst;
  82. cv::remap(src, dst, cv::Mat(xmap), cv::Mat(ymap), interpolation, borderMode);
  83. cv::Mat dst_gold;
  84. cv::warpAffine(src, dst_gold, M, size, flags, borderMode);
  85. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  86. }
  87. INSTANTIATE_TEST_CASE_P(CUDA_Warping, BuildWarpAffineMaps, testing::Combine(
  88. ALL_DEVICES,
  89. DIFFERENT_SIZES,
  90. DIRECT_INVERSE));
  91. ///////////////////////////////////////////////////////////////////
  92. // Gold implementation
  93. namespace
  94. {
  95. template <typename T, template <typename> class Interpolator> void warpAffineImpl(const cv::Mat& src, const cv::Mat& M, cv::Size dsize, cv::Mat& dst, int borderType, cv::Scalar borderVal)
  96. {
  97. const int cn = src.channels();
  98. dst.create(dsize, src.type());
  99. for (int y = 0; y < dsize.height; ++y)
  100. {
  101. for (int x = 0; x < dsize.width; ++x)
  102. {
  103. float xcoo = static_cast<float>(M.at<double>(0, 0) * x + M.at<double>(0, 1) * y + M.at<double>(0, 2));
  104. float ycoo = static_cast<float>(M.at<double>(1, 0) * x + M.at<double>(1, 1) * y + M.at<double>(1, 2));
  105. for (int c = 0; c < cn; ++c)
  106. dst.at<T>(y, x * cn + c) = Interpolator<T>::getValue(src, ycoo, xcoo, c, borderType, borderVal);
  107. }
  108. }
  109. }
  110. void warpAffineGold(const cv::Mat& src, const cv::Mat& M, bool inverse, cv::Size dsize, cv::Mat& dst, int interpolation, int borderType, cv::Scalar borderVal)
  111. {
  112. typedef void (*func_t)(const cv::Mat& src, const cv::Mat& M, cv::Size dsize, cv::Mat& dst, int borderType, cv::Scalar borderVal);
  113. static const func_t nearest_funcs[] =
  114. {
  115. warpAffineImpl<unsigned char, NearestInterpolator>,
  116. warpAffineImpl<signed char, NearestInterpolator>,
  117. warpAffineImpl<unsigned short, NearestInterpolator>,
  118. warpAffineImpl<short, NearestInterpolator>,
  119. warpAffineImpl<int, NearestInterpolator>,
  120. warpAffineImpl<float, NearestInterpolator>
  121. };
  122. static const func_t linear_funcs[] =
  123. {
  124. warpAffineImpl<unsigned char, LinearInterpolator>,
  125. warpAffineImpl<signed char, LinearInterpolator>,
  126. warpAffineImpl<unsigned short, LinearInterpolator>,
  127. warpAffineImpl<short, LinearInterpolator>,
  128. warpAffineImpl<int, LinearInterpolator>,
  129. warpAffineImpl<float, LinearInterpolator>
  130. };
  131. static const func_t cubic_funcs[] =
  132. {
  133. warpAffineImpl<unsigned char, CubicInterpolator>,
  134. warpAffineImpl<signed char, CubicInterpolator>,
  135. warpAffineImpl<unsigned short, CubicInterpolator>,
  136. warpAffineImpl<short, CubicInterpolator>,
  137. warpAffineImpl<int, CubicInterpolator>,
  138. warpAffineImpl<float, CubicInterpolator>
  139. };
  140. static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};
  141. if (inverse)
  142. funcs[interpolation][src.depth()](src, M, dsize, dst, borderType, borderVal);
  143. else
  144. {
  145. cv::Mat iM;
  146. cv::invertAffineTransform(M, iM);
  147. funcs[interpolation][src.depth()](src, iM, dsize, dst, borderType, borderVal);
  148. }
  149. }
  150. }
  151. ///////////////////////////////////////////////////////////////////
  152. // Test
  153. PARAM_TEST_CASE(WarpAffine, cv::cuda::DeviceInfo, cv::Size, MatType, Inverse, Interpolation, BorderType, UseRoi)
  154. {
  155. cv::cuda::DeviceInfo devInfo;
  156. cv::Size size;
  157. int type;
  158. bool inverse;
  159. int interpolation;
  160. int borderType;
  161. bool useRoi;
  162. virtual void SetUp()
  163. {
  164. devInfo = GET_PARAM(0);
  165. size = GET_PARAM(1);
  166. type = GET_PARAM(2);
  167. inverse = GET_PARAM(3);
  168. interpolation = GET_PARAM(4);
  169. borderType = GET_PARAM(5);
  170. useRoi = GET_PARAM(6);
  171. cv::cuda::setDevice(devInfo.deviceID());
  172. }
  173. };
  174. CUDA_TEST_P(WarpAffine, Accuracy)
  175. {
  176. cv::Mat src = randomMat(size, type);
  177. cv::Mat M = createTransformMatrix(size, CV_PI / 3);
  178. int flags = interpolation;
  179. if (inverse)
  180. flags |= cv::WARP_INVERSE_MAP;
  181. cv::Scalar val = randomScalar(0.0, 255.0);
  182. cv::cuda::GpuMat dst = createMat(size, type, useRoi);
  183. cv::cuda::warpAffine(loadMat(src, useRoi), dst, M, size, flags, borderType, val);
  184. cv::Mat dst_gold;
  185. warpAffineGold(src, M, inverse, size, dst_gold, interpolation, borderType, val);
  186. EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-1 : 1.0);
  187. }
  188. INSTANTIATE_TEST_CASE_P(CUDA_Warping, WarpAffine, testing::Combine(
  189. ALL_DEVICES,
  190. DIFFERENT_SIZES,
  191. 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)),
  192. DIRECT_INVERSE,
  193. testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)),
  194. testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_REFLECT), BorderType(cv::BORDER_WRAP)),
  195. WHOLE_SUBMAT));
  196. ///////////////////////////////////////////////////////////////////
  197. // Test NPP
  198. PARAM_TEST_CASE(WarpAffineNPP, cv::cuda::DeviceInfo, MatType, Inverse, Interpolation)
  199. {
  200. cv::cuda::DeviceInfo devInfo;
  201. int type;
  202. bool inverse;
  203. int interpolation;
  204. virtual void SetUp()
  205. {
  206. devInfo = GET_PARAM(0);
  207. type = GET_PARAM(1);
  208. inverse = GET_PARAM(2);
  209. interpolation = GET_PARAM(3);
  210. cv::cuda::setDevice(devInfo.deviceID());
  211. }
  212. };
  213. CUDA_TEST_P(WarpAffineNPP, Accuracy)
  214. {
  215. cv::Mat src = readImageType("stereobp/aloe-L.png", type);
  216. ASSERT_FALSE(src.empty());
  217. cv::Mat M = createTransformMatrix(src.size(), CV_PI / 4);
  218. int flags = interpolation;
  219. if (inverse)
  220. flags |= cv::WARP_INVERSE_MAP;
  221. cv::cuda::GpuMat dst;
  222. cv::cuda::warpAffine(loadMat(src), dst, M, src.size(), flags);
  223. cv::Mat dst_gold;
  224. warpAffineGold(src, M, inverse, src.size(), dst_gold, interpolation, cv::BORDER_CONSTANT, cv::Scalar::all(0));
  225. EXPECT_MAT_SIMILAR(dst_gold, dst, 2e-2);
  226. }
  227. INSTANTIATE_TEST_CASE_P(CUDA_Warping, WarpAffineNPP, testing::Combine(
  228. ALL_DEVICES,
  229. testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
  230. DIRECT_INVERSE,
  231. testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC))));
  232. }} // namespace
  233. #endif // HAVE_CUDA