mexopencv  3.4.1
MEX interface for OpenCV library
stereoRectify.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& R1, const Mat& R2, const Mat& P1, const Mat& P2,
25  const Mat& Q, const Rect& roi1, const Rect& roi2)
26 {
27  const char* fieldnames[] = {"R1", "R2", "P1", "P2", "Q", "roi1", "roi2"};
28  MxArray s = MxArray::Struct(fieldnames, 7);
29  s.set("R1", R1);
30  s.set("R2", R2);
31  s.set("P1", P1);
32  s.set("P2", P2);
33  s.set("Q", Q);
34  s.set("roi1", roi1);
35  s.set("roi2", roi2);
36  return s;
37 }
38 }
39 
47 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
48 {
49  // Check the number of arguments
50  nargchk(nrhs>=7 && (nrhs%2)==1 && nlhs<=1);
51 
52  // Argument vector
53  vector<MxArray> rhs(prhs, prhs+nrhs);
54 
55  // Option processing
56  int flags = cv::CALIB_ZERO_DISPARITY;
57  double alpha = -1;
58  Size newImageSize;
59  for (int i=7; i<nrhs; i+=2) {
60  string key(rhs[i].toString());
61  if (key == "ZeroDisparity")
62  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CALIB_ZERO_DISPARITY);
63  else if (key == "Alpha")
64  alpha = rhs[i+1].toDouble();
65  else if (key == "NewImageSize")
66  newImageSize = rhs[i+1].toSize();
67  else
68  mexErrMsgIdAndTxt("mexopencv:error",
69  "Unrecognized option %s", key.c_str());
70  }
71 
72  // Process
73  Mat cameraMatrix1(rhs[0].toMat(CV_64F)),
74  distCoeffs1(rhs[1].toMat(CV_64F)),
75  cameraMatrix2(rhs[2].toMat(CV_64F)),
76  distCoeffs2(rhs[3].toMat(CV_64F)),
77  R(rhs[5].toMat(CV_64F)),
78  T(rhs[6].toMat(CV_64F)),
79  R1, R2, P1, P2, Q;
80  Size imageSize(rhs[4].toSize());
81  Rect roi1, roi2;
82  stereoRectify(cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2,
83  imageSize, R, T, R1, R2, P1, P2, Q, flags, alpha, newImageSize,
84  &roi1, &roi2);
85  plhs[0] = toStruct(R1, R2, P1, P2, Q, roi1, roi2);
86 }
void stereoRectify(InputArray cameraMatrix1, InputArray distCoeffs1, InputArray cameraMatrix2, InputArray distCoeffs2, Size imageSize, InputArray R, InputArray T, OutputArray R1, OutputArray R2, OutputArray P1, OutputArray P2, OutputArray Q, int flags=CALIB_ZERO_DISPARITY, double alpha=-1, Size newImageSize=Size(), Rect *validPixROI1=0, Rect *validPixROI2=0)
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
#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...
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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)
MxArray toStruct(const Mat &R1, const Mat &R2, const Mat &P1, const Mat &P2, const Mat &Q, const Rect &roi1, const Rect &roi2)
Create a new MxArray from stereo rectified transforms.
cv::Mat toMat() const