features2d.hpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef OPENCV_FEATURES_2D_HPP
  43. #define OPENCV_FEATURES_2D_HPP
  44. #include "opencv2/opencv_modules.hpp"
  45. #include "opencv2/core.hpp"
  46. #ifdef HAVE_OPENCV_FLANN
  47. #include "opencv2/flann/miniflann.hpp"
  48. #endif
  49. /**
  50. @defgroup features2d 2D Features Framework
  51. @{
  52. @defgroup features2d_main Feature Detection and Description
  53. @defgroup features2d_match Descriptor Matchers
  54. Matchers of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to
  55. easily switch between different algorithms solving the same problem. This section is devoted to
  56. matching descriptors that are represented as vectors in a multidimensional space. All objects that
  57. implement vector descriptor matchers inherit the DescriptorMatcher interface.
  58. @defgroup features2d_draw Drawing Function of Keypoints and Matches
  59. @defgroup features2d_category Object Categorization
  60. This section describes approaches based on local 2D features and used to categorize objects.
  61. @defgroup feature2d_hal Hardware Acceleration Layer
  62. @{
  63. @defgroup features2d_hal_interface Interface
  64. @}
  65. @}
  66. */
  67. namespace cv
  68. {
  69. //! @addtogroup features2d_main
  70. //! @{
  71. // //! writes vector of keypoints to the file storage
  72. // CV_EXPORTS void write(FileStorage& fs, const String& name, const std::vector<KeyPoint>& keypoints);
  73. // //! reads vector of keypoints from the specified file storage node
  74. // CV_EXPORTS void read(const FileNode& node, CV_OUT std::vector<KeyPoint>& keypoints);
  75. /** @brief A class filters a vector of keypoints.
  76. Because now it is difficult to provide a convenient interface for all usage scenarios of the
  77. keypoints filter class, it has only several needed by now static methods.
  78. */
  79. class CV_EXPORTS KeyPointsFilter
  80. {
  81. public:
  82. KeyPointsFilter(){}
  83. /*
  84. * Remove keypoints within borderPixels of an image edge.
  85. */
  86. static void runByImageBorder( std::vector<KeyPoint>& keypoints, Size imageSize, int borderSize );
  87. /*
  88. * Remove keypoints of sizes out of range.
  89. */
  90. static void runByKeypointSize( std::vector<KeyPoint>& keypoints, float minSize,
  91. float maxSize=FLT_MAX );
  92. /*
  93. * Remove keypoints from some image by mask for pixels of this image.
  94. */
  95. static void runByPixelsMask( std::vector<KeyPoint>& keypoints, const Mat& mask );
  96. /*
  97. * Remove duplicated keypoints.
  98. */
  99. static void removeDuplicated( std::vector<KeyPoint>& keypoints );
  100. /*
  101. * Remove duplicated keypoints and sort the remaining keypoints
  102. */
  103. static void removeDuplicatedSorted( std::vector<KeyPoint>& keypoints );
  104. /*
  105. * Retain the specified number of the best keypoints (according to the response)
  106. */
  107. static void retainBest( std::vector<KeyPoint>& keypoints, int npoints );
  108. };
  109. /************************************ Base Classes ************************************/
  110. /** @brief Abstract base class for 2D image feature detectors and descriptor extractors
  111. */
  112. #ifdef __EMSCRIPTEN__
  113. class CV_EXPORTS_W Feature2D : public Algorithm
  114. #else
  115. class CV_EXPORTS_W Feature2D : public virtual Algorithm
  116. #endif
  117. {
  118. public:
  119. virtual ~Feature2D();
  120. /** @brief Detects keypoints in an image (first variant) or image set (second variant).
  121. @param image Image.
  122. @param keypoints The detected keypoints. In the second variant of the method keypoints[i] is a set
  123. of keypoints detected in images[i] .
  124. @param mask Mask specifying where to look for keypoints (optional). It must be a 8-bit integer
  125. matrix with non-zero values in the region of interest.
  126. */
  127. CV_WRAP virtual void detect( InputArray image,
  128. CV_OUT std::vector<KeyPoint>& keypoints,
  129. InputArray mask=noArray() );
  130. /** @overload
  131. @param images Image set.
  132. @param keypoints The detected keypoints. In the second variant of the method keypoints[i] is a set
  133. of keypoints detected in images[i] .
  134. @param masks Masks for each input image specifying where to look for keypoints (optional).
  135. masks[i] is a mask for images[i].
  136. */
  137. CV_WRAP virtual void detect( InputArrayOfArrays images,
  138. CV_OUT std::vector<std::vector<KeyPoint> >& keypoints,
  139. InputArrayOfArrays masks=noArray() );
  140. /** @brief Computes the descriptors for a set of keypoints detected in an image (first variant) or image set
  141. (second variant).
  142. @param image Image.
  143. @param keypoints Input collection of keypoints. Keypoints for which a descriptor cannot be
  144. computed are removed. Sometimes new keypoints can be added, for example: SIFT duplicates keypoint
  145. with several dominant orientations (for each orientation).
  146. @param descriptors Computed descriptors. In the second variant of the method descriptors[i] are
  147. descriptors computed for a keypoints[i]. Row j is the keypoints (or keypoints[i]) is the
  148. descriptor for keypoint j-th keypoint.
  149. */
  150. CV_WRAP virtual void compute( InputArray image,
  151. CV_OUT CV_IN_OUT std::vector<KeyPoint>& keypoints,
  152. OutputArray descriptors );
  153. /** @overload
  154. @param images Image set.
  155. @param keypoints Input collection of keypoints. Keypoints for which a descriptor cannot be
  156. computed are removed. Sometimes new keypoints can be added, for example: SIFT duplicates keypoint
  157. with several dominant orientations (for each orientation).
  158. @param descriptors Computed descriptors. In the second variant of the method descriptors[i] are
  159. descriptors computed for a keypoints[i]. Row j is the keypoints (or keypoints[i]) is the
  160. descriptor for keypoint j-th keypoint.
  161. */
  162. CV_WRAP virtual void compute( InputArrayOfArrays images,
  163. CV_OUT CV_IN_OUT std::vector<std::vector<KeyPoint> >& keypoints,
  164. OutputArrayOfArrays descriptors );
  165. /** Detects keypoints and computes the descriptors */
  166. CV_WRAP virtual void detectAndCompute( InputArray image, InputArray mask,
  167. CV_OUT std::vector<KeyPoint>& keypoints,
  168. OutputArray descriptors,
  169. bool useProvidedKeypoints=false );
  170. CV_WRAP virtual int descriptorSize() const;
  171. CV_WRAP virtual int descriptorType() const;
  172. CV_WRAP virtual int defaultNorm() const;
  173. CV_WRAP void write( const String& fileName ) const;
  174. CV_WRAP void read( const String& fileName );
  175. virtual void write( FileStorage&) const CV_OVERRIDE;
  176. // see corresponding cv::Algorithm method
  177. CV_WRAP virtual void read( const FileNode&) CV_OVERRIDE;
  178. //! Return true if detector object is empty
  179. CV_WRAP virtual bool empty() const CV_OVERRIDE;
  180. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  181. // see corresponding cv::Algorithm method
  182. CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); }
  183. #if CV_VERSION_MAJOR < 5
  184. inline void write(const Ptr<FileStorage>& fs, const String& name) const { CV_Assert(fs); Algorithm::write(*fs, name); }
  185. #endif
  186. };
  187. /** Feature detectors in OpenCV have wrappers with a common interface that enables you to easily switch
  188. between different algorithms solving the same problem. All objects that implement keypoint detectors
  189. inherit the FeatureDetector interface. */
  190. typedef Feature2D FeatureDetector;
  191. /** Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you
  192. to easily switch between different algorithms solving the same problem. This section is devoted to
  193. computing descriptors represented as vectors in a multidimensional space. All objects that implement
  194. the vector descriptor extractors inherit the DescriptorExtractor interface.
  195. */
  196. typedef Feature2D DescriptorExtractor;
  197. /** @brief Class for implementing the wrapper which makes detectors and extractors to be affine invariant,
  198. described as ASIFT in @cite YM11 .
  199. */
  200. class CV_EXPORTS_W AffineFeature : public Feature2D
  201. {
  202. public:
  203. /**
  204. @param backend The detector/extractor you want to use as backend.
  205. @param maxTilt The highest power index of tilt factor. 5 is used in the paper as tilt sampling range n.
  206. @param minTilt The lowest power index of tilt factor. 0 is used in the paper.
  207. @param tiltStep Tilt sampling step \f$\delta_t\f$ in Algorithm 1 in the paper.
  208. @param rotateStepBase Rotation sampling step factor b in Algorithm 1 in the paper.
  209. */
  210. CV_WRAP static Ptr<AffineFeature> create(const Ptr<Feature2D>& backend,
  211. int maxTilt = 5, int minTilt = 0, float tiltStep = 1.4142135623730951f, float rotateStepBase = 72);
  212. CV_WRAP virtual void setViewParams(const std::vector<float>& tilts, const std::vector<float>& rolls) = 0;
  213. CV_WRAP virtual void getViewParams(std::vector<float>& tilts, std::vector<float>& rolls) const = 0;
  214. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  215. };
  216. typedef AffineFeature AffineFeatureDetector;
  217. typedef AffineFeature AffineDescriptorExtractor;
  218. /** @brief Class for extracting keypoints and computing descriptors using the Scale Invariant Feature Transform
  219. (SIFT) algorithm by D. Lowe @cite Lowe04 .
  220. */
  221. class CV_EXPORTS_W SIFT : public Feature2D
  222. {
  223. public:
  224. /**
  225. @param nfeatures The number of best features to retain. The features are ranked by their scores
  226. (measured in SIFT algorithm as the local contrast)
  227. @param nOctaveLayers The number of layers in each octave. 3 is the value used in D. Lowe paper. The
  228. number of octaves is computed automatically from the image resolution.
  229. @param contrastThreshold The contrast threshold used to filter out weak features in semi-uniform
  230. (low-contrast) regions. The larger the threshold, the less features are produced by the detector.
  231. @note The contrast threshold will be divided by nOctaveLayers when the filtering is applied. When
  232. nOctaveLayers is set to default and if you want to use the value used in D. Lowe paper, 0.03, set
  233. this argument to 0.09.
  234. @param edgeThreshold The threshold used to filter out edge-like features. Note that the its meaning
  235. is different from the contrastThreshold, i.e. the larger the edgeThreshold, the less features are
  236. filtered out (more features are retained).
  237. @param sigma The sigma of the Gaussian applied to the input image at the octave \#0. If your image
  238. is captured with a weak camera with soft lenses, you might want to reduce the number.
  239. */
  240. CV_WRAP static Ptr<SIFT> create(int nfeatures = 0, int nOctaveLayers = 3,
  241. double contrastThreshold = 0.04, double edgeThreshold = 10,
  242. double sigma = 1.6);
  243. /** @brief Create SIFT with specified descriptorType.
  244. @param nfeatures The number of best features to retain. The features are ranked by their scores
  245. (measured in SIFT algorithm as the local contrast)
  246. @param nOctaveLayers The number of layers in each octave. 3 is the value used in D. Lowe paper. The
  247. number of octaves is computed automatically from the image resolution.
  248. @param contrastThreshold The contrast threshold used to filter out weak features in semi-uniform
  249. (low-contrast) regions. The larger the threshold, the less features are produced by the detector.
  250. @note The contrast threshold will be divided by nOctaveLayers when the filtering is applied. When
  251. nOctaveLayers is set to default and if you want to use the value used in D. Lowe paper, 0.03, set
  252. this argument to 0.09.
  253. @param edgeThreshold The threshold used to filter out edge-like features. Note that the its meaning
  254. is different from the contrastThreshold, i.e. the larger the edgeThreshold, the less features are
  255. filtered out (more features are retained).
  256. @param sigma The sigma of the Gaussian applied to the input image at the octave \#0. If your image
  257. is captured with a weak camera with soft lenses, you might want to reduce the number.
  258. @param descriptorType The type of descriptors. Only CV_32F and CV_8U are supported.
  259. */
  260. CV_WRAP static Ptr<SIFT> create(int nfeatures, int nOctaveLayers,
  261. double contrastThreshold, double edgeThreshold,
  262. double sigma, int descriptorType);
  263. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  264. };
  265. typedef SIFT SiftFeatureDetector;
  266. typedef SIFT SiftDescriptorExtractor;
  267. /** @brief Class implementing the BRISK keypoint detector and descriptor extractor, described in @cite LCS11 .
  268. */
  269. class CV_EXPORTS_W BRISK : public Feature2D
  270. {
  271. public:
  272. /** @brief The BRISK constructor
  273. @param thresh AGAST detection threshold score.
  274. @param octaves detection octaves. Use 0 to do single scale.
  275. @param patternScale apply this scale to the pattern used for sampling the neighbourhood of a
  276. keypoint.
  277. */
  278. CV_WRAP static Ptr<BRISK> create(int thresh=30, int octaves=3, float patternScale=1.0f);
  279. /** @brief The BRISK constructor for a custom pattern
  280. @param radiusList defines the radii (in pixels) where the samples around a keypoint are taken (for
  281. keypoint scale 1).
  282. @param numberList defines the number of sampling points on the sampling circle. Must be the same
  283. size as radiusList..
  284. @param dMax threshold for the short pairings used for descriptor formation (in pixels for keypoint
  285. scale 1).
  286. @param dMin threshold for the long pairings used for orientation determination (in pixels for
  287. keypoint scale 1).
  288. @param indexChange index remapping of the bits. */
  289. CV_WRAP static Ptr<BRISK> create(const std::vector<float> &radiusList, const std::vector<int> &numberList,
  290. float dMax=5.85f, float dMin=8.2f, const std::vector<int>& indexChange=std::vector<int>());
  291. /** @brief The BRISK constructor for a custom pattern, detection threshold and octaves
  292. @param thresh AGAST detection threshold score.
  293. @param octaves detection octaves. Use 0 to do single scale.
  294. @param radiusList defines the radii (in pixels) where the samples around a keypoint are taken (for
  295. keypoint scale 1).
  296. @param numberList defines the number of sampling points on the sampling circle. Must be the same
  297. size as radiusList..
  298. @param dMax threshold for the short pairings used for descriptor formation (in pixels for keypoint
  299. scale 1).
  300. @param dMin threshold for the long pairings used for orientation determination (in pixels for
  301. keypoint scale 1).
  302. @param indexChange index remapping of the bits. */
  303. CV_WRAP static Ptr<BRISK> create(int thresh, int octaves, const std::vector<float> &radiusList,
  304. const std::vector<int> &numberList, float dMax=5.85f, float dMin=8.2f,
  305. const std::vector<int>& indexChange=std::vector<int>());
  306. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  307. };
  308. /** @brief Class implementing the ORB (*oriented BRIEF*) keypoint detector and descriptor extractor
  309. described in @cite RRKB11 . The algorithm uses FAST in pyramids to detect stable keypoints, selects
  310. the strongest features using FAST or Harris response, finds their orientation using first-order
  311. moments and computes the descriptors using BRIEF (where the coordinates of random point pairs (or
  312. k-tuples) are rotated according to the measured orientation).
  313. */
  314. class CV_EXPORTS_W ORB : public Feature2D
  315. {
  316. public:
  317. enum { kBytes = 32, HARRIS_SCORE=0, FAST_SCORE=1 };
  318. /** @brief The ORB constructor
  319. @param nfeatures The maximum number of features to retain.
  320. @param scaleFactor Pyramid decimation ratio, greater than 1. scaleFactor==2 means the classical
  321. pyramid, where each next level has 4x less pixels than the previous, but such a big scale factor
  322. will degrade feature matching scores dramatically. On the other hand, too close to 1 scale factor
  323. will mean that to cover certain scale range you will need more pyramid levels and so the speed
  324. will suffer.
  325. @param nlevels The number of pyramid levels. The smallest level will have linear size equal to
  326. input_image_linear_size/pow(scaleFactor, nlevels - firstLevel).
  327. @param edgeThreshold This is size of the border where the features are not detected. It should
  328. roughly match the patchSize parameter.
  329. @param firstLevel The level of pyramid to put source image to. Previous layers are filled
  330. with upscaled source image.
  331. @param WTA_K The number of points that produce each element of the oriented BRIEF descriptor. The
  332. default value 2 means the BRIEF where we take a random point pair and compare their brightnesses,
  333. so we get 0/1 response. Other possible values are 3 and 4. For example, 3 means that we take 3
  334. random points (of course, those point coordinates are random, but they are generated from the
  335. pre-defined seed, so each element of BRIEF descriptor is computed deterministically from the pixel
  336. rectangle), find point of maximum brightness and output index of the winner (0, 1 or 2). Such
  337. output will occupy 2 bits, and therefore it will need a special variant of Hamming distance,
  338. denoted as NORM_HAMMING2 (2 bits per bin). When WTA_K=4, we take 4 random points to compute each
  339. bin (that will also occupy 2 bits with possible values 0, 1, 2 or 3).
  340. @param scoreType The default HARRIS_SCORE means that Harris algorithm is used to rank features
  341. (the score is written to KeyPoint::score and is used to retain best nfeatures features);
  342. FAST_SCORE is alternative value of the parameter that produces slightly less stable keypoints,
  343. but it is a little faster to compute.
  344. @param patchSize size of the patch used by the oriented BRIEF descriptor. Of course, on smaller
  345. pyramid layers the perceived image area covered by a feature will be larger.
  346. @param fastThreshold the fast threshold
  347. */
  348. CV_WRAP static Ptr<ORB> create(int nfeatures=500, float scaleFactor=1.2f, int nlevels=8, int edgeThreshold=31,
  349. int firstLevel=0, int WTA_K=2, int scoreType=ORB::HARRIS_SCORE, int patchSize=31, int fastThreshold=20);
  350. CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
  351. CV_WRAP virtual int getMaxFeatures() const = 0;
  352. CV_WRAP virtual void setScaleFactor(double scaleFactor) = 0;
  353. CV_WRAP virtual double getScaleFactor() const = 0;
  354. CV_WRAP virtual void setNLevels(int nlevels) = 0;
  355. CV_WRAP virtual int getNLevels() const = 0;
  356. CV_WRAP virtual void setEdgeThreshold(int edgeThreshold) = 0;
  357. CV_WRAP virtual int getEdgeThreshold() const = 0;
  358. CV_WRAP virtual void setFirstLevel(int firstLevel) = 0;
  359. CV_WRAP virtual int getFirstLevel() const = 0;
  360. CV_WRAP virtual void setWTA_K(int wta_k) = 0;
  361. CV_WRAP virtual int getWTA_K() const = 0;
  362. CV_WRAP virtual void setScoreType(int scoreType) = 0;
  363. CV_WRAP virtual int getScoreType() const = 0;
  364. CV_WRAP virtual void setPatchSize(int patchSize) = 0;
  365. CV_WRAP virtual int getPatchSize() const = 0;
  366. CV_WRAP virtual void setFastThreshold(int fastThreshold) = 0;
  367. CV_WRAP virtual int getFastThreshold() const = 0;
  368. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  369. };
  370. /** @brief Maximally stable extremal region extractor
  371. The class encapsulates all the parameters of the %MSER extraction algorithm (see [wiki
  372. article](http://en.wikipedia.org/wiki/Maximally_stable_extremal_regions)).
  373. - there are two different implementation of %MSER: one for grey image, one for color image
  374. - the grey image algorithm is taken from: @cite nister2008linear ; the paper claims to be faster
  375. than union-find method; it actually get 1.5~2m/s on my centrino L7200 1.2GHz laptop.
  376. - the color image algorithm is taken from: @cite forssen2007maximally ; it should be much slower
  377. than grey image method ( 3~4 times )
  378. - (Python) A complete example showing the use of the %MSER detector can be found at samples/python/mser.py
  379. */
  380. class CV_EXPORTS_W MSER : public Feature2D
  381. {
  382. public:
  383. /** @brief Full constructor for %MSER detector
  384. @param delta it compares \f$(size_{i}-size_{i-delta})/size_{i-delta}\f$
  385. @param min_area prune the area which smaller than minArea
  386. @param max_area prune the area which bigger than maxArea
  387. @param max_variation prune the area have similar size to its children
  388. @param min_diversity for color image, trace back to cut off mser with diversity less than min_diversity
  389. @param max_evolution for color image, the evolution steps
  390. @param area_threshold for color image, the area threshold to cause re-initialize
  391. @param min_margin for color image, ignore too small margin
  392. @param edge_blur_size for color image, the aperture size for edge blur
  393. */
  394. CV_WRAP static Ptr<MSER> create( int delta=5, int min_area=60, int max_area=14400,
  395. double max_variation=0.25, double min_diversity=.2,
  396. int max_evolution=200, double area_threshold=1.01,
  397. double min_margin=0.003, int edge_blur_size=5 );
  398. /** @brief Detect %MSER regions
  399. @param image input image (8UC1, 8UC3 or 8UC4, must be greater or equal than 3x3)
  400. @param msers resulting list of point sets
  401. @param bboxes resulting bounding boxes
  402. */
  403. CV_WRAP virtual void detectRegions( InputArray image,
  404. CV_OUT std::vector<std::vector<Point> >& msers,
  405. CV_OUT std::vector<Rect>& bboxes ) = 0;
  406. CV_WRAP virtual void setDelta(int delta) = 0;
  407. CV_WRAP virtual int getDelta() const = 0;
  408. CV_WRAP virtual void setMinArea(int minArea) = 0;
  409. CV_WRAP virtual int getMinArea() const = 0;
  410. CV_WRAP virtual void setMaxArea(int maxArea) = 0;
  411. CV_WRAP virtual int getMaxArea() const = 0;
  412. CV_WRAP virtual void setPass2Only(bool f) = 0;
  413. CV_WRAP virtual bool getPass2Only() const = 0;
  414. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  415. };
  416. /** @overload */
  417. CV_EXPORTS void FAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
  418. int threshold, bool nonmaxSuppression=true );
  419. /** @brief Detects corners using the FAST algorithm
  420. @param image grayscale image where keypoints (corners) are detected.
  421. @param keypoints keypoints detected on the image.
  422. @param threshold threshold on difference between intensity of the central pixel and pixels of a
  423. circle around this pixel.
  424. @param nonmaxSuppression if true, non-maximum suppression is applied to detected corners
  425. (keypoints).
  426. @param type one of the three neighborhoods as defined in the paper:
  427. FastFeatureDetector::TYPE_9_16, FastFeatureDetector::TYPE_7_12,
  428. FastFeatureDetector::TYPE_5_8
  429. Detects corners using the FAST algorithm by @cite Rosten06 .
  430. @note In Python API, types are given as cv2.FAST_FEATURE_DETECTOR_TYPE_5_8,
  431. cv2.FAST_FEATURE_DETECTOR_TYPE_7_12 and cv2.FAST_FEATURE_DETECTOR_TYPE_9_16. For corner
  432. detection, use cv2.FAST.detect() method.
  433. */
  434. CV_EXPORTS void FAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
  435. int threshold, bool nonmaxSuppression, int type );
  436. //! @} features2d_main
  437. //! @addtogroup features2d_main
  438. //! @{
  439. /** @brief Wrapping class for feature detection using the FAST method. :
  440. */
  441. class CV_EXPORTS_W FastFeatureDetector : public Feature2D
  442. {
  443. public:
  444. enum
  445. {
  446. TYPE_5_8 = 0, TYPE_7_12 = 1, TYPE_9_16 = 2,
  447. THRESHOLD = 10000, NONMAX_SUPPRESSION=10001, FAST_N=10002,
  448. };
  449. CV_WRAP static Ptr<FastFeatureDetector> create( int threshold=10,
  450. bool nonmaxSuppression=true,
  451. int type=FastFeatureDetector::TYPE_9_16 );
  452. CV_WRAP virtual void setThreshold(int threshold) = 0;
  453. CV_WRAP virtual int getThreshold() const = 0;
  454. CV_WRAP virtual void setNonmaxSuppression(bool f) = 0;
  455. CV_WRAP virtual bool getNonmaxSuppression() const = 0;
  456. CV_WRAP virtual void setType(int type) = 0;
  457. CV_WRAP virtual int getType() const = 0;
  458. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  459. };
  460. /** @overload */
  461. CV_EXPORTS void AGAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
  462. int threshold, bool nonmaxSuppression=true );
  463. /** @brief Detects corners using the AGAST algorithm
  464. @param image grayscale image where keypoints (corners) are detected.
  465. @param keypoints keypoints detected on the image.
  466. @param threshold threshold on difference between intensity of the central pixel and pixels of a
  467. circle around this pixel.
  468. @param nonmaxSuppression if true, non-maximum suppression is applied to detected corners
  469. (keypoints).
  470. @param type one of the four neighborhoods as defined in the paper:
  471. AgastFeatureDetector::AGAST_5_8, AgastFeatureDetector::AGAST_7_12d,
  472. AgastFeatureDetector::AGAST_7_12s, AgastFeatureDetector::OAST_9_16
  473. For non-Intel platforms, there is a tree optimised variant of AGAST with same numerical results.
  474. The 32-bit binary tree tables were generated automatically from original code using perl script.
  475. The perl script and examples of tree generation are placed in features2d/doc folder.
  476. Detects corners using the AGAST algorithm by @cite mair2010_agast .
  477. */
  478. CV_EXPORTS void AGAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
  479. int threshold, bool nonmaxSuppression, int type );
  480. //! @} features2d_main
  481. //! @addtogroup features2d_main
  482. //! @{
  483. /** @brief Wrapping class for feature detection using the AGAST method. :
  484. */
  485. class CV_EXPORTS_W AgastFeatureDetector : public Feature2D
  486. {
  487. public:
  488. enum
  489. {
  490. AGAST_5_8 = 0, AGAST_7_12d = 1, AGAST_7_12s = 2, OAST_9_16 = 3,
  491. THRESHOLD = 10000, NONMAX_SUPPRESSION = 10001,
  492. };
  493. CV_WRAP static Ptr<AgastFeatureDetector> create( int threshold=10,
  494. bool nonmaxSuppression=true,
  495. int type=AgastFeatureDetector::OAST_9_16 );
  496. CV_WRAP virtual void setThreshold(int threshold) = 0;
  497. CV_WRAP virtual int getThreshold() const = 0;
  498. CV_WRAP virtual void setNonmaxSuppression(bool f) = 0;
  499. CV_WRAP virtual bool getNonmaxSuppression() const = 0;
  500. CV_WRAP virtual void setType(int type) = 0;
  501. CV_WRAP virtual int getType() const = 0;
  502. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  503. };
  504. /** @brief Wrapping class for feature detection using the goodFeaturesToTrack function. :
  505. */
  506. class CV_EXPORTS_W GFTTDetector : public Feature2D
  507. {
  508. public:
  509. CV_WRAP static Ptr<GFTTDetector> create( int maxCorners=1000, double qualityLevel=0.01, double minDistance=1,
  510. int blockSize=3, bool useHarrisDetector=false, double k=0.04 );
  511. CV_WRAP static Ptr<GFTTDetector> create( int maxCorners, double qualityLevel, double minDistance,
  512. int blockSize, int gradiantSize, bool useHarrisDetector=false, double k=0.04 );
  513. CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
  514. CV_WRAP virtual int getMaxFeatures() const = 0;
  515. CV_WRAP virtual void setQualityLevel(double qlevel) = 0;
  516. CV_WRAP virtual double getQualityLevel() const = 0;
  517. CV_WRAP virtual void setMinDistance(double minDistance) = 0;
  518. CV_WRAP virtual double getMinDistance() const = 0;
  519. CV_WRAP virtual void setBlockSize(int blockSize) = 0;
  520. CV_WRAP virtual int getBlockSize() const = 0;
  521. CV_WRAP virtual void setHarrisDetector(bool val) = 0;
  522. CV_WRAP virtual bool getHarrisDetector() const = 0;
  523. CV_WRAP virtual void setK(double k) = 0;
  524. CV_WRAP virtual double getK() const = 0;
  525. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  526. };
  527. /** @brief Class for extracting blobs from an image. :
  528. The class implements a simple algorithm for extracting blobs from an image:
  529. 1. Convert the source image to binary images by applying thresholding with several thresholds from
  530. minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
  531. neighboring thresholds.
  532. 2. Extract connected components from every binary image by findContours and calculate their
  533. centers.
  534. 3. Group centers from several binary images by their coordinates. Close centers form one group that
  535. corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
  536. 4. From the groups, estimate final centers of blobs and their radiuses and return as locations and
  537. sizes of keypoints.
  538. This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
  539. to turn on/off corresponding filtration. Available filtrations:
  540. - **By color**. This filter compares the intensity of a binary image at the center of a blob to
  541. blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs
  542. and blobColor = 255 to extract light blobs.
  543. - **By area**. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
  544. - **By circularity**. Extracted blobs have circularity
  545. (\f$\frac{4*\pi*Area}{perimeter * perimeter}\f$) between minCircularity (inclusive) and
  546. maxCircularity (exclusive).
  547. - **By ratio of the minimum inertia to maximum inertia**. Extracted blobs have this ratio
  548. between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
  549. - **By convexity**. Extracted blobs have convexity (area / area of blob convex hull) between
  550. minConvexity (inclusive) and maxConvexity (exclusive).
  551. Default values of parameters are tuned to extract dark circular blobs.
  552. */
  553. class CV_EXPORTS_W SimpleBlobDetector : public Feature2D
  554. {
  555. public:
  556. struct CV_EXPORTS_W_SIMPLE Params
  557. {
  558. CV_WRAP Params();
  559. CV_PROP_RW float thresholdStep;
  560. CV_PROP_RW float minThreshold;
  561. CV_PROP_RW float maxThreshold;
  562. CV_PROP_RW size_t minRepeatability;
  563. CV_PROP_RW float minDistBetweenBlobs;
  564. CV_PROP_RW bool filterByColor;
  565. CV_PROP_RW uchar blobColor;
  566. CV_PROP_RW bool filterByArea;
  567. CV_PROP_RW float minArea, maxArea;
  568. CV_PROP_RW bool filterByCircularity;
  569. CV_PROP_RW float minCircularity, maxCircularity;
  570. CV_PROP_RW bool filterByInertia;
  571. CV_PROP_RW float minInertiaRatio, maxInertiaRatio;
  572. CV_PROP_RW bool filterByConvexity;
  573. CV_PROP_RW float minConvexity, maxConvexity;
  574. void read( const FileNode& fn );
  575. void write( FileStorage& fs ) const;
  576. };
  577. CV_WRAP static Ptr<SimpleBlobDetector>
  578. create(const SimpleBlobDetector::Params &parameters = SimpleBlobDetector::Params());
  579. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  580. };
  581. //! @} features2d_main
  582. //! @addtogroup features2d_main
  583. //! @{
  584. /** @brief Class implementing the KAZE keypoint detector and descriptor extractor, described in @cite ABD12 .
  585. @note AKAZE descriptor can only be used with KAZE or AKAZE keypoints .. [ABD12] KAZE Features. Pablo
  586. F. Alcantarilla, Adrien Bartoli and Andrew J. Davison. In European Conference on Computer Vision
  587. (ECCV), Fiorenze, Italy, October 2012.
  588. */
  589. class CV_EXPORTS_W KAZE : public Feature2D
  590. {
  591. public:
  592. enum
  593. {
  594. DIFF_PM_G1 = 0,
  595. DIFF_PM_G2 = 1,
  596. DIFF_WEICKERT = 2,
  597. DIFF_CHARBONNIER = 3
  598. };
  599. /** @brief The KAZE constructor
  600. @param extended Set to enable extraction of extended (128-byte) descriptor.
  601. @param upright Set to enable use of upright descriptors (non rotation-invariant).
  602. @param threshold Detector response threshold to accept point
  603. @param nOctaves Maximum octave evolution of the image
  604. @param nOctaveLayers Default number of sublevels per scale level
  605. @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
  606. DIFF_CHARBONNIER
  607. */
  608. CV_WRAP static Ptr<KAZE> create(bool extended=false, bool upright=false,
  609. float threshold = 0.001f,
  610. int nOctaves = 4, int nOctaveLayers = 4,
  611. int diffusivity = KAZE::DIFF_PM_G2);
  612. CV_WRAP virtual void setExtended(bool extended) = 0;
  613. CV_WRAP virtual bool getExtended() const = 0;
  614. CV_WRAP virtual void setUpright(bool upright) = 0;
  615. CV_WRAP virtual bool getUpright() const = 0;
  616. CV_WRAP virtual void setThreshold(double threshold) = 0;
  617. CV_WRAP virtual double getThreshold() const = 0;
  618. CV_WRAP virtual void setNOctaves(int octaves) = 0;
  619. CV_WRAP virtual int getNOctaves() const = 0;
  620. CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
  621. CV_WRAP virtual int getNOctaveLayers() const = 0;
  622. CV_WRAP virtual void setDiffusivity(int diff) = 0;
  623. CV_WRAP virtual int getDiffusivity() const = 0;
  624. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  625. };
  626. /** @brief Class implementing the AKAZE keypoint detector and descriptor extractor, described in @cite ANB13.
  627. @details AKAZE descriptors can only be used with KAZE or AKAZE keypoints. This class is thread-safe.
  628. @note When you need descriptors use Feature2D::detectAndCompute, which
  629. provides better performance. When using Feature2D::detect followed by
  630. Feature2D::compute scale space pyramid is computed twice.
  631. @note AKAZE implements T-API. When image is passed as UMat some parts of the algorithm
  632. will use OpenCL.
  633. @note [ANB13] Fast Explicit Diffusion for Accelerated Features in Nonlinear
  634. Scale Spaces. Pablo F. Alcantarilla, Jesús Nuevo and Adrien Bartoli. In
  635. British Machine Vision Conference (BMVC), Bristol, UK, September 2013.
  636. */
  637. class CV_EXPORTS_W AKAZE : public Feature2D
  638. {
  639. public:
  640. // AKAZE descriptor type
  641. enum
  642. {
  643. DESCRIPTOR_KAZE_UPRIGHT = 2, ///< Upright descriptors, not invariant to rotation
  644. DESCRIPTOR_KAZE = 3,
  645. DESCRIPTOR_MLDB_UPRIGHT = 4, ///< Upright descriptors, not invariant to rotation
  646. DESCRIPTOR_MLDB = 5
  647. };
  648. /** @brief The AKAZE constructor
  649. @param descriptor_type Type of the extracted descriptor: DESCRIPTOR_KAZE,
  650. DESCRIPTOR_KAZE_UPRIGHT, DESCRIPTOR_MLDB or DESCRIPTOR_MLDB_UPRIGHT.
  651. @param descriptor_size Size of the descriptor in bits. 0 -\> Full size
  652. @param descriptor_channels Number of channels in the descriptor (1, 2, 3)
  653. @param threshold Detector response threshold to accept point
  654. @param nOctaves Maximum octave evolution of the image
  655. @param nOctaveLayers Default number of sublevels per scale level
  656. @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
  657. DIFF_CHARBONNIER
  658. */
  659. CV_WRAP static Ptr<AKAZE> create(int descriptor_type=AKAZE::DESCRIPTOR_MLDB,
  660. int descriptor_size = 0, int descriptor_channels = 3,
  661. float threshold = 0.001f, int nOctaves = 4,
  662. int nOctaveLayers = 4, int diffusivity = KAZE::DIFF_PM_G2);
  663. CV_WRAP virtual void setDescriptorType(int dtype) = 0;
  664. CV_WRAP virtual int getDescriptorType() const = 0;
  665. CV_WRAP virtual void setDescriptorSize(int dsize) = 0;
  666. CV_WRAP virtual int getDescriptorSize() const = 0;
  667. CV_WRAP virtual void setDescriptorChannels(int dch) = 0;
  668. CV_WRAP virtual int getDescriptorChannels() const = 0;
  669. CV_WRAP virtual void setThreshold(double threshold) = 0;
  670. CV_WRAP virtual double getThreshold() const = 0;
  671. CV_WRAP virtual void setNOctaves(int octaves) = 0;
  672. CV_WRAP virtual int getNOctaves() const = 0;
  673. CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
  674. CV_WRAP virtual int getNOctaveLayers() const = 0;
  675. CV_WRAP virtual void setDiffusivity(int diff) = 0;
  676. CV_WRAP virtual int getDiffusivity() const = 0;
  677. CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
  678. };
  679. //! @} features2d_main
  680. /****************************************************************************************\
  681. * Distance *
  682. \****************************************************************************************/
  683. template<typename T>
  684. struct CV_EXPORTS Accumulator
  685. {
  686. typedef T Type;
  687. };
  688. template<> struct Accumulator<unsigned char> { typedef float Type; };
  689. template<> struct Accumulator<unsigned short> { typedef float Type; };
  690. template<> struct Accumulator<char> { typedef float Type; };
  691. template<> struct Accumulator<short> { typedef float Type; };
  692. /*
  693. * Squared Euclidean distance functor
  694. */
  695. template<class T>
  696. struct CV_EXPORTS SL2
  697. {
  698. enum { normType = NORM_L2SQR };
  699. typedef T ValueType;
  700. typedef typename Accumulator<T>::Type ResultType;
  701. ResultType operator()( const T* a, const T* b, int size ) const
  702. {
  703. return normL2Sqr<ValueType, ResultType>(a, b, size);
  704. }
  705. };
  706. /*
  707. * Euclidean distance functor
  708. */
  709. template<class T>
  710. struct L2
  711. {
  712. enum { normType = NORM_L2 };
  713. typedef T ValueType;
  714. typedef typename Accumulator<T>::Type ResultType;
  715. ResultType operator()( const T* a, const T* b, int size ) const
  716. {
  717. return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
  718. }
  719. };
  720. /*
  721. * Manhattan distance (city block distance) functor
  722. */
  723. template<class T>
  724. struct L1
  725. {
  726. enum { normType = NORM_L1 };
  727. typedef T ValueType;
  728. typedef typename Accumulator<T>::Type ResultType;
  729. ResultType operator()( const T* a, const T* b, int size ) const
  730. {
  731. return normL1<ValueType, ResultType>(a, b, size);
  732. }
  733. };
  734. /****************************************************************************************\
  735. * DescriptorMatcher *
  736. \****************************************************************************************/
  737. //! @addtogroup features2d_match
  738. //! @{
  739. /** @brief Abstract base class for matching keypoint descriptors.
  740. It has two groups of match methods: for matching descriptors of an image with another image or with
  741. an image set.
  742. */
  743. class CV_EXPORTS_W DescriptorMatcher : public Algorithm
  744. {
  745. public:
  746. enum
  747. {
  748. FLANNBASED = 1,
  749. BRUTEFORCE = 2,
  750. BRUTEFORCE_L1 = 3,
  751. BRUTEFORCE_HAMMING = 4,
  752. BRUTEFORCE_HAMMINGLUT = 5,
  753. BRUTEFORCE_SL2 = 6
  754. };
  755. virtual ~DescriptorMatcher();
  756. /** @brief Adds descriptors to train a CPU(trainDescCollectionis) or GPU(utrainDescCollectionis) descriptor
  757. collection.
  758. If the collection is not empty, the new descriptors are added to existing train descriptors.
  759. @param descriptors Descriptors to add. Each descriptors[i] is a set of descriptors from the same
  760. train image.
  761. */
  762. CV_WRAP virtual void add( InputArrayOfArrays descriptors );
  763. /** @brief Returns a constant link to the train descriptor collection trainDescCollection .
  764. */
  765. CV_WRAP const std::vector<Mat>& getTrainDescriptors() const;
  766. /** @brief Clears the train descriptor collections.
  767. */
  768. CV_WRAP virtual void clear() CV_OVERRIDE;
  769. /** @brief Returns true if there are no train descriptors in the both collections.
  770. */
  771. CV_WRAP virtual bool empty() const CV_OVERRIDE;
  772. /** @brief Returns true if the descriptor matcher supports masking permissible matches.
  773. */
  774. CV_WRAP virtual bool isMaskSupported() const = 0;
  775. /** @brief Trains a descriptor matcher
  776. Trains a descriptor matcher (for example, the flann index). In all methods to match, the method
  777. train() is run every time before matching. Some descriptor matchers (for example, BruteForceMatcher)
  778. have an empty implementation of this method. Other matchers really train their inner structures (for
  779. example, FlannBasedMatcher trains flann::Index ).
  780. */
  781. CV_WRAP virtual void train();
  782. /** @brief Finds the best match for each descriptor from a query set.
  783. @param queryDescriptors Query set of descriptors.
  784. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  785. collection stored in the class object.
  786. @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
  787. descriptor. So, matches size may be smaller than the query descriptors count.
  788. @param mask Mask specifying permissible matches between an input query and train matrices of
  789. descriptors.
  790. In the first variant of this method, the train descriptors are passed as an input argument. In the
  791. second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
  792. used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
  793. matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
  794. mask.at\<uchar\>(i,j) is non-zero.
  795. */
  796. CV_WRAP void match( InputArray queryDescriptors, InputArray trainDescriptors,
  797. CV_OUT std::vector<DMatch>& matches, InputArray mask=noArray() ) const;
  798. /** @brief Finds the k best matches for each descriptor from a query set.
  799. @param queryDescriptors Query set of descriptors.
  800. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  801. collection stored in the class object.
  802. @param mask Mask specifying permissible matches between an input query and train matrices of
  803. descriptors.
  804. @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
  805. @param k Count of best matches found per each query descriptor or less if a query descriptor has
  806. less than k possible matches in total.
  807. @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
  808. false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
  809. the matches vector does not contain matches for fully masked-out query descriptors.
  810. These extended variants of DescriptorMatcher::match methods find several best matches for each query
  811. descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::match
  812. for the details about query and train descriptors.
  813. */
  814. CV_WRAP void knnMatch( InputArray queryDescriptors, InputArray trainDescriptors,
  815. CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
  816. InputArray mask=noArray(), bool compactResult=false ) const;
  817. /** @brief For each query descriptor, finds the training descriptors not farther than the specified distance.
  818. @param queryDescriptors Query set of descriptors.
  819. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  820. collection stored in the class object.
  821. @param matches Found matches.
  822. @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
  823. false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
  824. the matches vector does not contain matches for fully masked-out query descriptors.
  825. @param maxDistance Threshold for the distance between matched descriptors. Distance means here
  826. metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
  827. in Pixels)!
  828. @param mask Mask specifying permissible matches between an input query and train matrices of
  829. descriptors.
  830. For each query descriptor, the methods find such training descriptors that the distance between the
  831. query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
  832. returned in the distance increasing order.
  833. */
  834. CV_WRAP void radiusMatch( InputArray queryDescriptors, InputArray trainDescriptors,
  835. CV_OUT std::vector<std::vector<DMatch> >& matches, float maxDistance,
  836. InputArray mask=noArray(), bool compactResult=false ) const;
  837. /** @overload
  838. @param queryDescriptors Query set of descriptors.
  839. @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
  840. descriptor. So, matches size may be smaller than the query descriptors count.
  841. @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
  842. descriptors and stored train descriptors from the i-th image trainDescCollection[i].
  843. */
  844. CV_WRAP void match( InputArray queryDescriptors, CV_OUT std::vector<DMatch>& matches,
  845. InputArrayOfArrays masks=noArray() );
  846. /** @overload
  847. @param queryDescriptors Query set of descriptors.
  848. @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
  849. @param k Count of best matches found per each query descriptor or less if a query descriptor has
  850. less than k possible matches in total.
  851. @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
  852. descriptors and stored train descriptors from the i-th image trainDescCollection[i].
  853. @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
  854. false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
  855. the matches vector does not contain matches for fully masked-out query descriptors.
  856. */
  857. CV_WRAP void knnMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
  858. InputArrayOfArrays masks=noArray(), bool compactResult=false );
  859. /** @overload
  860. @param queryDescriptors Query set of descriptors.
  861. @param matches Found matches.
  862. @param maxDistance Threshold for the distance between matched descriptors. Distance means here
  863. metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
  864. in Pixels)!
  865. @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
  866. descriptors and stored train descriptors from the i-th image trainDescCollection[i].
  867. @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
  868. false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
  869. the matches vector does not contain matches for fully masked-out query descriptors.
  870. */
  871. CV_WRAP void radiusMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, float maxDistance,
  872. InputArrayOfArrays masks=noArray(), bool compactResult=false );
  873. CV_WRAP void write( const String& fileName ) const
  874. {
  875. FileStorage fs(fileName, FileStorage::WRITE);
  876. write(fs);
  877. }
  878. CV_WRAP void read( const String& fileName )
  879. {
  880. FileStorage fs(fileName, FileStorage::READ);
  881. read(fs.root());
  882. }
  883. // Reads matcher object from a file node
  884. // see corresponding cv::Algorithm method
  885. CV_WRAP virtual void read( const FileNode& ) CV_OVERRIDE;
  886. // Writes matcher object to a file storage
  887. virtual void write( FileStorage& ) const CV_OVERRIDE;
  888. /** @brief Clones the matcher.
  889. @param emptyTrainData If emptyTrainData is false, the method creates a deep copy of the object,
  890. that is, copies both parameters and train data. If emptyTrainData is true, the method creates an
  891. object copy with the current parameters but with empty train data.
  892. */
  893. CV_WRAP CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
  894. /** @brief Creates a descriptor matcher of a given type with the default parameters (using default
  895. constructor).
  896. @param descriptorMatcherType Descriptor matcher type. Now the following matcher types are
  897. supported:
  898. - `BruteForce` (it uses L2 )
  899. - `BruteForce-L1`
  900. - `BruteForce-Hamming`
  901. - `BruteForce-Hamming(2)`
  902. - `FlannBased`
  903. */
  904. CV_WRAP static Ptr<DescriptorMatcher> create( const String& descriptorMatcherType );
  905. CV_WRAP static Ptr<DescriptorMatcher> create( int matcherType );
  906. // see corresponding cv::Algorithm method
  907. CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); }
  908. #if CV_VERSION_MAJOR < 5
  909. inline void write(const Ptr<FileStorage>& fs, const String& name) const { CV_Assert(fs); Algorithm::write(*fs, name); }
  910. #endif
  911. protected:
  912. /**
  913. * Class to work with descriptors from several images as with one merged matrix.
  914. * It is used e.g. in FlannBasedMatcher.
  915. */
  916. class CV_EXPORTS DescriptorCollection
  917. {
  918. public:
  919. DescriptorCollection();
  920. DescriptorCollection( const DescriptorCollection& collection );
  921. virtual ~DescriptorCollection();
  922. // Vector of matrices "descriptors" will be merged to one matrix "mergedDescriptors" here.
  923. void set( const std::vector<Mat>& descriptors );
  924. virtual void clear();
  925. const Mat& getDescriptors() const;
  926. const Mat getDescriptor( int imgIdx, int localDescIdx ) const;
  927. const Mat getDescriptor( int globalDescIdx ) const;
  928. void getLocalIdx( int globalDescIdx, int& imgIdx, int& localDescIdx ) const;
  929. int size() const;
  930. protected:
  931. Mat mergedDescriptors;
  932. std::vector<int> startIdxs;
  933. };
  934. //! In fact the matching is implemented only by the following two methods. These methods suppose
  935. //! that the class object has been trained already. Public match methods call these methods
  936. //! after calling train().
  937. virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
  938. InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
  939. virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
  940. InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
  941. static bool isPossibleMatch( InputArray mask, int queryIdx, int trainIdx );
  942. static bool isMaskedOut( InputArrayOfArrays masks, int queryIdx );
  943. CV_NODISCARD_STD static Mat clone_op( Mat m ) { return m.clone(); }
  944. void checkMasks( InputArrayOfArrays masks, int queryDescriptorsCount ) const;
  945. //! Collection of descriptors from train images.
  946. std::vector<Mat> trainDescCollection;
  947. std::vector<UMat> utrainDescCollection;
  948. };
  949. /** @brief Brute-force descriptor matcher.
  950. For each descriptor in the first set, this matcher finds the closest descriptor in the second set
  951. by trying each one. This descriptor matcher supports masking permissible matches of descriptor
  952. sets.
  953. */
  954. class CV_EXPORTS_W BFMatcher : public DescriptorMatcher
  955. {
  956. public:
  957. /** @brief Brute-force matcher constructor (obsolete). Please use BFMatcher.create()
  958. *
  959. *
  960. */
  961. CV_WRAP BFMatcher( int normType=NORM_L2, bool crossCheck=false );
  962. virtual ~BFMatcher() {}
  963. virtual bool isMaskSupported() const CV_OVERRIDE { return true; }
  964. /** @brief Brute-force matcher create method.
  965. @param normType One of NORM_L1, NORM_L2, NORM_HAMMING, NORM_HAMMING2. L1 and L2 norms are
  966. preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be used with ORB, BRISK and
  967. BRIEF, NORM_HAMMING2 should be used with ORB when WTA_K==3 or 4 (see ORB::ORB constructor
  968. description).
  969. @param crossCheck If it is false, this is will be default BFMatcher behaviour when it finds the k
  970. nearest neighbors for each query descriptor. If crossCheck==true, then the knnMatch() method with
  971. k=1 will only return pairs (i,j) such that for i-th query descriptor the j-th descriptor in the
  972. matcher's collection is the nearest and vice versa, i.e. the BFMatcher will only return consistent
  973. pairs. Such technique usually produces best results with minimal number of outliers when there are
  974. enough matches. This is alternative to the ratio test, used by D. Lowe in SIFT paper.
  975. */
  976. CV_WRAP static Ptr<BFMatcher> create( int normType=NORM_L2, bool crossCheck=false ) ;
  977. CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const CV_OVERRIDE;
  978. protected:
  979. virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
  980. InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
  981. virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
  982. InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
  983. int normType;
  984. bool crossCheck;
  985. };
  986. #if defined(HAVE_OPENCV_FLANN) || defined(CV_DOXYGEN)
  987. /** @brief Flann-based descriptor matcher.
  988. This matcher trains cv::flann::Index on a train descriptor collection and calls its nearest search
  989. methods to find the best matches. So, this matcher may be faster when matching a large train
  990. collection than the brute force matcher. FlannBasedMatcher does not support masking permissible
  991. matches of descriptor sets because flann::Index does not support this. :
  992. */
  993. class CV_EXPORTS_W FlannBasedMatcher : public DescriptorMatcher
  994. {
  995. public:
  996. CV_WRAP FlannBasedMatcher( const Ptr<flann::IndexParams>& indexParams=makePtr<flann::KDTreeIndexParams>(),
  997. const Ptr<flann::SearchParams>& searchParams=makePtr<flann::SearchParams>() );
  998. virtual void add( InputArrayOfArrays descriptors ) CV_OVERRIDE;
  999. virtual void clear() CV_OVERRIDE;
  1000. // Reads matcher object from a file node
  1001. virtual void read( const FileNode& ) CV_OVERRIDE;
  1002. // Writes matcher object to a file storage
  1003. virtual void write( FileStorage& ) const CV_OVERRIDE;
  1004. virtual void train() CV_OVERRIDE;
  1005. virtual bool isMaskSupported() const CV_OVERRIDE;
  1006. CV_WRAP static Ptr<FlannBasedMatcher> create();
  1007. CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const CV_OVERRIDE;
  1008. protected:
  1009. static void convertToDMatches( const DescriptorCollection& descriptors,
  1010. const Mat& indices, const Mat& distances,
  1011. std::vector<std::vector<DMatch> >& matches );
  1012. virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
  1013. InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
  1014. virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
  1015. InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
  1016. Ptr<flann::IndexParams> indexParams;
  1017. Ptr<flann::SearchParams> searchParams;
  1018. Ptr<flann::Index> flannIndex;
  1019. DescriptorCollection mergedDescriptors;
  1020. int addedDescCount;
  1021. };
  1022. #endif
  1023. //! @} features2d_match
  1024. /****************************************************************************************\
  1025. * Drawing functions *
  1026. \****************************************************************************************/
  1027. //! @addtogroup features2d_draw
  1028. //! @{
  1029. struct CV_EXPORTS DrawMatchesFlags
  1030. {
  1031. enum{ DEFAULT = 0, //!< Output image matrix will be created (Mat::create),
  1032. //!< i.e. existing memory of output image may be reused.
  1033. //!< Two source image, matches and single keypoints will be drawn.
  1034. //!< For each keypoint only the center point will be drawn (without
  1035. //!< the circle around keypoint with keypoint size and orientation).
  1036. DRAW_OVER_OUTIMG = 1, //!< Output image matrix will not be created (Mat::create).
  1037. //!< Matches will be drawn on existing content of output image.
  1038. NOT_DRAW_SINGLE_POINTS = 2, //!< Single keypoints will not be drawn.
  1039. DRAW_RICH_KEYPOINTS = 4 //!< For each keypoint the circle around keypoint with keypoint size and
  1040. //!< orientation will be drawn.
  1041. };
  1042. };
  1043. /** @brief Draws keypoints.
  1044. @param image Source image.
  1045. @param keypoints Keypoints from the source image.
  1046. @param outImage Output image. Its content depends on the flags value defining what is drawn in the
  1047. output image. See possible flags bit values below.
  1048. @param color Color of keypoints.
  1049. @param flags Flags setting drawing features. Possible flags bit values are defined by
  1050. DrawMatchesFlags. See details above in drawMatches .
  1051. @note
  1052. For Python API, flags are modified as cv2.DRAW_MATCHES_FLAGS_DEFAULT,
  1053. cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG,
  1054. cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS
  1055. */
  1056. CV_EXPORTS_W void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
  1057. const Scalar& color=Scalar::all(-1), int flags=DrawMatchesFlags::DEFAULT );
  1058. /** @brief Draws the found matches of keypoints from two images.
  1059. @param img1 First source image.
  1060. @param keypoints1 Keypoints from the first source image.
  1061. @param img2 Second source image.
  1062. @param keypoints2 Keypoints from the second source image.
  1063. @param matches1to2 Matches from the first image to the second one, which means that keypoints1[i]
  1064. has a corresponding point in keypoints2[matches[i]] .
  1065. @param outImg Output image. Its content depends on the flags value defining what is drawn in the
  1066. output image. See possible flags bit values below.
  1067. @param matchColor Color of matches (lines and connected keypoints). If matchColor==Scalar::all(-1)
  1068. , the color is generated randomly.
  1069. @param singlePointColor Color of single keypoints (circles), which means that keypoints do not
  1070. have the matches. If singlePointColor==Scalar::all(-1) , the color is generated randomly.
  1071. @param matchesMask Mask determining which matches are drawn. If the mask is empty, all matches are
  1072. drawn.
  1073. @param flags Flags setting drawing features. Possible flags bit values are defined by
  1074. DrawMatchesFlags.
  1075. This function draws matches of keypoints from two images in the output image. Match is a line
  1076. connecting two keypoints (circles). See cv::DrawMatchesFlags.
  1077. */
  1078. CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
  1079. InputArray img2, const std::vector<KeyPoint>& keypoints2,
  1080. const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
  1081. const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
  1082. const std::vector<char>& matchesMask=std::vector<char>(), int flags=DrawMatchesFlags::DEFAULT );
  1083. /** @overload */
  1084. CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
  1085. InputArray img2, const std::vector<KeyPoint>& keypoints2,
  1086. const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
  1087. const int matchesThickness, const Scalar& matchColor=Scalar::all(-1),
  1088. const Scalar& singlePointColor=Scalar::all(-1), const std::vector<char>& matchesMask=std::vector<char>(),
  1089. int flags=DrawMatchesFlags::DEFAULT );
  1090. CV_EXPORTS_AS(drawMatchesKnn) void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
  1091. InputArray img2, const std::vector<KeyPoint>& keypoints2,
  1092. const std::vector<std::vector<DMatch> >& matches1to2, InputOutputArray outImg,
  1093. const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
  1094. const std::vector<std::vector<char> >& matchesMask=std::vector<std::vector<char> >(), int flags=DrawMatchesFlags::DEFAULT );
  1095. //! @} features2d_draw
  1096. /****************************************************************************************\
  1097. * Functions to evaluate the feature detectors and [generic] descriptor extractors *
  1098. \****************************************************************************************/
  1099. CV_EXPORTS void evaluateFeatureDetector( const Mat& img1, const Mat& img2, const Mat& H1to2,
  1100. std::vector<KeyPoint>* keypoints1, std::vector<KeyPoint>* keypoints2,
  1101. float& repeatability, int& correspCount,
  1102. const Ptr<FeatureDetector>& fdetector=Ptr<FeatureDetector>() );
  1103. CV_EXPORTS void computeRecallPrecisionCurve( const std::vector<std::vector<DMatch> >& matches1to2,
  1104. const std::vector<std::vector<uchar> >& correctMatches1to2Mask,
  1105. std::vector<Point2f>& recallPrecisionCurve );
  1106. CV_EXPORTS float getRecall( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
  1107. CV_EXPORTS int getNearestPoint( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
  1108. /****************************************************************************************\
  1109. * Bag of visual words *
  1110. \****************************************************************************************/
  1111. //! @addtogroup features2d_category
  1112. //! @{
  1113. /** @brief Abstract base class for training the *bag of visual words* vocabulary from a set of descriptors.
  1114. For details, see, for example, *Visual Categorization with Bags of Keypoints* by Gabriella Csurka,
  1115. Christopher R. Dance, Lixin Fan, Jutta Willamowski, Cedric Bray, 2004. :
  1116. */
  1117. class CV_EXPORTS_W BOWTrainer
  1118. {
  1119. public:
  1120. BOWTrainer();
  1121. virtual ~BOWTrainer();
  1122. /** @brief Adds descriptors to a training set.
  1123. @param descriptors Descriptors to add to a training set. Each row of the descriptors matrix is a
  1124. descriptor.
  1125. The training set is clustered using clustermethod to construct the vocabulary.
  1126. */
  1127. CV_WRAP void add( const Mat& descriptors );
  1128. /** @brief Returns a training set of descriptors.
  1129. */
  1130. CV_WRAP const std::vector<Mat>& getDescriptors() const;
  1131. /** @brief Returns the count of all descriptors stored in the training set.
  1132. */
  1133. CV_WRAP int descriptorsCount() const;
  1134. CV_WRAP virtual void clear();
  1135. /** @overload */
  1136. CV_WRAP virtual Mat cluster() const = 0;
  1137. /** @brief Clusters train descriptors.
  1138. @param descriptors Descriptors to cluster. Each row of the descriptors matrix is a descriptor.
  1139. Descriptors are not added to the inner train descriptor set.
  1140. The vocabulary consists of cluster centers. So, this method returns the vocabulary. In the first
  1141. variant of the method, train descriptors stored in the object are clustered. In the second variant,
  1142. input descriptors are clustered.
  1143. */
  1144. CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;
  1145. protected:
  1146. std::vector<Mat> descriptors;
  1147. int size;
  1148. };
  1149. /** @brief kmeans -based class to train visual vocabulary using the *bag of visual words* approach. :
  1150. */
  1151. class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
  1152. {
  1153. public:
  1154. /** @brief The constructor.
  1155. @see cv::kmeans
  1156. */
  1157. CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
  1158. int attempts=3, int flags=KMEANS_PP_CENTERS );
  1159. virtual ~BOWKMeansTrainer();
  1160. // Returns trained vocabulary (i.e. cluster centers).
  1161. CV_WRAP virtual Mat cluster() const CV_OVERRIDE;
  1162. CV_WRAP virtual Mat cluster( const Mat& descriptors ) const CV_OVERRIDE;
  1163. protected:
  1164. int clusterCount;
  1165. TermCriteria termcrit;
  1166. int attempts;
  1167. int flags;
  1168. };
  1169. /** @brief Class to compute an image descriptor using the *bag of visual words*.
  1170. Such a computation consists of the following steps:
  1171. 1. Compute descriptors for a given image and its keypoints set.
  1172. 2. Find the nearest visual words from the vocabulary for each keypoint descriptor.
  1173. 3. Compute the bag-of-words image descriptor as is a normalized histogram of vocabulary words
  1174. encountered in the image. The i-th bin of the histogram is a frequency of i-th word of the
  1175. vocabulary in the given image.
  1176. */
  1177. class CV_EXPORTS_W BOWImgDescriptorExtractor
  1178. {
  1179. public:
  1180. /** @brief The constructor.
  1181. @param dextractor Descriptor extractor that is used to compute descriptors for an input image and
  1182. its keypoints.
  1183. @param dmatcher Descriptor matcher that is used to find the nearest word of the trained vocabulary
  1184. for each keypoint descriptor of the image.
  1185. */
  1186. CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor,
  1187. const Ptr<DescriptorMatcher>& dmatcher );
  1188. /** @overload */
  1189. BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher>& dmatcher );
  1190. virtual ~BOWImgDescriptorExtractor();
  1191. /** @brief Sets a visual vocabulary.
  1192. @param vocabulary Vocabulary (can be trained using the inheritor of BOWTrainer ). Each row of the
  1193. vocabulary is a visual word (cluster center).
  1194. */
  1195. CV_WRAP void setVocabulary( const Mat& vocabulary );
  1196. /** @brief Returns the set vocabulary.
  1197. */
  1198. CV_WRAP const Mat& getVocabulary() const;
  1199. /** @brief Computes an image descriptor using the set visual vocabulary.
  1200. @param image Image, for which the descriptor is computed.
  1201. @param keypoints Keypoints detected in the input image.
  1202. @param imgDescriptor Computed output image descriptor.
  1203. @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
  1204. pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
  1205. returned if it is non-zero.
  1206. @param descriptors Descriptors of the image keypoints that are returned if they are non-zero.
  1207. */
  1208. void compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
  1209. std::vector<std::vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
  1210. /** @overload
  1211. @param keypointDescriptors Computed descriptors to match with vocabulary.
  1212. @param imgDescriptor Computed output image descriptor.
  1213. @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
  1214. pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
  1215. returned if it is non-zero.
  1216. */
  1217. void compute( InputArray keypointDescriptors, OutputArray imgDescriptor,
  1218. std::vector<std::vector<int> >* pointIdxsOfClusters=0 );
  1219. // compute() is not constant because DescriptorMatcher::match is not constant
  1220. CV_WRAP_AS(compute) void compute2( const Mat& image, std::vector<KeyPoint>& keypoints, CV_OUT Mat& imgDescriptor )
  1221. { compute(image,keypoints,imgDescriptor); }
  1222. /** @brief Returns an image descriptor size if the vocabulary is set. Otherwise, it returns 0.
  1223. */
  1224. CV_WRAP int descriptorSize() const;
  1225. /** @brief Returns an image descriptor type.
  1226. */
  1227. CV_WRAP int descriptorType() const;
  1228. protected:
  1229. Mat vocabulary;
  1230. Ptr<DescriptorExtractor> dextractor;
  1231. Ptr<DescriptorMatcher> dmatcher;
  1232. };
  1233. //! @} features2d_category
  1234. //! @} features2d
  1235. } /* namespace cv */
  1236. #endif