mexopencv  3.4.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 #include "opencv2/features2d.hpp"
12 using namespace std;
13 using namespace cv;
14 
15 // Persistent objects
16 namespace {
18 int last_id = 0;
21 }
22 
30 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
31 {
32  // Check the number of arguments
33  nargchk(nrhs>=2 && nlhs<=3);
34 
35  // Argument vector
36  vector<MxArray> rhs(prhs, prhs+nrhs);
37  int id = rhs[0].toInt();
38  string method(rhs[1].toString());
39 
40  // Constructor is called. Create a new object from argument
41  if (method == "new") {
42  nargchk(nrhs==4 && nlhs<=1);
43  // extractor
44  Ptr<DescriptorExtractor> extractor;
45  if (rhs[2].isChar())
46  extractor = createDescriptorExtractor(
47  rhs[2].toString(), rhs.end(), rhs.end());
48  else if (rhs[2].isCell() && rhs[2].numel() >= 2) {
49  vector<MxArray> args(rhs[2].toVector<MxArray>());
50  extractor = createDescriptorExtractor(
51  args[0].toString(), args.begin() + 1, args.end());
52  }
53  else
54  mexErrMsgIdAndTxt("mexopencv:error",
55  "Invalid extractor arguments");
56  // matcher
57  Ptr<DescriptorMatcher> matcher;
58  if (rhs[3].isChar())
59  matcher = createDescriptorMatcher(
60  rhs[3].toString(), rhs.end(), rhs.end());
61  else if (rhs[3].isCell() && rhs[3].numel() >= 2) {
62  vector<MxArray> args(rhs[3].toVector<MxArray>());
63  matcher = createDescriptorMatcher(
64  args[0].toString(), args.begin() + 1, args.end());
65  }
66  else
67  mexErrMsgIdAndTxt("mexopencv:error", "Invalid matcher arguments");
68  obj_[++last_id] = makePtr<BOWImgDescriptorExtractor>(
69  extractor, matcher);
70  plhs[0] = MxArray(last_id);
71  mexLock();
72  return;
73  }
74 
75  // Big operation switch
77  if (obj.empty())
78  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
79  if (method == "delete") {
80  nargchk(nrhs==2 && nlhs==0);
81  obj_.erase(id);
82  mexUnlock();
83  }
84  else if (method == "descriptorSize") {
85  nargchk(nrhs==2 && nlhs<=1);
86  plhs[0] = MxArray(obj->descriptorSize());
87  }
88  else if (method == "descriptorType") {
89  nargchk(nrhs==2 && nlhs<=1);
90  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
91  }
92  else if (method == "compute") {
93  nargchk(nrhs==4 && nlhs<=3);
94  Mat image(rhs[2].toMat(CV_8U)),
95  imgDescriptor, descriptors;
96  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
97  vector<vector<int> > pointIdxsOfClusters;
98  obj->compute(image, keypoints, imgDescriptor,
99  (nlhs>1 ? &pointIdxsOfClusters : NULL),
100  (nlhs>2 ? &descriptors : NULL));
101  plhs[0] = MxArray(imgDescriptor);
102  if (nrhs>1)
103  plhs[1] = MxArray(pointIdxsOfClusters);
104  if (nrhs>2)
105  plhs[2] = MxArray(descriptors);
106  }
107  else if (method == "compute1") {
108  nargchk(nrhs==3 && nlhs<=2);
109  Mat keypointDescriptors(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
110  imgDescriptor;
111  vector<vector<int> > pointIdxsOfClusters;
112  obj->compute(keypointDescriptors, imgDescriptor,
113  (nlhs>1 ? &pointIdxsOfClusters : NULL));
114  plhs[0] = MxArray(imgDescriptor);
115  if (nrhs>1)
116  plhs[1] = MxArray(pointIdxsOfClusters);
117  }
118  else if (method == "compute2") {
119  nargchk(nrhs==4 && nlhs<=1);
120  Mat image(rhs[2].toMat(CV_8U)),
121  imgDescriptor;
122  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
123  obj->compute2(image, keypoints, imgDescriptor);
124  plhs[0] = MxArray(imgDescriptor);
125  }
126  else if (method == "get") {
127  nargchk(nrhs==3 && nlhs<=1);
128  string prop(rhs[2].toString());
129  if (prop == "Vocabulary")
130  plhs[0] = MxArray(obj->getVocabulary());
131  else
132  mexErrMsgIdAndTxt("mexopencv:error",
133  "Unrecognized property %s", prop.c_str());
134  }
135  else if (method == "set") {
136  nargchk(nrhs==4 && nlhs==0);
137  string prop(rhs[2].toString());
138  if (prop == "Vocabulary")
139  obj->setVocabulary(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F));
140  else
141  mexErrMsgIdAndTxt("mexopencv:error",
142  "Unrecognized property %s", prop.c_str());
143  }
144  else
145  mexErrMsgIdAndTxt("mexopencv:error",
146  "Unrecognized operation %s", method.c_str());
147 }
void compute(InputArray image, std::vector< KeyPoint > &keypoints, OutputArray imgDescriptor, std::vector< std::vector< int > > *pointIdxsOfClusters=0, Mat *descriptors=0)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
const Mat & getVocabulary() const
#define CV_8U
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.
STL namespace.
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
map< int, Ptr< BOWImgDescriptorExtractor > > obj_
Object container.
STL class.
#define CV_32F
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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
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.
void compute2(const Mat &image, std::vector< KeyPoint > &keypoints, Mat &imgDescriptor)
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
void setVocabulary(const Mat &vocabulary)
cv::Mat toMat() const