mexopencv  3.4.1
MEX interface for OpenCV library
GPCForest_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/optflow.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::optflow;
13 
14 //HACK: most examples in opencv use nTrees=5
15 static const int nTrees = 5;
17 
18 namespace {
19 // Persistent objects
21 int last_id = 0;
24 
29 
34 MxArray toStruct(const vector<pair<Point2i,Point2i> >& correspondences)
35 {
36  const char *fields[] = {"first", "second"};
37  MxArray s = MxArray::Struct(fields, 2, 1, correspondences.size());
38  for (mwIndex i = 0; i < correspondences.size(); ++i) {
39  s.set("first", correspondences[i].first, i);
40  s.set("second", correspondences[i].second, i);
41  }
42  return s;
43 }
44 }
45 
53 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
54 {
55  // Check the number of arguments
56  nargchk(nrhs>=2 && nlhs<=1);
57 
58  // Argument vector
59  vector<MxArray> rhs(prhs, prhs+nrhs);
60  int id = rhs[0].toInt();
61  string method(rhs[1].toString());
62 
63  // Constructor is called. Create a new object from argument
64  if (method == "new") {
65  nargchk(nrhs==2 && nlhs<=1);
67  plhs[0] = MxArray(last_id);
68  mexLock();
69  return;
70  }
71 
72  // Big operation switch
73  Ptr<GPCForest5> obj = obj_[id];
74  if (obj.empty())
75  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
76  if (method == "delete") {
77  nargchk(nrhs==2 && nlhs==0);
78  obj_.erase(id);
79  mexUnlock();
80  }
81  else if (method == "clear") {
82  nargchk(nrhs==2 && nlhs==0);
83  obj->clear();
84  }
85  else if (method == "load") {
86  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
87  string objname;
88  bool loadFromString = false;
89  for (int i=3; i<nrhs; i+=2) {
90  string key(rhs[i].toString());
91  if (key == "ObjName")
92  objname = rhs[i+1].toString();
93  else if (key == "FromString")
94  loadFromString = rhs[i+1].toBool();
95  else
96  mexErrMsgIdAndTxt("mexopencv:error",
97  "Unrecognized option %s", key.c_str());
98  }
99  obj_[id] = (loadFromString ?
100  Algorithm::loadFromString<GPCForest5>(rhs[2].toString(), objname) :
101  Algorithm::load<GPCForest5>(rhs[2].toString(), objname));
102  }
103  else if (method == "save") {
104  nargchk(nrhs==3 && nlhs==0);
105  obj->save(rhs[2].toString());
106  }
107  else if (method == "empty") {
108  nargchk(nrhs==2 && nlhs<=1);
109  plhs[0] = MxArray(obj->empty());
110  }
111  else if (method == "getDefaultName") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(obj->getDefaultName());
114  }
115  else if (method == "train") {
116  nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs==0);
117  unsigned maxTreeDepth = 20;
118  int minNumberOfSamples = 3;
119  GPCDescType descriptorType = GPC_DESCRIPTOR_DCT;
120  bool printProgress = false; // default was true
121  for (int i=5; i<nrhs; i+=2) {
122  string key(rhs[i].toString());
123  if (key == "MaxTreeDepth")
124  maxTreeDepth = static_cast<unsigned>(rhs[i+1].toInt());
125  else if (key == "MinNumberOfSamples")
126  minNumberOfSamples = rhs[i+1].toInt();
127  else if (key == "DescriptorType")
128  descriptorType = GPCDescTypeMap[rhs[i+1].toString()];
129  else if (key == "PrintProgress")
130  printProgress = rhs[i+1].toBool();
131  else
132  mexErrMsgIdAndTxt("mexopencv:error",
133  "Unrecognized option %s", key.c_str());
134  }
135  GPCTrainingParams params(
136  maxTreeDepth, minNumberOfSamples, descriptorType, printProgress);
137  if (!rhs[2].isCell() || !rhs[3].isCell() || !rhs[4].isCell())
138  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
139  if (!rhs[2].isEmpty() && rhs[2].at<MxArray>(0).isChar()) {
140  vector<string> imagesFrom(rhs[2].toVector<string>()),
141  imagesTo(rhs[3].toVector<string>()),
142  gt(rhs[4].toVector<string>());
143  obj->train(
144  vector<String>(imagesFrom.begin(), imagesFrom.end()),
145  vector<String>(imagesTo.begin(), imagesTo.end()),
146  vector<String>(gt.begin(), gt.end()),
147  params);
148  }
149  else {
150  vector<Mat> imagesFrom(rhs[2].toVector<Mat>()),
151  imagesTo(rhs[3].toVector<Mat>()),
152  gt(rhs[4].toVector<Mat>());
153  obj->train(imagesFrom, imagesTo, gt, params);
154  }
155  }
156  else if (method == "findCorrespondences") {
157  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
158  bool useOpenCL = false;
159  for (int i=4; i<nrhs; i+=2) {
160  string key(rhs[i].toString());
161  if (key == "UseOpenCL")
162  useOpenCL = rhs[i+1].toBool();
163  else
164  mexErrMsgIdAndTxt("mexopencv:error",
165  "Unrecognized option %s", key.c_str());
166  }
168  Mat imgFrom(rhs[2].toMat()),
169  imgTo(rhs[3].toMat());
171  obj->findCorrespondences(imgFrom, imgTo, corr, params);
172  plhs[0] = toStruct(corr);
173  }
174  else
175  mexErrMsgIdAndTxt("mexopencv:error",
176  "Unrecognized operation %s", method.c_str());
177 }
bool useOpenCL()
int last_id
Last object id to allocate.
Definition: GPCForest_.cpp:21
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: GPCForest_.cpp:53
void train(GPCTrainingSamples &samples, const GPCTrainingParams params=GPCTrainingParams())
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void findCorrespondences(InputArray imgFrom, InputArray imgTo, std::vector< std::pair< Point2i, Point2i > > &corr, const GPCMatchingParams params=GPCMatchingParams()) const
GPCDescType
STL namespace.
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
STL class.
cv::optflow::GPCForest< nTrees > GPCForest5
Definition: GPCForest_.cpp:16
map< int, Ptr< GPCForest5 > > obj_
Object container.
Definition: GPCForest_.cpp:23
virtual void clear()
static Ptr< GPCForest > create()
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
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
Definition: MxArray.hpp:312
MxArray toStruct(const vector< pair< Point2i, Point2i > > &correspondences)
Convert correspondences to struct array.
Definition: GPCForest_.cpp:34
STL class.
bool empty() const
const ConstMap< string, GPCDescType > GPCDescTypeMap
GPC descriptor types for option processing.
Definition: GPCForest_.cpp:26
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const