mexopencv  3.4.1
MEX interface for OpenCV library
solvePnPRansac.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<=4);
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 iterationsCount = 100;
43  float reprojectionError = 8.0f;
44  double confidence = 0.99;
45  int flags = cv::SOLVEPNP_ITERATIVE;
46  for (int i=3; i<nrhs; i+=2) {
47  string key(rhs[i].toString());
48  if (key == "DistCoeffs")
49  distCoeffs = rhs[i+1].toMat(CV_64F);
50  else if (key == "UseExtrinsicGuess")
51  useExtrinsicGuess = rhs[i+1].toBool();
52  else if (key == "Rvec")
53  rvec = rhs[i+1].toMat(CV_64F);
54  else if (key == "Tvec")
55  tvec = rhs[i+1].toMat(CV_64F);
56  else if (key == "Method")
57  flags = PnPMethod[rhs[i+1].toString()];
58  else if (key == "IterationsCount")
59  iterationsCount = rhs[i+1].toInt();
60  else if (key == "ReprojectionError")
61  reprojectionError = rhs[i+1].toFloat();
62  else if (key == "Confidence")
63  confidence = rhs[i+1].toDouble();
64  else
65  mexErrMsgIdAndTxt("mexopencv:error",
66  "Unrecognized option %s", key.c_str());
67  }
68  if (!rvec.empty() && !tvec.empty())
69  useExtrinsicGuess = true;
70 
71  // Process
72  Mat inliers;
73  bool success = false;
74  Mat cameraMatrix(rhs[2].toMat(CV_64F));
75  if (rhs[0].isNumeric() && rhs[1].isNumeric()) {
76  Mat objectPoints(rhs[0].toMat(CV_32F).reshape(3,0)),
77  imagePoints(rhs[1].toMat(CV_32F).reshape(2,0));
78  success = solvePnPRansac(objectPoints, imagePoints, cameraMatrix, distCoeffs,
79  rvec, tvec, useExtrinsicGuess, iterationsCount, reprojectionError,
80  confidence, (nlhs>3 ? inliers : noArray()), flags);
81  }
82  else if (rhs[0].isCell() && rhs[1].isCell()) {
83  vector<Point3f> objectPoints(rhs[0].toVector<Point3f>());
84  vector<Point2f> imagePoints(rhs[1].toVector<Point2f>());
85  success = solvePnPRansac(objectPoints, imagePoints, cameraMatrix, distCoeffs,
86  rvec, tvec, useExtrinsicGuess, iterationsCount, reprojectionError,
87  confidence, (nlhs>3 ? inliers : noArray()), flags);
88  }
89  else
90  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
91  plhs[0] = MxArray(rvec);
92  if (nlhs>1)
93  plhs[1] = MxArray(tvec);
94  if (nlhs>2)
95  plhs[2] = MxArray(success);
96  if (nlhs>3)
97  plhs[3] = MxArray(inliers);
98 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
const ConstMap< string, int > PnPMethod
Method used for solving the pose estimation problem.
SOLVEPNP_EPNP
SOLVEPNP_UPNP
SOLVEPNP_AP3P
SOLVEPNP_DLS
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
SOLVEPNP_P3P
SOLVEPNP_ITERATIVE
#define CV_32F
InputOutputArray noArray()
#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)
bool solvePnPRansac(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs, OutputArray rvec, OutputArray tvec, bool useExtrinsicGuess=false, int iterationsCount=100, float reprojectionError=8.0, double confidence=0.99, OutputArray inliers=noArray(), 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