DLS working

This commit is contained in:
edgarriba 2014-07-25 11:01:47 +02:00
parent 48c4a79d2e
commit a3f065c409
3 changed files with 279 additions and 397 deletions

File diff suppressed because one or more lines are too long

View File

@ -12,52 +12,58 @@ public:
dls(const cv::Mat& opoints, const cv::Mat& ipoints); dls(const cv::Mat& opoints, const cv::Mat& ipoints);
~dls(); ~dls();
void compute_pose(cv::Mat& R, cv::Mat& t); bool compute_pose(cv::Mat& R, cv::Mat& t);
private: private:
template <typename OpointType, typename O, typename IpointType, typename I> // initialisation
template <typename OpointType, typename IpointType>
void init_points(const cv::Mat& opoints, const cv::Mat& ipoints) void init_points(const cv::Mat& opoints, const cv::Mat& ipoints)
{ {
for(int i = 0; i < N; i++) for(int i = 0; i < N; i++)
{ {
p.at<O>(0,i) = opoints.at<OpointType>(0,i).x; p.at<double>(0,i) = opoints.at<OpointType>(0,i).x;
p.at<O>(1,i) = opoints.at<OpointType>(0,i).y; p.at<double>(1,i) = opoints.at<OpointType>(0,i).y;
p.at<O>(2,i) = opoints.at<OpointType>(0,i).z; p.at<double>(2,i) = opoints.at<OpointType>(0,i).z;
z.at<I>(0,i) = ipoints.at<IpointType>(0,i).x; z.at<double>(0,i) = ipoints.at<IpointType>(0,i).x;
z.at<I>(1,i) = ipoints.at<IpointType>(0,i).y; z.at<double>(1,i) = ipoints.at<IpointType>(0,i).y;
z.at<I>(2,i) = (I)1; z.at<double>(2,i) = (double)1;
} }
} }
void norm_z_vector(); void norm_z_vector();
// main algorithm
void run_kernel(const cv::Mat& pp);
void build_coeff_matrix(const cv::Mat& pp, cv::Mat& Mtilde, cv::Mat& D); void build_coeff_matrix(const cv::Mat& pp, cv::Mat& Mtilde, cv::Mat& D);
void compute_eigenvec(const cv::Mat& Mtilde, cv::Mat& eigenval_real, cv::Mat& eigenval_imag, void compute_eigenvec(const cv::Mat& Mtilde, cv::Mat& eigenval_real, cv::Mat& eigenval_imag,
cv::Mat& eigenvec_real, cv::Mat& eigenvec_imag); cv::Mat& eigenvec_real, cv::Mat& eigenvec_imag);
double min_val(const std::vector<double>& values); void fill_coeff(const cv::Mat * D);
// useful functions
cv::Mat LeftMultVec(const cv::Mat& v);
cv::Mat cayley_LS_M(const std::vector<double>& a, const std::vector<double>& b,
const std::vector<double>& c, const std::vector<double>& u);
cv::Mat Hessian(const double s[]);
cv::Mat cayley2rotbar(const cv::Mat& s);
cv::Mat skewsymm(const cv::Mat * X1);
// extra functions
cv::Mat rotx(const double t); cv::Mat rotx(const double t);
cv::Mat roty(const double t); cv::Mat roty(const double t);
cv::Mat rotz(const double t); cv::Mat rotz(const double t);
cv::Mat mean(const cv::Mat& M); cv::Mat mean(const cv::Mat& M);
void fill_coeff(const cv::Mat * D);
cv::Mat LeftMultVec(const cv::Mat& v);
cv::Mat cayley_LS_M(const std::vector<double>& a, const std::vector<double>& b,
const std::vector<double>& c, const std::vector<double>& u);
bool positive_eigenvalues(const cv::Mat& eigenvalues);
cv::Mat Hessian(const double s[]);
cv::Mat cayley2rotbar(const double s[]);
cv::Mat skewsymm(const double X1[]);
bool is_empty(const cv::Mat * v); bool is_empty(const cv::Mat * v);
void run_kernel(const cv::Mat& pp); bool positive_eigenvalues(const cv::Mat * eigenvalues);
cv::Mat p, z; // object-image points cv::Mat p, z; // object-image points
int N; // number of input points int N; // number of input points
std::vector<double> f1coeff, f2coeff, f3coeff, cost_; std::vector<double> f1coeff, f2coeff, f3coeff, cost_; // coefficient for coefficients matrix
std::vector<cv::Mat> C_est_, t_est_; // vector to store candidate std::vector<cv::Mat> C_est_, t_est_; // optimal candidates
cv::Mat C_est__, t_est__; // best found solution cv::Mat C_est__, t_est__; // optimal found solution
double cost__; double cost__; // optimal found solution
}; };
#endif // DLS_H #endif // DLS_H

View File

@ -49,6 +49,11 @@
#include <iostream> #include <iostream>
using namespace cv; using namespace cv;
void MatrixSize(const cv::Mat& mat)
{
cout << mat.rows << "x" << mat.cols << endl;
}
bool cv::solvePnP( InputArray _opoints, InputArray _ipoints, bool cv::solvePnP( InputArray _opoints, InputArray _ipoints,
InputArray _cameraMatrix, InputArray _distCoeffs, InputArray _cameraMatrix, InputArray _distCoeffs,
OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess, int flags ) OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess, int flags )
@ -96,23 +101,16 @@ bool cv::solvePnP( InputArray _opoints, InputArray _ipoints,
} }
else if (flags == DLS) else if (flags == DLS)
{ {
std::cout << "DLS" << std::endl;
cv::Mat undistortedPoints; cv::Mat undistortedPoints;
//cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs); cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
dls PnP(opoints, undistortedPoints);
cv::Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat(); cv::Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat();
bool result = PnP.compute_pose(R, tvec);
//dls PnP(opoints, undistortedPoints); if (result)
dls PnP(opoints, ipoints); // FOR TESTING cv::Rodrigues(R, rvec);
return result;
PnP.compute_pose(R, tvec);
cout << "after dls compute pose" << endl;
//TODO: DO SOMETHING WITH R and t
//cv::Rodrigues(R, rvec);
return true;
} }
else else
CV_Error(CV_StsBadArg, "The flags argument must be one of CV_ITERATIVE, CV_P3P or CV_EPNP"); CV_Error(CV_StsBadArg, "The flags argument must be one of CV_ITERATIVE, CV_P3P or CV_EPNP");