mexopencv  0.1
mex interface for opencv library
BOWKMeansTrainer_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
14 int last_id = 0;
16 map<int,Ptr<BOWKMeansTrainer> > obj_;
17 
19 const ConstMap<string,int> KmeansInitMap = ConstMap<string,int>
20  ("Random", cv::KMEANS_RANDOM_CENTERS)
21  ("PP", cv::KMEANS_PP_CENTERS);
22 }
23 
31 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
32 {
33  // Check the number of arguments
34  nargchk(nrhs>=2 && nlhs<=2);
35 
36  // Argument vector
37  vector<MxArray> rhs(prhs, prhs+nrhs);
38  int id = rhs[0].toInt();
39  string method(rhs[1].toString());
40 
41  // Constructor call
42  if (method == "new") {
43  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
44  int clusterCount = rhs[2].toInt();
45  TermCriteria criteria;
46  int attempts = 3;
47  int flags = cv::KMEANS_PP_CENTERS;
48  for (int i=3; i<nrhs; i+=2) {
49  string key(rhs[i].toString());
50  if (key == "Criteria")
51  criteria = rhs[i+1].toTermCriteria();
52  else if (key == "Attempts")
53  attempts = rhs[i+1].toInt();
54  else if (key == "Initialization")
55  flags = KmeansInitMap[rhs[i+1].toString()];
56  else
57  mexErrMsgIdAndTxt("mexopencv:error",
58  "Unknown option %s",key.c_str());
59  }
60  obj_[++last_id] = makePtr<BOWKMeansTrainer>(
61  clusterCount, criteria, attempts, flags);
62  plhs[0] = MxArray(last_id);
63  return;
64  }
65 
66  // Big operation switch
67  Ptr<BOWKMeansTrainer> obj = obj_[id];
68  if (method == "delete") {
69  nargchk(nrhs==2 && nlhs==0);
70  obj_.erase(id);
71  }
72  else if (method == "clear") {
73  nargchk(nrhs==2 && nlhs==0);
74  obj->clear();
75  }
76  else if (method == "getDescriptors") {
77  nargchk(nrhs==2 && nlhs<=1);
78  vector<Mat> descs(obj->getDescriptors());
79  plhs[0] = MxArray(descs);
80  }
81  else if (method == "descriptorsCount") {
82  nargchk(nrhs==2 && nlhs<=1);
83  int count = obj->descriptorsCount();
84  plhs[0] = MxArray(count);
85  }
86  else if (method == "add") {
87  nargchk(nrhs==3 && nlhs==0);
88  obj->add(rhs[2].toMat(CV_32F));
89  }
90  else if (method == "cluster") {
91  nargchk((nrhs==2 || nrhs==3) && nlhs<=1);
92  Mat vocabulary;
93  if (nrhs==2) // first variant
94  vocabulary = obj->cluster();
95  else // second variant
96  vocabulary = obj->cluster(rhs[2].toMat(CV_32F));
97  plhs[0] = MxArray(vocabulary);
98  }
99  else
100  mexErrMsgIdAndTxt("mexopencv:error",
101  "Unrecognized operation %s", method.c_str());
102 }
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
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927