mexopencv  3.4.1
MEX interface for OpenCV library
rectify3Collinear.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 {
27 MxArray toStruct(const Mat& R1, const Mat& R2, const Mat& R3,
28  const Mat& P1, const Mat& P2, const Mat& P3, const Mat& Q,
29  const Rect& roi1, const Rect& roi2, float ratio)
30 {
31  const char* fieldnames[] = {"R1", "R2", "R3", "P1", "P2", "P3", "Q",
32  "roi1", "roi2", "ratio"};
33  MxArray s = MxArray::Struct(fieldnames, 10);
34  s.set("R1", R1);
35  s.set("R2", R2);
36  s.set("R3", R3);
37  s.set("P1", P1);
38  s.set("P2", P2);
39  s.set("P3", P3);
40  s.set("Q", Q);
41  s.set("roi1", roi1);
42  s.set("roi2", roi2);
43  s.set("ratio", ratio);
44  return s;
45 }
46 }
47 
55 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
56 {
57  // Check the number of arguments
58  nargchk(nrhs>=11 && (nrhs%2)==1 && nlhs<=1);
59 
60  // Argument vector
61  vector<MxArray> rhs(prhs, prhs+nrhs);
62 
63  // Option processing
64  vector<Point2f> imgpt1, imgpt3;
65  double alpha = -1;
66  Size newImageSize;
67  int flags = cv::CALIB_ZERO_DISPARITY;
68  for (int i=11; i<nrhs; i+=2) {
69  string key(rhs[i].toString());
70  if (key == "ImgPoints1")
71  imgpt1 = rhs[i+1].toVector<Point2f>();
72  else if (key == "ImgPoints3")
73  imgpt3 = rhs[i+1].toVector<Point2f>();
74  else if (key == "Alpha")
75  alpha = rhs[i+1].toDouble();
76  else if (key == "NewImageSize")
77  newImageSize = rhs[i+1].toSize();
78  else if (key == "ZeroDisparity")
79  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_ZERO_DISPARITY);
80  else
81  mexErrMsgIdAndTxt("mexopencv:error",
82  "Unrecognized option %s", key.c_str());
83  }
84 
85  // Process
86  Mat cameraMatrix1(rhs[0].toMat(CV_64F)),
87  distCoeffs1(rhs[1].toMat(CV_64F)),
88  cameraMatrix2(rhs[2].toMat(CV_64F)),
89  distCoeffs2(rhs[3].toMat(CV_64F)),
90  cameraMatrix3(rhs[4].toMat(CV_64F)),
91  distCoeffs3(rhs[5].toMat(CV_64F)),
92  R12(rhs[7].toMat(CV_64F)),
93  T12(rhs[8].toMat(CV_64F)),
94  R13(rhs[9].toMat(CV_64F)),
95  T13(rhs[10].toMat(CV_64F)),
96  R1, R2, R3, P1, P2, P3, Q;
97  Size imageSize(rhs[6].toSize());
98  Rect roi1, roi2;
99  float ratio = rectify3Collinear(cameraMatrix1, distCoeffs1,
100  cameraMatrix2, distCoeffs2, cameraMatrix3, distCoeffs3,
101  imgpt1, imgpt3, imageSize, R12, T12, R13, T13,
102  R1, R2, R3, P1, P2, P3, Q, alpha, newImageSize, &roi1, &roi2, flags);
103  plhs[0] = toStruct(R1, R2, R3, P1, P2, P3, Q, roi1, roi2, ratio);
104 }
MxArray toStruct(const Mat &R1, const Mat &R2, const Mat &R3, const Mat &P1, const Mat &P2, const Mat &P3, const Mat &Q, const Rect &roi1, const Rect &roi2, float ratio)
Create a new MxArray from stereo rectified transforms.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL namespace.
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
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:174
float rectify3Collinear(InputArray cameraMatrix1, InputArray distCoeffs1, InputArray cameraMatrix2, InputArray distCoeffs2, InputArray cameraMatrix3, InputArray distCoeffs3, InputArrayOfArrays imgpt1, InputArrayOfArrays imgpt3, Size imageSize, InputArray R12, InputArray T12, InputArray R13, InputArray T13, OutputArray R1, OutputArray R2, OutputArray R3, OutputArray P1, OutputArray P2, OutputArray P3, OutputArray Q, double alpha, Size newImgSize, Rect *roi1, Rect *roi2, int flags)
#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_ZERO_DISPARITY
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)
cv::Mat toMat() const