Path: blob/master/modules/calib3d/test/test_fisheye.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"43#include <opencv2/ts/cuda_test.hpp> // EXPECT_MAT_NEAR44#include "../src/fisheye.hpp"45#include "opencv2/videoio.hpp"4647namespace opencv_test { namespace {4849class fisheyeTest : public ::testing::Test {5051protected:52const static cv::Size imageSize;53const static cv::Matx33d K;54const static cv::Vec4d D;55const static cv::Matx33d R;56const static cv::Vec3d T;57std::string datasets_repository_path;5859virtual void SetUp() {60datasets_repository_path = combine(cvtest::TS::ptr()->get_data_path(), "cv/cameracalibration/fisheye");61}6263protected:64std::string combine(const std::string& _item1, const std::string& _item2);65static void merge4(const cv::Mat& tl, const cv::Mat& tr, const cv::Mat& bl, const cv::Mat& br, cv::Mat& merged);66};6768////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////69/// TESTS::7071TEST_F(fisheyeTest, projectPoints)72{73double cols = this->imageSize.width,74rows = this->imageSize.height;7576const int N = 20;77cv::Mat distorted0(1, N*N, CV_64FC2), undist1, undist2, distorted1, distorted2;78undist2.create(distorted0.size(), CV_MAKETYPE(distorted0.depth(), 3));79cv::Vec2d* pts = distorted0.ptr<cv::Vec2d>();8081cv::Vec2d c(this->K(0, 2), this->K(1, 2));82for(int y = 0, k = 0; y < N; ++y)83for(int x = 0; x < N; ++x)84{85cv::Vec2d point(x*cols/(N-1.f), y*rows/(N-1.f));86pts[k++] = (point - c) * 0.85 + c;87}8889cv::fisheye::undistortPoints(distorted0, undist1, this->K, this->D);9091cv::Vec2d* u1 = undist1.ptr<cv::Vec2d>();92cv::Vec3d* u2 = undist2.ptr<cv::Vec3d>();93for(int i = 0; i < (int)distorted0.total(); ++i)94u2[i] = cv::Vec3d(u1[i][0], u1[i][1], 1.0);9596cv::fisheye::distortPoints(undist1, distorted1, this->K, this->D);97cv::fisheye::projectPoints(undist2, distorted2, cv::Vec3d::all(0), cv::Vec3d::all(0), this->K, this->D);9899EXPECT_MAT_NEAR(distorted0, distorted1, 1e-10);100EXPECT_MAT_NEAR(distorted0, distorted2, 1e-10);101}102103TEST_F(fisheyeTest, DISABLED_undistortImage)104{105cv::Matx33d theK = this->K;106cv::Mat theD = cv::Mat(this->D);107std::string file = combine(datasets_repository_path, "/calib-3_stereo_from_JY/left/stereo_pair_014.jpg");108cv::Matx33d newK = theK;109cv::Mat distorted = cv::imread(file), undistorted;110{111newK(0, 0) = 100;112newK(1, 1) = 100;113cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);114cv::Mat correct = cv::imread(combine(datasets_repository_path, "new_f_100.png"));115if (correct.empty())116CV_Assert(cv::imwrite(combine(datasets_repository_path, "new_f_100.png"), undistorted));117else118EXPECT_MAT_NEAR(correct, undistorted, 1e-10);119}120{121double balance = 1.0;122cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);123cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);124cv::Mat correct = cv::imread(combine(datasets_repository_path, "balance_1.0.png"));125if (correct.empty())126CV_Assert(cv::imwrite(combine(datasets_repository_path, "balance_1.0.png"), undistorted));127else128EXPECT_MAT_NEAR(correct, undistorted, 1e-10);129}130131{132double balance = 0.0;133cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);134cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);135cv::Mat correct = cv::imread(combine(datasets_repository_path, "balance_0.0.png"));136if (correct.empty())137CV_Assert(cv::imwrite(combine(datasets_repository_path, "balance_0.0.png"), undistorted));138else139EXPECT_MAT_NEAR(correct, undistorted, 1e-10);140}141}142143TEST_F(fisheyeTest, jacobians)144{145int n = 10;146cv::Mat X(1, n, CV_64FC3);147cv::Mat om(3, 1, CV_64F), theT(3, 1, CV_64F);148cv::Mat f(2, 1, CV_64F), c(2, 1, CV_64F);149cv::Mat k(4, 1, CV_64F);150double alpha;151152cv::RNG r;153154r.fill(X, cv::RNG::NORMAL, 2, 1);155X = cv::abs(X) * 10;156157r.fill(om, cv::RNG::NORMAL, 0, 1);158om = cv::abs(om);159160r.fill(theT, cv::RNG::NORMAL, 0, 1);161theT = cv::abs(theT); theT.at<double>(2) = 4; theT *= 10;162163r.fill(f, cv::RNG::NORMAL, 0, 1);164f = cv::abs(f) * 1000;165166r.fill(c, cv::RNG::NORMAL, 0, 1);167c = cv::abs(c) * 1000;168169r.fill(k, cv::RNG::NORMAL, 0, 1);170k*= 0.5;171172alpha = 0.01*r.gaussian(1);173174cv::Mat x1, x2, xpred;175cv::Matx33d theK(f.at<double>(0), alpha * f.at<double>(0), c.at<double>(0),1760, f.at<double>(1), c.at<double>(1),1770, 0, 1);178179cv::Mat jacobians;180cv::fisheye::projectPoints(X, x1, om, theT, theK, k, alpha, jacobians);181182//test on T:183cv::Mat dT(3, 1, CV_64FC1);184r.fill(dT, cv::RNG::NORMAL, 0, 1);185dT *= 1e-9*cv::norm(theT);186cv::Mat T2 = theT + dT;187cv::fisheye::projectPoints(X, x2, om, T2, theK, k, alpha, cv::noArray());188xpred = x1 + cv::Mat(jacobians.colRange(11,14) * dT).reshape(2, 1);189CV_Assert (cv::norm(x2 - xpred) < 1e-10);190191//test on om:192cv::Mat dom(3, 1, CV_64FC1);193r.fill(dom, cv::RNG::NORMAL, 0, 1);194dom *= 1e-9*cv::norm(om);195cv::Mat om2 = om + dom;196cv::fisheye::projectPoints(X, x2, om2, theT, theK, k, alpha, cv::noArray());197xpred = x1 + cv::Mat(jacobians.colRange(8,11) * dom).reshape(2, 1);198CV_Assert (cv::norm(x2 - xpred) < 1e-10);199200//test on f:201cv::Mat df(2, 1, CV_64FC1);202r.fill(df, cv::RNG::NORMAL, 0, 1);203df *= 1e-9*cv::norm(f);204cv::Matx33d K2 = theK + cv::Matx33d(df.at<double>(0), df.at<double>(0) * alpha, 0, 0, df.at<double>(1), 0, 0, 0, 0);205cv::fisheye::projectPoints(X, x2, om, theT, K2, k, alpha, cv::noArray());206xpred = x1 + cv::Mat(jacobians.colRange(0,2) * df).reshape(2, 1);207CV_Assert (cv::norm(x2 - xpred) < 1e-10);208209//test on c:210cv::Mat dc(2, 1, CV_64FC1);211r.fill(dc, cv::RNG::NORMAL, 0, 1);212dc *= 1e-9*cv::norm(c);213K2 = theK + cv::Matx33d(0, 0, dc.at<double>(0), 0, 0, dc.at<double>(1), 0, 0, 0);214cv::fisheye::projectPoints(X, x2, om, theT, K2, k, alpha, cv::noArray());215xpred = x1 + cv::Mat(jacobians.colRange(2,4) * dc).reshape(2, 1);216CV_Assert (cv::norm(x2 - xpred) < 1e-10);217218//test on k:219cv::Mat dk(4, 1, CV_64FC1);220r.fill(dk, cv::RNG::NORMAL, 0, 1);221dk *= 1e-9*cv::norm(k);222cv::Mat k2 = k + dk;223cv::fisheye::projectPoints(X, x2, om, theT, theK, k2, alpha, cv::noArray());224xpred = x1 + cv::Mat(jacobians.colRange(4,8) * dk).reshape(2, 1);225CV_Assert (cv::norm(x2 - xpred) < 1e-10);226227//test on alpha:228cv::Mat dalpha(1, 1, CV_64FC1);229r.fill(dalpha, cv::RNG::NORMAL, 0, 1);230dalpha *= 1e-9*cv::norm(f);231double alpha2 = alpha + dalpha.at<double>(0);232K2 = theK + cv::Matx33d(0, f.at<double>(0) * dalpha.at<double>(0), 0, 0, 0, 0, 0, 0, 0);233cv::fisheye::projectPoints(X, x2, om, theT, theK, k, alpha2, cv::noArray());234xpred = x1 + cv::Mat(jacobians.col(14) * dalpha).reshape(2, 1);235CV_Assert (cv::norm(x2 - xpred) < 1e-10);236}237238TEST_F(fisheyeTest, Calibration)239{240const int n_images = 34;241242std::vector<std::vector<cv::Point2d> > imagePoints(n_images);243std::vector<std::vector<cv::Point3d> > objectPoints(n_images);244245const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");246cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);247CV_Assert(fs_left.isOpened());248for(int i = 0; i < n_images; ++i)249fs_left[cv::format("image_%d", i )] >> imagePoints[i];250fs_left.release();251252cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);253CV_Assert(fs_object.isOpened());254for(int i = 0; i < n_images; ++i)255fs_object[cv::format("image_%d", i )] >> objectPoints[i];256fs_object.release();257258int flag = 0;259flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;260flag |= cv::fisheye::CALIB_CHECK_COND;261flag |= cv::fisheye::CALIB_FIX_SKEW;262263cv::Matx33d theK;264cv::Vec4d theD;265266cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,267cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));268269EXPECT_MAT_NEAR(theK, this->K, 1e-10);270EXPECT_MAT_NEAR(theD, this->D, 1e-10);271}272273TEST_F(fisheyeTest, Homography)274{275const int n_images = 1;276277std::vector<std::vector<cv::Point2d> > imagePoints(n_images);278std::vector<std::vector<cv::Point3d> > objectPoints(n_images);279280const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");281cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);282CV_Assert(fs_left.isOpened());283for(int i = 0; i < n_images; ++i)284fs_left[cv::format("image_%d", i )] >> imagePoints[i];285fs_left.release();286287cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);288CV_Assert(fs_object.isOpened());289for(int i = 0; i < n_images; ++i)290fs_object[cv::format("image_%d", i )] >> objectPoints[i];291fs_object.release();292293cv::internal::IntrinsicParams param;294param.Init(cv::Vec2d(cv::max(imageSize.width, imageSize.height) / CV_PI, cv::max(imageSize.width, imageSize.height) / CV_PI),295cv::Vec2d(imageSize.width / 2.0 - 0.5, imageSize.height / 2.0 - 0.5));296297cv::Mat _imagePoints (imagePoints[0]);298cv::Mat _objectPoints(objectPoints[0]);299300cv::Mat imagePointsNormalized = NormalizePixels(_imagePoints, param).reshape(1).t();301_objectPoints = _objectPoints.reshape(1).t();302cv::Mat objectPointsMean, covObjectPoints;303304int Np = imagePointsNormalized.cols;305cv::calcCovarMatrix(_objectPoints, covObjectPoints, objectPointsMean, cv::COVAR_NORMAL | cv::COVAR_COLS);306cv::SVD svd(covObjectPoints);307cv::Mat theR(svd.vt);308309if (cv::norm(theR(cv::Rect(2, 0, 1, 2))) < 1e-6)310theR = cv::Mat::eye(3,3, CV_64FC1);311if (cv::determinant(theR) < 0)312theR = -theR;313314cv::Mat theT = -theR * objectPointsMean;315cv::Mat X_new = theR * _objectPoints + theT * cv::Mat::ones(1, Np, CV_64FC1);316cv::Mat H = cv::internal::ComputeHomography(imagePointsNormalized, X_new.rowRange(0, 2));317318cv::Mat M = cv::Mat::ones(3, X_new.cols, CV_64FC1);319X_new.rowRange(0, 2).copyTo(M.rowRange(0, 2));320cv::Mat mrep = H * M;321322cv::divide(mrep, cv::Mat::ones(3,1, CV_64FC1) * mrep.row(2).clone(), mrep);323324cv::Mat merr = (mrep.rowRange(0, 2) - imagePointsNormalized).t();325326cv::Vec2d std_err;327cv::meanStdDev(merr.reshape(2), cv::noArray(), std_err);328std_err *= sqrt((double)merr.reshape(2).total() / (merr.reshape(2).total() - 1));329330cv::Vec2d correct_std_err(0.00516740156010384, 0.00644205331553901);331EXPECT_MAT_NEAR(std_err, correct_std_err, 1e-12);332}333334TEST_F(fisheyeTest, EstimateUncertainties)335{336const int n_images = 34;337338std::vector<std::vector<cv::Point2d> > imagePoints(n_images);339std::vector<std::vector<cv::Point3d> > objectPoints(n_images);340341const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");342cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);343CV_Assert(fs_left.isOpened());344for(int i = 0; i < n_images; ++i)345fs_left[cv::format("image_%d", i )] >> imagePoints[i];346fs_left.release();347348cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);349CV_Assert(fs_object.isOpened());350for(int i = 0; i < n_images; ++i)351fs_object[cv::format("image_%d", i )] >> objectPoints[i];352fs_object.release();353354int flag = 0;355flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;356flag |= cv::fisheye::CALIB_CHECK_COND;357flag |= cv::fisheye::CALIB_FIX_SKEW;358359cv::Matx33d theK;360cv::Vec4d theD;361std::vector<cv::Vec3d> rvec;362std::vector<cv::Vec3d> tvec;363364cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,365rvec, tvec, flag, cv::TermCriteria(3, 20, 1e-6));366367cv::internal::IntrinsicParams param, errors;368cv::Vec2d err_std;369double thresh_cond = 1e6;370int check_cond = 1;371param.Init(cv::Vec2d(theK(0,0), theK(1,1)), cv::Vec2d(theK(0,2), theK(1, 2)), theD);372param.isEstimate = std::vector<uchar>(9, 1);373param.isEstimate[4] = 0;374375errors.isEstimate = param.isEstimate;376377double rms;378379cv::internal::EstimateUncertainties(objectPoints, imagePoints, param, rvec, tvec,380errors, err_std, thresh_cond, check_cond, rms);381382EXPECT_MAT_NEAR(errors.f, cv::Vec2d(1.29837104202046, 1.31565641071524), 1e-10);383EXPECT_MAT_NEAR(errors.c, cv::Vec2d(0.890439368129246, 0.816096854937896), 1e-10);384EXPECT_MAT_NEAR(errors.k, cv::Vec4d(0.00516248605191506, 0.0168181467500934, 0.0213118690274604, 0.00916010877545648), 1e-10);385EXPECT_MAT_NEAR(err_std, cv::Vec2d(0.187475975266883, 0.185678953263995), 1e-10);386CV_Assert(fabs(rms - 0.263782587133546) < 1e-10);387CV_Assert(errors.alpha == 0);388}389390TEST_F(fisheyeTest, stereoRectify)391{392const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");393394cv::Size calibration_size = this->imageSize, requested_size = calibration_size;395cv::Matx33d K1 = this->K, K2 = K1;396cv::Mat D1 = cv::Mat(this->D), D2 = D1;397398cv::Vec3d theT = this->T;399cv::Matx33d theR = this->R;400401double balance = 0.0, fov_scale = 1.1;402cv::Mat R1, R2, P1, P2, Q;403cv::fisheye::stereoRectify(K1, D1, K2, D2, calibration_size, theR, theT, R1, R2, P1, P2, Q,404cv::CALIB_ZERO_DISPARITY, requested_size, balance, fov_scale);405406// Collected with these CMake flags: -DWITH_IPP=OFF -DCV_ENABLE_INTRINSICS=OFF -DCV_DISABLE_OPTIMIZATION=ON -DCMAKE_BUILD_TYPE=Debug407cv::Matx33d R1_ref(4080.9992853269091279, 0.03779164101000276, -0.0007920188690205426,409-0.03778569762983931, 0.9992646472015868, 0.006511981857667881,4100.001037534936357442, -0.006477400933964018, 0.9999784831677112411);412cv::Matx33d R2_ref(4130.9994868963898833, -0.03197579751378937, -0.001868774538573449,4140.03196298186616116, 0.9994677442608699, -0.0065265589947392,4150.002076471801477729, 0.006463478587068991, 0.9999769555891836416);417cv::Matx34d P1_ref(418420.8551870450913, 0, 586.501617798451, 0,4190, 420.8551870450913, 374.7667511986098, 0,4200, 0, 1, 0421);422cv::Matx34d P2_ref(423420.8551870450913, 0, 586.501617798451, -41.77758076597302,4240, 420.8551870450913, 374.7667511986098, 0,4250, 0, 1, 0426);427cv::Matx44d Q_ref(4281, 0, 0, -586.501617798451,4290, 1, 0, -374.7667511986098,4300, 0, 0, 420.8551870450913,4310, 0, 10.07370889670733, -0432);433434const double eps = 1e-10;435EXPECT_MAT_NEAR(R1_ref, R1, eps);436EXPECT_MAT_NEAR(R2_ref, R2, eps);437EXPECT_MAT_NEAR(P1_ref, P1, eps);438EXPECT_MAT_NEAR(P2_ref, P2, eps);439EXPECT_MAT_NEAR(Q_ref, Q, eps);440441if (::testing::Test::HasFailure())442{443std::cout << "Actual values are:" << std::endl444<< "R1 =" << std::endl << R1 << std::endl445<< "R2 =" << std::endl << R2 << std::endl446<< "P1 =" << std::endl << P1 << std::endl447<< "P2 =" << std::endl << P2 << std::endl448<< "Q =" << std::endl << Q << std::endl;449}450451#if 1 // Debug code452cv::Mat lmapx, lmapy, rmapx, rmapy;453//rewrite for fisheye454cv::fisheye::initUndistortRectifyMap(K1, D1, R1, P1, requested_size, CV_32F, lmapx, lmapy);455cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, requested_size, CV_32F, rmapx, rmapy);456457cv::Mat l, r, lundist, rundist;458for (int i = 0; i < 34; ++i)459{460SCOPED_TRACE(cv::format("image %d", i));461l = imread(combine(folder, cv::format("left/stereo_pair_%03d.jpg", i)), cv::IMREAD_COLOR);462r = imread(combine(folder, cv::format("right/stereo_pair_%03d.jpg", i)), cv::IMREAD_COLOR);463ASSERT_FALSE(l.empty());464ASSERT_FALSE(r.empty());465466int ndisp = 128;467cv::rectangle(l, cv::Rect(255, 0, 829, l.rows-1), cv::Scalar(0, 0, 255));468cv::rectangle(r, cv::Rect(255, 0, 829, l.rows-1), cv::Scalar(0, 0, 255));469cv::rectangle(r, cv::Rect(255-ndisp, 0, 829+ndisp ,l.rows-1), cv::Scalar(0, 0, 255));470cv::remap(l, lundist, lmapx, lmapy, cv::INTER_LINEAR);471cv::remap(r, rundist, rmapx, rmapy, cv::INTER_LINEAR);472473for (int ii = 0; ii < lundist.rows; ii += 20)474{475cv::line(lundist, cv::Point(0, ii), cv::Point(lundist.cols, ii), cv::Scalar(0, 255, 0));476cv::line(rundist, cv::Point(0, ii), cv::Point(lundist.cols, ii), cv::Scalar(0, 255, 0));477}478479cv::Mat rectification;480merge4(l, r, lundist, rundist, rectification);481482cv::imwrite(cv::format("fisheye_rectification_AB_%03d.png", i), rectification);483}484#endif485}486487TEST_F(fisheyeTest, stereoCalibrate)488{489const int n_images = 34;490491const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");492493std::vector<std::vector<cv::Point2d> > leftPoints(n_images);494std::vector<std::vector<cv::Point2d> > rightPoints(n_images);495std::vector<std::vector<cv::Point3d> > objectPoints(n_images);496497cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);498CV_Assert(fs_left.isOpened());499for(int i = 0; i < n_images; ++i)500fs_left[cv::format("image_%d", i )] >> leftPoints[i];501fs_left.release();502503cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);504CV_Assert(fs_right.isOpened());505for(int i = 0; i < n_images; ++i)506fs_right[cv::format("image_%d", i )] >> rightPoints[i];507fs_right.release();508509cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);510CV_Assert(fs_object.isOpened());511for(int i = 0; i < n_images; ++i)512fs_object[cv::format("image_%d", i )] >> objectPoints[i];513fs_object.release();514515cv::Matx33d K1, K2, theR;516cv::Vec3d theT;517cv::Vec4d D1, D2;518519int flag = 0;520flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;521flag |= cv::fisheye::CALIB_CHECK_COND;522flag |= cv::fisheye::CALIB_FIX_SKEW;523524cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,525K1, D1, K2, D2, imageSize, theR, theT, flag,526cv::TermCriteria(3, 12, 0));527528cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,529-0.06956823121068059, 0.9975601387249519, 0.005833595226966235,530-0.006071257768382089, -0.006271040135405457, 0.9999619062167968);531cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);532cv::Matx33d K1_correct (561.195925927249, 0, 621.282400272412,5330, 562.849402029712, 380.555455380889,5340, 0, 1);535536cv::Matx33d K2_correct (560.395452535348, 0, 678.971652040359,5370, 561.90171021422, 380.401340535339,5380, 0, 1);539540cv::Vec4d D1_correct (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);541cv::Vec4d D2_correct (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);542543EXPECT_MAT_NEAR(theR, R_correct, 1e-10);544EXPECT_MAT_NEAR(theT, T_correct, 1e-10);545546EXPECT_MAT_NEAR(K1, K1_correct, 1e-10);547EXPECT_MAT_NEAR(K2, K2_correct, 1e-10);548549EXPECT_MAT_NEAR(D1, D1_correct, 1e-10);550EXPECT_MAT_NEAR(D2, D2_correct, 1e-10);551552}553554TEST_F(fisheyeTest, stereoCalibrateFixIntrinsic)555{556const int n_images = 34;557558const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");559560std::vector<std::vector<cv::Point2d> > leftPoints(n_images);561std::vector<std::vector<cv::Point2d> > rightPoints(n_images);562std::vector<std::vector<cv::Point3d> > objectPoints(n_images);563564cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);565CV_Assert(fs_left.isOpened());566for(int i = 0; i < n_images; ++i)567fs_left[cv::format("image_%d", i )] >> leftPoints[i];568fs_left.release();569570cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);571CV_Assert(fs_right.isOpened());572for(int i = 0; i < n_images; ++i)573fs_right[cv::format("image_%d", i )] >> rightPoints[i];574fs_right.release();575576cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);577CV_Assert(fs_object.isOpened());578for(int i = 0; i < n_images; ++i)579fs_object[cv::format("image_%d", i )] >> objectPoints[i];580fs_object.release();581582cv::Matx33d theR;583cv::Vec3d theT;584585int flag = 0;586flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;587flag |= cv::fisheye::CALIB_CHECK_COND;588flag |= cv::fisheye::CALIB_FIX_SKEW;589flag |= cv::fisheye::CALIB_FIX_INTRINSIC;590591cv::Matx33d K1 (561.195925927249, 0, 621.282400272412,5920, 562.849402029712, 380.555455380889,5930, 0, 1);594595cv::Matx33d K2 (560.395452535348, 0, 678.971652040359,5960, 561.90171021422, 380.401340535339,5970, 0, 1);598599cv::Vec4d D1 (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);600cv::Vec4d D2 (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);601602cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,603K1, D1, K2, D2, imageSize, theR, theT, flag,604cv::TermCriteria(3, 12, 0));605606cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,607-0.06956823121068059, 0.9975601387249519, 0.005833595226966235,608-0.006071257768382089, -0.006271040135405457, 0.9999619062167968);609cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);610611612EXPECT_MAT_NEAR(theR, R_correct, 1e-10);613EXPECT_MAT_NEAR(theT, T_correct, 1e-10);614}615616TEST_F(fisheyeTest, CalibrationWithDifferentPointsNumber)617{618const int n_images = 2;619620std::vector<std::vector<cv::Point2d> > imagePoints(n_images);621std::vector<std::vector<cv::Point3d> > objectPoints(n_images);622623std::vector<cv::Point2d> imgPoints1(10);624std::vector<cv::Point2d> imgPoints2(15);625626std::vector<cv::Point3d> objectPoints1(imgPoints1.size());627std::vector<cv::Point3d> objectPoints2(imgPoints2.size());628629for (size_t i = 0; i < imgPoints1.size(); i++)630{631imgPoints1[i] = cv::Point2d((double)i, (double)i);632objectPoints1[i] = cv::Point3d((double)i, (double)i, 10.0);633}634635for (size_t i = 0; i < imgPoints2.size(); i++)636{637imgPoints2[i] = cv::Point2d(i + 0.5, i + 0.5);638objectPoints2[i] = cv::Point3d(i + 0.5, i + 0.5, 10.0);639}640641imagePoints[0] = imgPoints1;642imagePoints[1] = imgPoints2;643objectPoints[0] = objectPoints1;644objectPoints[1] = objectPoints2;645646cv::Matx33d theK = cv::Matx33d::eye();647cv::Vec4d theD;648649int flag = 0;650flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;651flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS;652flag |= cv::fisheye::CALIB_FIX_SKEW;653654cv::fisheye::calibrate(objectPoints, imagePoints, cv::Size(100, 100), theK, theD,655cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));656}657658////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////659/// fisheyeTest::660661const cv::Size fisheyeTest::imageSize(1280, 800);662663const cv::Matx33d fisheyeTest::K(558.478087865323, 0, 620.458515360843,6640, 560.506767351568, 381.939424848348,6650, 0, 1);666667const cv::Vec4d fisheyeTest::D(-0.0014613319981768, -0.00329861110580401, 0.00605760088590183, -0.00374209380722371);668669const cv::Matx33d fisheyeTest::R ( 9.9756700084424932e-01, 6.9698277640183867e-02, 1.4929569991321144e-03,670-6.9711825162322980e-02, 9.9748249845531767e-01, 1.2997180766418455e-02,671-5.8331736398316541e-04,-1.3069635393884985e-02, 9.9991441852366736e-01);672673const cv::Vec3d fisheyeTest::T(-9.9217369356044638e-02, 3.1741831972356663e-03, 1.8551007952921010e-04);674675std::string fisheyeTest::combine(const std::string& _item1, const std::string& _item2)676{677std::string item1 = _item1, item2 = _item2;678std::replace(item1.begin(), item1.end(), '\\', '/');679std::replace(item2.begin(), item2.end(), '\\', '/');680681if (item1.empty())682return item2;683684if (item2.empty())685return item1;686687char last = item1[item1.size()-1];688return item1 + (last != '/' ? "/" : "") + item2;689}690691void fisheyeTest::merge4(const cv::Mat& tl, const cv::Mat& tr, const cv::Mat& bl, const cv::Mat& br, cv::Mat& merged)692{693int type = tl.type();694cv::Size sz = tl.size();695ASSERT_EQ(type, tr.type()); ASSERT_EQ(type, bl.type()); ASSERT_EQ(type, br.type());696ASSERT_EQ(sz.width, tr.cols); ASSERT_EQ(sz.width, bl.cols); ASSERT_EQ(sz.width, br.cols);697ASSERT_EQ(sz.height, tr.rows); ASSERT_EQ(sz.height, bl.rows); ASSERT_EQ(sz.height, br.rows);698699merged.create(cv::Size(sz.width * 2, sz.height * 2), type);700tl.copyTo(merged(cv::Rect(0, 0, sz.width, sz.height)));701tr.copyTo(merged(cv::Rect(sz.width, 0, sz.width, sz.height)));702bl.copyTo(merged(cv::Rect(0, sz.height, sz.width, sz.height)));703br.copyTo(merged(cv::Rect(sz.width, sz.height, sz.width, sz.height)));704}705706}} // namespace707708709