mexopencv  0.1
mex interface for opencv library
stereoCalibrate.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
25 MxArray toStruct(const Mat& cameraMatrix1, const Mat& distCoeffs1,
26  const Mat& cameraMatrix2, const Mat& distCoeffs2, const Mat& R,
27  const Mat& T, const Mat& E, const Mat& F, double reprojErr)
28 {
29  const char* fieldnames[] = {"cameraMatrix1", "distCoeffs1",
30  "cameraMatrix2", "distCoeffs2", "R", "T", "E", "F", "reprojErr"};
31  MxArray s = MxArray::Struct(fieldnames, 9);
32  s.set("cameraMatrix1", cameraMatrix1);
33  s.set("distCoeffs1", distCoeffs1);
34  s.set("cameraMatrix2", cameraMatrix2);
35  s.set("distCoeffs2", distCoeffs2);
36  s.set("R", R);
37  s.set("T", T);
38  s.set("E", E);
39  s.set("F", F);
40  s.set("reprojErr", reprojErr);
41  return s;
42 }
43 }
44 
52 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
53 {
54  // Check the number of arguments
55  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
56 
57  // Argument vector
58  vector<MxArray> rhs(prhs, prhs+nrhs);
59 
60  // Option processing
61  Mat cameraMatrix1, distCoeffs1,
62  cameraMatrix2, distCoeffs2;
63  int flags = cv::CALIB_FIX_INTRINSIC;
64  TermCriteria criteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6);
65  for (int i=4; i<nrhs; i+=2) {
66  string key(rhs[i].toString());
67  if (key == "CameraMatrix1")
68  cameraMatrix1 = rhs[i+1].toMat(CV_64F);
69  else if (key == "DistCoeffs1")
70  distCoeffs1 = rhs[i+1].toMat(CV_64F);
71  else if (key == "CameraMatrix2")
72  cameraMatrix2 = rhs[i+1].toMat(CV_64F);
73  else if (key == "DistCoeffs2")
74  distCoeffs2 = rhs[i+1].toMat(CV_64F);
75  else if (key == "FixIntrinsic")
76  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_INTRINSIC);
77  else if (key == "UseIntrinsicGuess")
78  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_INTRINSIC_GUESS);
79  else if (key == "FixPrincipalPoint")
80  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_PRINCIPAL_POINT);
81  else if (key == "FixFocalLength")
82  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_FOCAL_LENGTH);
83  else if (key == "FixAspectRatio")
84  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_ASPECT_RATIO);
85  else if (key == "SameFocalLength")
86  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_SAME_FOCAL_LENGTH);
87  else if (key == "ZeroTangentDist")
88  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_ZERO_TANGENT_DIST);
89  else if (key == "FixK1")
90  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K1);
91  else if (key == "FixK2")
92  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K2);
93  else if (key == "FixK3")
94  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K3);
95  else if (key == "FixK4")
96  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K4);
97  else if (key == "FixK5")
98  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K5);
99  else if (key == "FixK6")
100  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K6);
101  else if (key == "RationalModel")
102  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_RATIONAL_MODEL);
103  else if (key == "ThinPrismModel")
104  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_THIN_PRISM_MODEL);
105  else if (key == "FixS1S2S3S4")
106  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_S1_S2_S3_S4);
107  else if (key == "TiltedModel")
108  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_TILTED_MODEL);
109  else if (key == "FixTauXTauY")
110  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_TAUX_TAUY);
111  else if (key == "UseLU")
112  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_LU);
113  else if (key == "Criteria")
114  criteria = rhs[i+1].toTermCriteria();
115  else
116  mexErrMsgIdAndTxt("mexopencv:error",
117  "Unrecognized option %s",key.c_str());
118  }
119 
120  // Process
121  vector<vector<Point3f> > objectPoints(MxArrayToVectorVectorPoint3<float>(rhs[0]));
122  vector<vector<Point2f> > imagePoints1(MxArrayToVectorVectorPoint<float>(rhs[1]));
123  vector<vector<Point2f> > imagePoints2(MxArrayToVectorVectorPoint<float>(rhs[2]));
124  Size imageSize(rhs[3].toSize());
125  Mat R, T, E, F;
126  double reprojErr = stereoCalibrate(objectPoints, imagePoints1, imagePoints2,
127  cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, imageSize,
128  R, T, E, F, flags, criteria);
129  plhs[0] = toStruct(cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2,
130  R, T, E, F, reprojErr);
131 }
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:159
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
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
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
Definition: MxArray.hpp:312
Global constant definitions.