mexopencv  3.4.1
MEX interface for OpenCV library
solveP3P.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  ("P3P", cv::SOLVEPNP_P3P)
17  ("AP3P", cv::SOLVEPNP_AP3P);
18 }
19 
27 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
28 {
29  // Check the number of arguments
30  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=3);
31 
32  // Argument vector
33  vector<MxArray> rhs(prhs, prhs + nrhs);
34 
35  // Option processing
36  Mat distCoeffs;
37  int flags = cv::SOLVEPNP_P3P;
38  for (int i=3; i<nrhs; i+=2) {
39  string key(rhs[i].toString());
40  if (key == "DistCoeffs")
41  distCoeffs = rhs[i+1].toMat(CV_64F);
42  else if (key == "Method")
43  flags = P3PMethod[rhs[i+1].toString()];
44  else
45  mexErrMsgIdAndTxt("mexopencv:error",
46  "Unrecognized option %s", key.c_str());
47  }
48 
49  // Process
50  vector<Mat> rvecs, tvecs;
51  int solutions = 0;
52  Mat cameraMatrix(rhs[2].toMat(CV_64F));
53  if (rhs[0].isNumeric() && rhs[1].isNumeric()) {
54  Mat objectPoints(rhs[0].toMat(CV_64F).reshape(3,0)),
55  imagePoints(rhs[1].toMat(CV_64F).reshape(2,0));
56  solutions = solveP3P(objectPoints, imagePoints, cameraMatrix, distCoeffs,
57  rvecs, tvecs, flags);
58  }
59  else if (rhs[0].isCell() && rhs[1].isCell()) {
60  vector<Point3d> objectPoints(rhs[0].toVector<Point3d>());
61  vector<Point2d> imagePoints(rhs[1].toVector<Point2d>());
62  solutions = solveP3P(objectPoints, imagePoints, cameraMatrix, distCoeffs,
63  rvecs, tvecs, flags);
64  }
65  else
66  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
67  plhs[0] = MxArray(rvecs);
68  if (nlhs > 1)
69  plhs[1] = MxArray(tvecs);
70  if (nlhs > 2)
71  plhs[2] = MxArray(solutions);
72 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: solveP3P.cpp:27
SOLVEPNP_AP3P
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
SOLVEPNP_P3P
const ConstMap< string, int > P3PMethod
Method used for solving the pose estimation problem.
Definition: solveP3P.cpp:15
#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...
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)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const
int solveP3P(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs, OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, int flags)