mexopencv  0.1
mex interface for opencv library
ShapeContextDistanceExtractor_.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<ShapeContextDistanceExtractor> > 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_ShapeContextDistanceExtractor(
42  rhs.begin() + 2, rhs.end());
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<ShapeContextDistanceExtractor> 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<ShapeContextDistanceExtractor>(rhs[2].toString(), objname) :
74  Algorithm::load<ShapeContextDistanceExtractor>(rhs[2].toString(), objname));
75  */
77  // HACK: workaround for missing ShapeContextDistanceExtractor::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 == "setImages") {
116  nargchk(nrhs==4 && nlhs==0);
117  Mat image1(rhs[2].toMat(CV_8U)),
118  image2(rhs[3].toMat(CV_8U));
119  obj->setImages(image1, image2);
120  }
121  else if (method == "setCostExtractor") {
122  nargchk(nrhs>=3 && nlhs==0);
123  Ptr<HistogramCostExtractor> comparer = create_HistogramCostExtractor(
124  rhs[2].toString(), rhs.begin() + 3, rhs.end());
125  obj->setCostExtractor(comparer);
126  }
127  else if (method == "setTransformAlgorithm") {
128  nargchk(nrhs>=3 && nlhs==0);
129  Ptr<ShapeTransformer> transformer = create_ShapeTransformer(
130  rhs[2].toString(), rhs.begin() + 3, rhs.end());
131  obj->setTransformAlgorithm(transformer);
132  }
133  else if (method == "getImages") {
134  nargchk(nrhs==2 && nlhs<=2);
135  Mat image1, image2;
136  obj->getImages(image1, image2);
137  plhs[0] = MxArray(image1);
138  if (nlhs > 1)
139  plhs[1] = MxArray(image2);
140  }
141  else if (method == "getCostExtractor") {
142  nargchk(nrhs==2 && nlhs<=1);
143  Ptr<HistogramCostExtractor> comparer = obj->getCostExtractor();
144  plhs[0] = toStruct(comparer);
145  }
146  else if (method == "getTransformAlgorithm") {
147  nargchk(nrhs==2 && nlhs<=1);
148  Ptr<ShapeTransformer> transformer = obj->getTransformAlgorithm();
149  plhs[0] = toStruct(transformer);
150  }
151  else if (method == "get") {
152  nargchk(nrhs==3 && nlhs<=1);
153  string prop(rhs[2].toString());
154  if (prop == "AngularBins")
155  plhs[0] = MxArray(obj->getAngularBins());
156  else if (prop == "RadialBins")
157  plhs[0] = MxArray(obj->getRadialBins());
158  else if (prop == "InnerRadius")
159  plhs[0] = MxArray(obj->getInnerRadius());
160  else if (prop == "OuterRadius")
161  plhs[0] = MxArray(obj->getOuterRadius());
162  else if (prop == "RotationInvariant")
163  plhs[0] = MxArray(obj->getRotationInvariant());
164  else if (prop == "ShapeContextWeight")
165  plhs[0] = MxArray(obj->getShapeContextWeight());
166  else if (prop == "ImageAppearanceWeight")
167  plhs[0] = MxArray(obj->getImageAppearanceWeight());
168  else if (prop == "BendingEnergyWeight")
169  plhs[0] = MxArray(obj->getBendingEnergyWeight());
170  else if (prop == "Iterations")
171  plhs[0] = MxArray(obj->getIterations());
172  else if (prop == "StdDev")
173  plhs[0] = MxArray(obj->getStdDev());
174  else
175  mexErrMsgIdAndTxt("mexopencv:error",
176  "Unrecognized property %s", prop.c_str());
177  }
178  else if (method == "set") {
179  nargchk(nrhs==4 && nlhs==0);
180  string prop(rhs[2].toString());
181  if (prop == "AngularBins")
182  obj->setAngularBins(rhs[3].toInt());
183  else if (prop == "RadialBins")
184  obj->setRadialBins(rhs[3].toInt());
185  else if (prop == "InnerRadius")
186  obj->setInnerRadius(rhs[3].toFloat());
187  else if (prop == "OuterRadius")
188  obj->setOuterRadius(rhs[3].toFloat());
189  else if (prop == "RotationInvariant")
190  obj->setRotationInvariant(rhs[3].toBool());
191  else if (prop == "ShapeContextWeight")
192  obj->setShapeContextWeight(rhs[3].toFloat());
193  else if (prop == "ImageAppearanceWeight")
194  obj->setImageAppearanceWeight(rhs[3].toFloat());
195  else if (prop == "BendingEnergyWeight")
196  obj->setBendingEnergyWeight(rhs[3].toFloat());
197  else if (prop == "Iterations")
198  obj->setIterations(rhs[3].toInt());
199  else if (prop == "StdDev")
200  obj->setStdDev(rhs[3].toFloat());
201  else
202  mexErrMsgIdAndTxt("mexopencv:error",
203  "Unrecognized property %s", prop.c_str());
204  }
205  else
206  mexErrMsgIdAndTxt("mexopencv:error",
207  "Unrecognized operation %s", method.c_str());
208 }
Common definitions for the shape module.
cv::Ptr< cv::ShapeTransformer > create_ShapeTransformer(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of ShapeTransformer using options in arguments.
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
cv::Ptr< cv::ShapeContextDistanceExtractor > create_ShapeContextDistanceExtractor(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of ShapeContextDistanceExtractor using options in arguments.
cv::Ptr< cv::HistogramCostExtractor > create_HistogramCostExtractor(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of HistogramCostExtractor using options in arguments.