mexopencv  0.1
mex interface for opencv library
BOWImgDescriptorExtractor_.cpp
Go to the documentation of this file.
1 
9 #include "mexopencv.hpp"
10 #include "mexopencv_features2d.hpp"
11 using namespace std;
12 using namespace cv;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
19 map<int,Ptr<BOWImgDescriptorExtractor> > obj_;
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<=3);
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==4 && nlhs<=1);
42  // extractor
43  Ptr<DescriptorExtractor> extractor;
44  if (rhs[2].isChar())
45  extractor = createDescriptorExtractor(
46  rhs[2].toString(), rhs.end(), rhs.end());
47  else if (rhs[2].isCell() && rhs[2].numel() >= 2) {
48  vector<MxArray> args(rhs[2].toVector<MxArray>());
49  extractor = createDescriptorExtractor(
50  args[0].toString(), args.begin() + 1, args.end());
51  }
52  else
53  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
54  // matcher
55  Ptr<DescriptorMatcher> matcher;
56  if (rhs[3].isChar())
57  matcher = createDescriptorMatcher(
58  rhs[3].toString(), rhs.end(), rhs.end());
59  else if (rhs[3].isCell() && rhs[3].numel() >= 2) {
60  vector<MxArray> args(rhs[3].toVector<MxArray>());
61  matcher = createDescriptorMatcher(
62  args[0].toString(), args.begin() + 1, args.end());
63  }
64  else
65  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
66  obj_[++last_id] = makePtr<BOWImgDescriptorExtractor>(
67  extractor, matcher);
68  plhs[0] = MxArray(last_id);
69  return;
70  }
71 
72  // Big operation switch
73  Ptr<BOWImgDescriptorExtractor> obj = obj_[id];
74  if (method == "delete") {
75  nargchk(nrhs==2 && nlhs==0);
76  obj_.erase(id);
77  }
78  else if (method == "descriptorSize") {
79  nargchk(nrhs==2 && nlhs<=1);
80  plhs[0] = MxArray(obj->descriptorSize());
81  }
82  else if (method == "descriptorType") {
83  nargchk(nrhs==2 && nlhs<=1);
84  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
85  }
86  else if (method == "compute") {
87  nargchk(nrhs==4 && nlhs<=3);
88  Mat image(rhs[2].toMat(CV_8U)),
89  imgDescriptor, descriptors;
90  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
91  vector<vector<int> > pointIdxsOfClusters;
92  obj->compute(image, keypoints, imgDescriptor,
93  (nlhs>1 ? &pointIdxsOfClusters : NULL),
94  (nlhs>2 ? &descriptors : NULL));
95  plhs[0] = MxArray(imgDescriptor);
96  if (nrhs>1)
97  plhs[1] = MxArray(pointIdxsOfClusters);
98  if (nrhs>2)
99  plhs[2] = MxArray(descriptors);
100  }
101  else if (method == "compute1") {
102  nargchk(nrhs==3 && nlhs<=2);
103  Mat keypointDescriptors(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
104  imgDescriptor;
105  vector<vector<int> > pointIdxsOfClusters;
106  obj->compute(keypointDescriptors, imgDescriptor,
107  (nlhs>1 ? &pointIdxsOfClusters : NULL));
108  plhs[0] = MxArray(imgDescriptor);
109  if (nrhs>1)
110  plhs[1] = MxArray(pointIdxsOfClusters);
111  }
112  else if (method == "compute2") {
113  nargchk(nrhs==4 && nlhs<=1);
114  Mat image(rhs[2].toMat(CV_8U)),
115  imgDescriptor;
116  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
117  obj->compute2(image, keypoints, imgDescriptor);
118  plhs[0] = MxArray(imgDescriptor);
119  }
120  else if (method == "get") {
121  nargchk(nrhs==3 && nlhs<=1);
122  string prop(rhs[2].toString());
123  if (prop == "Vocabulary")
124  plhs[0] = MxArray(obj->getVocabulary());
125  else
126  mexErrMsgIdAndTxt("mexopencv:error",
127  "Unrecognized property %s", prop.c_str());
128  }
129  else if (method == "set") {
130  nargchk(nrhs==4 && nlhs==0);
131  string prop(rhs[2].toString());
132  if (prop == "Vocabulary")
133  obj->setVocabulary(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F));
134  else
135  mexErrMsgIdAndTxt("mexopencv:error",
136  "Unrecognized property %s", prop.c_str());
137  }
138  else
139  mexErrMsgIdAndTxt("mexopencv:error",
140  "Unrecognized operation %s", method.c_str());
141 }
cv::Ptr< cv::DescriptorExtractor > createDescriptorExtractor(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Factory function for DescriptorExtractor creation.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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
Common definitions for the features2d and xfeatures2d modules.
cv::Ptr< cv::DescriptorMatcher > createDescriptorMatcher(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Factory function for DescriptorMatcher creation.
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
Global constant definitions.