mexopencv  3.4.1
MEX interface for OpenCV library
solvePnP.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/calib3d.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 namespace {
16  ("Iterative", cv::SOLVEPNP_ITERATIVE)
17  ("EPnP", cv::SOLVEPNP_EPNP)
18  ("P3P", cv::SOLVEPNP_P3P)
19  ("AP3P", cv::SOLVEPNP_AP3P)
20  ("DLS", cv::SOLVEPNP_DLS)
21  ("UPnP", cv::SOLVEPNP_UPNP);
22 }
23 
31 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
32 {
33  // Check the number of arguments
34  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=3);
35 
36  // Argument vector
37  vector<MxArray> rhs(prhs, prhs+nrhs);
38 
39  // Option processing
40  Mat distCoeffs, rvec, tvec;
41  bool useExtrinsicGuess = false;
42  int flags = cv::SOLVEPNP_ITERATIVE;
43  for (int i=3; i<nrhs; i+=2) {
44  string key(rhs[i].toString());
45  if (key == "DistCoeffs")
46  distCoeffs = rhs[i+1].toMat(CV_64F);
47  else if (key == "UseExtrinsicGuess")
48  useExtrinsicGuess = rhs[i+1].toBool();
49  else if (key == "Rvec")
50  rvec = rhs[i+1].toMat(CV_64F);
51  else if (key == "Tvec")
52  tvec = rhs[i+1].toMat(CV_64F);
53  else if (key == "Method")
54  flags = PnPMethod[rhs[i+1].toString()];
55  else
56  mexErrMsgIdAndTxt("mexopencv:error",
57  "Unrecognized option %s", key.c_str());
58  }
59  if (!rvec.empty() && !tvec.empty() && flags == cv::SOLVEPNP_ITERATIVE)
60  useExtrinsicGuess = true;
61 
62  // Process
63  bool success = false;
64  Mat cameraMatrix(rhs[2].toMat(CV_64F));
65  if (rhs[0].isNumeric() && rhs[1].isNumeric()) {
66  Mat objectPoints(rhs[0].toMat(CV_64F).reshape(3,0)),
67  imagePoints(rhs[1].toMat(CV_64F).reshape(2,0));
68  success = solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs,
69  rvec, tvec, useExtrinsicGuess, flags);
70  }
71  else if (rhs[0].isCell() && rhs[1].isCell()) {
72  vector<Point3d> objectPoints(rhs[0].toVector<Point3d>());
73  vector<Point2d> imagePoints(rhs[1].toVector<Point2d>());
74  success = solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs,
75  rvec, tvec, useExtrinsicGuess, flags);
76  }
77  else
78  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
79  plhs[0] = MxArray(rvec);
80  if (nlhs>1)
81  plhs[1] = MxArray(tvec);
82  if (nlhs>2)
83  plhs[2] = MxArray(success);
84 }
SOLVEPNP_EPNP
SOLVEPNP_UPNP
SOLVEPNP_AP3P
const ConstMap< string, int > PnPMethod
Method used for solving the pose estimation problem.
Definition: solvePnP.cpp:15
SOLVEPNP_DLS
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
SOLVEPNP_P3P
SOLVEPNP_ITERATIVE
#define CV_64F
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: solvePnP.cpp:31
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
STL class.
Global constant definitions.
T c_str(T... args)
bool solvePnP(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs, OutputArray rvec, OutputArray tvec, bool useExtrinsicGuess=false, int flags=SOLVEPNP_ITERATIVE)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
bool empty() const
cv::Mat toMat() const