mexopencv  3.4.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 #include "opencv2/shape.hpp"
11 using namespace std;
12 using namespace cv;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=2);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs>=2 && nlhs<=1);
43  rhs.begin() + 2, rhs.end());
44  plhs[0] = MxArray(last_id);
45  mexLock();
46  return;
47  }
48 
49  // Big operation switch
51  if (obj.empty())
52  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
53  if (method == "delete") {
54  nargchk(nrhs==2 && nlhs==0);
55  obj_.erase(id);
56  mexUnlock();
57  }
58  else if (method == "clear") {
59  nargchk(nrhs==2 && nlhs==0);
60  obj->clear();
61  }
62  else if (method == "load") {
63  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
64  string objname;
65  bool loadFromString = false;
66  for (int i=3; i<nrhs; i+=2) {
67  string key(rhs[i].toString());
68  if (key == "ObjName")
69  objname = rhs[i+1].toString();
70  else if (key == "FromString")
71  loadFromString = rhs[i+1].toBool();
72  else
73  mexErrMsgIdAndTxt("mexopencv:error",
74  "Unrecognized option %s", key.c_str());
75  }
76  /*
77  obj_[id] = (loadFromString ?
78  Algorithm::loadFromString<ShapeContextDistanceExtractor>(rhs[2].toString(), objname) :
79  Algorithm::load<ShapeContextDistanceExtractor>(rhs[2].toString(), objname));
80  */
82  // HACK: workaround for missing ShapeContextDistanceExtractor::create()
83  FileStorage fs(rhs[2].toString(), FileStorage::READ +
84  (loadFromString ? FileStorage::MEMORY : 0));
85  if (!fs.isOpened())
86  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
87  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
88  if (fn.empty())
89  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
90  obj->read(fn);
91  //*/
92  }
93  else if (method == "save") {
94  nargchk(nrhs==3 && nlhs==0);
95  obj->save(rhs[2].toString());
96  }
97  else if (method == "empty") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(obj->empty());
100  }
101  else if (method == "getDefaultName") {
102  nargchk(nrhs==2 && nlhs<=1);
103  plhs[0] = MxArray(obj->getDefaultName());
104  }
105  else if (method == "computeDistance") {
106  nargchk(nrhs==4 && nlhs<=1);
107  float dist = 0;
108  if (rhs[2].isNumeric() && rhs[3].isNumeric()) {
109  // contours expected as 1xNx2
110  Mat contour1(rhs[2].toMat(CV_32F).reshape(2,1)),
111  contour2(rhs[3].toMat(CV_32F).reshape(2,1));
112  dist = obj->computeDistance(contour1, contour2);
113  }
114  else if (rhs[2].isCell() && rhs[3].isCell()) {
115  vector<Point2f> contour1(rhs[2].toVector<Point2f>()),
116  contour2(rhs[3].toVector<Point2f>());
117  dist = obj->computeDistance(contour1, contour2);
118  }
119  else
120  mexErrMsgIdAndTxt("mexopencv:error", "Invalid contour argument");
121  plhs[0] = MxArray(dist);
122  }
123  else if (method == "setImages") {
124  nargchk(nrhs==4 && nlhs==0);
125  Mat image1(rhs[2].toMat(CV_8U)),
126  image2(rhs[3].toMat(CV_8U));
127  obj->setImages(image1, image2);
128  }
129  else if (method == "setCostExtractor") {
130  nargchk(nrhs>=3 && nlhs==0);
132  rhs[2].toString(), rhs.begin() + 3, rhs.end());
133  obj->setCostExtractor(comparer);
134  }
135  else if (method == "setTransformAlgorithm") {
136  nargchk(nrhs>=3 && nlhs==0);
138  rhs[2].toString(), rhs.begin() + 3, rhs.end());
139  obj->setTransformAlgorithm(transformer);
140  }
141  else if (method == "getImages") {
142  nargchk(nrhs==2 && nlhs<=2);
143  Mat image1, image2;
144  obj->getImages(image1, image2);
145  plhs[0] = MxArray(image1);
146  if (nlhs > 1)
147  plhs[1] = MxArray(image2);
148  }
149  else if (method == "getCostExtractor") {
150  nargchk(nrhs==2 && nlhs<=1);
152  plhs[0] = toStruct(comparer);
153  }
154  else if (method == "getTransformAlgorithm") {
155  nargchk(nrhs==2 && nlhs<=1);
156  Ptr<ShapeTransformer> transformer = obj->getTransformAlgorithm();
157  plhs[0] = toStruct(transformer);
158  }
159  else if (method == "get") {
160  nargchk(nrhs==3 && nlhs<=1);
161  string prop(rhs[2].toString());
162  if (prop == "AngularBins")
163  plhs[0] = MxArray(obj->getAngularBins());
164  else if (prop == "RadialBins")
165  plhs[0] = MxArray(obj->getRadialBins());
166  else if (prop == "InnerRadius")
167  plhs[0] = MxArray(obj->getInnerRadius());
168  else if (prop == "OuterRadius")
169  plhs[0] = MxArray(obj->getOuterRadius());
170  else if (prop == "RotationInvariant")
171  plhs[0] = MxArray(obj->getRotationInvariant());
172  else if (prop == "ShapeContextWeight")
173  plhs[0] = MxArray(obj->getShapeContextWeight());
174  else if (prop == "ImageAppearanceWeight")
175  plhs[0] = MxArray(obj->getImageAppearanceWeight());
176  else if (prop == "BendingEnergyWeight")
177  plhs[0] = MxArray(obj->getBendingEnergyWeight());
178  else if (prop == "Iterations")
179  plhs[0] = MxArray(obj->getIterations());
180  else if (prop == "StdDev")
181  plhs[0] = MxArray(obj->getStdDev());
182  else
183  mexErrMsgIdAndTxt("mexopencv:error",
184  "Unrecognized property %s", prop.c_str());
185  }
186  else if (method == "set") {
187  nargchk(nrhs==4 && nlhs==0);
188  string prop(rhs[2].toString());
189  if (prop == "AngularBins")
190  obj->setAngularBins(rhs[3].toInt());
191  else if (prop == "RadialBins")
192  obj->setRadialBins(rhs[3].toInt());
193  else if (prop == "InnerRadius")
194  obj->setInnerRadius(rhs[3].toFloat());
195  else if (prop == "OuterRadius")
196  obj->setOuterRadius(rhs[3].toFloat());
197  else if (prop == "RotationInvariant")
198  obj->setRotationInvariant(rhs[3].toBool());
199  else if (prop == "ShapeContextWeight")
200  obj->setShapeContextWeight(rhs[3].toFloat());
201  else if (prop == "ImageAppearanceWeight")
202  obj->setImageAppearanceWeight(rhs[3].toFloat());
203  else if (prop == "BendingEnergyWeight")
204  obj->setBendingEnergyWeight(rhs[3].toFloat());
205  else if (prop == "Iterations")
206  obj->setIterations(rhs[3].toInt());
207  else if (prop == "StdDev")
208  obj->setStdDev(rhs[3].toFloat());
209  else
210  mexErrMsgIdAndTxt("mexopencv:error",
211  "Unrecognized property %s", prop.c_str());
212  }
213  else
214  mexErrMsgIdAndTxt("mexopencv:error",
215  "Unrecognized operation %s", method.c_str());
216 }
virtual void setTransformAlgorithm(Ptr< ShapeTransformer > transformer)=0
Common definitions for the shape module.
virtual void setAngularBins(int nAngularBins)=0
T empty(T... args)
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.
virtual float getBendingEnergyWeight() const=0
virtual void setRadialBins(int nRadialBins)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void getImages(OutputArray image1, OutputArray image2) const=0
#define CV_8U
virtual void setBendingEnergyWeight(float bendingEnergyWeight)=0
virtual float getImageAppearanceWeight() const=0
STL namespace.
T end(T... args)
virtual bool isOpened() const
virtual float computeDistance(InputArray contour1, InputArray contour2)=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual int getAngularBins() const=0
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
virtual void setStdDev(float sigma)=0
virtual bool getRotationInvariant() const=0
virtual float getOuterRadius() const=0
virtual void setImageAppearanceWeight(float imageAppearanceWeight)=0
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
virtual float getShapeContextWeight() const=0
map< int, Ptr< ShapeContextDistanceExtractor > > obj_
Object container.
virtual int getRadialBins() const=0
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...
virtual void setOuterRadius(float outerRadius)=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual float getStdDev() const=0
virtual void setIterations(int iterations)=0
virtual void setInnerRadius(float innerRadius)=0
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
FileNode getFirstTopLevelNode() const
virtual float getInnerRadius() const=0
virtual void setCostExtractor(Ptr< HistogramCostExtractor > comparer)=0
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
virtual Ptr< HistogramCostExtractor > getCostExtractor() const=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
T c_str(T... args)
virtual void setImages(InputArray image1, InputArray image2)=0
virtual void setRotationInvariant(bool rotationInvariant)=0
virtual Ptr< ShapeTransformer > getTransformAlgorithm() const=0
virtual void save(const String &filename) const
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.
virtual bool empty() const
virtual void setShapeContextWeight(float shapeContextWeight)=0
cv::Mat toMat() const
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.
virtual int getIterations() const=0