mexopencv  3.4.1
MEX interface for OpenCV library
calibrateCameraCharuco.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>=4 && (nrhs%2)==0 && 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=4; 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> > charucoCorners(MxArrayToVectorVectorPoint<float>(rhs[0]));
87  vector<vector<int> > charucoIds(MxArrayToVectorVectorPrimitive<int>(rhs[1]));
88  Ptr<CharucoBoard> board;
89  {
90  vector<MxArray> args(rhs[2].toVector<MxArray>());
91  board = create_CharucoBoard(args.begin(), args.end());
92  }
93  Size imageSize(rhs[3].toSize());
94  vector<Vec3d> rvecs, tvecs;
95  Mat stdDeviationsIntrinsics, stdDeviationsExtrinsics, perViewErrors;
96  double reprojErr = calibrateCameraCharuco(charucoCorners, charucoIds,
97  board, imageSize, cameraMatrix, distCoeffs,
98  (nlhs>3 ? rvecs : noArray()),
99  (nlhs>4 ? tvecs : noArray()),
100  (nlhs>5 ? stdDeviationsIntrinsics : noArray()),
101  (nlhs>6 ? stdDeviationsExtrinsics : noArray()),
102  (nlhs>7 ? perViewErrors : noArray()),
103  flags, criteria);
104  plhs[0] = MxArray(cameraMatrix);
105  if (nlhs > 1)
106  plhs[1] = MxArray(distCoeffs);
107  if (nlhs > 2)
108  plhs[2] = MxArray(reprojErr);
109  if (nlhs > 3)
110  plhs[3] = MxArray(rvecs);
111  if (nlhs > 4)
112  plhs[4] = MxArray(tvecs);
113  if (nlhs > 5)
114  plhs[5] = MxArray(stdDeviationsIntrinsics);
115  if (nlhs > 6)
116  plhs[6] = MxArray(stdDeviationsExtrinsics);
117  if (nlhs > 7)
118  plhs[7] = MxArray(perViewErrors);
119 }
CALIB_FIX_PRINCIPAL_POINT
CALIB_FIX_K2
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
CALIB_USE_INTRINSIC_GUESS
STL namespace.
T end(T... args)
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
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.
T begin(T... args)
CALIB_FIX_K1
T c_str(T... args)
CALIB_FIX_K5
CALIB_FIX_FOCAL_LENGTH
double calibrateCameraCharuco(InputArrayOfArrays charucoCorners, InputArrayOfArrays charucoIds, const Ptr< CharucoBoard > &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))
cv::Ptr< cv::aruco::CharucoBoard > create_CharucoBoard(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of CharucoBoard using options in arguments.
CALIB_USE_QR
CALIB_FIX_K6