mexopencv  0.1
mex interface for opencv library
mexopencv_features2d.cpp
Go to the documentation of this file.
1 
8 using std::vector;
9 using std::string;
10 using namespace cv;
11 
12 #ifdef HAVE_OPENCV_XFEATURES2D
13 using namespace cv::xfeatures2d;
14 #endif
15 
16 
17 /**************************************************************\
18 * Feature Detection and Description *
19 \**************************************************************/
20 
21 Ptr<BRISK> createBRISK(
22  vector<MxArray>::const_iterator first,
23  vector<MxArray>::const_iterator last)
24 {
25  if (((last-first) % 2) != 0)
26  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
27  // second variant for custom patterns
28  if ((last-first) >= 2 && !first->isChar()) {
29  vector<float> radiusList(first->toVector<float>()); ++first;
30  vector<int> numberList(first->toVector<int>()); ++first;
31  float dMax = 5.85f, dMin = 8.2f;
32  vector<int> indexChange;
33  for (; first != last; first += 2) {
34  string key((*first).toString());
35  const MxArray& val = *(first + 1);
36  if (key == "DMax")
37  dMax = val.toFloat();
38  else if (key == "DMin")
39  dMin = val.toFloat();
40  else if (key == "IndexChange")
41  indexChange = val.toVector<int>();
42  else
43  mexErrMsgIdAndTxt("mexopencv:error",
44  "Unrecognized option %s", key.c_str());
45  }
46  return BRISK::create(radiusList, numberList, dMax, dMin, indexChange);
47  }
48  // first variant
49  else {
50  int thresh = 30;
51  int octaves = 3;
52  float patternScale = 1.0f;
53  for (; first != last; first += 2) {
54  string key((*first).toString());
55  const MxArray& val = *(first + 1);
56  if (key == "Threshold")
57  thresh = val.toInt();
58  else if (key == "Octaves")
59  octaves = val.toInt();
60  else if (key == "PatternScale")
61  patternScale = val.toFloat();
62  else
63  mexErrMsgIdAndTxt("mexopencv:error",
64  "Unrecognized option %s", key.c_str());
65  }
66  return BRISK::create(thresh, octaves, patternScale);
67  }
68 }
69 
70 Ptr<ORB> createORB(
71  vector<MxArray>::const_iterator first,
72  vector<MxArray>::const_iterator last)
73 {
74  if (((last-first) % 2) != 0)
75  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
76  int nfeatures = 500;
77  float scaleFactor = 1.2f;
78  int nlevels = 8;
79  int edgeThreshold = 31;
80  int firstLevel = 0;
81  int WTA_K = 2;
82  int scoreType = ORB::HARRIS_SCORE;
83  int patchSize = 31;
84  int fastThreshold = 20;
85  for (; first != last; first += 2) {
86  string key((*first).toString());
87  const MxArray& val = *(first + 1);
88  if (key == "MaxFeatures")
89  nfeatures = val.toInt();
90  else if (key == "ScaleFactor")
91  scaleFactor = val.toFloat();
92  else if (key == "NLevels")
93  nlevels = val.toInt();
94  else if (key == "EdgeThreshold")
95  edgeThreshold = val.toInt();
96  else if (key == "FirstLevel")
97  firstLevel = val.toInt();
98  else if (key == "WTA_K")
99  WTA_K = val.toInt();
100  else if (key == "ScoreType")
101  scoreType = ORBScoreType[val.toString()];
102  else if (key == "PatchSize")
103  patchSize = val.toInt();
104  else if (key == "FastThreshold")
105  fastThreshold = val.toInt();
106  else
107  mexErrMsgIdAndTxt("mexopencv:error",
108  "Unrecognized option %s", key.c_str());
109  }
110  return ORB::create(nfeatures, scaleFactor, nlevels, edgeThreshold,
111  firstLevel, WTA_K, scoreType, patchSize, fastThreshold);
112 }
113 
114 Ptr<MSER> createMSER(
115  vector<MxArray>::const_iterator first,
116  vector<MxArray>::const_iterator last)
117 {
118  if (((last-first) % 2) != 0)
119  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
120  int delta = 5;
121  int min_area = 60;
122  int max_area = 14400;
123  double max_variation = 0.25;
124  double min_diversity = 0.2;
125  int max_evolution = 200;
126  double area_threshold = 1.01;
127  double min_margin = 0.003;
128  int edge_blur_size = 5;
129  for (; first != last; first += 2) {
130  string key((*first).toString());
131  const MxArray& val = *(first + 1);
132  if (key == "Delta")
133  delta = val.toInt();
134  else if (key == "MinArea")
135  min_area = val.toInt();
136  else if (key == "MaxArea")
137  max_area = val.toInt();
138  else if (key == "MaxVariation")
139  max_variation = val.toDouble();
140  else if (key == "MinDiversity")
141  min_diversity = val.toDouble();
142  else if (key == "MaxEvolution")
143  max_evolution = val.toInt();
144  else if (key == "AreaThreshold")
145  area_threshold = val.toDouble();
146  else if (key == "MinMargin")
147  min_margin = val.toDouble();
148  else if (key == "EdgeBlurSize")
149  edge_blur_size = val.toInt();
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized option %s", key.c_str());
153  }
154  return MSER::create(delta, min_area, max_area, max_variation,
155  min_diversity, max_evolution, area_threshold, min_margin,
156  edge_blur_size);
157 }
158 
159 Ptr<FastFeatureDetector> createFastFeatureDetector(
160  vector<MxArray>::const_iterator first,
161  vector<MxArray>::const_iterator last)
162 {
163  if (((last-first) % 2) != 0)
164  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
165  int threshold = 10;
166  bool nonmaxSuppression = true;
167  int type = FastFeatureDetector::TYPE_9_16;
168  for (; first != last; first += 2) {
169  string key((*first).toString());
170  const MxArray& val = *(first + 1);
171  if (key == "Threshold")
172  threshold = val.toInt();
173  else if (key == "NonmaxSuppression")
174  nonmaxSuppression = val.toBool();
175  else if (key == "Type")
176  type = FASTTypeMap[val.toString()];
177  else
178  mexErrMsgIdAndTxt("mexopencv:error",
179  "Unrecognized option %s", key.c_str());
180  }
181  return FastFeatureDetector::create(threshold, nonmaxSuppression, type);
182 }
183 
184 Ptr<GFTTDetector> createGFTTDetector(
185  vector<MxArray>::const_iterator first,
186  vector<MxArray>::const_iterator last)
187 {
188  if (((last-first) % 2) != 0)
189  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
190  int maxCorners = 1000;
191  double qualityLevel = 0.01;
192  double minDistance = 1;
193  int blockSize = 3;
194  bool useHarrisDetector = false;
195  double k = 0.04;
196  for (; first != last; first += 2) {
197  string key((*first).toString());
198  const MxArray& val = *(first + 1);
199  if (key == "MaxFeatures")
200  maxCorners = val.toInt();
201  else if (key == "QualityLevel")
202  qualityLevel = val.toDouble();
203  else if (key == "MinDistance")
204  minDistance = val.toDouble();
205  else if (key == "BlockSize")
206  blockSize = val.toInt();
207  else if (key == "HarrisDetector")
208  useHarrisDetector = val.toBool();
209  else if(key == "K")
210  k = val.toDouble();
211  else
212  mexErrMsgIdAndTxt("mexopencv:error",
213  "Unrecognized option %s", key.c_str());
214  }
215  return GFTTDetector::create(maxCorners, qualityLevel, minDistance,
216  blockSize, useHarrisDetector, k);
217 }
218 
219 Ptr<SimpleBlobDetector> createSimpleBlobDetector(
220  vector<MxArray>::const_iterator first,
221  vector<MxArray>::const_iterator last)
222 {
223  if (((last-first) % 2) != 0)
224  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
225  SimpleBlobDetector::Params parameters;
226  for (; first != last; first += 2) {
227  string key((*first).toString());
228  const MxArray& val = *(first + 1);
229  if (key == "ThresholdStep")
230  parameters.thresholdStep = val.toFloat();
231  else if (key == "MinThreshold")
232  parameters.minThreshold = val.toFloat();
233  else if (key == "MaxThreshold")
234  parameters.maxThreshold = val.toFloat();
235  else if (key == "MinRepeatability")
236  parameters.minRepeatability = val.toInt();
237  else if (key == "MinDistBetweenBlobs")
238  parameters.minDistBetweenBlobs = val.toFloat();
239  else if (key == "FilterByColor")
240  parameters.filterByColor = val.toBool();
241  else if (key == "BlobColor")
242  parameters.blobColor = static_cast<uchar>(val.toInt());
243  else if (key == "FilterByArea")
244  parameters.filterByArea = val.toBool();
245  else if (key == "MinArea")
246  parameters.minArea = val.toFloat();
247  else if (key == "MaxArea")
248  parameters.maxArea = val.toFloat();
249  else if (key == "FilterByCircularity")
250  parameters.filterByCircularity = val.toBool();
251  else if (key == "MinCircularity")
252  parameters.minCircularity = val.toFloat();
253  else if (key == "MaxCircularity")
254  parameters.maxCircularity = val.toFloat();
255  else if (key == "FilterByInertia")
256  parameters.filterByInertia = val.toBool();
257  else if (key == "MinInertiaRatio")
258  parameters.minInertiaRatio = val.toFloat();
259  else if (key == "MaxInertiaRatio")
260  parameters.maxInertiaRatio = val.toFloat();
261  else if (key == "FilterByConvexity")
262  parameters.filterByConvexity = val.toBool();
263  else if (key == "MinConvexity")
264  parameters.minConvexity = val.toFloat();
265  else if (key == "MaxConvexity")
266  parameters.maxConvexity = val.toFloat();
267  else
268  mexErrMsgIdAndTxt("mexopencv:error",
269  "Unrecognized option %s", key.c_str());
270  }
271  return SimpleBlobDetector::create(parameters);
272 }
273 
274 Ptr<KAZE> createKAZE(
275  vector<MxArray>::const_iterator first,
276  vector<MxArray>::const_iterator last)
277 {
278  if (((last-first) % 2) != 0)
279  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
280  bool extended = false;
281  bool upright = false;
282  float threshold = 0.001f;
283  int nOctaves = 4;
284  int nOctaveLayers = 4;
285  int diffusivity = KAZE::DIFF_PM_G2;
286  for (; first != last; first += 2) {
287  string key((*first).toString());
288  const MxArray& val = *(first + 1);
289  if (key == "Extended")
290  extended = val.toBool();
291  else if (key == "Upright")
292  upright = val.toBool();
293  else if (key == "Threshold")
294  threshold = val.toFloat();
295  else if (key == "NOctaves")
296  nOctaves = val.toInt();
297  else if (key == "NOctaveLayers")
298  nOctaveLayers = val.toInt();
299  else if(key == "Diffusivity")
300  diffusivity = KAZEDiffusivityType[val.toString()];
301  else
302  mexErrMsgIdAndTxt("mexopencv:error",
303  "Unrecognized option %s", key.c_str());
304  }
305  return KAZE::create(extended, upright, threshold,
306  nOctaves, nOctaveLayers, diffusivity);
307 }
308 
309 Ptr<AKAZE> createAKAZE(
310  vector<MxArray>::const_iterator first,
311  vector<MxArray>::const_iterator last)
312 {
313  if (((last-first) % 2) != 0)
314  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
315  int descriptor_type = AKAZE::DESCRIPTOR_MLDB;
316  int descriptor_size = 0;
317  int descriptor_channels = 3;
318  float threshold = 0.001f;
319  int nOctaves = 4;
320  int nOctaveLayers = 4;
321  int diffusivity = KAZE::DIFF_PM_G2;
322  for (; first != last; first += 2) {
323  string key((*first).toString());
324  const MxArray& val = *(first + 1);
325  if (key == "DescriptorType")
326  descriptor_type = AKAZEDescriptorType[val.toString()];
327  else if (key == "DescriptorSize")
328  descriptor_size = val.toInt();
329  else if (key == "DescriptorChannels")
330  descriptor_channels = val.toInt();
331  else if (key == "Threshold")
332  threshold = val.toFloat();
333  else if (key == "NOctaves")
334  nOctaves = val.toInt();
335  else if (key == "NOctaveLayers")
336  nOctaveLayers = val.toInt();
337  else if (key == "Diffusivity")
338  diffusivity = KAZEDiffusivityType[val.toString()];
339  else
340  mexErrMsgIdAndTxt("mexopencv:error",
341  "Unrecognized option %s", key.c_str());
342  }
343  return AKAZE::create(descriptor_type, descriptor_size, descriptor_channels,
344  threshold, nOctaves, nOctaveLayers, diffusivity);
345 }
346 
347 Ptr<AgastFeatureDetector> createAgastFeatureDetector(
348  vector<MxArray>::const_iterator first,
349  vector<MxArray>::const_iterator last)
350 {
351  if (((last-first) % 2) != 0)
352  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
353  int threshold = 10;
354  bool nonmaxSuppression = true;
355  int type = AgastFeatureDetector::OAST_9_16;
356  for (; first != last; first += 2) {
357  string key((*first).toString());
358  const MxArray& val = *(first + 1);
359  if (key == "Threshold")
360  threshold = val.toInt();
361  else if (key == "NonmaxSuppression")
362  nonmaxSuppression = val.toBool();
363  else if (key == "Type")
364  type = AgastTypeMap[val.toString()];
365  else
366  mexErrMsgIdAndTxt("mexopencv:error",
367  "Unrecognized option %s", key.c_str());
368  }
369  return AgastFeatureDetector::create(threshold, nonmaxSuppression, type);
370 }
371 
372 #ifdef HAVE_OPENCV_XFEATURES2D
373 Ptr<SIFT> createSIFT(
374  vector<MxArray>::const_iterator first,
375  vector<MxArray>::const_iterator last)
376 {
377  if (((last-first) % 2) != 0)
378  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
379  int nfeatures = 0;
380  int nOctaveLayers = 3;
381  double contrastThreshold = 0.04;
382  double edgeThreshold = 10;
383  double sigma = 1.6;
384  for (; first != last; first += 2) {
385  string key((*first).toString());
386  const MxArray& val = *(first + 1);
387  if (key == "NFeatures")
388  nfeatures = val.toInt();
389  else if (key == "NOctaveLayers")
390  nOctaveLayers = val.toInt();
391  else if (key == "ConstrastThreshold")
392  contrastThreshold = val.toDouble();
393  else if (key == "EdgeThreshold")
394  edgeThreshold = val.toDouble();
395  else if (key == "Sigma")
396  sigma = val.toDouble();
397  else
398  mexErrMsgIdAndTxt("mexopencv:error",
399  "Unrecognized option %s", key.c_str());
400  }
401  return SIFT::create(nfeatures, nOctaveLayers, contrastThreshold,
402  edgeThreshold, sigma);
403 }
404 
405 Ptr<SURF> createSURF(
406  vector<MxArray>::const_iterator first,
407  vector<MxArray>::const_iterator last)
408 {
409  if (((last-first) % 2) != 0)
410  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
411  double hessianThreshold = 100;
412  int nOctaves = 4;
413  int nOctaveLayers = 3;
414  bool extended = false;
415  bool upright = false;
416  for (; first != last; first += 2) {
417  string key((*first).toString());
418  const MxArray& val = *(first + 1);
419  if (key == "HessianThreshold")
420  hessianThreshold = val.toDouble();
421  else if (key == "NOctaves")
422  nOctaves = val.toInt();
423  else if (key == "NOctaveLayers")
424  nOctaveLayers = val.toInt();
425  else if (key == "Extended")
426  extended = val.toBool();
427  else if (key == "Upright")
428  upright = val.toBool();
429  else
430  mexErrMsgIdAndTxt("mexopencv:error",
431  "Unrecognized option %s", key.c_str());
432  }
433  return SURF::create(hessianThreshold, nOctaves, nOctaveLayers,
434  extended, upright);
435 }
436 
437 Ptr<FREAK> createFREAK(
438  vector<MxArray>::const_iterator first,
439  vector<MxArray>::const_iterator last)
440 {
441  if (((last-first) % 2) != 0)
442  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
443  bool orientationNormalized = true;
444  bool scaleNormalized = true;
445  float patternScale = 22.0f;
446  int nOctaves = 4;
447  vector<int> selectedPairs;
448  for (; first != last; first += 2) {
449  string key((*first).toString());
450  const MxArray& val = *(first + 1);
451  if (key == "OrientationNormalized")
452  orientationNormalized = val.toBool();
453  else if (key == "ScaleNormalized")
454  scaleNormalized = val.toBool();
455  else if (key == "PatternScale")
456  patternScale = val.toFloat();
457  else if (key == "NOctaves")
458  nOctaves = val.toInt();
459  else if (key == "SelectedPairs")
460  selectedPairs = val.toVector<int>();
461  else
462  mexErrMsgIdAndTxt("mexopencv:error",
463  "Unrecognized option %s", key.c_str());
464  }
465  return FREAK::create(orientationNormalized, scaleNormalized, patternScale,
466  nOctaves, selectedPairs);
467 }
468 
469 Ptr<StarDetector> createStarDetector(
470  vector<MxArray>::const_iterator first,
471  vector<MxArray>::const_iterator last)
472 {
473  if (((last-first) % 2) != 0)
474  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
475  int maxSize = 45;
476  int responseThreshold = 30;
477  int lineThresholdProjected = 10;
478  int lineThresholdBinarized = 8;
479  int suppressNonmaxSize = 5;
480  for (; first != last; first += 2) {
481  string key((*first).toString());
482  const MxArray& val = *(first + 1);
483  if (key == "MaxSize")
484  maxSize = val.toInt();
485  else if (key == "ResponseThreshold")
486  responseThreshold = val.toInt();
487  else if (key == "LineThresholdProjected")
488  lineThresholdProjected = val.toInt();
489  else if (key == "LineThresholdBinarized")
490  lineThresholdBinarized = val.toInt();
491  else if (key == "SuppressNonmaxSize")
492  suppressNonmaxSize = val.toInt();
493  else
494  mexErrMsgIdAndTxt("mexopencv:error",
495  "Unrecognized option %s", key.c_str());
496  }
497  return StarDetector::create(maxSize, responseThreshold,
498  lineThresholdProjected, lineThresholdBinarized, suppressNonmaxSize);
499 }
500 
501 Ptr<BriefDescriptorExtractor> createBriefDescriptorExtractor(
502  vector<MxArray>::const_iterator first,
503  vector<MxArray>::const_iterator last)
504 {
505  if (((last-first) % 2) != 0)
506  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
507  int bytes = 32;
508  bool use_orientation = false;
509  for (; first != last; first += 2) {
510  string key((*first).toString());
511  const MxArray& val = *(first + 1);
512  if (key == "Bytes")
513  bytes = val.toInt();
514  else if (key == "UseOrientation")
515  use_orientation = val.toBool();
516  else
517  mexErrMsgIdAndTxt("mexopencv:error",
518  "Unrecognized option %s", key.c_str());
519  }
520  return BriefDescriptorExtractor::create(bytes, use_orientation);
521 }
522 
523 Ptr<LUCID> createLUCID(
524  vector<MxArray>::const_iterator first,
525  vector<MxArray>::const_iterator last)
526 {
527  if (((last-first) % 2) != 0)
528  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
529  int lucid_kernel = 1;
530  int blur_kernel = 2;
531  for (; first != last; first += 2) {
532  string key((*first).toString());
533  const MxArray& val = *(first + 1);
534  if (key == "LucidKernel")
535  lucid_kernel = val.toInt();
536  else if (key == "BlurKernel")
537  blur_kernel = val.toInt();
538  else
539  mexErrMsgIdAndTxt("mexopencv:error",
540  "Unrecognized option %s", key.c_str());
541  }
542  return LUCID::create(lucid_kernel, blur_kernel);
543 }
544 
545 Ptr<LATCH> createLATCH(
546  vector<MxArray>::const_iterator first,
547  vector<MxArray>::const_iterator last)
548 {
549  if (((last-first) % 2) != 0)
550  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
551  int bytes = 32;
552  bool rotationInvariance = true;
553  int half_ssd_size = 3;
554  for (; first != last; first += 2) {
555  string key(first->toString());
556  const MxArray& val = *(first + 1);
557  if (key == "Bytes")
558  bytes = val.toInt();
559  else if (key == "RotationInvariance")
560  rotationInvariance = val.toBool();
561  else if (key == "HalfSize")
562  half_ssd_size = val.toInt();
563  else
564  mexErrMsgIdAndTxt("mexopencv:error",
565  "Unrecognized option %s", key.c_str());
566  }
567  return LATCH::create(bytes, rotationInvariance, half_ssd_size);
568 }
569 
570 Ptr<DAISY> createDAISY(
571  vector<MxArray>::const_iterator first,
572  vector<MxArray>::const_iterator last)
573 {
574  if (((last-first) % 2) != 0)
575  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
576  float radius = 15;
577  int q_radius = 3;
578  int q_theta = 8;
579  int q_hist = 8;
580  int norm = DAISY::NRM_NONE;
581  Mat H;
582  bool interpolation = true;
583  bool use_orientation = false;
584  for (; first != last; first += 2) {
585  string key(first->toString());
586  const MxArray& val = *(first + 1);
587  if (key == "Radius")
588  radius = val.toFloat();
589  else if (key == "RadiusQuant")
590  q_radius = val.toInt();
591  else if (key == "AngleQuant")
592  q_theta = val.toInt();
593  else if (key == "GradOrientationsQuant")
594  q_hist = val.toInt();
595  else if (key == "Normalization")
596  norm = DAISYNormType[val.toString()];
597  else if (key == "H")
598  H = val.toMat();
599  else if (key == "Interpolation")
600  interpolation = val.toBool();
601  else if (key == "UseOrientation")
602  use_orientation = val.toBool();
603  else
604  mexErrMsgIdAndTxt("mexopencv:error",
605  "Unrecognized option %s", key.c_str());
606  }
607  return DAISY::create(radius, q_radius, q_theta, q_hist,
608  norm, H, interpolation, use_orientation);
609 }
610 
611 Ptr<MSDDetector> createMSDDetector(
612  vector<MxArray>::const_iterator first,
613  vector<MxArray>::const_iterator last)
614 {
615  if (((last-first) % 2) != 0)
616  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
617  int patch_radius = 3;
618  int search_area_radius = 5;
619  int nms_radius = 5;
620  int nms_scale_radius = 0;
621  float th_saliency = 250.0f;
622  int kNN = 4;
623  float scale_factor = 1.25f;
624  int n_scales = -1;
625  bool compute_orientation = false;
626  for (; first != last; first += 2) {
627  string key((*first).toString());
628  const MxArray& val = *(first + 1);
629  if (key == "PatchRadius")
630  patch_radius = val.toInt();
631  else if (key == "SearchAreaRadius")
632  search_area_radius = val.toInt();
633  else if (key == "NMSRadius")
634  nms_radius = val.toInt();
635  else if (key == "NMSScaleRadius")
636  nms_scale_radius = val.toInt();
637  else if (key == "ThSaliency")
638  th_saliency = val.toFloat();
639  else if (key == "KNN")
640  kNN = val.toInt();
641  else if (key == "ScaleFactor")
642  scale_factor = val.toFloat();
643  else if (key == "NScales")
644  n_scales = val.toInt();
645  else if (key == "ComputeOrientation")
646  compute_orientation = val.toBool();
647  else
648  mexErrMsgIdAndTxt("mexopencv:error",
649  "Unrecognized option %s", key.c_str());
650  }
651  return MSDDetector::create(patch_radius, search_area_radius, nms_radius,
652  nms_scale_radius, th_saliency, kNN, scale_factor, n_scales,
653  compute_orientation);
654 }
655 #endif
656 
657 Ptr<FeatureDetector> createFeatureDetector(
658  const string& type,
659  vector<MxArray>::const_iterator first,
660  vector<MxArray>::const_iterator last)
661 {
662  Ptr<FeatureDetector> p;
663  if (type == "BRISK")
664  p = createBRISK(first, last);
665  else if (type == "ORB")
666  p = createORB(first, last);
667  else if (type == "MSER")
668  p = createMSER(first, last);
669  else if (type == "FastFeatureDetector")
670  p = createFastFeatureDetector(first, last);
671  else if (type == "GFTTDetector")
672  p = createGFTTDetector(first, last);
673  else if (type == "SimpleBlobDetector")
674  p = createSimpleBlobDetector(first, last);
675  else if (type == "KAZE")
676  p = createKAZE(first, last);
677  else if (type == "AKAZE")
678  p = createAKAZE(first, last);
679  else if (type == "AgastFeatureDetector")
680  p = createAgastFeatureDetector(first, last);
681 #ifdef HAVE_OPENCV_XFEATURES2D
682  else if (type == "SIFT")
683  p = createSIFT(first, last);
684  else if (type == "SURF")
685  p = createSURF(first, last);
686  else if (type == "StarDetector")
687  p = createStarDetector(first, last);
688  else if (type == "MSDDetector")
689  p = createMSDDetector(first, last);
690 #endif
691  else
692  mexErrMsgIdAndTxt("mexopencv:error",
693  "Unrecognized detector %s", type.c_str());
694  if (p.empty())
695  mexErrMsgIdAndTxt("mexopencv:error",
696  "Failed to create FeatureDetector of type %s", type.c_str());
697  return p;
698 }
699 
700 Ptr<DescriptorExtractor> createDescriptorExtractor(
701  const string& type,
702  vector<MxArray>::const_iterator first,
703  vector<MxArray>::const_iterator last)
704 {
705  Ptr<DescriptorExtractor> p;
706  if (type == "BRISK")
707  p = createBRISK(first, last);
708  else if (type == "ORB")
709  p = createORB(first, last);
710  else if (type == "KAZE")
711  p = createKAZE(first, last);
712  else if (type == "AKAZE")
713  p = createAKAZE(first, last);
714 #ifdef HAVE_OPENCV_XFEATURES2D
715  else if (type == "SIFT")
716  p = createSIFT(first, last);
717  else if (type == "SURF")
718  p = createSURF(first, last);
719  else if (type == "FREAK")
720  p = createFREAK(first, last);
721  else if (type == "BriefDescriptorExtractor")
722  p = createBriefDescriptorExtractor(first, last);
723  else if (type == "LUCID")
724  p = createLUCID(first, last);
725  else if (type == "LATCH")
726  p = createLATCH(first, last);
727  else if (type == "DAISY")
728  p = createDAISY(first, last);
729 #endif
730  else
731  mexErrMsgIdAndTxt("mexopencv:error",
732  "Unrecognized extractor %s", type.c_str());
733  if (p.empty())
734  mexErrMsgIdAndTxt("mexopencv:error",
735  "Failed to create DescriptorExtractor of type %s", type.c_str());
736  return p;
737 }
738 
739 
740 /**************************************************************\
741 * Descriptor Matching *
742 \**************************************************************/
743 
747  ("Random", cvflann::FLANN_CENTERS_RANDOM)
748  ("Gonzales", cvflann::FLANN_CENTERS_GONZALES)
749  ("KMeansPP", cvflann::FLANN_CENTERS_KMEANSPP)
750  ("Groupwise", cvflann::FLANN_CENTERS_GROUPWISE);
751 
757 Ptr<flann::IndexParams> toIndexParams(const MxArray& m)
758 {
759  Ptr<flann::IndexParams> p;
760  vector<MxArray> rhs(m.toVector<MxArray>());
761  if (rhs.empty())
762  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
763  string type(rhs[0].toString());
764  if (type == "Linear") {
765  if (rhs.size() != 1)
766  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
767  p = makePtr<flann::LinearIndexParams>();
768  }
769  else if (type == "KDTree") {
770  if ((rhs.size() % 2) == 0)
771  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
772  int trees = 4;
773  for (size_t i = 1; i<rhs.size(); i += 2) {
774  string key(rhs[i].toString());
775  if (key == "Trees")
776  trees = rhs[i+1].toInt();
777  else
778  mexErrMsgIdAndTxt("mexopencv:error",
779  "Unrecognized option %s", key.c_str());
780  }
781  p = makePtr<flann::KDTreeIndexParams>(trees);
782  }
783  else if (type == "KMeans") {
784  if ((rhs.size() % 2) == 0)
785  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
786  int branching = 32;
787  int iterations = 11;
788  cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM;
789  float cb_index = 0.2f;
790  for (size_t i = 1; i<rhs.size(); i += 2) {
791  string key(rhs[i].toString());
792  if (key == "Branching")
793  branching = rhs[i+1].toInt();
794  else if (key == "Iterations")
795  iterations = rhs[i+1].toInt();
796  else if (key == "CentersInit")
797  centers_init = CentersInit[rhs[i+1].toString()];
798  else if (key == "CBIndex")
799  cb_index = rhs[i+1].toFloat();
800  else
801  mexErrMsgIdAndTxt("mexopencv:error",
802  "Unrecognized option %s", key.c_str());
803  }
804  p = makePtr<flann::KMeansIndexParams>(
805  branching, iterations, centers_init, cb_index);
806  }
807  else if (type == "Composite") {
808  if ((rhs.size() % 2) == 0)
809  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
810  int trees = 4;
811  int branching = 32;
812  int iterations = 11;
813  cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM;
814  float cb_index = 0.2f;
815  for (size_t i = 1; i<rhs.size(); i += 2) {
816  string key(rhs[i].toString());
817  if (key == "Trees")
818  trees = rhs[i+1].toInt();
819  else if (key == "Branching")
820  branching = rhs[i+1].toInt();
821  else if (key == "Iterations")
822  iterations = rhs[i+1].toInt();
823  else if (key == "CentersInit")
824  centers_init = CentersInit[rhs[i+1].toString()];
825  else if (key == "CBIndex")
826  cb_index = rhs[i+1].toFloat();
827  else
828  mexErrMsgIdAndTxt("mexopencv:error",
829  "Unrecognized option %s", key.c_str());
830  }
831  p = makePtr<flann::CompositeIndexParams>(
832  trees, branching, iterations, centers_init, cb_index);
833  }
834  else if (type == "LSH") {
835  if ((rhs.size() % 2) == 0)
836  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
837  int table_number = 20;
838  int key_size = 15;
839  int multi_probe_level = 0;
840  for (size_t i = 1; i<rhs.size(); i += 2) {
841  string key(rhs[i].toString());
842  if (key == "TableNumber")
843  table_number = rhs[i+1].toInt();
844  else if (key == "KeySize")
845  key_size = rhs[i+1].toInt();
846  else if (key == "MultiProbeLevel")
847  multi_probe_level = rhs[i+1].toInt();
848  else
849  mexErrMsgIdAndTxt("mexopencv:error",
850  "Unrecognized option %s", key.c_str());
851  }
852  p = makePtr<flann::LshIndexParams>(
853  table_number, key_size, multi_probe_level);
854  }
855  else if (type == "Autotuned") {
856  if ((rhs.size() % 2) == 0)
857  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
858  float target_precision = 0.8f;
859  float build_weight = 0.01f;
860  float memory_weight = 0;
861  float sample_fraction = 0.1f;
862  for (size_t i = 1; i<rhs.size(); i += 2) {
863  string key(rhs[i].toString());
864  if (key == "TargetPrecision")
865  target_precision = rhs[i+1].toFloat();
866  else if (key == "BuildWeight")
867  build_weight = rhs[i+1].toFloat();
868  else if (key == "MemoryWeight")
869  memory_weight = rhs[i+1].toFloat();
870  else if (key == "SampleFraction")
871  sample_fraction = rhs[i+1].toFloat();
872  else
873  mexErrMsgIdAndTxt("mexopencv:error",
874  "Unrecognized option %s", key.c_str());
875  }
876  p = makePtr<flann::AutotunedIndexParams>(
877  target_precision, build_weight, memory_weight, sample_fraction);
878  }
879  else if (type == "Saved") {
880  if(rhs.size() != 2)
881  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
882  string filename(rhs[1].toString());
883  p = makePtr<flann::SavedIndexParams>(filename);
884  }
885  else if (type == "HierarchicalClustering") {
886  if ((rhs.size() % 2) == 0)
887  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
888  int branching = 32;
889  cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM;
890  int trees = 4;
891  int leaf_size = 100;
892  for (size_t i = 1; i<rhs.size(); i += 2) {
893  string key(rhs[i].toString());
894  if (key == "Branching")
895  branching = rhs[i+1].toInt();
896  else if (key == "CentersInit")
897  centers_init = CentersInit[rhs[i+1].toString()];
898  else if (key == "Trees")
899  trees = rhs[i+1].toInt();
900  else if (key == "LeafSize")
901  leaf_size = rhs[i+1].toInt();
902  else
903  mexErrMsgIdAndTxt("mexopencv:error",
904  "Unrecognized option %s", key.c_str());
905  }
906  p = makePtr<flann::HierarchicalClusteringIndexParams>(
907  branching, centers_init, trees, leaf_size);
908  }
909  else
910  mexErrMsgIdAndTxt("mexopencv:error",
911  "Unrecognized IndexParams type: %s", type.c_str());
912  return p;
913 }
914 
920 Ptr<flann::SearchParams> toSearchParams(const MxArray& m)
921 {
922  vector<MxArray> rhs(m.toVector<MxArray>());
923  if ((rhs.size() % 2) != 0)
924  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
925  int checks = 32;
926  float eps = 0;
927  bool sorted = true;
928  for (size_t i = 0; i<rhs.size(); i += 2) {
929  string key(rhs[i].toString());
930  if (key == "Checks")
931  checks = rhs[i+1].toInt();
932  else if (key == "EPS")
933  eps = rhs[i+1].toFloat();
934  else if (key == "Sorted")
935  sorted = rhs[i+1].toBool();
936  else
937  mexErrMsgIdAndTxt("mexopencv:error",
938  "Unrecognized option %s", key.c_str());
939  }
940  return makePtr<flann::SearchParams>(checks, eps, sorted);
941 }
942 
943 Ptr<FlannBasedMatcher> createFlannBasedMatcher(
944  vector<MxArray>::const_iterator first,
945  vector<MxArray>::const_iterator last)
946 {
947  if (((last-first) % 2) != 0)
948  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
949  Ptr<flann::IndexParams> indexParams;
950  Ptr<flann::SearchParams> searchParams;
951  for (; first != last; first += 2) {
952  string key((*first).toString());
953  const MxArray& val = *(first + 1);
954  if (key == "Index")
955  indexParams = toIndexParams(val);
956  else if (key == "Search")
957  searchParams = toSearchParams(val);
958  else
959  mexErrMsgIdAndTxt("mexopencv:error",
960  "Unrecognized option %s", key.c_str());
961  }
962  if (indexParams.empty())
963  indexParams = makePtr<flann::KDTreeIndexParams>();
964  if (searchParams.empty())
965  searchParams = makePtr<flann::SearchParams>();
966  return makePtr<FlannBasedMatcher>(indexParams, searchParams);
967 }
968 
969 Ptr<BFMatcher> createBFMatcher(
970  vector<MxArray>::const_iterator first,
971  vector<MxArray>::const_iterator last)
972 {
973  if (((last-first) % 2) != 0)
974  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
975  int normType = cv::NORM_L2;
976  bool crossCheck = false;
977  for (; first != last; first += 2) {
978  string key((*first).toString());
979  const MxArray& val = *(first + 1);
980  if (key == "NormType")
981  normType = NormType[val.toString()];
982  else if (key == "CrossCheck")
983  crossCheck = val.toBool();
984  else
985  mexErrMsgIdAndTxt("mexopencv:error",
986  "Unrecognized option %s", key.c_str());
987  }
988  return makePtr<BFMatcher>(normType, crossCheck);
989 }
990 
991 Ptr<DescriptorMatcher> createDescriptorMatcher(
992  const string& type,
993  vector<MxArray>::const_iterator first,
994  vector<MxArray>::const_iterator last)
995 {
996  Ptr<DescriptorMatcher> p;
997  if ((last-first) > 0) {
998  if (type == "FlannBasedMatcher")
999  p = createFlannBasedMatcher(first, last);
1000  else if (type == "BFMatcher")
1001  p = createBFMatcher(first, last);
1002  else
1003  mexErrMsgIdAndTxt("mexopencv:error",
1004  "Unrecognized matcher %s", type.c_str());
1005  }
1006  else
1007  p = DescriptorMatcher::create(type);
1008  if (p.empty())
1009  mexErrMsgIdAndTxt("mexopencv:error",
1010  "Failed to create DescriptorMatcher of type %s", type.c_str());
1011  return p;
1012 }
1013 
const ConstMap< std::string, int > AgastTypeMap
AGAST neighborhood types.
const ConstMap< std::string, int > KAZEDiffusivityType
KAZE Diffusivity type.
cv::Ptr< cv::MSER > createMSER(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of MSER using options in arguments.
const ConstMap< string, cvflann::flann_centers_init_t > CentersInit
IndexParams centers initialization methods.
cv::Ptr< cv::AKAZE > createAKAZE(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of AKAZE using options in arguments.
Ptr< flann::SearchParams > toSearchParams(const MxArray &m)
Convert MxArray to FLANN search parameters.
cv::Ptr< cv::BFMatcher > createBFMatcher(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of BFMatcher using options in arguments.
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.
const ConstMap< std::string, int > AKAZEDescriptorType
AKAZE descriptor type.
cv::Ptr< cv::GFTTDetector > createGFTTDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of GFTTDetector using options in arguments.
cv::Ptr< cv::KAZE > createKAZE(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of KAZE using options in arguments.
cv::Ptr< cv::FastFeatureDetector > createFastFeatureDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of FastFeatureDetector using options in arguments.
double toDouble() const
Convert MxArray to double.
Definition: MxArray.cpp:496
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
Ptr< flann::IndexParams > toIndexParams(const MxArray &m)
Convert MxArray to FLANN index parameters.
cv::Ptr< cv::ORB > createORB(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of ORB using options in arguments.
float toFloat() const
Convert MxArray to float.
Definition: MxArray.cpp:503
std::vector< T > toVector() const
Convert MxArray to std::vector<T> of primitive types.
Definition: MxArray.hpp:1151
cv::Ptr< cv::FeatureDetector > createFeatureDetector(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Factory function for FeatureDetector creation.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
cv::Ptr< cv::FlannBasedMatcher > createFlannBasedMatcher(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of FlannBasedMatcher using options in arguments.
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.
bool toBool() const
Convert MxArray to bool.
Definition: MxArray.cpp:510
cv::Ptr< cv::AgastFeatureDetector > createAgastFeatureDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of AgastFeatureDetector using options in arguments.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
const ConstMap< std::string, int > NormType
Norm type map for option processing.
Definition: mexopencv.hpp:135
cv::Ptr< cv::SimpleBlobDetector > createSimpleBlobDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of SimpleBlobDetector using options in arguments.
const ConstMap< std::string, int > FASTTypeMap
FAST types.
cv::Ptr< cv::BRISK > createBRISK(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of BRISK using options in arguments.
const ConstMap< std::string, int > ORBScoreType
ORB score types.