mexopencv  3.4.1
MEX interface for OpenCV library
BOWKMeansTrainer_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/features2d.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 namespace {
15 int last_id = 0;
18 
21  ("Random", cv::KMEANS_RANDOM_CENTERS)
22  ("PP", cv::KMEANS_PP_CENTERS);
23 }
24 
32 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
33 {
34  // Check the number of arguments
35  nargchk(nrhs>=2 && nlhs<=2);
36 
37  // Argument vector
38  vector<MxArray> rhs(prhs, prhs+nrhs);
39  int id = rhs[0].toInt();
40  string method(rhs[1].toString());
41 
42  // Constructor call
43  if (method == "new") {
44  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
45  int clusterCount = rhs[2].toInt();
46  TermCriteria criteria;
47  int attempts = 3;
48  int flags = cv::KMEANS_PP_CENTERS;
49  for (int i=3; i<nrhs; i+=2) {
50  string key(rhs[i].toString());
51  if (key == "Criteria")
52  criteria = rhs[i+1].toTermCriteria();
53  else if (key == "Attempts")
54  attempts = rhs[i+1].toInt();
55  else if (key == "Initialization")
56  flags = KmeansInitMap[rhs[i+1].toString()];
57  else
58  mexErrMsgIdAndTxt("mexopencv:error",
59  "Unrecognized option %s", key.c_str());
60  }
61  obj_[++last_id] = makePtr<BOWKMeansTrainer>(
62  clusterCount, criteria, attempts, flags);
63  plhs[0] = MxArray(last_id);
64  mexLock();
65  return;
66  }
67 
68  // Big operation switch
69  Ptr<BOWKMeansTrainer> obj = obj_[id];
70  if (obj.empty())
71  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
72  if (method == "delete") {
73  nargchk(nrhs==2 && nlhs==0);
74  obj_.erase(id);
75  mexUnlock();
76  }
77  else if (method == "clear") {
78  nargchk(nrhs==2 && nlhs==0);
79  obj->clear();
80  }
81  else if (method == "getDescriptors") {
82  nargchk(nrhs==2 && nlhs<=1);
83  vector<Mat> descs(obj->getDescriptors());
84  plhs[0] = MxArray(descs);
85  }
86  else if (method == "descriptorsCount") {
87  nargchk(nrhs==2 && nlhs<=1);
88  int count = obj->descriptorsCount();
89  plhs[0] = MxArray(count);
90  }
91  else if (method == "add") {
92  nargchk(nrhs==3 && nlhs==0);
93  obj->add(rhs[2].toMat(CV_32F));
94  }
95  else if (method == "cluster") {
96  nargchk((nrhs==2 || nrhs==3) && nlhs<=1);
97  Mat vocabulary;
98  if (nrhs==2) // first variant
99  vocabulary = obj->cluster();
100  else // second variant
101  vocabulary = obj->cluster(rhs[2].toMat(CV_32F));
102  plhs[0] = MxArray(vocabulary);
103  }
104  else
105  mexErrMsgIdAndTxt("mexopencv:error",
106  "Unrecognized operation %s", method.c_str());
107 }
virtual Mat cluster() const
void add(const Mat &descriptors)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
KMEANS_PP_CENTERS
virtual void clear()
STL namespace.
KMEANS_RANDOM_CENTERS
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
#define CV_32F
int last_id
Last object id to allocate.
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...
const std::vector< Mat > & getDescriptors() const
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 count(T... args)
STL class.
bool empty() const
int descriptorsCount() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
T c_str(T... args)
map< int, Ptr< BOWKMeansTrainer > > obj_
Object container.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
const ConstMap< string, int > KmeansInitMap
KMeans initalization types.
cv::Mat toMat() const