a big patch; use special proxy types (Input/OutputArray, Input/OutputArrayOfArrays) for passing in vectors, matrices etc.
parent
335370a7c0
commit
abeeb40d46
94 changed files with 10407 additions and 9207 deletions
@ -1,324 +1,329 @@ |
|||||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||||
//
|
//
|
||||||
// By downloading, copying, installing or using the software you agree to this license.
|
// 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,
|
// If you do not agree to this license, do not download, install,
|
||||||
// copy or use the software.
|
// copy or use the software.
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// License Agreement
|
// License Agreement
|
||||||
// For Open Source Computer Vision Library
|
// For Open Source Computer Vision Library
|
||||||
//
|
//
|
||||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||||
// Third party copyrights are property of their respective owners.
|
// Third party copyrights are property of their respective owners.
|
||||||
//
|
//
|
||||||
// Redistribution and use in source and binary forms, with or without modification,
|
// Redistribution and use in source and binary forms, with or without modification,
|
||||||
// are permitted provided that the following conditions are met:
|
// are permitted provided that the following conditions are met:
|
||||||
//
|
//
|
||||||
// * Redistribution's of source code must retain the above copyright notice,
|
// * Redistribution's of source code must retain the above copyright notice,
|
||||||
// this list of conditions and the following disclaimer.
|
// this list of conditions and the following disclaimer.
|
||||||
//
|
//
|
||||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||||
// this list of conditions and the following disclaimer in the documentation
|
// this list of conditions and the following disclaimer in the documentation
|
||||||
// and/or other materials provided with the distribution.
|
// and/or other materials provided with the distribution.
|
||||||
//
|
//
|
||||||
// * The name of the copyright holders may not be used to endorse or promote products
|
// * The name of the copyright holders may not be used to endorse or promote products
|
||||||
// derived from this software without specific prior written permission.
|
// derived from this software without specific prior written permission.
|
||||||
//
|
//
|
||||||
// This software is provided by the copyright holders and contributors "as is" and
|
// 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
|
// any express or implied warranties, including, but not limited to, the implied
|
||||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
// 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,
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||||
// indirect, incidental, special, exemplary, or consequential damages
|
// indirect, incidental, special, exemplary, or consequential damages
|
||||||
// (including, but not limited to, procurement of substitute goods or services;
|
// (including, but not limited to, procurement of substitute goods or services;
|
||||||
// loss of use, data, or profits; or business interruption) however caused
|
// loss of use, data, or profits; or business interruption) however caused
|
||||||
// and on any theory of liability, whether in contract, strict liability,
|
// and on any theory of liability, whether in contract, strict liability,
|
||||||
// or tort (including negligence or otherwise) arising in any way out of
|
// 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.
|
// the use of this software, even if advised of the possibility of such damage.
|
||||||
//
|
//
|
||||||
//M*/
|
//M*/
|
||||||
|
|
||||||
#include "precomp.hpp" |
#include "precomp.hpp" |
||||||
using namespace cv; |
using namespace cv; |
||||||
|
|
||||||
void cv::solvePnP( const Mat& opoints, const Mat& ipoints, |
void cv::solvePnP( const InputArray& _opoints, const InputArray& _ipoints, |
||||||
const Mat& cameraMatrix, const Mat& distCoeffs, |
const InputArray& _cameraMatrix, const InputArray& _distCoeffs, |
||||||
Mat& rvec, Mat& tvec, bool useExtrinsicGuess ) |
OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess ) |
||||||
{ |
{ |
||||||
CV_Assert(opoints.isContinuous() && opoints.depth() == CV_32F && |
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat(); |
||||||
((opoints.rows == 1 && opoints.channels() == 3) || |
int npoints = opoints.checkVector(3, CV_32F); |
||||||
opoints.cols*opoints.channels() == 3) && |
CV_Assert( npoints >= 0 && npoints == ipoints.checkVector(2, CV_32F) ); |
||||||
ipoints.isContinuous() && ipoints.depth() == CV_32F && |
|
||||||
((ipoints.rows == 1 && ipoints.channels() == 2) || |
_rvec.create(3, 1, CV_64F); |
||||||
ipoints.cols*ipoints.channels() == 2)); |
_tvec.create(3, 1, CV_64F); |
||||||
|
CvMat c_objectPoints = opoints, c_imagePoints = ipoints; |
||||||
rvec.create(3, 1, CV_64F); |
CvMat c_cameraMatrix = _cameraMatrix.getMat(), c_distCoeffs = _distCoeffs.getMat(); |
||||||
tvec.create(3, 1, CV_64F); |
CvMat c_rvec = _rvec.getMat(), c_tvec = _tvec.getMat(); |
||||||
CvMat _objectPoints = opoints, _imagePoints = ipoints; |
cvFindExtrinsicCameraParams2(&c_objectPoints, &c_imagePoints, &c_cameraMatrix, |
||||||
CvMat _cameraMatrix = cameraMatrix, _distCoeffs = distCoeffs; |
c_distCoeffs.rows*c_distCoeffs.cols ? &c_distCoeffs : 0, |
||||||
CvMat _rvec = rvec, _tvec = tvec; |
&c_rvec, &c_tvec, useExtrinsicGuess ); |
||||||
cvFindExtrinsicCameraParams2(&_objectPoints, &_imagePoints, &_cameraMatrix, |
|
||||||
distCoeffs.data ? &_distCoeffs : 0, |
|
||||||
&_rvec, &_tvec, useExtrinsicGuess ); |
|
||||||
} |
} |
||||||
|
|
||||||
namespace cv |
namespace cv |
||||||
{ |
{ |
||||||
namespace pnpransac |
namespace pnpransac |
||||||
{ |
|
||||||
const int MIN_POINTS_COUNT = 4; |
|
||||||
|
|
||||||
void project3dPoints(const Mat& points, const Mat& rvec, const Mat& tvec, Mat& modif_points) |
|
||||||
{ |
{ |
||||||
modif_points.create(1, points.cols, CV_32FC3); |
const int MIN_POINTS_COUNT = 4; |
||||||
Mat R(3, 3, CV_64FC1); |
|
||||||
Rodrigues(rvec, R); |
|
||||||
Mat transformation(3, 4, CV_64F); |
|
||||||
Mat r = transformation.colRange(0, 2); |
|
||||||
R.copyTo(r); |
|
||||||
Mat t = transformation.colRange(3, 4); |
|
||||||
tvec.copyTo(t); |
|
||||||
transform(points, modif_points, transformation); |
|
||||||
} |
|
||||||
|
|
||||||
class Mutex |
void project3dPoints(const Mat& points, const Mat& rvec, const Mat& tvec, Mat& modif_points) |
||||||
{ |
|
||||||
public: |
|
||||||
Mutex() {} |
|
||||||
void lock() |
|
||||||
{ |
{ |
||||||
#ifdef HAVE_TBB |
modif_points.create(1, points.cols, CV_32FC3); |
||||||
slock.acquire(resultsMutex); |
Mat R(3, 3, CV_64FC1); |
||||||
#endif |
Rodrigues(rvec, R); |
||||||
|
Mat transformation(3, 4, CV_64F); |
||||||
|
Mat r = transformation.colRange(0, 2); |
||||||
|
R.copyTo(r); |
||||||
|
Mat t = transformation.colRange(3, 4); |
||||||
|
tvec.copyTo(t); |
||||||
|
transform(points, modif_points, transformation); |
||||||
} |
} |
||||||
|
|
||||||
void unlock() |
class Mutex |
||||||
{ |
{ |
||||||
#ifdef HAVE_TBB |
public: |
||||||
slock.release(); |
Mutex() {} |
||||||
#endif |
void lock() |
||||||
} |
{ |
||||||
|
#ifdef HAVE_TBB |
||||||
|
slock.acquire(resultsMutex); |
||||||
|
#endif |
||||||
|
} |
||||||
|
|
||||||
private: |
void unlock() |
||||||
#ifdef HAVE_TBB |
{ |
||||||
tbb::mutex resultsMutex; |
#ifdef HAVE_TBB |
||||||
tbb::mutex::scoped_lock slock; |
slock.release(); |
||||||
#endif |
#endif |
||||||
}; |
} |
||||||
|
|
||||||
struct CameraParameters |
private: |
||||||
{ |
#ifdef HAVE_TBB |
||||||
void init(Mat _intrinsics, Mat _distCoeffs) |
tbb::mutex resultsMutex; |
||||||
|
tbb::mutex::scoped_lock slock; |
||||||
|
#endif |
||||||
|
}; |
||||||
|
|
||||||
|
struct CameraParameters |
||||||
{ |
{ |
||||||
_intrinsics.copyTo(intrinsics); |
void init(Mat _intrinsics, Mat _distCoeffs) |
||||||
_distCoeffs.copyTo(distortion); |
{ |
||||||
} |
_intrinsics.copyTo(intrinsics); |
||||||
|
_distCoeffs.copyTo(distortion); |
||||||
|
} |
||||||
|
|
||||||
Mat intrinsics; |
Mat intrinsics; |
||||||
Mat distortion; |
Mat distortion; |
||||||
}; |
}; |
||||||
|
|
||||||
struct Parameters |
struct Parameters |
||||||
{ |
|
||||||
int iterationsCount; |
|
||||||
float reprojectionError; |
|
||||||
int minInliersCount; |
|
||||||
bool useExtrinsicGuess; |
|
||||||
CameraParameters camera; |
|
||||||
}; |
|
||||||
|
|
||||||
void pnpTask(const vector<char>& pointsMask, const Mat& objectPoints, const Mat& imagePoints, |
|
||||||
const Parameters& params, vector<int>& inliers, Mat& rvec, Mat& tvec, |
|
||||||
const Mat& rvecInit, const Mat& tvecInit, Mutex& resultsMutex) |
|
||||||
{ |
|
||||||
Mat modelObjectPoints(1, MIN_POINTS_COUNT, CV_32FC3), modelImagePoints(1, MIN_POINTS_COUNT, CV_32FC2); |
|
||||||
for (size_t i = 0, colIndex = 0; i < pointsMask.size(); i++) |
|
||||||
{ |
|
||||||
if (pointsMask[i]) |
|
||||||
{ |
{ |
||||||
Mat colModelImagePoints = modelImagePoints(Rect(colIndex, 0, 1, 1)); |
int iterationsCount; |
||||||
imagePoints.col(i).copyTo(colModelImagePoints); |
float reprojectionError; |
||||||
Mat colModelObjectPoints = modelObjectPoints(Rect(colIndex, 0, 1, 1)); |
int minInliersCount; |
||||||
objectPoints.col(i).copyTo(colModelObjectPoints); |
bool useExtrinsicGuess; |
||||||
colIndex = colIndex+1; |
CameraParameters camera; |
||||||
} |
}; |
||||||
} |
|
||||||
|
void pnpTask(const vector<char>& pointsMask, const Mat& objectPoints, const Mat& imagePoints, |
||||||
//filter same 3d points, hang in solvePnP
|
const Parameters& params, vector<int>& inliers, Mat& rvec, Mat& tvec, |
||||||
double eps = 1e-10; |
const Mat& rvecInit, const Mat& tvecInit, Mutex& resultsMutex) |
||||||
int num_same_points = 0; |
|
||||||
for (int i = 0; i < MIN_POINTS_COUNT; i++) |
|
||||||
for (int j = i + 1; j < MIN_POINTS_COUNT; j++) |
|
||||||
{ |
{ |
||||||
if (norm(modelObjectPoints.at<Vec3f>(0, i) - modelObjectPoints.at<Vec3f>(0, j)) < eps) |
Mat modelObjectPoints(1, MIN_POINTS_COUNT, CV_32FC3), modelImagePoints(1, MIN_POINTS_COUNT, CV_32FC2); |
||||||
num_same_points++; |
for (size_t i = 0, colIndex = 0; i < pointsMask.size(); i++) |
||||||
} |
{ |
||||||
if (num_same_points > 0) |
if (pointsMask[i]) |
||||||
return; |
{ |
||||||
|
Mat colModelImagePoints = modelImagePoints(Rect(colIndex, 0, 1, 1)); |
||||||
Mat localRvec, localTvec; |
imagePoints.col(i).copyTo(colModelImagePoints); |
||||||
rvecInit.copyTo(localRvec); |
Mat colModelObjectPoints = modelObjectPoints(Rect(colIndex, 0, 1, 1)); |
||||||
tvecInit.copyTo(localTvec); |
objectPoints.col(i).copyTo(colModelObjectPoints); |
||||||
|
colIndex = colIndex+1; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
solvePnP(modelObjectPoints, modelImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec, params.useExtrinsicGuess); |
//filter same 3d points, hang in solvePnP
|
||||||
|
double eps = 1e-10; |
||||||
|
int num_same_points = 0; |
||||||
|
for (int i = 0; i < MIN_POINTS_COUNT; i++) |
||||||
|
for (int j = i + 1; j < MIN_POINTS_COUNT; j++) |
||||||
|
{ |
||||||
|
if (norm(modelObjectPoints.at<Vec3f>(0, i) - modelObjectPoints.at<Vec3f>(0, j)) < eps) |
||||||
|
num_same_points++; |
||||||
|
} |
||||||
|
if (num_same_points > 0) |
||||||
|
return; |
||||||
|
|
||||||
|
Mat localRvec, localTvec; |
||||||
|
rvecInit.copyTo(localRvec); |
||||||
|
tvecInit.copyTo(localTvec); |
||||||
|
|
||||||
|
solvePnP(modelObjectPoints, modelImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec, params.useExtrinsicGuess); |
||||||
|
|
||||||
|
vector<Point2f> projected_points; |
||||||
|
projected_points.resize(objectPoints.cols); |
||||||
|
projectPoints(objectPoints, localRvec, localTvec, params.camera.intrinsics, params.camera.distortion, projected_points); |
||||||
|
|
||||||
|
Mat rotatedPoints; |
||||||
|
project3dPoints(objectPoints, localRvec, localTvec, rotatedPoints); |
||||||
|
|
||||||
|
vector<int> localInliers; |
||||||
|
for (int i = 0; i < objectPoints.cols; i++) |
||||||
|
{ |
||||||
|
Point2f p(imagePoints.at<Vec2f>(0, i)[0], imagePoints.at<Vec2f>(0, i)[1]); |
||||||
|
if ((norm(p - projected_points[i]) < params.reprojectionError) |
||||||
|
&& (rotatedPoints.at<Vec3f>(0, i)[2] > 0)) //hack
|
||||||
|
{ |
||||||
|
localInliers.push_back(i); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
vector<Point2f> projected_points; |
if (localInliers.size() > inliers.size()) |
||||||
projected_points.resize(objectPoints.cols); |
{ |
||||||
projectPoints(objectPoints, localRvec, localTvec, params.camera.intrinsics, params.camera.distortion, projected_points); |
resultsMutex.lock(); |
||||||
|
|
||||||
Mat rotatedPoints; |
inliers.clear(); |
||||||
project3dPoints(objectPoints, localRvec, localTvec, rotatedPoints); |
inliers.resize(localInliers.size()); |
||||||
|
memcpy(&inliers[0], &localInliers[0], sizeof(int) * localInliers.size()); |
||||||
|
localRvec.copyTo(rvec); |
||||||
|
localTvec.copyTo(tvec); |
||||||
|
|
||||||
vector<int> localInliers; |
resultsMutex.unlock(); |
||||||
for (size_t i = 0; i < objectPoints.cols; i++) |
} |
||||||
{ |
|
||||||
Point2f p(imagePoints.at<Vec2f>(0, i)[0], imagePoints.at<Vec2f>(0, i)[1]); |
|
||||||
if ((norm(p - projected_points[i]) < params.reprojectionError) |
|
||||||
&& (rotatedPoints.at<Vec3f>(0, i)[2] > 0)) //hack
|
|
||||||
{ |
|
||||||
localInliers.push_back(i); |
|
||||||
} |
} |
||||||
} |
|
||||||
|
|
||||||
if (localInliers.size() > inliers.size()) |
class PnPSolver |
||||||
{ |
|
||||||
resultsMutex.lock(); |
|
||||||
|
|
||||||
inliers.clear(); |
|
||||||
inliers.resize(localInliers.size()); |
|
||||||
memcpy(&inliers[0], &localInliers[0], sizeof(int) * localInliers.size()); |
|
||||||
localRvec.copyTo(rvec); |
|
||||||
localTvec.copyTo(tvec); |
|
||||||
|
|
||||||
resultsMutex.unlock(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
class PnPSolver |
|
||||||
{ |
|
||||||
public: |
|
||||||
void operator()( const BlockedRange& r ) const |
|
||||||
{ |
{ |
||||||
vector<char> pointsMask(objectPoints.cols, 0); |
public: |
||||||
memset(&pointsMask[0], 1, MIN_POINTS_COUNT ); |
void operator()( const BlockedRange& r ) const |
||||||
for( size_t i=r.begin(); i!=r.end(); ++i ) |
|
||||||
{ |
{ |
||||||
generateVar(pointsMask); |
vector<char> pointsMask(objectPoints.cols, 0); |
||||||
pnpTask(pointsMask, objectPoints, imagePoints, parameters, |
memset(&pointsMask[0], 1, MIN_POINTS_COUNT ); |
||||||
inliers, rvec, tvec, initRvec, initTvec, syncMutex); |
for( int i=r.begin(); i!=r.end(); ++i ) |
||||||
if ((int)inliers.size() > parameters.minInliersCount) |
{ |
||||||
{ |
generateVar(pointsMask); |
||||||
#ifdef HAVE_TBB |
pnpTask(pointsMask, objectPoints, imagePoints, parameters, |
||||||
tbb::task::self().cancel_group_execution(); |
inliers, rvec, tvec, initRvec, initTvec, syncMutex); |
||||||
#else |
if ((int)inliers.size() > parameters.minInliersCount) |
||||||
break; |
{ |
||||||
#endif |
#ifdef HAVE_TBB |
||||||
} |
tbb::task::self().cancel_group_execution(); |
||||||
|
#else |
||||||
|
break; |
||||||
|
#endif |
||||||
|
} |
||||||
|
} |
||||||
} |
} |
||||||
} |
PnPSolver(const Mat& objectPoints, const Mat& imagePoints, const Parameters& parameters, |
||||||
PnPSolver(const Mat& objectPoints, const Mat& imagePoints, const Parameters& parameters, |
Mat& rvec, Mat& tvec, vector<int>& inliers): |
||||||
Mat& rvec, Mat& tvec, vector<int>& inliers): |
objectPoints(objectPoints), imagePoints(imagePoints), parameters(parameters), |
||||||
objectPoints(objectPoints), imagePoints(imagePoints), parameters(parameters), |
rvec(rvec), tvec(tvec), inliers(inliers) |
||||||
rvec(rvec), tvec(tvec), inliers(inliers) |
{ |
||||||
{ |
rvec.copyTo(initRvec); |
||||||
rvec.copyTo(initRvec); |
tvec.copyTo(initTvec); |
||||||
tvec.copyTo(initTvec); |
} |
||||||
} |
private: |
||||||
private: |
const Mat& objectPoints; |
||||||
const Mat& objectPoints; |
const Mat& imagePoints; |
||||||
const Mat& imagePoints; |
const Parameters& parameters; |
||||||
const Parameters& parameters; |
Mat &rvec, &tvec; |
||||||
vector<int>& inliers; |
vector<int>& inliers; |
||||||
Mat &rvec, &tvec; |
Mat initRvec, initTvec; |
||||||
Mat initRvec, initTvec; |
|
||||||
|
static RNG generator; |
||||||
static RNG generator; |
static Mutex syncMutex; |
||||||
static Mutex syncMutex; |
|
||||||
|
void generateVar(vector<char>& mask) const |
||||||
void generateVar(vector<char>& mask) const |
{ |
||||||
{ |
size_t size = mask.size(); |
||||||
size_t size = mask.size(); |
for (size_t i = 0; i < size; i++) |
||||||
for (size_t i = 0; i < size; i++) |
{ |
||||||
{ |
int i1 = generator.uniform(0, size); |
||||||
int i1 = generator.uniform(0, size); |
int i2 = generator.uniform(0, size); |
||||||
int i2 = generator.uniform(0, size); |
char curr = mask[i1]; |
||||||
char curr = mask[i1]; |
mask[i1] = mask[i2]; |
||||||
mask[i1] = mask[i2]; |
mask[i2] = curr; |
||||||
mask[i2] = curr; |
} |
||||||
} |
} |
||||||
} |
}; |
||||||
}; |
|
||||||
|
|
||||||
Mutex PnPSolver::syncMutex; |
Mutex PnPSolver::syncMutex; |
||||||
RNG PnPSolver::generator; |
RNG PnPSolver::generator; |
||||||
|
|
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
void cv::solvePnPRansac(const Mat& opoints, const Mat& ipoints, |
void cv::solvePnPRansac(const InputArray& _opoints, const InputArray& _ipoints, |
||||||
const Mat& cameraMatrix, const Mat& distCoeffs, Mat& rvec, Mat& tvec, bool useExtrinsicGuess, |
const InputArray& _cameraMatrix, const InputArray& _distCoeffs, |
||||||
int iterationsCount, float reprojectionError, int minInliersCount, vector<int>* inliers) |
OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess, |
||||||
|
int iterationsCount, float reprojectionError, int minInliersCount, |
||||||
|
OutputArray _inliers) |
||||||
{ |
{ |
||||||
|
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat(); |
||||||
|
Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat(); |
||||||
|
|
||||||
|
CV_Assert(opoints.isContinuous()); |
||||||
|
CV_Assert(opoints.depth() == CV_32F); |
||||||
|
CV_Assert((opoints.rows == 1 && opoints.channels() == 3) || opoints.cols*opoints.channels() == 3); |
||||||
|
CV_Assert(ipoints.isContinuous()); |
||||||
|
CV_Assert(ipoints.depth() == CV_32F); |
||||||
|
CV_Assert((ipoints.rows == 1 && ipoints.channels() == 2) || ipoints.cols*ipoints.channels() == 2); |
||||||
|
|
||||||
|
_rvec.create(3, 1, CV_64FC1); |
||||||
|
_tvec.create(3, 1, CV_64FC1); |
||||||
|
Mat rvec = _rvec.getMat(); |
||||||
|
Mat tvec = _tvec.getMat(); |
||||||
|
|
||||||
|
Mat objectPoints = opoints.reshape(3, 1), imagePoints = ipoints.reshape(2, 1); |
||||||
|
|
||||||
|
if (minInliersCount <= 0) |
||||||
|
minInliersCount = objectPoints.cols; |
||||||
|
cv::pnpransac::Parameters params; |
||||||
|
params.iterationsCount = iterationsCount; |
||||||
|
params.minInliersCount = minInliersCount; |
||||||
|
params.reprojectionError = reprojectionError; |
||||||
|
params.useExtrinsicGuess = useExtrinsicGuess; |
||||||
|
params.camera.init(cameraMatrix, distCoeffs); |
||||||
|
|
||||||
|
vector<int> localInliers; |
||||||
|
Mat localRvec, localTvec; |
||||||
|
rvec.copyTo(localRvec); |
||||||
|
tvec.copyTo(localTvec); |
||||||
|
|
||||||
|
if (objectPoints.cols >= pnpransac::MIN_POINTS_COUNT) |
||||||
|
{ |
||||||
|
parallel_for(BlockedRange(0,iterationsCount), cv::pnpransac::PnPSolver(objectPoints, imagePoints, params, |
||||||
|
localRvec, localTvec, localInliers)); |
||||||
|
} |
||||||
|
|
||||||
CV_Assert(opoints.isContinuous()); |
if (localInliers.size() >= (size_t)pnpransac::MIN_POINTS_COUNT) |
||||||
CV_Assert(opoints.depth() == CV_32F); |
{ |
||||||
CV_Assert((opoints.rows == 1 && opoints.channels() == 3) || opoints.cols*opoints.channels() == 3); |
size_t pointsCount = localInliers.size(); |
||||||
CV_Assert(ipoints.isContinuous()); |
Mat inlierObjectPoints(1, pointsCount, CV_32FC3), inlierImagePoints(1, pointsCount, CV_32FC2); |
||||||
CV_Assert(ipoints.depth() == CV_32F); |
int index; |
||||||
CV_Assert((ipoints.rows == 1 && ipoints.channels() == 2) || ipoints.cols*ipoints.channels() == 2); |
for (size_t i = 0; i < localInliers.size(); i++) |
||||||
|
{ |
||||||
rvec.create(3, 1, CV_64FC1); |
index = localInliers[i]; |
||||||
tvec.create(3, 1, CV_64FC1); |
Mat colInlierImagePoints = inlierImagePoints(Rect(i, 0, 1, 1)); |
||||||
|
imagePoints.col(index).copyTo(colInlierImagePoints); |
||||||
Mat objectPoints = opoints.reshape(3, 1), imagePoints = ipoints.reshape(2, 1); |
Mat colInlierObjectPoints = inlierObjectPoints(Rect(i, 0, 1, 1)); |
||||||
|
objectPoints.col(index).copyTo(colInlierObjectPoints); |
||||||
if (minInliersCount <= 0) |
} |
||||||
minInliersCount = objectPoints.cols; |
solvePnP(inlierObjectPoints, inlierImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec, true); |
||||||
cv::pnpransac::Parameters params; |
localRvec.copyTo(rvec); |
||||||
params.iterationsCount = iterationsCount; |
localTvec.copyTo(tvec); |
||||||
params.minInliersCount = minInliersCount; |
if (_inliers.needed()) |
||||||
params.reprojectionError = reprojectionError; |
Mat(localInliers).copyTo(_inliers); |
||||||
params.useExtrinsicGuess = useExtrinsicGuess; |
} |
||||||
params.camera.init(cameraMatrix, distCoeffs); |
else |
||||||
|
|
||||||
vector<int> localInliers; |
|
||||||
Mat localRvec, localTvec; |
|
||||||
rvec.copyTo(localRvec); |
|
||||||
tvec.copyTo(localTvec); |
|
||||||
|
|
||||||
if (objectPoints.cols >= pnpransac::MIN_POINTS_COUNT) |
|
||||||
{ |
|
||||||
parallel_for(BlockedRange(0,iterationsCount), cv::pnpransac::PnPSolver(objectPoints, imagePoints, params, |
|
||||||
localRvec, localTvec, localInliers)); |
|
||||||
} |
|
||||||
|
|
||||||
if (localInliers.size() >= pnpransac::MIN_POINTS_COUNT) |
|
||||||
{ |
|
||||||
size_t pointsCount = localInliers.size(); |
|
||||||
Mat inlierObjectPoints(1, pointsCount, CV_32FC3), inlierImagePoints(1, pointsCount, CV_32FC2); |
|
||||||
int index; |
|
||||||
for (size_t i = 0; i < localInliers.size(); i++) |
|
||||||
{ |
{ |
||||||
index = localInliers[i]; |
tvec.setTo(Scalar(0)); |
||||||
Mat colInlierImagePoints = inlierImagePoints(Rect(i, 0, 1, 1)); |
Mat R = Mat::eye(3, 3, CV_64F); |
||||||
imagePoints.col(index).copyTo(colInlierImagePoints); |
Rodrigues(R, rvec); |
||||||
Mat colInlierObjectPoints = inlierObjectPoints(Rect(i, 0, 1, 1)); |
if( _inliers.needed() ) |
||||||
objectPoints.col(index).copyTo(colInlierObjectPoints); |
_inliers.release(); |
||||||
} |
} |
||||||
solvePnP(inlierObjectPoints, inlierImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec, true); |
return; |
||||||
localRvec.copyTo(rvec); |
|
||||||
localTvec.copyTo(tvec); |
|
||||||
if (inliers) |
|
||||||
*inliers = localInliers; |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
tvec.setTo(Scalar(0)); |
|
||||||
Mat R = Mat::ones(3, 3, CV_64F); |
|
||||||
Rodrigues(R, rvec); |
|
||||||
} |
|
||||||
return; |
|
||||||
} |
} |
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue