mexopencv  3.4.1
MEX interface for OpenCV library
convertPointsFromHomogeneous.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 Nx3/Nx1x3/1xNx3 or Nx4/Nx1x4/1xNx4 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-(2/3) numeric matrix
35  plhs[0] = MxArray(dst);
36  }
37  else if (rhs[0].isCell() && !rhs[0].isEmpty()) {
38  mwSize dims = rhs[0].at<MxArray>(0).numel();
39  if (dims == 3) {
40  // input is cell array {[x,y,z], [x,y,z], ..}
41  vector<Point3d> src(rhs[0].toVector<Point3d>());
42  vector<Point2d> dst;
44  plhs[0] = MxArray(dst); // 1xN cell-array {[x,y], ...}
45  //plhs[0] = MxArray(Mat(dst,false).reshape(1,0)); // N-by-2 numeric matrix
46  }
47  else if (dims == 4) {
48  // input is cell array {[x,y,z,w], [x,y,z,w], ..}
49  //vector<Vec4d> src(rhs[0].toVector<Vec4d>());
50  vector<Vec4d> src(MxArrayToVectorVec<double,4>(rhs[0]));
51  vector<Point3d> dst;
53  plhs[0] = MxArray(dst); // 1xN cell-array {[x,y,z], ...}
54  //plhs[0] = MxArray(Mat(dst,false).reshape(1,0)); // N-by-3 numeric matrix
55  }
56  else
57  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
58  }
59  else
60  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
61 }
void convertPointsFromHomogeneous(InputArray src, OutputArray dst)
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
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
STL class.
Global constant definitions.
cv::Mat toMat() const