mexopencv  3.4.1
MEX interface for OpenCV library
convertPointsToHomogeneous.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==1 && nlhs<=1);
24 
25  // Argument vector
26  vector<MxArray> rhs(prhs, prhs+nrhs);
27 
28  // Process
29  if (rhs[0].isNumeric()) {
30  // input is Nx2/Nx1x2/1xNx2 or Nx3/Nx1x3/1xNx3 matrix
31  Mat src(rhs[0].toMat(rhs[0].isSingle() ? CV_32F : CV_64F)), dst;
32  bool cn1 = (src.channels() == 1);
34  if (cn1) dst = dst.reshape(1,0); // N-by-(3/4) numeric matrix
35  plhs[0] = MxArray(dst);
36  }
37  else if (rhs[0].isCell() && !rhs[0].isEmpty()) {
38  mwSize dim = rhs[0].at<MxArray>(0).numel();
39  if (dim == 2) {
40  // input is cell array {[x,y], [x,y], ..}
41  vector<Point2d> src(rhs[0].toVector<Point2d>());
42  vector<Point3d> dst;
44  plhs[0] = MxArray(dst); // 1xN cell-array {[x,y,z], ...}
45  //plhs[0] = MxArray(Mat(dst,false).reshape(1,0)); // N-by-3 numeric matrix
46  }
47  else if (dim == 3) {
48  // input is cell array {[x,y,z], [x,y,z], ..}
49  vector<Point3d> src(rhs[0].toVector<Point3d>());
50  vector<Vec4d> dst;
52  plhs[0] = MxArray(dst); // 1xN cell-array {[x,y,z,w], ...}
53  //plhs[0] = MxArray(Mat(dst, false).reshape(1, 0)); // N-by-4 numeric matrix
54  }
55  else
56  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
57  }
58  else
59  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
60 }
STL namespace.
void convertPointsToHomogeneous(InputArray src, OutputArray dst)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
T at(T... args)
#define CV_32F
Mat reshape(int cn, int rows=0) const
#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...
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
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL class.
Global constant definitions.
cv::Mat toMat() const