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);
29 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
32 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=4);
35 vector<MxArray> rhs(prhs, prhs+nrhs);
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();
52 rvec = rhs[i+1].toMat(CV_64F);
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();
64 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
66 if (!rvec.empty() && !tvec.empty())
67 useExtrinsicGuess =
true;
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);
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);
88 mexErrMsgIdAndTxt(
"mexopencv:error",
"Invalid argument");
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.