mexopencv  3.4.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 #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<HausdorffDistanceExtractor>(rhs[2].toString(), objname) :
79  Algorithm::load<HausdorffDistanceExtractor>(rhs[2].toString(), objname));
80  */
82  // HACK: workaround for missing HausdorffDistanceExtractor::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 == "get") {
124  nargchk(nrhs==3 && nlhs<=1);
125  string prop(rhs[2].toString());
126  if (prop == "DistanceFlag")
127  plhs[0] = MxArray(NormTypeInv[obj->getDistanceFlag()]);
128  else if (prop == "RankProportion")
129  plhs[0] = MxArray(obj->getRankProportion());
130  else
131  mexErrMsgIdAndTxt("mexopencv:error",
132  "Unrecognized property %s", prop.c_str());
133  }
134  else if (method == "set") {
135  nargchk(nrhs==4 && nlhs==0);
136  string prop(rhs[2].toString());
137  if (prop == "DistanceFlag")
138  obj->setDistanceFlag(NormType[rhs[3].toString()]);
139  else if (prop == "RankProportion")
140  obj->setRankProportion(rhs[3].toFloat());
141  else
142  mexErrMsgIdAndTxt("mexopencv:error",
143  "Unrecognized property %s", prop.c_str());
144  }
145  else
146  mexErrMsgIdAndTxt("mexopencv:error",
147  "Unrecognized operation %s", method.c_str());
148 }
Common definitions for the shape module.
T empty(T... args)
virtual void setDistanceFlag(int distanceFlag)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL namespace.
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:160
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 getDistanceFlag() const=0
virtual void clear()
#define CV_32F
map< int, Ptr< HausdorffDistanceExtractor > > obj_
Object container.
virtual void read(const FileNode &fn)
virtual float getRankProportion() 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...
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
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual void setRankProportion(float rankProportion)=0
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
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:150
cv::Mat toMat() const