features2d.hpp 71 KB

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