Path: blob/master/modules/calib3d/test/test_cameracalibration_artificial.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, 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 "test_chessboardgenerator.hpp"4445namespace opencv_test { namespace {4647//template<class T> ostream& operator<<(ostream& out, const Mat_<T>& mat)48//{49// for(Mat_<T>::const_iterator pos = mat.begin(), end = mat.end(); pos != end; ++pos)50// out << *pos << " ";51// return out;52//}53//ostream& operator<<(ostream& out, const Mat& mat) { return out << Mat_<double>(mat); }5455Mat calcRvec(const vector<Point3f>& points, const Size& cornerSize)56{57Point3f p00 = points[0];58Point3f p10 = points[1];59Point3f p01 = points[cornerSize.width];6061Vec3d ex(p10.x - p00.x, p10.y - p00.y, p10.z - p00.z);62Vec3d ey(p01.x - p00.x, p01.y - p00.y, p01.z - p00.z);63Vec3d ez = ex.cross(ey);6465Mat rot(3, 3, CV_64F);66*rot.ptr<Vec3d>(0) = ex;67*rot.ptr<Vec3d>(1) = ey;68*rot.ptr<Vec3d>(2) = ez * (1.0/cv::norm(ez)); // TODO cvtest6970Mat res;71cvtest::Rodrigues(rot.t(), res);72return res.reshape(1, 1);73}7475class CV_CalibrateCameraArtificialTest : public cvtest::BaseTest76{77public:78CV_CalibrateCameraArtificialTest() :79r(0)80{81}82~CV_CalibrateCameraArtificialTest() {}83protected:84int r;8586const static int JUST_FIND_CORNERS = 0;87const static int USE_CORNERS_SUBPIX = 1;88const static int USE_4QUAD_CORNERS = 2;89const static int ARTIFICIAL_CORNERS = 4;909192bool checkErr(double a, double a0, double eps, double delta)93{94return fabs(a - a0) > eps * (fabs(a0) + delta);95}9697void compareCameraMatrs(const Mat_<double>& camMat, const Mat& camMat_est)98{99if ( camMat_est.at<double>(0, 1) != 0 || camMat_est.at<double>(1, 0) != 0 ||100camMat_est.at<double>(2, 0) != 0 || camMat_est.at<double>(2, 1) != 0 ||101camMat_est.at<double>(2, 2) != 1)102{103ts->printf( cvtest::TS::LOG, "Bad shape of camera matrix returned \n");104ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);105}106107double fx_e = camMat_est.at<double>(0, 0), fy_e = camMat_est.at<double>(1, 1);108double cx_e = camMat_est.at<double>(0, 2), cy_e = camMat_est.at<double>(1, 2);109110double fx = camMat(0, 0), fy = camMat(1, 1), cx = camMat(0, 2), cy = camMat(1, 2);111112const double eps = 1e-2;113const double dlt = 1e-5;114115bool fail = checkErr(fx_e, fx, eps, dlt) || checkErr(fy_e, fy, eps, dlt) ||116checkErr(cx_e, cx, eps, dlt) || checkErr(cy_e, cy, eps, dlt);117118if (fail)119{120ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);121}122ts->printf( cvtest::TS::LOG, "%d) Expected [Fx Fy Cx Cy] = [%.3f %.3f %.3f %.3f]\n", r, fx, fy, cx, cy);123ts->printf( cvtest::TS::LOG, "%d) Estimated [Fx Fy Cx Cy] = [%.3f %.3f %.3f %.3f]\n", r, fx_e, fy_e, cx_e, cy_e);124}125126void compareDistCoeffs(const Mat_<double>& distCoeffs, const Mat& distCoeffs_est)127{128const double *dt_e = distCoeffs_est.ptr<double>();129130double k1_e = dt_e[0], k2_e = dt_e[1], k3_e = dt_e[4];131double p1_e = dt_e[2], p2_e = dt_e[3];132133double k1 = distCoeffs(0, 0), k2 = distCoeffs(0, 1), k3 = distCoeffs(0, 4);134double p1 = distCoeffs(0, 2), p2 = distCoeffs(0, 3);135136const double eps = 5e-2;137const double dlt = 1e-3;138139const double eps_k3 = 5;140const double dlt_k3 = 1e-3;141142bool fail = checkErr(k1_e, k1, eps, dlt) || checkErr(k2_e, k2, eps, dlt) || checkErr(k3_e, k3, eps_k3, dlt_k3) ||143checkErr(p1_e, p1, eps, dlt) || checkErr(p2_e, p2, eps, dlt);144145if (fail)146{147// commented according to vp123's recomendation. TODO - improve accuaracy148//ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); ss149}150ts->printf( cvtest::TS::LOG, "%d) DistCoeff exp=(%.2f, %.2f, %.4f, %.4f %.2f)\n", r, k1, k2, p1, p2, k3);151ts->printf( cvtest::TS::LOG, "%d) DistCoeff est=(%.2f, %.2f, %.4f, %.4f %.2f)\n", r, k1_e, k2_e, p1_e, p2_e, k3_e);152ts->printf( cvtest::TS::LOG, "%d) AbsError = [%.5f %.5f %.5f %.5f %.5f]\n", r, fabs(k1-k1_e), fabs(k2-k2_e), fabs(p1-p1_e), fabs(p2-p2_e), fabs(k3-k3_e));153}154155void compareShiftVecs(const vector<Mat>& tvecs, const vector<Mat>& tvecs_est)156{157const double eps = 1e-2;158const double dlt = 1e-4;159160int err_count = 0;161const int errMsgNum = 4;162for(size_t i = 0; i < tvecs.size(); ++i)163{164const Point3d& tvec = *tvecs[i].ptr<Point3d>();165const Point3d& tvec_est = *tvecs_est[i].ptr<Point3d>();166167double n1 = cv::norm(tvec_est - tvec); // TODO cvtest168double n2 = cv::norm(tvec); // TODO cvtest169if (n1 > eps* (n2 + dlt))170{171if (err_count++ < errMsgNum)172{173if (err_count == errMsgNum)174ts->printf( cvtest::TS::LOG, "%d) ...\n", r);175else176{177ts->printf( cvtest::TS::LOG, "%d) Bad accuracy in returned tvecs. Index = %d\n", r, i);178ts->printf( cvtest::TS::LOG, "%d) norm(tvec_est - tvec) = %f, norm(tvec_exp) = %f \n", r, n1, n2);179}180}181ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);182}183}184}185186void compareRotationVecs(const vector<Mat>& rvecs, const vector<Mat>& rvecs_est)187{188const double eps = 2e-2;189const double dlt = 1e-4;190191Mat rmat, rmat_est;192int err_count = 0;193const int errMsgNum = 4;194for(size_t i = 0; i < rvecs.size(); ++i)195{196cvtest::Rodrigues(rvecs[i], rmat);197cvtest::Rodrigues(rvecs_est[i], rmat_est);198199if (cvtest::norm(rmat_est, rmat, NORM_L2) > eps* (cvtest::norm(rmat, NORM_L2) + dlt))200{201if (err_count++ < errMsgNum)202{203if (err_count == errMsgNum)204ts->printf( cvtest::TS::LOG, "%d) ...\n", r);205else206{207ts->printf( cvtest::TS::LOG, "%d) Bad accuracy in returned rvecs (rotation matrs). Index = %d\n", r, i);208ts->printf( cvtest::TS::LOG, "%d) norm(rot_mat_est - rot_mat_exp) = %f, norm(rot_mat_exp) = %f \n", r,209cvtest::norm(rmat_est, rmat, NORM_L2), cvtest::norm(rmat, NORM_L2));210211}212}213ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);214}215}216}217218double reprojectErrorWithoutIntrinsics(const vector<Point3f>& cb3d, const vector<Mat>& _rvecs_exp, const vector<Mat>& _tvecs_exp,219const vector<Mat>& rvecs_est, const vector<Mat>& tvecs_est)220{221const static Mat eye33 = Mat::eye(3, 3, CV_64F);222const static Mat zero15 = Mat::zeros(1, 5, CV_64F);223Mat _chessboard3D(cb3d);224vector<Point2f> uv_exp, uv_est;225double res = 0;226227for(size_t i = 0; i < rvecs_exp.size(); ++i)228{229projectPoints(_chessboard3D, _rvecs_exp[i], _tvecs_exp[i], eye33, zero15, uv_exp);230projectPoints(_chessboard3D, rvecs_est[i], tvecs_est[i], eye33, zero15, uv_est);231for(size_t j = 0; j < cb3d.size(); ++j)232res += cv::norm(uv_exp[i] - uv_est[i]); // TODO cvtest233}234return res;235}236237Size2f sqSile;238239vector<Point3f> chessboard3D;240vector<Mat> boards, rvecs_exp, tvecs_exp, rvecs_spnp, tvecs_spnp;241vector< vector<Point3f> > objectPoints;242vector< vector<Point2f> > imagePoints_art;243vector< vector<Point2f> > imagePoints_findCb;244245246void prepareForTest(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, size_t brdsNum, const ChessBoardGenerator& cbg)247{248sqSile = Size2f(1.f, 1.f);249Size cornersSize = cbg.cornersSize();250251chessboard3D.clear();252for(int j = 0; j < cornersSize.height; ++j)253for(int i = 0; i < cornersSize.width; ++i)254chessboard3D.push_back(Point3f(sqSile.width * i, sqSile.height * j, 0));255256boards.resize(brdsNum);257rvecs_exp.resize(brdsNum);258tvecs_exp.resize(brdsNum);259objectPoints.clear();260objectPoints.resize(brdsNum, chessboard3D);261imagePoints_art.clear();262imagePoints_findCb.clear();263264vector<Point2f> corners_art, corners_fcb;265for(size_t i = 0; i < brdsNum; ++i)266{267for(;;)268{269boards[i] = cbg(bg, camMat, distCoeffs, sqSile, corners_art);270if(findChessboardCorners(boards[i], cornersSize, corners_fcb))271break;272}273274//cv::namedWindow("CB"); imshow("CB", boards[i]); cv::waitKey();275276imagePoints_art.push_back(corners_art);277imagePoints_findCb.push_back(corners_fcb);278279tvecs_exp[i].create(1, 3, CV_64F);280*tvecs_exp[i].ptr<Point3d>() = cbg.corners3d[0];281rvecs_exp[i] = calcRvec(cbg.corners3d, cbg.cornersSize());282}283284}285286void runTest(const Size& imgSize, const Mat_<double>& camMat, const Mat_<double>& distCoeffs, size_t brdsNum, const Size& cornersSize, int flag = 0)287{288const TermCriteria tc(TermCriteria::EPS|TermCriteria::MAX_ITER, 30, 0.1);289290vector< vector<Point2f> > imagePoints;291292switch(flag)293{294case JUST_FIND_CORNERS: imagePoints = imagePoints_findCb; break;295case ARTIFICIAL_CORNERS: imagePoints = imagePoints_art; break;296297case USE_CORNERS_SUBPIX:298for(size_t i = 0; i < brdsNum; ++i)299{300Mat gray;301cvtColor(boards[i], gray, COLOR_BGR2GRAY);302vector<Point2f> tmp = imagePoints_findCb[i];303cornerSubPix(gray, tmp, Size(5, 5), Size(-1,-1), tc);304imagePoints.push_back(tmp);305}306break;307case USE_4QUAD_CORNERS:308for(size_t i = 0; i < brdsNum; ++i)309{310Mat gray;311cvtColor(boards[i], gray, COLOR_BGR2GRAY);312vector<Point2f> tmp = imagePoints_findCb[i];313find4QuadCornerSubpix(gray, tmp, Size(5, 5));314imagePoints.push_back(tmp);315}316break;317default:318throw std::exception();319}320321Mat camMat_est = Mat::eye(3, 3, CV_64F), distCoeffs_est = Mat::zeros(1, 5, CV_64F);322vector<Mat> rvecs_est, tvecs_est;323324int flags = /*CALIB_FIX_K3|*/CALIB_FIX_K4|CALIB_FIX_K5|CALIB_FIX_K6; //CALIB_FIX_K3; //CALIB_FIX_ASPECT_RATIO | | CALIB_ZERO_TANGENT_DIST;325TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 100, DBL_EPSILON);326double rep_error = calibrateCamera(objectPoints, imagePoints, imgSize, camMat_est, distCoeffs_est, rvecs_est, tvecs_est, flags, criteria);327rep_error /= brdsNum * cornersSize.area();328329const double thres = 1;330if (rep_error > thres)331{332ts->printf( cvtest::TS::LOG, "%d) Too big reproject error = %f\n", r, rep_error);333ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);334}335336compareCameraMatrs(camMat, camMat_est);337compareDistCoeffs(distCoeffs, distCoeffs_est);338compareShiftVecs(tvecs_exp, tvecs_est);339compareRotationVecs(rvecs_exp, rvecs_est);340341double rep_errorWOI = reprojectErrorWithoutIntrinsics(chessboard3D, rvecs_exp, tvecs_exp, rvecs_est, tvecs_est);342rep_errorWOI /= brdsNum * cornersSize.area();343344const double thres2 = 0.01;345if (rep_errorWOI > thres2)346{347ts->printf( cvtest::TS::LOG, "%d) Too big reproject error without intrinsics = %f\n", r, rep_errorWOI);348ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);349}350351ts->printf( cvtest::TS::LOG, "%d) Testing solvePnP...\n", r);352rvecs_spnp.resize(brdsNum);353tvecs_spnp.resize(brdsNum);354for(size_t i = 0; i < brdsNum; ++i)355solvePnP(Mat(objectPoints[i]), Mat(imagePoints[i]), camMat, distCoeffs, rvecs_spnp[i], tvecs_spnp[i]);356357compareShiftVecs(tvecs_exp, tvecs_spnp);358compareRotationVecs(rvecs_exp, rvecs_spnp);359}360361void run(int)362{363364ts->set_failed_test_info(cvtest::TS::OK);365RNG& rng = theRNG();366367int progress = 0;368int repeat_num = 3;369for(r = 0; r < repeat_num; ++r)370{371const int brds_num = 20;372373Mat bg(Size(640, 480), CV_8UC3);374randu(bg, Scalar::all(32), Scalar::all(255));375GaussianBlur(bg, bg, Size(5, 5), 2);376377double fx = 300 + (20 * (double)rng - 10);378double fy = 300 + (20 * (double)rng - 10);379380double cx = bg.cols/2 + (40 * (double)rng - 20);381double cy = bg.rows/2 + (40 * (double)rng - 20);382383Mat_<double> camMat(3, 3);384camMat << fx, 0., cx, 0, fy, cy, 0., 0., 1.;385386double k1 = 0.5 + (double)rng/5;387double k2 = (double)rng/5;388double k3 = (double)rng/5;389390double p1 = 0.001 + (double)rng/10;391double p2 = 0.001 + (double)rng/10;392393Mat_<double> distCoeffs(1, 5, 0.0);394distCoeffs << k1, k2, p1, p2, k3;395396ChessBoardGenerator cbg(Size(9, 8));397cbg.min_cos = 0.9;398cbg.cov = 0.8;399400progress = update_progress(progress, r, repeat_num, 0);401ts->printf( cvtest::TS::LOG, "\n");402prepareForTest(bg, camMat, distCoeffs, brds_num, cbg);403404ts->printf( cvtest::TS::LOG, "artificial corners\n");405runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), ARTIFICIAL_CORNERS);406progress = update_progress(progress, r, repeat_num, 0);407408ts->printf( cvtest::TS::LOG, "findChessboard corners\n");409runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), JUST_FIND_CORNERS);410progress = update_progress(progress, r, repeat_num, 0);411412ts->printf( cvtest::TS::LOG, "cornersSubPix corners\n");413runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), USE_CORNERS_SUBPIX);414progress = update_progress(progress, r, repeat_num, 0);415416ts->printf( cvtest::TS::LOG, "4quad corners\n");417runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), USE_4QUAD_CORNERS);418progress = update_progress(progress, r, repeat_num, 0);419}420}421};422423TEST(Calib3d_CalibrateCamera_CPP, DISABLED_accuracy_on_artificial_data) { CV_CalibrateCameraArtificialTest test; test.safe_run(); }424425}} // namespace426427428