mexopencv  0.1
mex interface for opencv library
convertPointsFromHomogeneous.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 Nx3/Nx1x3/1xNx3 or Nx4/Nx1x4/1xNx4 matrix
30  Mat src(rhs[0].toMat(rhs[0].isSingle() ? CV_32F : CV_64F)), dst;
31  bool cn1 = (src.channels() == 1);
32  convertPointsFromHomogeneous(src, dst);
33  if (cn1) dst = dst.reshape(1,0);// N-by-(2/3) numeric matrix
34  plhs[0] = MxArray(dst);
35  }
36  else if (rhs[0].isCell() && !rhs[0].isEmpty()) {
37  mwSize dims = rhs[0].at<MxArray>(0).numel();
38  if (dims == 3) {
39  // input is cell array {[x,y,z], [x,y,z], ..}
40  vector<Point3d> src(rhs[0].toVector<Point3d>());
41  vector<Point2d> dst;
42  convertPointsFromHomogeneous(src, dst);
43  plhs[0] = MxArray(dst); // 1xN cell-array {[x,y], ...}
44  //plhs[0] = MxArray(Mat(dst,false).reshape(1,0)); // N-by-2 numeric matrix
45  }
46  else if (dims == 4) {
47  // input is cell array {[x,y,z,w], [x,y,z,w], ..}
48  //vector<Vec4d> src(rhs[0].toVector<Vec4d>());
49  vector<Vec4d> src(MxArrayToVectorVec<double,4>(rhs[0]));
50  vector<Point3d> dst;
51  convertPointsFromHomogeneous(src, dst);
52  plhs[0] = MxArray(dst); // 1xN cell-array {[x,y,z], ...}
53  //plhs[0] = MxArray(Mat(dst,false).reshape(1,0)); // N-by-3 numeric matrix
54  }
55  else
56  mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
57  }
58  else
59  mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
60 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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
Global constant definitions.