test_undistort_points.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include <opencv2/ts/cuda_test.hpp> // EXPECT_MAT_NEAR
  5. #include "opencv2/core/types.hpp"
  6. #include "test_precomp.hpp"
  7. namespace opencv_test { namespace {
  8. class UndistortPointsTest : public ::testing::Test
  9. {
  10. protected:
  11. void generate3DPointCloud(vector<Point3f>& points, Point3f pmin = Point3f(-1,
  12. -1, 5), Point3f pmax = Point3f(1, 1, 10));
  13. void generateCameraMatrix(Mat& cameraMatrix);
  14. void generateDistCoeffs(Mat& distCoeffs, int count);
  15. cv::Mat generateRotationVector();
  16. std::vector<cv::Point2d> distortPoints(const cv::Mat &cameraMatrix, const cv::Mat &dist, const std::vector<cv::Point2d> &points);
  17. double thresh = 1.0e-2;
  18. };
  19. void UndistortPointsTest::generate3DPointCloud(vector<Point3f>& points, Point3f pmin, Point3f pmax)
  20. {
  21. RNG rng_Point = cv::theRNG(); // fix the seed to use "fixed" input 3D points
  22. for (size_t i = 0; i < points.size(); i++)
  23. {
  24. float _x = rng_Point.uniform(pmin.x, pmax.x);
  25. float _y = rng_Point.uniform(pmin.y, pmax.y);
  26. float _z = rng_Point.uniform(pmin.z, pmax.z);
  27. points[i] = Point3f(_x, _y, _z);
  28. }
  29. }
  30. void UndistortPointsTest::generateCameraMatrix(Mat& cameraMatrix)
  31. {
  32. const double fcMinVal = 1e-3;
  33. const double fcMaxVal = 100;
  34. cameraMatrix.create(3, 3, CV_64FC1);
  35. cameraMatrix.setTo(Scalar(0));
  36. cameraMatrix.at<double>(0,0) = theRNG().uniform(fcMinVal, fcMaxVal);
  37. cameraMatrix.at<double>(1,1) = theRNG().uniform(fcMinVal, fcMaxVal);
  38. cameraMatrix.at<double>(0,2) = theRNG().uniform(fcMinVal, fcMaxVal);
  39. cameraMatrix.at<double>(1,2) = theRNG().uniform(fcMinVal, fcMaxVal);
  40. cameraMatrix.at<double>(2,2) = 1;
  41. }
  42. void UndistortPointsTest::generateDistCoeffs(Mat& distCoeffs, int count)
  43. {
  44. distCoeffs = Mat::zeros(count, 1, CV_64FC1);
  45. for (int i = 0; i < count; i++)
  46. distCoeffs.at<double>(i,0) = theRNG().uniform(-0.1, 0.1);
  47. }
  48. cv::Mat UndistortPointsTest::generateRotationVector()
  49. {
  50. Mat rvec(1, 3, CV_64F);
  51. theRNG().fill(rvec, RNG::UNIFORM, -0.2, 0.2);
  52. return rvec;
  53. }
  54. std::vector<cv::Point2d> UndistortPointsTest::distortPoints(const cv::Mat &cameraMatrix, const cv::Mat &dist, const std::vector<cv::Point2d> &points)
  55. {
  56. CV_Assert(cameraMatrix.rows == 3 && cameraMatrix.cols == 3);
  57. CV_Assert(cameraMatrix.type() == CV_64F);
  58. CV_Assert(dist.rows * dist.cols == 12);
  59. CV_Assert(dist.type() == CV_64F);
  60. double *k = reinterpret_cast<double *>(dist.data);
  61. double fx = cameraMatrix.at<double>(0, 0);
  62. double fy = cameraMatrix.at<double>(1, 1);
  63. double cx = cameraMatrix.at<double>(0, 2);
  64. double cy = cameraMatrix.at<double>(1, 2);
  65. std::vector<cv::Point2d> distortedPoints;
  66. distortedPoints.reserve(points.size());
  67. for (const cv::Point2d p : points) {
  68. double x = (p.x - cx) / fx;
  69. double y = (p.y - cy) / fy;
  70. double r2 = x*x + y*y;
  71. double cdist = (1 + ((k[4]*r2 + k[1])*r2 + k[0])*r2)/(1 + ((k[7]*r2 + k[6])*r2 + k[5])*r2);
  72. CV_Assert(cdist >= 0);
  73. double deltaX = 2*k[2]*x*y + k[3]*(r2 + 2*x*x)+ k[8]*r2+k[9]*r2*r2;
  74. double deltaY = k[2]*(r2 + 2*y*y) + 2*k[3]*x*y+ k[10]*r2+k[11]*r2*r2;
  75. distortedPoints.push_back(cv::Point2d((x * cdist + deltaX) * fx + cx, (y * cdist + deltaY) * fy + cy));
  76. }
  77. return distortedPoints;
  78. }
  79. TEST_F(UndistortPointsTest, accuracy)
  80. {
  81. Mat intrinsics, distCoeffs;
  82. generateCameraMatrix(intrinsics);
  83. vector<Point3f> points(500);
  84. generate3DPointCloud(points);
  85. Mat rvec = generateRotationVector();
  86. Mat R;
  87. cv::Rodrigues(rvec, R);
  88. int modelMembersCount[] = {4,5,8};
  89. for (int idx = 0; idx < 3; idx++)
  90. {
  91. generateDistCoeffs(distCoeffs, modelMembersCount[idx]);
  92. /* Project points with distortion */
  93. vector<Point2f> projectedPoints;
  94. projectPoints(Mat(points), Mat::zeros(3,1,CV_64FC1),
  95. Mat::zeros(3,1,CV_64FC1), intrinsics,
  96. distCoeffs, projectedPoints);
  97. /* Project points without distortion */
  98. vector<Point2f> realUndistortedPoints;
  99. projectPoints(Mat(points), rvec,
  100. Mat::zeros(3,1,CV_64FC1), intrinsics,
  101. Mat::zeros(4,1,CV_64FC1), realUndistortedPoints);
  102. /* Undistort points */
  103. Mat undistortedPoints;
  104. undistortPoints(Mat(projectedPoints), undistortedPoints, intrinsics, distCoeffs, R, intrinsics);
  105. EXPECT_MAT_NEAR(realUndistortedPoints, undistortedPoints.t(), thresh);
  106. }
  107. }
  108. TEST_F(UndistortPointsTest, undistortImagePointsAccuracy)
  109. {
  110. Mat intrinsics, distCoeffs;
  111. generateCameraMatrix(intrinsics);
  112. vector<Point3f> points(500);
  113. generate3DPointCloud(points);
  114. int modelMembersCount[] = {4,5,8};
  115. for (int idx = 0; idx < 3; idx++)
  116. {
  117. generateDistCoeffs(distCoeffs, modelMembersCount[idx]);
  118. /* Project points with distortion */
  119. vector<Point2f> projectedPoints;
  120. projectPoints(Mat(points), Mat::zeros(3,1,CV_64FC1),
  121. Mat::zeros(3,1,CV_64FC1), intrinsics,
  122. distCoeffs, projectedPoints);
  123. /* Project points without distortion */
  124. vector<Point2f> realUndistortedPoints;
  125. projectPoints(Mat(points), Mat::zeros(3, 1, CV_64FC1),
  126. Mat::zeros(3,1,CV_64FC1), intrinsics,
  127. Mat::zeros(4,1,CV_64FC1), realUndistortedPoints);
  128. /* Undistort points */
  129. Mat undistortedPoints;
  130. TermCriteria termCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, thresh / 2);
  131. undistortImagePoints(Mat(projectedPoints), undistortedPoints, intrinsics, distCoeffs,
  132. termCriteria);
  133. EXPECT_MAT_NEAR(realUndistortedPoints, undistortedPoints.t(), thresh);
  134. }
  135. }
  136. TEST_F(UndistortPointsTest, stop_criteria)
  137. {
  138. Mat cameraMatrix = (Mat_<double>(3,3,CV_64F) << 857.48296979, 0, 968.06224829,
  139. 0, 876.71824265, 556.37145899,
  140. 0, 0, 1);
  141. Mat distCoeffs = (Mat_<double>(5,1,CV_64F) <<
  142. -2.57614020e-01, 8.77086999e-02, -2.56970803e-04, -5.93390389e-04, -1.52194091e-02);
  143. Point2d pt_distorted(theRNG().uniform(0.0, 1920.0), theRNG().uniform(0.0, 1080.0));
  144. std::vector<Point2d> pt_distorted_vec;
  145. pt_distorted_vec.push_back(pt_distorted);
  146. const double maxError = 1e-6;
  147. TermCriteria criteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 100, maxError);
  148. std::vector<Point2d> pt_undist_vec;
  149. Mat rVec = Mat(Matx31d(0.1, -0.2, 0.2));
  150. Mat R;
  151. cv::Rodrigues(rVec, R);
  152. undistortPoints(pt_distorted_vec, pt_undist_vec, cameraMatrix, distCoeffs, R, noArray(), criteria);
  153. std::vector<Point3d> pt_undist_vec_homogeneous;
  154. pt_undist_vec_homogeneous.emplace_back(pt_undist_vec[0].x, pt_undist_vec[0].y, 1.0 );
  155. std::vector<Point2d> pt_redistorted_vec;
  156. projectPoints(pt_undist_vec_homogeneous, -rVec,
  157. Mat::zeros(3,1,CV_64F), cameraMatrix, distCoeffs, pt_redistorted_vec);
  158. const double obtainedError = sqrt( pow(pt_distorted.x - pt_redistorted_vec[0].x, 2) + pow(pt_distorted.y - pt_redistorted_vec[0].y, 2) );
  159. ASSERT_LE(obtainedError, maxError);
  160. }
  161. TEST_F(UndistortPointsTest, regression_14583)
  162. {
  163. const int col = 720;
  164. // const int row = 540;
  165. float camera_matrix_value[] = {
  166. 437.8995f, 0.0f, 342.9241f,
  167. 0.0f, 438.8216f, 273.7163f,
  168. 0.0f, 0.0f, 1.0f
  169. };
  170. cv::Mat camera_interior(3, 3, CV_32F, camera_matrix_value);
  171. float camera_distort_value[] = {-0.34329f, 0.11431f, 0.0f, 0.0f, -0.017375f};
  172. cv::Mat camera_distort(1, 5, CV_32F, camera_distort_value);
  173. float distort_points_value[] = {col, 0.};
  174. cv::Mat distort_pt(1, 1, CV_32FC2, distort_points_value);
  175. cv::Mat undistort_pt;
  176. cv::undistortPoints(distort_pt, undistort_pt, camera_interior,
  177. camera_distort, cv::Mat(), camera_interior);
  178. EXPECT_NEAR(distort_pt.at<Vec2f>(0)[0], undistort_pt.at<Vec2f>(0)[0], col / 2)
  179. << "distort point: " << distort_pt << std::endl
  180. << "undistort point: " << undistort_pt;
  181. }
  182. TEST_F(UndistortPointsTest, regression_27916)
  183. {
  184. cv::Mat K = (cv::Mat_<double>(3, 3) <<
  185. 1570.8956145992222, 0., 744.87337646727406, 0.,
  186. 1570.3494207432338, 575.55087456337526, 0., 0., 1.);
  187. cv::Mat dist = (cv::Mat_<double>(1, 12) <<
  188. -2.8247717583453804, -0.80078070764368037,
  189. -0.014595359484103326, 0.0018820998949700702, 1.9827795585249783,
  190. -2.7306773773930897, -1.217725820479524, 2.4052243546080136,
  191. -0.0020670359760441713, 3.4660880793174063e-05,
  192. 0.014100351510458799, -3.0935329736207612e-05);
  193. const cv::TermCriteria termCriteria(TermCriteria::MAX_ITER | TermCriteria::EPS, 100, thresh / 2);
  194. std::vector<cv::Point2d> distortedPoints, distortedPoints2;
  195. std::vector<cv::Point2d> undistortedPoints;
  196. for (int i = 0; i < 50; i++)
  197. {
  198. for (int j = 0; j < 50; j++)
  199. {
  200. distortedPoints.push_back(cv::Point2d(i, j));
  201. }
  202. }
  203. cv::undistortPoints(distortedPoints, undistortedPoints, K, dist, cv::noArray(), K, termCriteria);
  204. distortedPoints2 = distortPoints(K, dist, undistortedPoints);
  205. EXPECT_MAT_NEAR(distortedPoints2, distortedPoints, thresh);
  206. }
  207. }} // namespace