From ace6eaef7e685888d6f6a5a5fa421f935f7f9ba9 Mon Sep 17 00:00:00 2001 From: Alexander Shishkov Date: Mon, 6 Feb 2012 06:47:43 +0000 Subject: [PATCH] added one more test for undistort --- .../calib3d/test/test_undistort_points.cpp | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 modules/calib3d/test/test_undistort_points.cpp diff --git a/modules/calib3d/test/test_undistort_points.cpp b/modules/calib3d/test/test_undistort_points.cpp new file mode 100644 index 0000000000..8b35d7c593 --- /dev/null +++ b/modules/calib3d/test/test_undistort_points.cpp @@ -0,0 +1,97 @@ +#include "test_precomp.hpp" +#include + +using namespace cv; +using namespace std; + +class CV_UndistortTest : public cvtest::BaseTest +{ +public: + CV_UndistortTest(); + ~CV_UndistortTest(); +protected: + void run(int); +private: + void generate3DPointCloud(vector& points, Point3f pmin = Point3f(-1, + -1, 5), Point3f pmax = Point3f(1, 1, 10)); + void generateCameraMatrix(Mat& cameraMatrix); + void generateDistCoeffs(Mat& distCoeffs, int count); + + double thresh; + RNG rng; +}; + +CV_UndistortTest::CV_UndistortTest() +{ + thresh = 1.0e-2; +} +CV_UndistortTest::~CV_UndistortTest() {} + +void CV_UndistortTest::generate3DPointCloud(vector& points, Point3f pmin, Point3f pmax) +{ + const Point3f delta = pmax - pmin; + for (size_t i = 0; i < points.size(); i++) + { + Point3f p(float(rand()) / RAND_MAX, float(rand()) / RAND_MAX, + float(rand()) / RAND_MAX); + p.x *= delta.x; + p.y *= delta.y; + p.z *= delta.z; + p = p + pmin; + points[i] = p; + } +} +void CV_UndistortTest::generateCameraMatrix(Mat& cameraMatrix) +{ + const double fcMinVal = 1e-3; + const double fcMaxVal = 100; + cameraMatrix.create(3, 3, CV_64FC1); + cameraMatrix.setTo(Scalar(0)); + cameraMatrix.at(0,0) = rng.uniform(fcMinVal, fcMaxVal); + cameraMatrix.at(1,1) = rng.uniform(fcMinVal, fcMaxVal); + cameraMatrix.at(0,2) = rng.uniform(fcMinVal, fcMaxVal); + cameraMatrix.at(1,2) = rng.uniform(fcMinVal, fcMaxVal); + cameraMatrix.at(2,2) = 1; +} +void CV_UndistortTest::generateDistCoeffs(Mat& distCoeffs, int count) +{ + distCoeffs = Mat::zeros(count, 1, CV_64FC1); + for (int i = 0; i < count; i++) + distCoeffs.at(i,0) = rng.uniform(0.0, 1.0e-3); +} + +void CV_UndistortTest::run(int /* start_from */) +{ + Mat intrinsics, distCoeffs; + generateCameraMatrix(intrinsics); + vector points(500); + generate3DPointCloud(points); + vector projectedPoints; + projectedPoints.resize(points.size()); + + int modelMembersCount[] = {4,5,8}; + for (int idx = 0; idx < 3; idx++) + { + generateDistCoeffs(distCoeffs, modelMembersCount[idx]); + projectPoints(Mat(points), Mat::zeros(3,1,CV_64FC1), Mat::zeros(3,1,CV_64FC1), intrinsics, distCoeffs, projectedPoints); + + vector realUndistortedPoints; + projectPoints(Mat(points), Mat::zeros(3,1,CV_64FC1), Mat::zeros(3,1,CV_64FC1), intrinsics, Mat::zeros(4,1,CV_64FC1), realUndistortedPoints); + + Mat undistortedPoints; + undistortPoints(Mat(projectedPoints), undistortedPoints, intrinsics, distCoeffs); + + Mat p; + perspectiveTransform(undistortedPoints, p, intrinsics); + undistortedPoints = p; + double diff = norm(Mat(realUndistortedPoints), undistortedPoints); + if (diff > thresh) + { + ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); + return; + } + ts->set_failed_test_info(cvtest::TS::OK); + } +} + +TEST(Calib3d_Undistort, accuracy) { CV_UndistortTest test; test.safe_run(); } \ No newline at end of file