mexopencv  3.4.1
MEX interface for OpenCV library
calibrateCamera.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 
20 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
21 {
22  // Check the number of arguments
23  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=8);
24 
25  // Argument vector
26  vector<MxArray> rhs(prhs, prhs+nrhs);
27 
28  // Option processing
29  Mat cameraMatrix, distCoeffs;
30  int flags = 0;
31  TermCriteria criteria(TermCriteria::COUNT+TermCriteria::EPS, 30, DBL_EPSILON);
32  for (int i=3; i<nrhs; i+=2) {
33  string key(rhs[i].toString());
34  if (key == "CameraMatrix")
35  cameraMatrix = rhs[i+1].toMat(CV_64F);
36  else if (key == "DistCoeffs")
37  distCoeffs = rhs[i+1].toMat(CV_64F);
38  else if (key == "UseIntrinsicGuess")
39  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_INTRINSIC_GUESS);
40  else if (key == "FixPrincipalPoint")
41  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_PRINCIPAL_POINT);
42  else if (key == "FixFocalLength")
43  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_FOCAL_LENGTH);
44  else if (key == "FixAspectRatio")
45  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_ASPECT_RATIO);
46  else if (key == "ZeroTangentDist")
47  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_ZERO_TANGENT_DIST);
48  else if (key == "FixTangentDist")
49  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_TANGENT_DIST);
50  else if (key == "FixK1")
51  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K1);
52  else if (key == "FixK2")
53  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K2);
54  else if (key == "FixK3")
55  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K3);
56  else if (key == "FixK4")
57  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K4);
58  else if (key == "FixK5")
59  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K5);
60  else if (key == "FixK6")
61  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_K6);
62  else if (key == "RationalModel")
63  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_RATIONAL_MODEL);
64  else if (key == "ThinPrismModel")
65  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_THIN_PRISM_MODEL);
66  else if (key == "FixS1S2S3S4")
67  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_S1_S2_S3_S4);
68  else if (key == "TiltedModel")
69  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_TILTED_MODEL);
70  else if (key == "FixTauXTauY")
71  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_FIX_TAUX_TAUY);
72  else if (key == "UseLU")
73  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_LU);
74  else if (key == "UseQR")
75  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_USE_QR);
76  else if (key == "Criteria")
77  criteria = rhs[i+1].toTermCriteria();
78  else
79  mexErrMsgIdAndTxt("mexopencv:error",
80  "Unrecognized option %s", key.c_str());
81  }
82 
83  // Process
84  vector<vector<Point3f> > objectPoints(MxArrayToVectorVectorPoint3<float>(rhs[0]));
85  vector<vector<Point2f> > imagePoints(MxArrayToVectorVectorPoint<float>(rhs[1]));
86  Size imageSize(rhs[2].toSize());
87  vector<Mat> rvecs, tvecs;
88  Mat stdDeviationsIntrinsics, stdDeviationsExtrinsics, perViewErrors;
89  double reprojErr = calibrateCamera(
90  objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs,
91  (nlhs>3 ? rvecs : noArray()),
92  (nlhs>4 ? tvecs : noArray()),
93  (nlhs>5 ? stdDeviationsIntrinsics : noArray()),
94  (nlhs>6 ? stdDeviationsExtrinsics : noArray()),
95  (nlhs>7 ? perViewErrors : noArray()),
96  flags, criteria);
97  plhs[0] = MxArray(cameraMatrix);
98  if (nlhs > 1)
99  plhs[1] = MxArray(distCoeffs);
100  if (nlhs > 2)
101  plhs[2] = MxArray(reprojErr);
102  if (nlhs > 3)
103  plhs[3] = MxArray(rvecs);
104  if (nlhs > 4)
105  plhs[4] = MxArray(tvecs);
106  if (nlhs > 5)
107  plhs[5] = MxArray(stdDeviationsIntrinsics);
108  if (nlhs > 6)
109  plhs[6] = MxArray(stdDeviationsExtrinsics);
110  if (nlhs > 7)
111  plhs[7] = MxArray(perViewErrors);
112 }
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
CALIB_FIX_TANGENT_DIST
double calibrateCamera(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs, OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, OutputArray stdDeviationsIntrinsics, OutputArray stdDeviationsExtrinsics, OutputArray perViewErrors, int flags=0, TermCriteria criteria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, DBL_EPSILON))
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_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
CALIB_THIN_PRISM_MODEL
STL class.
Global constant definitions.
CALIB_FIX_K1
T c_str(T... args)
CALIB_FIX_K5
CALIB_FIX_FOCAL_LENGTH
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
CALIB_USE_QR
CALIB_FIX_K6