test_warp_perspective.cpp 10 KB

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