mexopencv  3.4.1
MEX interface for OpenCV library
SVD_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
13 // Persistent objects
15 int last_id = 0;
18 }
19 
27 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
28 {
29  // Check the number of arguments
30  nargchk(nrhs>=2 && nlhs<=3);
31 
32  // Argument vector
33  vector<MxArray> rhs(prhs, prhs+nrhs);
34  int id = rhs[0].toInt();
35  string method(rhs[1].toString());
36 
37  // Constructor call
38  if (method == "new") {
39  nargchk(nrhs==2 && nlhs<=1);
40  obj_[++last_id] = makePtr<SVD>();
41  plhs[0] = MxArray(last_id);
42  mexLock();
43  return;
44  }
45  else if (method == "backSubst_static") {
46  nargchk(nrhs==6 && nlhs<=1);
47  Mat w(rhs[2].toMat(rhs[2].isSingle() ? CV_32F : CV_64F)),
48  u(rhs[3].toMat(rhs[3].isSingle() ? CV_32F : CV_64F)),
49  vt(rhs[4].toMat(rhs[4].isSingle() ? CV_32F : CV_64F)),
50  src(rhs[5].toMat(rhs[5].isSingle() ? CV_32F : CV_64F)),
51  dst;
52  SVD::backSubst(w, u, vt, src, dst);
53  plhs[0] = MxArray(dst);
54  return;
55  }
56  else if (method == "solveZ_static") {
57  nargchk(nrhs==3 && nlhs<=1);
58  Mat src(rhs[2].toMat(rhs[2].isSingle() ? CV_32F : CV_64F)),
59  dst;
60  SVD::solveZ(src, dst);
61  plhs[0] = MxArray(dst);
62  return;
63  }
64  else if (method == "compute_static") {
65  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=3);
66  int flags = 0;
67  for (int i=3; i<nrhs; i+=2) {
68  string key(rhs[i].toString());
69  if (key == "Flags")
70  flags = rhs[i+1].toInt();
71  else if (key == "ModifyA")
72  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::MODIFY_A);
73  else if (key == "NoUV")
74  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::NO_UV);
75  else if (key == "FullUV")
76  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::FULL_UV);
77  else
78  mexErrMsgIdAndTxt("mexopencv:error",
79  "Unrecognized option %s", key.c_str());
80  }
81  Mat src(rhs[2].toMat(rhs[2].isSingle() ? CV_32F : CV_64F)),
82  w, u, vt;
83  SVD::compute(src, w, u, vt, flags);
84  plhs[0] = MxArray(w);
85  if (nlhs>1)
86  plhs[1] = MxArray(u);
87  if (nlhs>2)
88  plhs[2] = MxArray(vt);
89  return;
90  }
91 
92  // Big operation switch
93  Ptr<SVD> obj = obj_[id];
94  if (obj.empty())
95  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
96  if (method == "delete") {
97  nargchk(nrhs==2 && nlhs==0);
98  obj_.erase(id);
99  mexUnlock();
100  }
101  else if (method == "compute") {
102  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
103  int flags = 0;
104  for (int i=3; i<nrhs; i+=2) {
105  string key(rhs[i].toString());
106  if (key == "Flags")
107  flags = rhs[i+1].toInt();
108  else if (key == "ModifyA")
109  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::MODIFY_A);
110  else if (key == "NoUV")
111  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::NO_UV);
112  else if (key == "FullUV")
113  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::FULL_UV);
114  else
115  mexErrMsgIdAndTxt("mexopencv:error",
116  "Unrecognized option %s", key.c_str());
117  }
118  Mat src(rhs[2].toMat(rhs[2].isSingle() ? CV_32F : CV_64F));
119  obj->operator()(src, flags);
120  }
121  else if (method == "backSubst") {
122  nargchk(nrhs==3 && nlhs<=1);
123  Mat src(rhs[2].toMat(rhs[2].isSingle() ? CV_32F : CV_64F)),
124  dst;
125  obj->backSubst(src, dst);
126  plhs[0] = MxArray(dst);
127  }
128  else if (method == "get") {
129  nargchk(nrhs==3 && nlhs<=1);
130  string prop(rhs[2].toString());
131  if (prop == "u")
132  plhs[0] = MxArray(obj->u);
133  else if (prop == "vt")
134  plhs[0] = MxArray(obj->vt);
135  else if (prop == "w")
136  plhs[0] = MxArray(obj->w);
137  else
138  mexErrMsgIdAndTxt("mexopencv:error",
139  "Unrecognized property %s", prop.c_str());
140  }
141  else if (method == "set") {
142  nargchk(nrhs==4 && nlhs==0);
143  string prop(rhs[2].toString());
144  if (prop == "u")
145  obj->u = rhs[3].toMat(rhs[3].isSingle() ? CV_32F : CV_64F);
146  else if (prop == "vt")
147  obj->vt = rhs[3].toMat(rhs[3].isSingle() ? CV_32F : CV_64F);
148  else if (prop == "w")
149  obj->w = rhs[3].toMat(rhs[3].isSingle() ? CV_32F : CV_64F);
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized property %s", prop.c_str());
153  }
154  else
155  mexErrMsgIdAndTxt("mexopencv:error",
156  "Unrecognized operation %s", method.c_str());
157 }
int last_id
Last object id to allocate.
Definition: SVD_.cpp:15
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void backSubst(InputArray rhs, OutputArray dst) const
STL namespace.
map< int, Ptr< SVD > > obj_
Object container.
Definition: SVD_.cpp:17
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: SVD_.cpp:27
#define CV_32F
#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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
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.
bool empty() const
Global constant definitions.
T c_str(T... args)
cv::Mat toMat() const