opencv/modules/calib3d/src/solvepnp.cpp

249 lines
9.6 KiB
C++
Raw Normal View History

2011-03-05 08:18:49 +08:00
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
2011-03-05 08:18:49 +08:00
#include "precomp.hpp"
2014-07-16 06:06:29 +08:00
#include "dls.h"
#include "epnp.h"
#include "p3p.h"
#include "opencv2/calib3d/calib3d_c.h"
#include <iostream>
2011-03-05 08:18:49 +08:00
using namespace cv;
bool cv::solvePnP( InputArray _opoints, InputArray _ipoints,
InputArray _cameraMatrix, InputArray _distCoeffs,
OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess, int flags )
2011-03-05 08:18:49 +08:00
{
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat();
int npoints = std::max(opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
2012-06-08 01:21:29 +08:00
CV_Assert( npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) );
_rvec.create(3, 1, CV_64F);
_tvec.create(3, 1, CV_64F);
Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat();
if (flags == EPNP)
{
2012-06-08 01:21:29 +08:00
cv::Mat undistortedPoints;
cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
epnp PnP(cameraMatrix, opoints, undistortedPoints);
cv::Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat();
PnP.compute_pose(R, tvec);
cv::Rodrigues(R, rvec);
2012-06-08 01:21:29 +08:00
return true;
}
else if (flags == P3P)
2012-06-08 01:21:29 +08:00
{
CV_Assert( npoints == 4);
cv::Mat undistortedPoints;
cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
p3p P3Psolver(cameraMatrix);
cv::Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat();
bool result = P3Psolver.solve(R, tvec, opoints, undistortedPoints);
if (result)
2012-06-08 01:21:29 +08:00
cv::Rodrigues(R, rvec);
return result;
}
else if (flags == ITERATIVE)
2012-06-08 01:21:29 +08:00
{
CvMat c_objectPoints = opoints, c_imagePoints = ipoints;
CvMat c_cameraMatrix = cameraMatrix, c_distCoeffs = distCoeffs;
CvMat c_rvec = _rvec.getMat(), c_tvec = _tvec.getMat();
cvFindExtrinsicCameraParams2(&c_objectPoints, &c_imagePoints, &c_cameraMatrix,
c_distCoeffs.rows*c_distCoeffs.cols ? &c_distCoeffs : 0,
&c_rvec, &c_tvec, useExtrinsicGuess );
return true;
2014-07-15 15:58:49 +08:00
}
else if (flags == DLS)
{
2014-08-05 23:02:06 +08:00
cv::Mat undistortedPoints;
cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
2014-07-16 06:06:29 +08:00
2014-08-05 23:02:06 +08:00
dls PnP(opoints, undistortedPoints);
2014-07-22 23:56:25 +08:00
2014-08-05 23:02:06 +08:00
cv::Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat();
bool result = PnP.compute_pose(R, tvec);
2014-07-25 17:01:47 +08:00
if (result)
2014-08-05 23:02:06 +08:00
cv::Rodrigues(R, rvec);
2014-07-25 17:01:47 +08:00
return result;
2012-06-08 01:21:29 +08:00
}
else
2014-07-30 17:22:25 +08:00
CV_Error(CV_StsBadArg, "The flags argument must be one of CV_ITERATIVE, CV_P3P, CV_EPNP or CV_DLS");
2012-06-08 01:21:29 +08:00
return false;
2011-03-05 08:18:49 +08:00
}
2014-07-02 20:53:12 +08:00
class PnPRansacCallback : public PointSetRegistrator::Callback
{
2012-06-08 01:21:29 +08:00
2014-07-02 20:53:12 +08:00
public:
2014-08-05 23:02:06 +08:00
PnPRansacCallback(Mat _cameraMatrix=Mat(3,3,CV_64F), Mat _distCoeffs=Mat(4,1,CV_64F), int _flags=cv::ITERATIVE,
bool _useExtrinsicGuess=false, Mat _rvec=Mat(), Mat _tvec=Mat() )
: cameraMatrix(_cameraMatrix), distCoeffs(_distCoeffs), flags(_flags), useExtrinsicGuess(_useExtrinsicGuess),
rvec(_rvec), tvec(_tvec) {}
2014-07-02 20:53:12 +08:00
/* Pre: True */
2014-07-09 16:37:37 +08:00
/* Post: compute _model with given points an return number of found models */
2014-07-02 20:53:12 +08:00
int runKernel( InputArray _m1, InputArray _m2, OutputArray _model ) const
{
Mat opoints = _m1.getMat(), ipoints = _m2.getMat();
2012-06-08 01:21:29 +08:00
2014-07-04 23:20:17 +08:00
bool correspondence = cv::solvePnP( _m1, _m2, cameraMatrix, distCoeffs,
2014-08-05 23:02:06 +08:00
rvec, tvec, useExtrinsicGuess, flags );
2014-07-04 23:20:17 +08:00
2014-08-05 23:02:06 +08:00
Mat _local_model;
cv::hconcat(rvec, tvec, _local_model);
_local_model.copyTo(_model);
2014-07-02 20:53:12 +08:00
2014-07-04 23:20:17 +08:00
return correspondence;
2014-07-02 20:53:12 +08:00
}
/* Pre: True */
/* Post: fill _err with projection errors */
void computeError( InputArray _m1, InputArray _m2, InputArray _model, OutputArray _err ) const
{
2014-07-04 23:20:17 +08:00
Mat opoints = _m1.getMat(), ipoints = _m2.getMat(), model = _model.getMat();
2014-07-02 20:53:12 +08:00
int i, count = opoints.cols;
2014-07-09 16:37:37 +08:00
Mat _rvec = model.col(0);
Mat _tvec = model.col(1);
2014-07-02 20:53:12 +08:00
2014-08-04 20:12:48 +08:00
2014-07-04 23:20:17 +08:00
Mat projpoints(count, 2, CV_32FC1);
cv::projectPoints(opoints, _rvec, _tvec, cameraMatrix, distCoeffs, projpoints);
2014-07-02 20:53:12 +08:00
const Point2f* ipoints_ptr = ipoints.ptr<Point2f>();
const Point2f* projpoints_ptr = projpoints.ptr<Point2f>();
2014-07-04 23:20:17 +08:00
_err.create(count, 1, CV_32FC1);
2014-07-02 20:53:12 +08:00
float* err = _err.getMat().ptr<float>();
for ( i = 0; i < count; ++i)
2014-08-11 15:44:24 +08:00
err[i] = (float)cv::norm( ipoints_ptr[i] - projpoints_ptr[i] );
2014-07-02 20:53:12 +08:00
}
2014-07-04 23:20:17 +08:00
Mat cameraMatrix;
Mat distCoeffs;
int flags;
bool useExtrinsicGuess;
Mat rvec;
Mat tvec;
2014-07-02 20:53:12 +08:00
};
2014-07-09 16:37:37 +08:00
bool cv::solvePnPRansac(InputArray _opoints, InputArray _ipoints,
InputArray _cameraMatrix, InputArray _distCoeffs,
OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess,
int iterationsCount, float reprojectionError, double confidence,
OutputArray _inliers, int flags)
2011-03-05 08:18:49 +08:00
{
2014-07-04 23:20:17 +08:00
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat();
2014-07-09 23:27:08 +08:00
int npoints = std::max(opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
CV_Assert( npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) );
2012-06-08 01:21:29 +08:00
CV_Assert(opoints.isContinuous());
2014-07-08 22:07:30 +08:00
CV_Assert(opoints.depth() == CV_32F || opoints.depth() == CV_64F);
CV_Assert((opoints.rows == 1 && opoints.channels() == 3) || opoints.cols*opoints.channels() == 3);
CV_Assert(ipoints.isContinuous());
2014-07-08 22:07:30 +08:00
CV_Assert(ipoints.depth() == CV_32F || ipoints.depth() == CV_64F);
CV_Assert((ipoints.rows == 1 && ipoints.channels() == 2) || ipoints.cols*ipoints.channels() == 2);
2012-06-08 01:21:29 +08:00
2014-07-04 23:20:17 +08:00
_rvec.create(3, 1, CV_64FC1);
_tvec.create(3, 1, CV_64FC1);
2014-07-03 18:23:03 +08:00
2014-07-09 16:37:37 +08:00
Mat rvec = useExtrinsicGuess ? _rvec.getMat() : Mat(3, 1, CV_64FC1);
Mat tvec = useExtrinsicGuess ? _tvec.getMat() : Mat(3, 1, CV_64FC1);
2014-07-09 23:27:08 +08:00
Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat();
2014-07-02 20:53:12 +08:00
2014-07-09 16:37:37 +08:00
Ptr<PointSetRegistrator::Callback> cb; // pointer to callback
cb = makePtr<PnPRansacCallback>( cameraMatrix, distCoeffs, flags, useExtrinsicGuess, rvec, tvec);
2014-07-02 20:53:12 +08:00
2014-08-08 19:52:15 +08:00
int model_points = flags == P3P ? 4 : 6; // minimum of number of model points
2014-08-05 23:02:06 +08:00
double param1 = reprojectionError; // reprojection error
2014-08-07 20:59:01 +08:00
double param2 = confidence; // confidence
int param3 = iterationsCount; // number maximum iterations
2012-06-08 01:21:29 +08:00
2014-07-04 23:20:17 +08:00
cv::Mat _local_model(3, 2, CV_64FC1);
cv::Mat _mask_local_inliers(1, opoints.rows, CV_8UC1);
2014-08-05 23:02:06 +08:00
// call Ransac
int result = createRANSACPointSetRegistrator(cb, model_points, param1, param2, param3)->run(opoints, ipoints, _local_model, _mask_local_inliers);
if( result <= 0 || _local_model.rows <= 0)
{
2014-08-05 23:02:06 +08:00
_rvec.assign(rvec); // output rotation vector
_tvec.assign(tvec); // output translation vector
if( _inliers.needed() )
_inliers.release();
return false;
}
else
{
_rvec.assign(_local_model.col(0)); // output rotation vector
_tvec.assign(_local_model.col(1)); // output translation vector
}
2012-06-08 01:21:29 +08:00
2014-08-05 23:02:06 +08:00
if(_inliers.needed())
{
2014-08-05 23:02:06 +08:00
Mat _local_inliers;
int count = 0;
for (int i = 0; i < _mask_local_inliers.rows; ++i)
2012-06-08 01:21:29 +08:00
{
2014-08-05 23:02:06 +08:00
if((int)_mask_local_inliers.at<uchar>(i) == 1) // inliers mask
2012-06-08 01:21:29 +08:00
{
2014-08-05 23:02:06 +08:00
_local_inliers.push_back(count); // output inliers vector
count++;
2012-06-08 01:21:29 +08:00
}
}
2014-08-05 23:02:06 +08:00
_local_inliers.copyTo(_inliers);
}
2014-07-09 16:37:37 +08:00
return true;
2011-03-05 08:18:49 +08:00
}