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