11 #include "opencv2/xfeatures2d.hpp" 21 map<int,Ptr<SIFT> > obj_;
31 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
37 vector<MxArray> rhs(prhs, prhs+nrhs);
38 int id = rhs[0].toInt();
39 string method(rhs[1].toString());
42 if (method ==
"new") {
44 obj_[++last_id] = createSIFT(rhs.begin() + 2, rhs.end());
50 Ptr<SIFT> obj = obj_[id];
51 if (method ==
"delete") {
55 else if (method ==
"typeid") {
57 plhs[0] =
MxArray(
string(
typeid(*obj).name()));
59 else if (method ==
"clear") {
63 else if (method ==
"load") {
64 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
66 bool loadFromString =
false;
67 for (
int i=3; i<nrhs; i+=2) {
68 string key(rhs[i].toString());
70 objname = rhs[i+1].toString();
71 else if (key ==
"FromString")
72 loadFromString = rhs[i+1].toBool();
74 mexErrMsgIdAndTxt(
"mexopencv:error",
75 "Unrecognized option %s", key.c_str());
77 obj_[id] = (loadFromString ?
78 Algorithm::loadFromString<SIFT>(rhs[2].toString(), objname) :
79 Algorithm::load<SIFT>(rhs[2].toString(), objname));
81 else if (method ==
"save") {
83 obj->save(rhs[2].toString());
85 else if (method ==
"empty") {
87 plhs[0] =
MxArray(obj->empty());
89 else if (method ==
"getDefaultName") {
91 plhs[0] =
MxArray(obj->getDefaultName());
93 else if (method ==
"defaultNorm") {
97 else if (method ==
"descriptorSize") {
99 plhs[0] =
MxArray(obj->descriptorSize());
101 else if (method ==
"descriptorType") {
105 else if (method ==
"detect") {
106 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
107 if (rhs[2].isNumeric()) {
109 for (
int i=3; i<nrhs; i+=2) {
110 string key(rhs[i].toString());
112 mask = rhs[i+1].toMat(CV_8U);
114 mexErrMsgIdAndTxt(
"mexopencv:error",
115 "Unrecognized option %s", key.c_str());
117 Mat image(rhs[2].toMat(CV_8U));
118 vector<KeyPoint> keypoints;
119 obj->detect(image, keypoints, mask);
122 else if (rhs[2].isCell()) {
124 for (
int i=3; i<nrhs; i+=2) {
125 string key(rhs[i].toString());
128 vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
130 masks.reserve(arr.size());
131 for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
132 masks.push_back(it->toMat(CV_8U));
135 mexErrMsgIdAndTxt(
"mexopencv:error",
136 "Unrecognized option %s", key.c_str());
141 vector<MxArray> arr(rhs[2].toVector<MxArray>());
142 images.reserve(arr.size());
143 for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
144 images.push_back(it->toMat(CV_8U));
146 vector<vector<KeyPoint> > keypoints;
147 obj->detect(images, keypoints, masks);
151 mexErrMsgIdAndTxt(
"mexopencv:error",
"Invalid arguments");
153 else if (method ==
"compute") {
155 if (rhs[2].isNumeric()) {
156 Mat image(rhs[2].toMat(CV_8U)), descriptors;
157 vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
158 obj->compute(image, keypoints, descriptors);
159 plhs[0] =
MxArray(descriptors);
163 else if (rhs[2].isCell()) {
165 vector<Mat> images, descriptors;
167 vector<MxArray> arr(rhs[2].toVector<MxArray>());
168 images.reserve(arr.size());
169 for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
170 images.push_back(it->toMat(CV_8U));
172 vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
173 const_mem_fun_ref_t<vector<KeyPoint>,
MxArray>(
174 &MxArray::toVector<KeyPoint>)));
175 obj->compute(images, keypoints, descriptors);
176 plhs[0] =
MxArray(descriptors);
181 mexErrMsgIdAndTxt(
"mexopencv:error",
"Invalid arguments");
183 else if (method ==
"detectAndCompute") {
184 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
186 vector<KeyPoint> keypoints;
187 bool useProvidedKeypoints =
false;
188 for (
int i=3; i<nrhs; i+=2) {
189 string key(rhs[i].toString());
191 mask = rhs[i+1].toMat(CV_8U);
192 else if (key ==
"Keypoints") {
193 keypoints = rhs[i+1].toVector<KeyPoint>();
194 useProvidedKeypoints =
true;
197 mexErrMsgIdAndTxt(
"mexopencv:error",
198 "Unrecognized option %s", key.c_str());
200 Mat image(rhs[2].toMat(CV_8U)), descriptors;
201 obj->detectAndCompute(image, mask, keypoints, descriptors,
202 useProvidedKeypoints);
205 plhs[1] =
MxArray(descriptors);
208 mexErrMsgIdAndTxt(
"mexopencv:error",
209 "Unrecognized operation %s",method.c_str());
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Common definitions for the features2d and xfeatures2d modules.
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.