20 map<int,Ptr<DescriptorMatcher> > obj_;
30 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
36 vector<MxArray> rhs(prhs, prhs+nrhs);
37 int id = rhs[0].toInt();
38 string method(rhs[1].toString());
41 if (method ==
"new") {
44 rhs[2].toString(), rhs.begin() + 3, rhs.end());
50 Ptr<DescriptorMatcher> 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 FileStorage fs(rhs[2].toString(), FileStorage::READ +
78 (loadFromString ? FileStorage::MEMORY : 0));
79 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
81 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
83 else if (method ==
"save") {
85 obj->save(rhs[2].toString());
87 else if (method ==
"empty") {
89 plhs[0] =
MxArray(obj->empty());
91 else if (method ==
"getDefaultName") {
93 plhs[0] =
MxArray(obj->getDefaultName());
95 else if (method ==
"isMaskSupported") {
97 plhs[0] =
MxArray(obj->isMaskSupported());
99 else if (method ==
"getTrainDescriptors") {
101 plhs[0] =
MxArray(obj->getTrainDescriptors());
103 else if (method ==
"add") {
105 vector<Mat> descriptors;
107 vector<MxArray> va(rhs[2].toVector<MxArray>());
108 descriptors.reserve(va.size());
109 for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
110 descriptors.push_back(it->toMat(
111 it->isUint8() ? CV_8U : CV_32F));
113 obj->add(descriptors);
115 else if (method ==
"train") {
119 else if (method ==
"match") {
121 Mat queryDescriptors(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F));
122 vector<DMatch> matches;
123 if (nrhs>=4 && rhs[3].isNumeric()) {
125 Mat trainDescriptors(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F));
127 for (
int i=4; i<nrhs; i+=2) {
128 string key(rhs[i].toString());
130 mask = rhs[i+1].toMat(CV_8U);
132 mexErrMsgIdAndTxt(
"mexopencv:error",
133 "Unrecognized option %s", key.c_str());
135 obj->match(queryDescriptors, trainDescriptors, matches, mask);
140 for (
int i=3; i<nrhs; i+=2) {
141 string key(rhs[i].toString());
144 vector<MxArray> va(rhs[i+1].toVector<MxArray>());
146 masks.reserve(va.size());
147 for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
148 masks.push_back(it->toMat(CV_8U));
151 mexErrMsgIdAndTxt(
"mexopencv:error",
152 "Unrecognized option %s", key.c_str());
154 obj->match(queryDescriptors, matches, masks);
158 else if (method ==
"knnMatch") {
160 Mat queryDescriptors(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F));
161 vector<vector<DMatch> > matches;
162 if (nrhs>=5 && rhs[3].isNumeric() && rhs[4].isNumeric()) {
164 Mat trainDescriptors(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F));
165 int k = rhs[4].toInt();
167 bool compactResult =
false;
168 for (
int i=5; i<nrhs; i+=2) {
169 string key(rhs[i].toString());
171 mask = rhs[i+1].toMat(CV_8U);
172 else if (key ==
"CompactResult")
173 compactResult = rhs[i+1].toBool();
175 mexErrMsgIdAndTxt(
"mexopencv:error",
176 "Unrecognized option %s", key.c_str());
178 obj->knnMatch(queryDescriptors, trainDescriptors, matches,
179 k, mask, compactResult);
183 int k = rhs[3].toInt();
185 bool compactResult =
false;
186 for (
int i=4; i<nrhs; i+=2) {
187 string key(rhs[i].toString());
190 vector<MxArray> va(rhs[i+1].toVector<MxArray>());
192 masks.reserve(va.size());
193 for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
194 masks.push_back(it->toMat(CV_8U));
196 else if (key ==
"CompactResult")
197 compactResult = rhs[i+1].toBool();
199 mexErrMsgIdAndTxt(
"mexopencv:error",
200 "Unrecognized option %s", key.c_str());
202 obj->knnMatch(queryDescriptors, matches, k, masks, compactResult);
206 else if (method ==
"radiusMatch") {
208 Mat queryDescriptors(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F));
209 vector<vector<DMatch> > matches;
210 if (nrhs>=5 && rhs[3].isNumeric() && rhs[4].isNumeric()) {
212 Mat trainDescriptors(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F));
213 float maxDistance = rhs[4].toFloat();
215 bool compactResult =
false;
216 for (
int i=5; i<nrhs; i+=2) {
217 string key(rhs[i].toString());
219 mask = rhs[i+1].toMat(CV_8U);
220 else if (key ==
"CompactResult")
221 compactResult = rhs[i+1].toBool();
223 mexErrMsgIdAndTxt(
"mexopencv:error",
224 "Unrecognized option %s", key.c_str());
226 obj->radiusMatch(queryDescriptors, trainDescriptors, matches,
227 maxDistance, mask, compactResult);
231 float maxDistance = rhs[3].toFloat();
233 bool compactResult =
false;
234 for (
int i=4; i<nrhs; i+=2) {
235 string key(rhs[i].toString());
238 vector<MxArray> va(rhs[i+1].toVector<MxArray>());
240 masks.reserve(va.size());
241 for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
242 masks.push_back(it->toMat(CV_8U));
244 else if (key ==
"CompactResult")
245 compactResult = rhs[i+1].toBool();
247 mexErrMsgIdAndTxt(
"mexopencv:error",
248 "Unrecognized option %s", key.c_str());
250 obj->radiusMatch(queryDescriptors, matches,
251 maxDistance, masks, compactResult);
256 mexErrMsgIdAndTxt(
"mexopencv:error",
257 "Unrecognized operation %s",method.c_str());
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.
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.
Global constant definitions.