mexopencv  0.1
mex interface for opencv library
projectPoints.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Check the number of arguments
22  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  Mat distCoeffs;
29  double aspectRatio = 0;
30  for (int i=4; i<nrhs; i+=2) {
31  string key(rhs[i].toString());
32  if (key=="DistCoeffs")
33  distCoeffs = rhs[i+1].toMat(CV_64F);
34  else if (key=="AspectRatio")
35  aspectRatio = rhs[i+1].toDouble();
36  else
37  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
38  }
39 
40  // Process
41  Mat rvec(rhs[1].toMat(CV_64F)),
42  tvec(rhs[2].toMat(CV_64F)),
43  cameraMatrix(rhs[3].toMat(CV_64F)),
44  jacobian;
45  if (rhs[0].isNumeric()) {
46  Mat objectPoints(rhs[0].toMat(CV_64F)),
47  imagePoints;
48  projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs,
49  imagePoints, (nlhs>1 ? jacobian : noArray()), aspectRatio);
50  if (objectPoints.channels() == 1 && objectPoints.cols == 3)
51  imagePoints = imagePoints.reshape(1,0); // Nx2
52  plhs[0] = MxArray(imagePoints);
53  }
54  else if (rhs[0].isCell()) {
55  vector<Point3d> objectPoints(rhs[0].toVector<Point3d>());
56  vector<Point2d> imagePoints;
57  projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs,
58  imagePoints, (nlhs>1 ? jacobian : noArray()), aspectRatio);
59  plhs[0] = MxArray(imagePoints);
60  }
61  else
62  mexErrMsgIdAndTxt("mexopencv:error","Invalid argument");
63  if (nlhs>1)
64  plhs[1] = MxArray(jacobian);
65 }
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.