mexopencv  0.1
mex interface for opencv library
HausdorffDistanceExtractor_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "mexopencv_shape.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
18 map<int,Ptr<HausdorffDistanceExtractor> > obj_;
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && nlhs<=2);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35  int id = rhs[0].toInt();
36  string method(rhs[1].toString());
37 
38  // Constructor is called. Create a new object from argument
39  if (method == "new") {
40  nargchk(nrhs>=2 && nlhs<=1);
41  obj_[++last_id] = create_HausdorffDistanceExtractor(
42  rhs.begin() + 2, rhs.end());
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<HausdorffDistanceExtractor> obj = obj_[id];
49  if (method == "delete") {
50  nargchk(nrhs==2 && nlhs==0);
51  obj_.erase(id);
52  }
53  else if (method == "clear") {
54  nargchk(nrhs==2 && nlhs==0);
55  obj->clear();
56  }
57  else if (method == "load") {
58  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
59  string objname;
60  bool loadFromString = false;
61  for (int i=3; i<nrhs; i+=2) {
62  string key(rhs[i].toString());
63  if (key == "ObjName")
64  objname = rhs[i+1].toString();
65  else if (key == "FromString")
66  loadFromString = rhs[i+1].toBool();
67  else
68  mexErrMsgIdAndTxt("mexopencv:error",
69  "Unrecognized option %s", key.c_str());
70  }
71  /*
72  obj_[id] = (loadFromString ?
73  Algorithm::loadFromString<HausdorffDistanceExtractor>(rhs[2].toString(), objname) :
74  Algorithm::load<HausdorffDistanceExtractor>(rhs[2].toString(), objname));
75  */
77  // HACK: workaround for missing HausdorffDistanceExtractor::create()
78  FileStorage fs(rhs[2].toString(), FileStorage::READ +
79  (loadFromString ? FileStorage::MEMORY : 0));
80  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
81  if (obj.empty())
82  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
83  //*/
84  }
85  else if (method == "save") {
86  nargchk(nrhs==3 && nlhs==0);
87  obj->save(rhs[2].toString());
88  }
89  else if (method == "empty") {
90  nargchk(nrhs==2 && nlhs<=1);
91  plhs[0] = MxArray(obj->empty());
92  }
93  else if (method == "getDefaultName") {
94  nargchk(nrhs==2 && nlhs<=1);
95  plhs[0] = MxArray(obj->getDefaultName());
96  }
97  else if (method == "computeDistance") {
98  nargchk(nrhs==4 && nlhs<=1);
99  float dist = 0;
100  if (rhs[2].isNumeric() && rhs[3].isNumeric()) {
101  // contours expected as 1xNx2
102  Mat contour1(rhs[2].toMat(CV_32F).reshape(2,1)),
103  contour2(rhs[3].toMat(CV_32F).reshape(2,1));
104  dist = obj->computeDistance(contour1, contour2);
105  }
106  else if (rhs[2].isCell() && rhs[3].isCell()) {
107  vector<Point2f> contour1(rhs[2].toVector<Point2f>()),
108  contour2(rhs[3].toVector<Point2f>());
109  dist = obj->computeDistance(contour1, contour2);
110  }
111  else
112  mexErrMsgIdAndTxt("mexopencv:error","Invalid argument");
113  plhs[0] = MxArray(dist);
114  }
115  else if (method == "get") {
116  nargchk(nrhs==3 && nlhs<=1);
117  string prop(rhs[2].toString());
118  if (prop == "DistanceFlag")
119  plhs[0] = MxArray(NormTypeInv[obj->getDistanceFlag()]);
120  else if (prop == "RankProportion")
121  plhs[0] = MxArray(obj->getRankProportion());
122  else
123  mexErrMsgIdAndTxt("mexopencv:error",
124  "Unrecognized property %s", prop.c_str());
125  }
126  else if (method == "set") {
127  nargchk(nrhs==4 && nlhs==0);
128  string prop(rhs[2].toString());
129  if (prop == "DistanceFlag")
130  obj->setDistanceFlag(NormType[rhs[3].toString()]);
131  else if (prop == "RankProportion")
132  obj->setRankProportion(rhs[3].toFloat());
133  else
134  mexErrMsgIdAndTxt("mexopencv:error",
135  "Unrecognized property %s", prop.c_str());
136  }
137  else
138  mexErrMsgIdAndTxt("mexopencv:error",
139  "Unrecognized operation %s", method.c_str());
140 }
Common definitions for the shape module.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:145
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.
cv::Ptr< cv::HausdorffDistanceExtractor > create_HausdorffDistanceExtractor(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of HausdorffDistanceExtractor using options in arguments.
const ConstMap< std::string, int > NormType
Norm type map for option processing.
Definition: mexopencv.hpp:135