mexopencv  3.4.1
MEX interface for OpenCV library
fisheyeStereoCalibrate.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 {
24 MxArray toStruct(const Mat& K1, const Mat& D1, const Mat& K2, const Mat& D2,
25  const Mat& R, const Mat& T, double rms)
26 {
27  const char* fieldnames[] = {"cameraMatrix1", "distCoeffs1",
28  "cameraMatrix2", "distCoeffs2", "R", "T", "reprojErr"};
29  MxArray s = MxArray::Struct(fieldnames, 7);
30  s.set("cameraMatrix1", K1);
31  s.set("distCoeffs1", D1);
32  s.set("cameraMatrix2", K2);
33  s.set("distCoeffs2", D2);
34  s.set("R", R);
35  s.set("T", T);
36  s.set("reprojErr", rms);
37  return s;
38 }
39 }
40 
48 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
49 {
50  // Check the number of arguments
51  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
52 
53  // Argument vector
54  vector<MxArray> rhs(prhs, prhs+nrhs);
55 
56  // Option processing
57  Mat K1, D1, K2, D2;
58  int flags = cv::CALIB_FIX_INTRINSIC;
59  TermCriteria criteria(TermCriteria::COUNT+TermCriteria::EPS, 100, DBL_EPSILON);
60  for (int i=4; i<nrhs; i+=2) {
61  string key(rhs[i].toString());
62  if (key == "CameraMatrix1")
63  K1 = rhs[i+1].toMat(CV_64F);
64  else if (key == "DistCoeffs1")
65  D1 = rhs[i+1].toMat(CV_64F);
66  else if (key == "CameraMatrix2")
67  K2 = rhs[i+1].toMat(CV_64F);
68  else if (key == "DistCoeffs2")
69  D2 = rhs[i+1].toMat(CV_64F);
70  else if (key == "UseIntrinsicGuess")
71  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_USE_INTRINSIC_GUESS);
72  else if (key == "RecomputeExtrinsic")
73  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC);
74  else if (key == "CheckCond")
75  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_CHECK_COND);
76  else if (key == "FixSkew")
77  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_FIX_SKEW);
78  else if (key == "FixK1")
79  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_FIX_K1);
80  else if (key == "FixK2")
81  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_FIX_K2);
82  else if (key == "FixK3")
83  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_FIX_K3);
84  else if (key == "FixK4")
85  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_FIX_K4);
86  else if (key == "FixIntrinsic")
87  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::fisheye::CALIB_FIX_INTRINSIC);
88  else if (key == "Criteria")
89  criteria = rhs[i+1].toTermCriteria();
90  else
91  mexErrMsgIdAndTxt("mexopencv:error",
92  "Unrecognized option %s", key.c_str());
93  }
94 
95  // Process
96  vector<vector<Point3d> > objectPoints(MxArrayToVectorVectorPoint3<double>(rhs[0]));
97  vector<vector<Point2d> > imagePoints1(MxArrayToVectorVectorPoint<double>(rhs[1]));
98  vector<vector<Point2d> > imagePoints2(MxArrayToVectorVectorPoint<double>(rhs[2]));
99  Size imageSize(rhs[3].toSize());
100  Mat R, T;
101  double rms = fisheye::stereoCalibrate(objectPoints, imagePoints1, imagePoints2,
102  K1, D1, K2, D2, imageSize, R, T, flags, criteria);
103  plhs[0] = toStruct(K1, D1, K2, D2, R, T, rms);
104 }
STL namespace.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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
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))
#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...
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
STL class.
Global constant definitions.
T c_str(T... args)
MxArray toStruct(const Mat &K1, const Mat &D1, const Mat &K2, const Mat &D2, const Mat &R, const Mat &T, double rms)
Create a new MxArray from stereo calibration results.
CALIB_FIX_INTRINSIC