mexopencv  3.4.1
MEX interface for OpenCV library
stereoCalibrate.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/calib3d.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 namespace {
26 MxArray toStruct(const Mat& cameraMatrix1, const Mat& distCoeffs1,
27  const Mat& cameraMatrix2, const Mat& distCoeffs2, const Mat& R,
28  const Mat& T, const Mat& E, const Mat& F, double reprojErr)
29 {
30  const char* fieldnames[] = {"cameraMatrix1", "distCoeffs1",
31  "cameraMatrix2", "distCoeffs2", "R", "T", "E", "F", "reprojErr"};
32  MxArray s = MxArray::Struct(fieldnames, 9);
33  s.set("cameraMatrix1", cameraMatrix1);
34  s.set("distCoeffs1", distCoeffs1);
35  s.set("cameraMatrix2", cameraMatrix2);
36  s.set("distCoeffs2", distCoeffs2);
37  s.set("R", R);
38  s.set("T", T);
39  s.set("E", E);
40  s.set("F", F);
41  s.set("reprojErr", reprojErr);
42  return s;
43 }
44 }
45 
53 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
54 {
55  // Check the number of arguments
56  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
57 
58  // Argument vector
59  vector<MxArray> rhs(prhs, prhs+nrhs);
60 
61  // Option processing
62  Mat cameraMatrix1, distCoeffs1,
63  cameraMatrix2, distCoeffs2,
64  R, T;
65  int flags = cv::CALIB_FIX_INTRINSIC;
66  TermCriteria criteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6);
67  for (int i=4; i<nrhs; i+=2) {
68  string key(rhs[i].toString());
69  if (key == "CameraMatrix1")
70  cameraMatrix1 = rhs[i+1].toMat(CV_64F);
71  else if (key == "DistCoeffs1")
72  distCoeffs1 = rhs[i+1].toMat(CV_64F);
73  else if (key == "CameraMatrix2")
74  cameraMatrix2 = rhs[i+1].toMat(CV_64F);
75  else if (key == "DistCoeffs2")
76  distCoeffs2 = rhs[i+1].toMat(CV_64F);
77  else if (key == "R")
78  R = rhs[i+1].toMat(CV_64F);
79  else if (key == "T")
80  T = rhs[i+1].toMat(CV_64F);
81  else if (key == "FixIntrinsic")
82  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_INTRINSIC);
83  else if (key == "UseIntrinsicGuess")
84  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_INTRINSIC_GUESS);
85  else if (key == "UseExtrinsicGuess")
86  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_EXTRINSIC_GUESS);
87  else if (key == "FixPrincipalPoint")
88  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_PRINCIPAL_POINT);
89  else if (key == "FixFocalLength")
90  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_FOCAL_LENGTH);
91  else if (key == "FixAspectRatio")
92  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_ASPECT_RATIO);
93  else if (key == "SameFocalLength")
94  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_SAME_FOCAL_LENGTH);
95  else if (key == "ZeroTangentDist")
96  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_ZERO_TANGENT_DIST);
97  else if (key == "FixTangentDist")
98  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_TANGENT_DIST);
99  else if (key == "FixK1")
100  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K1);
101  else if (key == "FixK2")
102  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K2);
103  else if (key == "FixK3")
104  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K3);
105  else if (key == "FixK4")
106  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K4);
107  else if (key == "FixK5")
108  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K5);
109  else if (key == "FixK6")
110  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K6);
111  else if (key == "RationalModel")
112  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_RATIONAL_MODEL);
113  else if (key == "ThinPrismModel")
114  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_THIN_PRISM_MODEL);
115  else if (key == "FixS1S2S3S4")
116  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_S1_S2_S3_S4);
117  else if (key == "TiltedModel")
118  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_TILTED_MODEL);
119  else if (key == "FixTauXTauY")
120  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_TAUX_TAUY);
121  else if (key == "UseLU")
122  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_LU);
123  else if (key == "UseQR")
124  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_QR);
125  else if (key == "Criteria")
126  criteria = rhs[i+1].toTermCriteria();
127  else
128  mexErrMsgIdAndTxt("mexopencv:error",
129  "Unrecognized option %s", key.c_str());
130  }
131 
132  // Process
133  vector<vector<Point3f> > objectPoints(MxArrayToVectorVectorPoint3<float>(rhs[0]));
134  vector<vector<Point2f> > imagePoints1(MxArrayToVectorVectorPoint<float>(rhs[1]));
135  vector<vector<Point2f> > imagePoints2(MxArrayToVectorVectorPoint<float>(rhs[2]));
136  Size imageSize(rhs[3].toSize());
137  Mat E, F, perViewErrors;
138  double reprojErr = stereoCalibrate(objectPoints, imagePoints1, imagePoints2,
139  cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, imageSize,
140  R, T, E, F, (nlhs>1 ? perViewErrors : noArray()), flags, criteria);
141  plhs[0] = toStruct(cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2,
142  R, T, E, F, reprojErr);
143  if (nlhs > 1)
144  plhs[1] = MxArray(perViewErrors);
145 }
CALIB_FIX_PRINCIPAL_POINT
CALIB_FIX_K2
CALIB_USE_INTRINSIC_GUESS
STL namespace.
CALIB_FIX_K4
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
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.
double stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2, InputOutputArray cameraMatrix1, InputOutputArray distCoeffs1, InputOutputArray cameraMatrix2, InputOutputArray distCoeffs2, Size imageSize, InputOutputArray R, InputOutputArray T, OutputArray E, OutputArray F, OutputArray perViewErrors, int flags=CALIB_FIX_INTRINSIC, TermCriteria criteria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6))
CALIB_FIX_TANGENT_DIST
CALIB_FIX_S1_S2_S3_S4
CALIB_TILTED_MODEL
CALIB_USE_LU
CALIB_ZERO_TANGENT_DIST
CALIB_FIX_K3
CALIB_RATIONAL_MODEL
InputOutputArray noArray()
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:174
#define CV_64F
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
CALIB_FIX_ASPECT_RATIO
CALIB_USE_EXTRINSIC_GUESS
CALIB_FIX_TAUX_TAUY
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
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
CALIB_SAME_FOCAL_LENGTH
CALIB_THIN_PRISM_MODEL
STL class.
Global constant definitions.
CALIB_FIX_K1
T c_str(T... args)
CALIB_FIX_K5
CALIB_FIX_FOCAL_LENGTH
MxArray toStruct(const Mat &cameraMatrix1, const Mat &distCoeffs1, const Mat &cameraMatrix2, const Mat &distCoeffs2, const Mat &R, const Mat &T, const Mat &E, const Mat &F, double reprojErr)
Create a new MxArray from stereo calibration results.
CALIB_FIX_INTRINSIC
CALIB_USE_QR
CALIB_FIX_K6