mexopencv  0.1
mex interface for opencv library
solvePnPRansac.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
15  ("Iterative", cv::SOLVEPNP_ITERATIVE)
16  ("EPnP", cv::SOLVEPNP_EPNP)
17  ("P3P", cv::SOLVEPNP_P3P)
18  ("DLS", cv::SOLVEPNP_DLS)
19  ("UPnP", cv::SOLVEPNP_UPNP);
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=4);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36 
37  // Option processing
38  Mat distCoeffs;
39  Mat rvec, tvec;
40  bool useExtrinsicGuess = false;
41  int iterationsCount = 100;
42  float reprojectionError = 8.0f;
43  double confidence = 0.99;
44  int flags = cv::SOLVEPNP_ITERATIVE;
45  for (int i=3; i<nrhs; i+=2) {
46  string key(rhs[i].toString());
47  if (key=="DistCoeffs")
48  distCoeffs = rhs[i+1].toMat(CV_64F);
49  else if (key=="UseExtrinsicGuess")
50  useExtrinsicGuess = rhs[i+1].toBool();
51  else if (key=="Rvec")
52  rvec = rhs[i+1].toMat(CV_64F);
53  else if (key=="Tvec")
54  tvec = rhs[i+1].toMat(CV_64F);
55  else if (key=="Method")
56  flags = PnPMethod[rhs[i+1].toString()];
57  else if (key=="IterationsCount")
58  iterationsCount = rhs[i+1].toInt();
59  else if (key=="ReprojectionError")
60  reprojectionError = rhs[i+1].toFloat();
61  else if (key=="Confidence")
62  confidence = rhs[i+1].toDouble();
63  else
64  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
65  }
66  if (!rvec.empty() && !tvec.empty())
67  useExtrinsicGuess = true;
68 
69  // Process
70  Mat inliers;
71  bool success = false;
72  Mat cameraMatrix(rhs[2].toMat(CV_64F));
73  if (rhs[0].isNumeric() && rhs[1].isNumeric()) {
74  Mat objectPoints(rhs[0].toMat(CV_64F).reshape(3,0)),
75  imagePoints(rhs[1].toMat(CV_64F).reshape(2,0));
76  success = solvePnPRansac(objectPoints, imagePoints, cameraMatrix, distCoeffs,
77  rvec, tvec, useExtrinsicGuess, iterationsCount, reprojectionError,
78  confidence, (nlhs>2 ? inliers : noArray()), flags);
79  }
80  else if (rhs[0].isCell() && rhs[1].isCell()) {
81  vector<Point3d> objectPoints(rhs[0].toVector<Point3d>());
82  vector<Point2d> imagePoints(rhs[1].toVector<Point2d>());
83  success = solvePnPRansac(objectPoints, imagePoints, cameraMatrix, distCoeffs,
84  rvec, tvec, useExtrinsicGuess, iterationsCount, reprojectionError,
85  confidence, (nlhs>2 ? inliers : noArray()), flags);
86  }
87  else
88  mexErrMsgIdAndTxt("mexopencv:error","Invalid argument");
89  plhs[0] = MxArray(rvec);
90  if (nlhs>1)
91  plhs[1] = MxArray(tvec);
92  if (nlhs>2)
93  plhs[2] = MxArray(inliers);
94  if (nlhs>3)
95  plhs[3] = MxArray(success);
96 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927