test_calib3d.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #if defined HAVE_CUDA && defined HAVE_OPENCV_CALIB3D
  44. #include "opencv2/calib3d.hpp"
  45. namespace opencv_test { namespace {
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  47. // transformPoints
  48. struct TransformPoints : testing::TestWithParam<cv::cuda::DeviceInfo>
  49. {
  50. cv::cuda::DeviceInfo devInfo;
  51. virtual void SetUp()
  52. {
  53. devInfo = GetParam();
  54. cv::cuda::setDevice(devInfo.deviceID());
  55. }
  56. };
  57. CUDA_TEST_P(TransformPoints, Accuracy)
  58. {
  59. cv::Mat src = randomMat(cv::Size(1000, 1), CV_32FC3, 0, 10);
  60. cv::Mat rvec = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
  61. cv::Mat tvec = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
  62. cv::cuda::GpuMat dst;
  63. cv::cuda::transformPoints(loadMat(src), rvec, tvec, dst);
  64. ASSERT_EQ(src.size(), dst.size());
  65. ASSERT_EQ(src.type(), dst.type());
  66. cv::Mat h_dst(dst);
  67. cv::Mat rot;
  68. cv::Rodrigues(rvec, rot);
  69. for (int i = 0; i < h_dst.cols; ++i)
  70. {
  71. cv::Point3f res = h_dst.at<cv::Point3f>(0, i);
  72. cv::Point3f p = src.at<cv::Point3f>(0, i);
  73. cv::Point3f res_gold(
  74. rot.at<float>(0, 0) * p.x + rot.at<float>(0, 1) * p.y + rot.at<float>(0, 2) * p.z + tvec.at<float>(0, 0),
  75. rot.at<float>(1, 0) * p.x + rot.at<float>(1, 1) * p.y + rot.at<float>(1, 2) * p.z + tvec.at<float>(0, 1),
  76. rot.at<float>(2, 0) * p.x + rot.at<float>(2, 1) * p.y + rot.at<float>(2, 2) * p.z + tvec.at<float>(0, 2));
  77. ASSERT_POINT3_NEAR(res_gold, res, 1e-5);
  78. }
  79. }
  80. INSTANTIATE_TEST_CASE_P(CUDA_Calib3D, TransformPoints, ALL_DEVICES);
  81. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  82. // ProjectPoints
  83. struct ProjectPoints : testing::TestWithParam<cv::cuda::DeviceInfo>
  84. {
  85. cv::cuda::DeviceInfo devInfo;
  86. virtual void SetUp()
  87. {
  88. devInfo = GetParam();
  89. cv::cuda::setDevice(devInfo.deviceID());
  90. }
  91. };
  92. CUDA_TEST_P(ProjectPoints, Accuracy)
  93. {
  94. cv::Mat src = randomMat(cv::Size(1000, 1), CV_32FC3, 0, 10);
  95. cv::Mat rvec = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
  96. cv::Mat tvec = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
  97. cv::Mat camera_mat = randomMat(cv::Size(3, 3), CV_32F, 0.5, 1);
  98. camera_mat.at<float>(0, 1) = 0.f;
  99. camera_mat.at<float>(1, 0) = 0.f;
  100. camera_mat.at<float>(2, 0) = 0.f;
  101. camera_mat.at<float>(2, 1) = 0.f;
  102. cv::cuda::GpuMat dst;
  103. cv::cuda::projectPoints(loadMat(src), rvec, tvec, camera_mat, cv::Mat(), dst);
  104. ASSERT_EQ(1, dst.rows);
  105. ASSERT_EQ(MatType(CV_32FC2), MatType(dst.type()));
  106. std::vector<cv::Point2f> dst_gold;
  107. cv::projectPoints(src, rvec, tvec, camera_mat, cv::Mat(1, 8, CV_32F, cv::Scalar::all(0)), dst_gold);
  108. ASSERT_EQ(dst_gold.size(), static_cast<size_t>(dst.cols));
  109. cv::Mat h_dst(dst);
  110. for (size_t i = 0; i < dst_gold.size(); ++i)
  111. {
  112. cv::Point2f res = h_dst.at<cv::Point2f>(0, (int)i);
  113. cv::Point2f res_gold = dst_gold[i];
  114. ASSERT_LE(cv::norm(res_gold - res) / cv::norm(res_gold), 1e-3f);
  115. }
  116. }
  117. INSTANTIATE_TEST_CASE_P(CUDA_Calib3D, ProjectPoints, ALL_DEVICES);
  118. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  119. // SolvePnPRansac
  120. struct SolvePnPRansac : testing::TestWithParam<cv::cuda::DeviceInfo>
  121. {
  122. cv::cuda::DeviceInfo devInfo;
  123. virtual void SetUp()
  124. {
  125. devInfo = GetParam();
  126. cv::cuda::setDevice(devInfo.deviceID());
  127. }
  128. };
  129. CUDA_TEST_P(SolvePnPRansac, Accuracy)
  130. {
  131. cv::Mat object = randomMat(cv::Size(5000, 1), CV_32FC3, -2000, 2000);
  132. cv::Mat camera_mat = randomMat(cv::Size(3, 3), CV_32F, 0.5, 1);
  133. camera_mat.at<float>(0, 1) = 0.f;
  134. camera_mat.at<float>(1, 0) = 0.f;
  135. camera_mat.at<float>(2, 0) = 0.f;
  136. camera_mat.at<float>(2, 1) = 0.f;
  137. std::vector<cv::Point2f> image_vec;
  138. cv::Mat rvec_gold;
  139. cv::Mat tvec_gold;
  140. rvec_gold = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
  141. tvec_gold = randomMat(cv::Size(3, 1), CV_32F, 0, 1000);
  142. cv::projectPoints(object, rvec_gold, tvec_gold, camera_mat, cv::Mat(1, 8, CV_32F, cv::Scalar::all(0)), image_vec);
  143. cv::Mat rvec, tvec;
  144. std::vector<int> inliers;
  145. cv::cuda::solvePnPRansac(object, cv::Mat(1, (int)image_vec.size(), CV_32FC2, &image_vec[0]),
  146. camera_mat, cv::Mat(1, 8, CV_32F, cv::Scalar::all(0)),
  147. rvec, tvec, false, 200, 2.f, 100, &inliers);
  148. ASSERT_LE(cv::norm(rvec - rvec_gold), 1e-3);
  149. ASSERT_LE(cv::norm(tvec, tvec_gold, NORM_L2 | NORM_RELATIVE), 1e-3);
  150. }
  151. INSTANTIATE_TEST_CASE_P(CUDA_Calib3D, SolvePnPRansac, ALL_DEVICES);
  152. }} // namespace
  153. #endif // HAVE_CUDA