mexopencv  0.1
mex interface for opencv library
calibrateCamera.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>=3 && (nrhs%2)==1 && nlhs<=5);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  Mat cameraMatrix, distCoeffs;
29  int flags = 0;
30  TermCriteria criteria(TermCriteria::COUNT+TermCriteria::EPS, 30, DBL_EPSILON);
31  for (int i=3; i<nrhs; i+=2) {
32  string key(rhs[i].toString());
33  if (key == "CameraMatrix")
34  cameraMatrix = rhs[i+1].toMat(CV_64F);
35  else if (key == "DistCoeffs")
36  distCoeffs = rhs[i+1].toMat(CV_64F);
37  else if (key == "UseIntrinsicGuess")
38  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_INTRINSIC_GUESS);
39  else if (key == "FixPrincipalPoint")
40  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_PRINCIPAL_POINT);
41  else if (key == "FixAspectRatio")
42  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_ASPECT_RATIO);
43  else if (key == "ZeroTangentDist")
44  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_ZERO_TANGENT_DIST);
45  else if (key == "FixK1")
46  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K1);
47  else if (key == "FixK2")
48  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K2);
49  else if (key == "FixK3")
50  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K3);
51  else if (key == "FixK4")
52  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K4);
53  else if (key == "FixK5")
54  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K5);
55  else if (key == "FixK6")
56  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K6);
57  else if (key == "RationalModel")
58  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_RATIONAL_MODEL);
59  else if (key == "ThinPrismModel")
60  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_THIN_PRISM_MODEL);
61  else if (key == "FixS1S2S3S4")
62  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_S1_S2_S3_S4);
63  else if (key == "TiltedModel")
64  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_TILTED_MODEL);
65  else if (key == "FixTauXTauY")
66  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_TAUX_TAUY);
67  else if (key == "UseLU")
68  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_LU);
69  else if (key == "Criteria")
70  criteria = rhs[i+1].toTermCriteria();
71  else
72  mexErrMsgIdAndTxt("mexopencv:error",
73  "Unrecognized option %s",key.c_str());
74  }
75 
76  // Process
77  vector<vector<Point3f> > objectPoints(MxArrayToVectorVectorPoint3<float>(rhs[0]));
78  vector<vector<Point2f> > imagePoints(MxArrayToVectorVectorPoint<float>(rhs[1]));
79  Size imageSize(rhs[2].toSize());
80  vector<Mat> rvecs, tvecs;
81  double reprojErr = calibrateCamera(objectPoints, imagePoints, imageSize,
82  cameraMatrix, distCoeffs, (nlhs>3 ? rvecs : noArray()),
83  (nlhs>4 ? tvecs : noArray()), flags, criteria);
84  plhs[0] = MxArray(cameraMatrix);
85  if (nlhs>1)
86  plhs[1] = MxArray(distCoeffs);
87  if (nlhs>2)
88  plhs[2] = MxArray(reprojErr);
89  if (nlhs>3)
90  plhs[3] = MxArray(rvecs);
91  if (nlhs>4)
92  plhs[4] = MxArray(tvecs);
93 }
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:159
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.