mexopencv  0.1
mex interface for opencv library
convertPointsToHomogeneous.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Check the number of arguments
22  nargchk(nrhs==1 && nlhs<=1);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Process
28  if (rhs[0].isNumeric()) {
29  // input is Nx2/Nx1x2/1xNx2 or Nx3/Nx1x3/1xNx3 matrix
30  Mat src(rhs[0].toMat(rhs[0].isSingle() ? CV_32F : CV_64F)), dst;
31  bool cn1 = (src.channels() == 1);
32  convertPointsToHomogeneous(src, dst);
33  if (cn1) dst = dst.reshape(1,0); // N-by-(3/4) numeric matrix
34  plhs[0] = MxArray(dst);
35  }
36  else if (rhs[0].isCell() && !rhs[0].isEmpty()) {
37  mwSize dim = rhs[0].at<MxArray>(0).numel();
38  if (dim == 2) {
39  // input is cell array {[x,y], [x,y], ..}
40  vector<Point2d> src(rhs[0].toVector<Point2d>());
41  vector<Point3d> dst;
42  convertPointsToHomogeneous(src, dst);
43  plhs[0] = MxArray(dst); // 1xN cell-array {[x,y,z], ...}
44  //plhs[0] = MxArray(Mat(dst,false).reshape(1,0)); // N-by-3 numeric matrix
45  }
46  else if (dim == 3) {
47  // input is cell array {[x,y,z], [x,y,z], ..}
48  vector<Point3d> src(rhs[0].toVector<Point3d>());
49  vector<Vec4d> dst;
50  convertPointsToHomogeneous(src, dst);
51  plhs[0] = MxArray(dst); // 1xN cell-array {[x,y,z,w], ...}
52  //plhs[0] = MxArray(Mat(dst, false).reshape(1, 0)); // N-by-4 numeric matrix
53  }
54  else
55  mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
56  }
57  else
58  mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
59 }
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.