mexopencv  3.4.1
MEX interface for OpenCV library
PCTSignaturesSQFD_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::xfeatures2d;
13 
14 namespace {
15 // Persistent objects
17 int last_id = 0;
20 
30 
36 }
37 
45 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
46 {
47  // Check the number of arguments
48  nargchk(nrhs>=2 && nlhs<=1);
49 
50  // Argument vector
51  vector<MxArray> rhs(prhs, prhs+nrhs);
52  int id = rhs[0].toInt();
53  string method(rhs[1].toString());
54 
55  // constructor call
56  if (method == "new") {
57  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
58  int distanceFunction = 3;
59  int similarityFunction = 2;
60  float similarityParameter = 1.0f;
61  for (int i=2; i<nrhs; i+=2) {
62  string key(rhs[i].toString());
63  if (key == "DistanceFunction")
64  distanceFunction = (rhs[i+1].isChar()) ?
65  DistanceFuncMap[rhs[i+1].toString()] : rhs[i+1].toInt();
66  else if (key == "SimilarityFunction")
67  similarityFunction = (rhs[i+1].isChar()) ?
68  SimilarityFuncMap[rhs[i+1].toString()] : rhs[i+1].toInt();
69  else if (key == "SimilarityParameter")
70  similarityParameter = rhs[i+1].toFloat();
71  else
72  mexErrMsgIdAndTxt("mexopencv:error",
73  "Unrecognized option %s", key.c_str());
74  }
76  distanceFunction, similarityFunction, similarityParameter);
77  plhs[0] = MxArray(last_id);
78  mexLock();
79  return;
80  }
81 
82  // Big operation switch
83  Ptr<PCTSignaturesSQFD> obj = obj_[id];
84  if (obj.empty())
85  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
86  if (method == "delete") {
87  nargchk(nrhs==2 && nlhs==0);
88  obj_.erase(id);
89  mexUnlock();
90  }
91  else if (method == "clear") {
92  nargchk(nrhs==2 && nlhs==0);
93  obj->clear();
94  }
95  else if (method == "save") {
96  nargchk(nrhs==3 && nlhs==0);
97  obj->save(rhs[2].toString());
98  }
99  else if (method == "load") {
100  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
101  string objname;
102  bool loadFromString = false;
103  for (int i=3; i<nrhs; i+=2) {
104  string key(rhs[i].toString());
105  if (key == "ObjName")
106  objname = rhs[i+1].toString();
107  else if (key == "FromString")
108  loadFromString = rhs[i+1].toBool();
109  else
110  mexErrMsgIdAndTxt("mexopencv:error",
111  "Unrecognized option %s", key.c_str());
112  }
113  obj_[id] = (loadFromString ?
114  Algorithm::loadFromString<PCTSignaturesSQFD>(rhs[2].toString(), objname) :
115  Algorithm::load<PCTSignaturesSQFD>(rhs[2].toString(), objname));
116  }
117  else if (method == "empty") {
118  nargchk(nrhs==2 && nlhs<=1);
119  plhs[0] = MxArray(obj->empty());
120  }
121  else if (method == "getDefaultName") {
122  nargchk(nrhs==2 && nlhs<=1);
123  plhs[0] = MxArray(obj->getDefaultName());
124  }
125  else if (method == "computeQuadraticFormDistance") {
126  nargchk(nrhs==4 && nlhs<=1);
127  Mat signature0(rhs[2].toMat(CV_32F)),
128  signature1(rhs[3].toMat(CV_32F));
129  float dist = obj->computeQuadraticFormDistance(signature0, signature1);
130  plhs[0] = MxArray(dist);
131  }
132  else if (method == "computeQuadraticFormDistances") {
133  nargchk(nrhs==4 && nlhs<=1);
134  Mat sourceSignature(rhs[2].toMat(CV_32F));
135  //vector<Mat> imageSignatures(rhs[3].toVector<Mat>());
136  vector<Mat> imageSignatures;
137  {
138  vector<MxArray> arr(rhs[3].toVector<MxArray>());
139  imageSignatures.reserve(arr.size());
140  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
141  imageSignatures.push_back(it->toMat(CV_32F));
142  }
143  vector<float> distances;
144  obj->computeQuadraticFormDistances(sourceSignature, imageSignatures,
145  distances);
146  plhs[0] = MxArray(distances);
147  }
148  else
149  mexErrMsgIdAndTxt("mexopencv:error",
150  "Unrecognized operation %s", method.c_str());
151 }
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
const ConstMap< string, int > SimilarityFuncMap
Similarity function selector for option processing.
STL namespace.
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void computeQuadraticFormDistances(const Mat &sourceSignature, const std::vector< Mat > &imageSignatures, std::vector< float > &distances) const=0
T push_back(T... args)
virtual void clear()
#define CV_32F
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
map< int, Ptr< PCTSignaturesSQFD > > obj_
Object container.
virtual float computeQuadraticFormDistance(InputArray _signature0, InputArray _signature1) 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
T size(T... args)
STL class.
bool empty() const
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
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
const ConstMap< string, int > DistanceFuncMap
Lp distance function selector for option processing.
cv::Mat toMat() const
T reserve(T... args)