descriptor.hpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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) 2014, Biagio Montesano, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef __OPENCV_DESCRIPTOR_HPP__
  42. #define __OPENCV_DESCRIPTOR_HPP__
  43. #include <map>
  44. #include <vector>
  45. #include <list>
  46. #if defined _MSC_VER && _MSC_VER <= 1700
  47. #include <stdint.h>
  48. #else
  49. #include <inttypes.h>
  50. #endif
  51. #include <stdio.h>
  52. #include <iostream>
  53. #include "opencv2/core/utility.hpp"
  54. #include <opencv2/imgproc.hpp>
  55. #include "opencv2/core.hpp"
  56. /* define data types */
  57. typedef uint64_t UINT64;
  58. typedef uint32_t UINT32;
  59. typedef uint16_t UINT16;
  60. typedef uint8_t UINT8;
  61. /* define constants */
  62. #define UINT64_1 ((UINT64)0x01)
  63. #define UINT32_1 ((UINT32)0x01)
  64. namespace cv
  65. {
  66. namespace line_descriptor
  67. {
  68. //! @addtogroup line_descriptor
  69. //! @{
  70. /** @brief A class to represent a line
  71. As aformentioned, it is been necessary to design a class that fully stores the information needed to
  72. characterize completely a line and plot it on image it was extracted from, when required.
  73. *KeyLine* class has been created for such goal; it is mainly inspired to Feature2d's KeyPoint class,
  74. since KeyLine shares some of *KeyPoint*'s fields, even if a part of them assumes a different
  75. meaning, when speaking about lines. In particular:
  76. - the *class_id* field is used to gather lines extracted from different octaves which refer to
  77. same line inside original image (such lines and the one they represent in original image share
  78. the same *class_id* value)
  79. - the *angle* field represents line's slope with respect to (positive) X axis
  80. - the *pt* field represents line's midpoint
  81. - the *response* field is computed as the ratio between the line's length and maximum between
  82. image's width and height
  83. - the *size* field is the area of the smallest rectangle containing line
  84. Apart from fields inspired to KeyPoint class, KeyLines stores information about extremes of line in
  85. original image and in octave it was extracted from, about line's length and number of pixels it
  86. covers.
  87. */
  88. struct CV_EXPORTS_W_SIMPLE KeyLine
  89. {
  90. public:
  91. /** orientation of the line */
  92. CV_PROP_RW float angle;
  93. /** object ID, that can be used to cluster keylines by the line they represent */
  94. CV_PROP_RW int class_id;
  95. /** octave (pyramid layer), from which the keyline has been extracted */
  96. CV_PROP_RW int octave;
  97. /** coordinates of the middlepoint */
  98. CV_PROP_RW Point2f pt;
  99. /** the response, by which the strongest keylines have been selected.
  100. It's represented by the ratio between line's length and maximum between
  101. image's width and height */
  102. CV_PROP_RW float response;
  103. /** minimum area containing line */
  104. CV_PROP_RW float size;
  105. /** lines's extremes in original image */
  106. CV_PROP_RW float startPointX;
  107. CV_PROP_RW float startPointY;
  108. CV_PROP_RW float endPointX;
  109. CV_PROP_RW float endPointY;
  110. /** line's extremes in image it was extracted from */
  111. CV_PROP_RW float sPointInOctaveX;
  112. CV_PROP_RW float sPointInOctaveY;
  113. CV_PROP_RW float ePointInOctaveX;
  114. CV_PROP_RW float ePointInOctaveY;
  115. /** the length of line */
  116. CV_PROP_RW float lineLength;
  117. /** number of pixels covered by the line */
  118. CV_PROP_RW int numOfPixels;
  119. /** Returns the start point of the line in the original image */
  120. CV_WRAP Point2f getStartPoint() const
  121. {
  122. return Point2f(startPointX, startPointY);
  123. }
  124. /** Returns the end point of the line in the original image */
  125. CV_WRAP Point2f getEndPoint() const
  126. {
  127. return Point2f(endPointX, endPointY);
  128. }
  129. /** Returns the start point of the line in the octave it was extracted from */
  130. CV_WRAP Point2f getStartPointInOctave() const
  131. {
  132. return Point2f(sPointInOctaveX, sPointInOctaveY);
  133. }
  134. /** Returns the end point of the line in the octave it was extracted from */
  135. CV_WRAP Point2f getEndPointInOctave() const
  136. {
  137. return Point2f(ePointInOctaveX, ePointInOctaveY);
  138. }
  139. /** constructor */
  140. CV_WRAP KeyLine()
  141. {
  142. }
  143. };
  144. /** @brief Class implements both functionalities for detection of lines and computation of their
  145. binary descriptor.
  146. Class' interface is mainly based on the ones of classical detectors and extractors, such as
  147. Feature2d's @ref features2d_main and @ref features2d_match. Retrieved information about lines is
  148. stored in line_descriptor::KeyLine objects.
  149. */
  150. class CV_EXPORTS_W BinaryDescriptor : public Algorithm
  151. {
  152. public:
  153. /** @brief List of BinaryDescriptor parameters:
  154. */
  155. struct CV_EXPORTS Params
  156. {
  157. /*CV_WRAP*/
  158. Params();
  159. /** the number of image octaves (default = 1) */
  160. int numOfOctave_;
  161. /** the width of band; (default: 7) */
  162. int widthOfBand_;
  163. /** image's reduction ratio in construction of Gaussian pyramids */
  164. int reductionRatio;
  165. int ksize_;
  166. /** read parameters from a FileNode object and store them (struct function) */
  167. void read( const FileNode& fn );
  168. /** store parameters to a FileStorage object (struct function) */
  169. void write( FileStorage& fs ) const;
  170. };
  171. /** @brief Constructor
  172. @param parameters configuration parameters BinaryDescriptor::Params
  173. If no argument is provided, constructor sets default values (see comments in the code snippet in
  174. previous section). Default values are strongly recommended.
  175. */
  176. BinaryDescriptor( const BinaryDescriptor::Params &parameters = BinaryDescriptor::Params() );
  177. /** @brief Create a BinaryDescriptor object with default parameters (or with the ones provided)
  178. and return a smart pointer to it
  179. */
  180. CV_WRAP static Ptr<BinaryDescriptor> createBinaryDescriptor();
  181. static Ptr<BinaryDescriptor> createBinaryDescriptor( Params parameters );
  182. /** destructor */
  183. ~BinaryDescriptor();
  184. /** @brief Get current number of octaves
  185. */
  186. CV_WRAP int getNumOfOctaves();
  187. /** @brief Set number of octaves
  188. @param octaves number of octaves
  189. */
  190. CV_WRAP void setNumOfOctaves( int octaves );
  191. /** @brief Get current width of bands
  192. */
  193. CV_WRAP int getWidthOfBand();
  194. /** @brief Set width of bands
  195. @param width width of bands
  196. */
  197. CV_WRAP void setWidthOfBand( int width );
  198. /** @brief Get current reduction ratio (used in Gaussian pyramids)
  199. */
  200. CV_WRAP int getReductionRatio();
  201. /** @brief Set reduction ratio (used in Gaussian pyramids)
  202. @param rRatio reduction ratio
  203. */
  204. CV_WRAP void setReductionRatio( int rRatio );
  205. /** @brief Read parameters from a FileNode object and store them
  206. @param fn source FileNode file
  207. */
  208. virtual void read( const cv::FileNode& fn ) CV_OVERRIDE;
  209. /** @brief Store parameters to a FileStorage object
  210. @param fs output FileStorage file
  211. */
  212. virtual void write( cv::FileStorage& fs ) const CV_OVERRIDE;
  213. /** @brief Requires line detection
  214. @param image input image
  215. @param keypoints vector that will store extracted lines for one or more images
  216. @param mask mask matrix to detect only KeyLines of interest
  217. */
  218. CV_WRAP void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, const Mat& mask = Mat() );
  219. /** @overload
  220. @param images input images
  221. @param keylines set of vectors that will store extracted lines for one or more images
  222. @param masks vector of mask matrices to detect only KeyLines of interest from each input image
  223. */
  224. void detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, const std::vector<Mat>& masks =
  225. std::vector<Mat>() ) const;
  226. /** @brief Requires descriptors computation
  227. @param image input image
  228. @param keylines vector containing lines for which descriptors must be computed
  229. @param descriptors
  230. @param returnFloatDescr flag (when set to true, original non-binary descriptors are returned)
  231. */
  232. CV_WRAP void compute( const Mat& image, CV_OUT CV_IN_OUT std::vector<KeyLine>& keylines, CV_OUT Mat& descriptors, bool returnFloatDescr = false ) const;
  233. /** @overload
  234. @param images input images
  235. @param keylines set of vectors containing lines for which descriptors must be computed
  236. @param descriptors
  237. @param returnFloatDescr flag (when set to true, original non-binary descriptors are returned)
  238. */
  239. void compute( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, std::vector<Mat>& descriptors, bool returnFloatDescr =
  240. false ) const;
  241. /** @brief Return descriptor size
  242. */
  243. int descriptorSize() const;
  244. /** @brief Return data type
  245. */
  246. int descriptorType() const;
  247. /** returns norm mode */
  248. /*CV_WRAP*/
  249. int defaultNorm() const;
  250. /** @brief Define operator '()' to perform detection of KeyLines and computation of descriptors in a row.
  251. @param image input image
  252. @param mask mask matrix to select which lines in KeyLines must be accepted among the ones
  253. extracted (used when *keylines* is not empty)
  254. @param keylines vector that contains input lines (when filled, the detection part will be skipped
  255. and input lines will be passed as input to the algorithm computing descriptors)
  256. @param descriptors matrix that will store final descriptors
  257. @param useProvidedKeyLines flag (when set to true, detection phase will be skipped and only
  258. computation of descriptors will be executed, using lines provided in *keylines*)
  259. @param returnFloatDescr flag (when set to true, original non-binary descriptors are returned)
  260. */
  261. virtual void operator()( InputArray image, InputArray mask, CV_OUT std::vector<KeyLine>& keylines, OutputArray descriptors,
  262. bool useProvidedKeyLines = false, bool returnFloatDescr = false ) const;
  263. protected:
  264. /** implementation of line detection */
  265. virtual void detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, const Mat& mask = Mat() ) const;
  266. /** implementation of descriptors' computation */
  267. virtual void computeImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, Mat& descriptors, bool returnFloatDescr,
  268. bool useDetectionData ) const;
  269. private:
  270. /** struct to represent lines extracted from an octave */
  271. struct OctaveLine
  272. {
  273. unsigned int octaveCount; //the octave which this line is detected
  274. unsigned int lineIDInOctave; //the line ID in that octave image
  275. unsigned int lineIDInScaleLineVec; //the line ID in Scale line vector
  276. float lineLength; //the length of line in original image scale
  277. };
  278. // A 2D line (normal equation parameters).
  279. struct SingleLine
  280. {
  281. //note: rho and theta are based on coordinate origin, i.e. the top-left corner of image
  282. double rho; //unit: pixel length
  283. double theta; //unit: rad
  284. double linePointX; // = rho * cos(theta);
  285. double linePointY; // = rho * sin(theta);
  286. //for EndPoints, the coordinate origin is the top-left corner of image.
  287. double startPointX;
  288. double startPointY;
  289. double endPointX;
  290. double endPointY;
  291. //direction of a line, the angle between positive line direction (dark side is in the left) and positive X axis.
  292. double direction;
  293. //mean gradient magnitude
  294. double gradientMagnitude;
  295. //mean gray value of pixels in dark side of line
  296. double darkSideGrayValue;
  297. //mean gray value of pixels in light side of line
  298. double lightSideGrayValue;
  299. //the length of line
  300. double lineLength;
  301. //the width of line;
  302. double width;
  303. //number of pixels
  304. int numOfPixels;
  305. //the decriptor of line
  306. std::vector<double> descriptor;
  307. };
  308. // Specifies a vector of lines.
  309. typedef std::vector<SingleLine> Lines_list;
  310. struct OctaveSingleLine
  311. {
  312. /*endPoints, the coordinate origin is the top-left corner of the original image.
  313. *startPointX = sPointInOctaveX * (factor)^octaveCount; */
  314. float startPointX;
  315. float startPointY;
  316. float endPointX;
  317. float endPointY;
  318. //endPoints, the coordinate origin is the top-left corner of the octave image.
  319. float sPointInOctaveX;
  320. float sPointInOctaveY;
  321. float ePointInOctaveX;
  322. float ePointInOctaveY;
  323. //direction of a line, the angle between positive line direction (dark side is in the left) and positive X axis.
  324. float direction;
  325. //the summation of gradient magnitudes of pixels on lines
  326. float salience;
  327. //the length of line
  328. float lineLength;
  329. //number of pixels
  330. unsigned int numOfPixels;
  331. //the octave which this line is detected
  332. unsigned int octaveCount;
  333. //the decriptor of line
  334. std::vector<float> descriptor;
  335. OctaveSingleLine() : startPointX(0), startPointY(0), endPointX(0), endPointY(0),
  336. sPointInOctaveX(0), sPointInOctaveY(0), ePointInOctaveX(0), ePointInOctaveY(0),
  337. direction(0), salience(0), lineLength(0), numOfPixels(0), octaveCount(0),
  338. descriptor(std::vector<float>())
  339. {}
  340. };
  341. struct Pixel
  342. {
  343. unsigned int x; //X coordinate
  344. unsigned int y; //Y coordinate
  345. };
  346. struct EdgeChains
  347. {
  348. std::vector<unsigned int> xCors; //all the x coordinates of edge points
  349. std::vector<unsigned int> yCors; //all the y coordinates of edge points
  350. std::vector<unsigned int> sId; //the start index of each edge in the coordinate arrays
  351. unsigned int numOfEdges; //the number of edges whose length are larger than minLineLen; numOfEdges < sId.size;
  352. };
  353. struct LineChains
  354. {
  355. std::vector<unsigned int> xCors; //all the x coordinates of line points
  356. std::vector<unsigned int> yCors; //all the y coordinates of line points
  357. std::vector<unsigned int> sId; //the start index of each line in the coordinate arrays
  358. unsigned int numOfLines; //the number of lines whose length are larger than minLineLen; numOfLines < sId.size;
  359. };
  360. typedef std::list<Pixel> PixelChain; //each edge is a pixel chain
  361. struct CV_EXPORTS_W_SIMPLE EDLineParam
  362. {
  363. CV_PROP_RW int ksize;
  364. CV_PROP_RW float sigma;
  365. CV_PROP_RW float gradientThreshold;
  366. CV_PROP_RW float anchorThreshold;
  367. CV_PROP_RW int scanIntervals;
  368. CV_PROP_RW int minLineLen;
  369. CV_PROP_RW double lineFitErrThreshold;
  370. };
  371. #define RELATIVE_ERROR_FACTOR 100.0
  372. #define MLN10 2.30258509299404568402
  373. #define log_gamma(x) ((x)>15.0?log_gamma_windschitl(x):log_gamma_lanczos(x))
  374. /** This class is used to detect lines from input image.
  375. * First, edges are extracted from input image following the method presented in Cihan Topal and
  376. * Cuneyt Akinlar's paper:"Edge Drawing: A Heuristic Approach to Robust Real-Time Edge Detection", 2010.
  377. * Then, lines are extracted from the edge image following the method presented in Cuneyt Akinlar and
  378. * Cihan Topal's paper:"EDLines: A real-time line segment detector with a false detection control", 2011
  379. * PS: The linking step of edge detection has a little bit difference with the Edge drawing algorithm
  380. * described in the paper. The edge chain doesn't stop when the pixel direction is changed.
  381. */
  382. class CV_EXPORTS_W EDLineDetector
  383. {
  384. public:
  385. CV_WRAP EDLineDetector();
  386. CV_WRAP_AS(EDLineDetectorWithParams) EDLineDetector( EDLineParam param );
  387. ~EDLineDetector();
  388. /** @brief Creates an EDLineDetector object, using smart pointers.
  389. */
  390. CV_WRAP static Ptr<EDLineDetector> createEDLineDetector();
  391. CV_WRAP_AS(createEDLineDetectorWithParams) static Ptr<EDLineDetector> createEDLineDetector(EDLineParam params);
  392. /*extract edges from image
  393. *image: In, gray image;
  394. *edges: Out, store the edges, each edge is a pixel chain
  395. *return -1: error happen
  396. */
  397. int EdgeDrawing( cv::Mat &image, EdgeChains &edgeChains );
  398. /*extract lines from image
  399. *image: In, gray image;
  400. *lines: Out, store the extracted lines,
  401. *return -1: error happen
  402. */
  403. int EDline( cv::Mat &image, LineChains &lines );
  404. /** extract line from image, and store them */
  405. CV_WRAP int EDline( cv::Mat &image );
  406. cv::Mat dxImg_; //store the dxImg;
  407. cv::Mat dyImg_; //store the dyImg;
  408. cv::Mat gImgWO_; //store the gradient image without threshold;
  409. LineChains lines_; //store the detected line chains;
  410. //store the line Equation coefficients, vec3=[w1,w2,w3] for line w1*x + w2*y + w3=0;
  411. std::vector<std::vector<double> > lineEquations_;
  412. //store the line endpoints, [x1,y1,x2,y3]
  413. std::vector<std::vector<float> > lineEndpoints_;
  414. //store the line direction
  415. std::vector<float> lineDirection_;
  416. //store the line salience, which is the summation of gradients of pixels on line
  417. std::vector<float> lineSalience_;
  418. // image sizes
  419. unsigned int imageWidth;
  420. unsigned int imageHeight;
  421. /*The threshold of line fit error;
  422. *If lineFitErr is large than this threshold, then
  423. *the pixel chain is not accepted as a single line segment.*/
  424. double lineFitErrThreshold_;
  425. /*the threshold of pixel gradient magnitude.
  426. *Only those pixel whose gradient magnitude are larger than this threshold will be
  427. *taken as possible edge points. Default value is 36*/
  428. short gradienThreshold_;
  429. /*If the pixel's gradient value is bigger than both of its neighbors by a
  430. *certain threshold (ANCHOR_THRESHOLD), the pixel is marked to be an anchor.
  431. *Default value is 8*/
  432. unsigned char anchorThreshold_;
  433. /*anchor testing can be performed at different scan intervals, i.e.,
  434. *every row/column, every second row/column etc.
  435. *Default value is 2*/
  436. unsigned int scanIntervals_;
  437. int minLineLen_; //minimal acceptable line length
  438. private:
  439. void InitEDLine_();
  440. /*For an input edge chain, find the best fit line, the default chain length is minLineLen_
  441. *xCors: In, pointer to the X coordinates of pixel chain;
  442. *yCors: In, pointer to the Y coordinates of pixel chain;
  443. *offsetS:In, start index of this chain in vector;
  444. *lineEquation: Out, [a,b] which are the coefficient of lines y=ax+b(horizontal) or x=ay+b(vertical);
  445. *return: line fit error; -1:error happens;
  446. */
  447. double LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, std::vector<double> &lineEquation );
  448. /*For an input pixel chain, find the best fit line. Only do the update based on new points.
  449. *For A*x=v, Least square estimation of x = Inv(A^T * A) * (A^T * v);
  450. *If some new observations are added, i.e, [A; A'] * x = [v; v'],
  451. *then x' = Inv(A^T * A + (A')^T * A') * (A^T * v + (A')^T * v');
  452. *xCors: In, pointer to the X coordinates of pixel chain;
  453. *yCors: In, pointer to the Y coordinates of pixel chain;
  454. *offsetS:In, start index of this chain in vector;
  455. *newOffsetS: In, start index of extended part;
  456. *offsetE:In, end index of this chain in vector;
  457. *lineEquation: Out, [a,b] which are the coefficient of lines y=ax+b(horizontal) or x=ay+b(vertical);
  458. *return: line fit error; -1:error happens;
  459. */
  460. double LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int newOffsetS, unsigned int offsetE,
  461. std::vector<double> &lineEquation );
  462. /** Validate line based on the Helmholtz principle, which basically states that
  463. * for a structure to be perceptually meaningful, the expectation of this structure
  464. * by chance must be very low.
  465. */
  466. bool LineValidation_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int offsetE, std::vector<double> &lineEquation,
  467. float &direction );
  468. bool bValidate_; //flag to decide whether line will be validated
  469. int ksize_; //the size of Gaussian kernel: ksize X ksize, default value is 5.
  470. float sigma_; //the sigma of Gaussian kernal, default value is 1.0.
  471. /*For example, there two edges in the image:
  472. *edge1 = [(7,4), (8,5), (9,6),| (10,7)|, (11, 8), (12,9)] and
  473. *edge2 = [(14,9), (15,10), (16,11), (17,12),| (18, 13)|, (19,14)] ; then we store them as following:
  474. *pFirstPartEdgeX_ = [10, 11, 12, 18, 19];//store the first part of each edge[from middle to end]
  475. *pFirstPartEdgeY_ = [7, 8, 9, 13, 14];
  476. *pFirstPartEdgeS_ = [0,3,5];// the index of start point of first part of each edge
  477. *pSecondPartEdgeX_ = [10, 9, 8, 7, 18, 17, 16, 15, 14];//store the second part of each edge[from middle to front]
  478. *pSecondPartEdgeY_ = [7, 6, 5, 4, 13, 12, 11, 10, 9];//anchor points(10, 7) and (18, 13) are stored again
  479. *pSecondPartEdgeS_ = [0, 4, 9];// the index of start point of second part of each edge
  480. *This type of storage order is because of the order of edge detection process.
  481. *For each edge, start from one anchor point, first go right, then go left or first go down, then go up*/
  482. //store the X coordinates of the first part of the pixels for chains
  483. unsigned int *pFirstPartEdgeX_;
  484. //store the Y coordinates of the first part of the pixels for chains
  485. unsigned int *pFirstPartEdgeY_;
  486. //store the start index of every edge chain in the first part arrays
  487. unsigned int *pFirstPartEdgeS_;
  488. //store the X coordinates of the second part of the pixels for chains
  489. unsigned int *pSecondPartEdgeX_;
  490. //store the Y coordinates of the second part of the pixels for chains
  491. unsigned int *pSecondPartEdgeY_;
  492. //store the start index of every edge chain in the second part arrays
  493. unsigned int *pSecondPartEdgeS_;
  494. //store the X coordinates of anchors
  495. unsigned int *pAnchorX_;
  496. //store the Y coordinates of anchors
  497. unsigned int *pAnchorY_;
  498. //edges
  499. cv::Mat edgeImage_;
  500. cv::Mat gImg_; //store the gradient image;
  501. cv::Mat dirImg_; //store the direction image
  502. double logNT_;
  503. cv::Mat_<float> ATA; //the previous matrix of A^T * A;
  504. cv::Mat_<float> ATV; //the previous vector of A^T * V;
  505. cv::Mat_<float> fitMatT; //the matrix used in line fit function;
  506. cv::Mat_<float> fitVec; //the vector used in line fit function;
  507. cv::Mat_<float> tempMatLineFit; //the matrix used in line fit function;
  508. cv::Mat_<float> tempVecLineFit; //the vector used in line fit function;
  509. /** Compare doubles by relative error.
  510. The resulting rounding error after floating point computations
  511. depend on the specific operations done. The same number computed by
  512. different algorithms could present different rounding errors. For a
  513. useful comparison, an estimation of the relative rounding error
  514. should be considered and compared to a factor times EPS. The factor
  515. should be related to the accumulated rounding error in the chain of
  516. computation. Here, as a simplification, a fixed factor is used.
  517. */
  518. static int double_equal( double a, double b )
  519. {
  520. double abs_diff, aa, bb, abs_max;
  521. /* trivial case */
  522. if( a == b )
  523. return true;
  524. abs_diff = fabs( a - b );
  525. aa = fabs( a );
  526. bb = fabs( b );
  527. abs_max = aa > bb ? aa : bb;
  528. /* DBL_MIN is the smallest normalized number, thus, the smallest
  529. number whose relative error is bounded by DBL_EPSILON. For
  530. smaller numbers, the same quantization steps as for DBL_MIN
  531. are used. Then, for smaller numbers, a meaningful "relative"
  532. error should be computed by dividing the difference by DBL_MIN. */
  533. if( abs_max < DBL_MIN )
  534. abs_max = DBL_MIN;
  535. /* equal if relative error <= factor x eps */
  536. return ( abs_diff / abs_max ) <= ( RELATIVE_ERROR_FACTOR * DBL_EPSILON );
  537. }
  538. /** Computes the natural logarithm of the absolute value of
  539. the gamma function of x using the Lanczos approximation.
  540. See http://www.rskey.org/gamma.htm
  541. The formula used is
  542. @f[
  543. \Gamma(x) = \frac{ \sum_{n=0}^{N} q_n x^n }{ \Pi_{n=0}^{N} (x+n) }
  544. (x+5.5)^{x+0.5} e^{-(x+5.5)}
  545. @f]
  546. so
  547. @f[
  548. \log\Gamma(x) = \log\left( \sum_{n=0}^{N} q_n x^n \right)
  549. + (x+0.5) \log(x+5.5) - (x+5.5) - \sum_{n=0}^{N} \log(x+n)
  550. @f]
  551. and
  552. q0 = 75122.6331530,
  553. q1 = 80916.6278952,
  554. q2 = 36308.2951477,
  555. q3 = 8687.24529705,
  556. q4 = 1168.92649479,
  557. q5 = 83.8676043424,
  558. q6 = 2.50662827511.
  559. */
  560. static double log_gamma_lanczos( double x )
  561. {
  562. static double q[7] =
  563. { 75122.6331530, 80916.6278952, 36308.2951477, 8687.24529705, 1168.92649479, 83.8676043424, 2.50662827511 };
  564. double a = ( x + 0.5 ) * log( x + 5.5 ) - ( x + 5.5 );
  565. double b = 0.0;
  566. int n;
  567. for ( n = 0; n < 7; n++ )
  568. {
  569. a -= log( x + (double) n );
  570. b += q[n] * pow( x, (double) n );
  571. }
  572. return a + log( b );
  573. }
  574. /** Computes the natural logarithm of the absolute value of
  575. the gamma function of x using Windschitl method.
  576. See http://www.rskey.org/gamma.htm
  577. The formula used is
  578. @f[
  579. \Gamma(x) = \sqrt{\frac{2\pi}{x}} \left( \frac{x}{e}
  580. \sqrt{ x\sinh(1/x) + \frac{1}{810x^6} } \right)^x
  581. @f]
  582. so
  583. @f[
  584. \log\Gamma(x) = 0.5\log(2\pi) + (x-0.5)\log(x) - x
  585. + 0.5x\log\left( x\sinh(1/x) + \frac{1}{810x^6} \right).
  586. @f]
  587. This formula is a good approximation when x > 15.
  588. */
  589. static double log_gamma_windschitl( double x )
  590. {
  591. return 0.918938533204673 + ( x - 0.5 ) * log( x ) - x + 0.5 * x * log( x * sinh( 1 / x ) + 1 / ( 810.0 * pow( x, 6.0 ) ) );
  592. }
  593. /** Computes -log10(NFA).
  594. NFA stands for Number of False Alarms:
  595. @f[
  596. \mathrm{NFA} = NT \cdot B(n,k,p)
  597. @f]
  598. - NT - number of tests
  599. - B(n,k,p) - tail of binomial distribution with parameters n,k and p:
  600. @f[
  601. B(n,k,p) = \sum_{j=k}^n
  602. \left(\begin{array}{c}n\\j\end{array}\right)
  603. p^{j} (1-p)^{n-j}
  604. @f]
  605. The value -log10(NFA) is equivalent but more intuitive than NFA:
  606. - -1 corresponds to 10 mean false alarms
  607. - 0 corresponds to 1 mean false alarm
  608. - 1 corresponds to 0.1 mean false alarms
  609. - 2 corresponds to 0.01 mean false alarms
  610. - ...
  611. Used this way, the bigger the value, better the detection,
  612. and a logarithmic scale is used.
  613. @param n,k,p binomial parameters.
  614. @param logNT logarithm of Number of Tests
  615. The computation is based in the gamma function by the following
  616. relation:
  617. @f[
  618. \left(\begin{array}{c}n\\k\end{array}\right)
  619. = \frac{ \Gamma(n+1) }{ \Gamma(k+1) \cdot \Gamma(n-k+1) }.
  620. @f]
  621. We use efficient algorithms to compute the logarithm of
  622. the gamma function.
  623. To make the computation faster, not all the sum is computed, part
  624. of the terms are neglected based on a bound to the error obtained
  625. (an error of 10% in the result is accepted).
  626. */
  627. static double nfa( int n, int k, double p, double logNT )
  628. {
  629. double tolerance = 0.1; /* an error of 10% in the result is accepted */
  630. double log1term, term, bin_term, mult_term, bin_tail, err, p_term;
  631. int i;
  632. /* check parameters */
  633. if( n < 0 || k < 0 || k > n || p <= 0.0 || p >= 1.0 )
  634. CV_Error(Error::StsBadArg, "nfa: wrong n, k or p values.\n");
  635. /* trivial cases */
  636. if( n == 0 || k == 0 )
  637. return -logNT;
  638. if( n == k )
  639. return -logNT - (double) n * log10( p );
  640. /* probability term */
  641. p_term = p / ( 1.0 - p );
  642. /* compute the first term of the series */
  643. /*
  644. binomial_tail(n,k,p) = sum_{i=k}^n bincoef(n,i) * p^i * (1-p)^{n-i}
  645. where bincoef(n,i) are the binomial coefficients.
  646. But
  647. bincoef(n,k) = gamma(n+1) / ( gamma(k+1) * gamma(n-k+1) ).
  648. We use this to compute the first term. Actually the log of it.
  649. */
  650. log1term = log_gamma( (double) n + 1.0 )- log_gamma( (double ) k + 1.0 )- log_gamma( (double ) ( n - k ) + 1.0 )
  651. + (double) k * log( p )
  652. + (double) ( n - k ) * log( 1.0 - p );
  653. term = exp( log1term );
  654. /* in some cases no more computations are needed */
  655. if( double_equal( term, 0.0 ) )
  656. { /* the first term is almost zero */
  657. if( (double) k > (double) n * p ) /* at begin or end of the tail? */
  658. return -log1term / MLN10 - logNT; /* end: use just the first term */
  659. else
  660. return -logNT; /* begin: the tail is roughly 1 */
  661. }
  662. /* compute more terms if needed */
  663. bin_tail = term;
  664. for ( i = k + 1; i <= n; i++ )
  665. {
  666. /* As
  667. term_i = bincoef(n,i) * p^i * (1-p)^(n-i)
  668. and
  669. bincoef(n,i)/bincoef(n,i-1) = n-i+1 / i,
  670. then,
  671. term_i / term_i-1 = (n-i+1)/i * p/(1-p)
  672. and
  673. term_i = term_i-1 * (n-i+1)/i * p/(1-p).
  674. p/(1-p) is computed only once and stored in 'p_term'.
  675. */
  676. bin_term = (double) ( n - i + 1 ) / (double) i;
  677. mult_term = bin_term * p_term;
  678. term *= mult_term;
  679. bin_tail += term;
  680. if( bin_term < 1.0 )
  681. {
  682. /* When bin_term<1 then mult_term_j<mult_term_i for j>i.
  683. Then, the error on the binomial tail when truncated at
  684. the i term can be bounded by a geometric series of form
  685. term_i * sum mult_term_i^j. */
  686. err = term * ( ( 1.0 - pow( mult_term, (double) ( n - i + 1 ) ) ) / ( 1.0 - mult_term ) - 1.0 );
  687. /* One wants an error at most of tolerance*final_result, or:
  688. tolerance * abs(-log10(bin_tail)-logNT).
  689. Now, the error that can be accepted on bin_tail is
  690. given by tolerance*final_result divided by the derivative
  691. of -log10(x) when x=bin_tail. that is:
  692. tolerance * abs(-log10(bin_tail)-logNT) / (1/bin_tail)
  693. Finally, we truncate the tail if the error is less than:
  694. tolerance * abs(-log10(bin_tail)-logNT) * bin_tail */
  695. if( err < tolerance * fabs( -log10( bin_tail ) - logNT ) * bin_tail )
  696. break;
  697. }
  698. }
  699. return -log10( bin_tail ) - logNT;
  700. }
  701. };
  702. // Specifies a vector of lines.
  703. typedef std::vector<OctaveSingleLine> LinesVec;
  704. // each element in ScaleLines is a vector of lines
  705. // which corresponds the same line detected in different octave images.
  706. typedef std::vector<LinesVec> ScaleLines;
  707. /* compute Gaussian pyramids */
  708. void computeGaussianPyramid( const Mat& image, const int numOctaves );
  709. /* compute Sobel's derivatives */
  710. void computeSobel( const Mat& image, const int numOctaves );
  711. /* conversion of an LBD descriptor to its binary representation */
  712. unsigned char binaryConversion( float* f1, float* f2 );
  713. /* compute LBD descriptors using EDLine extractor */
  714. int computeLBD( ScaleLines &keyLines, bool useDetectionData = false );
  715. /* gathers lines in groups using EDLine extractor.
  716. Each group contains the same line, detected in different octaves */
  717. int OctaveKeyLines( cv::Mat& image, ScaleLines &keyLines );
  718. /* the local gaussian coefficient applied to the orthogonal line direction within each band */
  719. std::vector<double> gaussCoefL_;
  720. /* the global gaussian coefficient applied to each row within line support region */
  721. std::vector<double> gaussCoefG_;
  722. /* descriptor parameters */
  723. Params params;
  724. /* vector of sizes of downsampled and blurred images */
  725. std::vector<cv::Size> images_sizes;
  726. /*For each octave of image, we define an EDLineDetector, because we can get gradient images (dxImg, dyImg, gImg)
  727. *from the EDLineDetector class without extra computation cost. Another reason is that, if we use
  728. *a single EDLineDetector to detect lines in different octave of images, then we need to allocate and release
  729. *memory for gradient images (dxImg, dyImg, gImg) repeatedly for their varying size*/
  730. std::vector<Ptr<EDLineDetector> > edLineVec_;
  731. /* Sobel's derivatives */
  732. std::vector<cv::Mat> dxImg_vector, dyImg_vector;
  733. /* Gaussian pyramid */
  734. std::vector<cv::Mat> octaveImages;
  735. };
  736. /**
  737. Lines extraction methodology
  738. ----------------------------
  739. The lines extraction methodology described in the following is mainly based on @cite EDL . The
  740. extraction starts with a Gaussian pyramid generated from an original image, downsampled N-1 times,
  741. blurred N times, to obtain N layers (one for each octave), with layer 0 corresponding to input
  742. image. Then, from each layer (octave) in the pyramid, lines are extracted using LSD algorithm.
  743. Differently from EDLine lines extractor used in original article, LSD furnishes information only
  744. about lines extremes; thus, additional information regarding slope and equation of line are computed
  745. via analytic methods. The number of pixels is obtained using *LineIterator*. Extracted lines are
  746. returned in the form of KeyLine objects, but since extraction is based on a method different from
  747. the one used in *BinaryDescriptor* class, data associated to a line's extremes in original image and
  748. in octave it was extracted from, coincide. KeyLine's field *class_id* is used as an index to
  749. indicate the order of extraction of a line inside a single octave.
  750. */
  751. struct CV_EXPORTS_W_SIMPLE LSDParam
  752. {
  753. CV_PROP_RW double scale ;
  754. CV_PROP_RW double sigma_scale;
  755. CV_PROP_RW double quant;
  756. CV_PROP_RW double ang_th;
  757. CV_PROP_RW double log_eps;
  758. CV_PROP_RW double density_th ;
  759. CV_PROP_RW int n_bins ;
  760. CV_WRAP LSDParam():scale(0.8),
  761. sigma_scale(0.6),
  762. quant(2.0),
  763. ang_th(22.5),
  764. log_eps(0),
  765. density_th(0.7),
  766. n_bins(1024){}
  767. };
  768. class CV_EXPORTS_W LSDDetector : public Algorithm
  769. {
  770. public:
  771. /* constructor */
  772. CV_WRAP LSDDetector() : params()
  773. {
  774. }
  775. ;
  776. CV_WRAP_AS(LSDDetectorWithParams) LSDDetector(LSDParam _params) : params(_params)
  777. {
  778. }
  779. ;
  780. /** @brief Creates ad LSDDetector object, using smart pointers.
  781. */
  782. CV_WRAP static Ptr<LSDDetector> createLSDDetector();
  783. CV_WRAP_AS(createLSDDetectorWithParams) static Ptr<LSDDetector> createLSDDetector(LSDParam params);
  784. /** @brief Detect lines inside an image.
  785. @param image input image
  786. @param keypoints vector that will store extracted lines for one or more images
  787. @param scale scale factor used in pyramids generation
  788. @param numOctaves number of octaves inside pyramid
  789. @param mask mask matrix to detect only KeyLines of interest
  790. */
  791. CV_WRAP void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, int scale, int numOctaves, const Mat& mask = Mat() );
  792. /** @overload
  793. @param images input images
  794. @param keylines set of vectors that will store extracted lines for one or more images
  795. @param scale scale factor used in pyramids generation
  796. @param numOctaves number of octaves inside pyramid
  797. @param masks vector of mask matrices to detect only KeyLines of interest from each input image
  798. */
  799. CV_WRAP void detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, int scale, int numOctaves,
  800. const std::vector<Mat>& masks = std::vector<Mat>() ) const;
  801. private:
  802. /* compute Gaussian pyramid of input image */
  803. void computeGaussianPyramid( const Mat& image, int numOctaves, int scale );
  804. /* implementation of line detection */
  805. void detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, int numOctaves, int scale, const Mat& mask ) const;
  806. /* matrices for Gaussian pyramids */
  807. std::vector<cv::Mat> gaussianPyrs;
  808. /* parameters */
  809. LSDParam params;
  810. };
  811. /** @brief furnishes all functionalities for querying a dataset provided by user or internal to
  812. class (that user must, anyway, populate) on the model of @ref features2d_match
  813. Once descriptors have been extracted from an image (both they represent lines and points), it
  814. becomes interesting to be able to match a descriptor with another one extracted from a different
  815. image and representing the same line or point, seen from a differente perspective or on a different
  816. scale. In reaching such goal, the main headache is designing an efficient search algorithm to
  817. associate a query descriptor to one extracted from a dataset. In the following, a matching modality
  818. based on *Multi-Index Hashing (MiHashing)* will be described.
  819. Multi-Index Hashing
  820. -------------------
  821. The theory described in this section is based on @cite MIH . Given a dataset populated with binary
  822. codes, each code is indexed *m* times into *m* different hash tables, according to *m* substrings it
  823. has been divided into. Thus, given a query code, all the entries close to it at least in one
  824. substring are returned by search as *neighbor candidates*. Returned entries are then checked for
  825. validity by verifying that their full codes are not distant (in Hamming space) more than *r* bits
  826. from query code. In details, each binary code **h** composed of *b* bits is divided into *m*
  827. disjoint substrings \f$\mathbf{h}^{(1)}, ..., \mathbf{h}^{(m)}\f$, each with length
  828. \f$\lfloor b/m \rfloor\f$ or \f$\lceil b/m \rceil\f$ bits. Formally, when two codes **h** and **g** differ
  829. by at the most *r* bits, in at the least one of their *m* substrings they differ by at the most
  830. \f$\lfloor r/m \rfloor\f$ bits. In particular, when \f$||\mathbf{h}-\mathbf{g}||_H \le r\f$ (where \f$||.||_H\f$
  831. is the Hamming norm), there must exist a substring *k* (with \f$1 \le k \le m\f$) such that
  832. \f[||\mathbf{h}^{(k)} - \mathbf{g}^{(k)}||_H \le \left\lfloor \frac{r}{m} \right\rfloor .\f]
  833. That means that if Hamming distance between each of the *m* substring is strictly greater than
  834. \f$\lfloor r/m \rfloor\f$, then \f$||\mathbf{h}-\mathbf{g}||_H\f$ must be larger that *r* and that is a
  835. contradiction. If the codes in dataset are divided into *m* substrings, then *m* tables will be
  836. built. Given a query **q** with substrings \f$\{\mathbf{q}^{(i)}\}^m_{i=1}\f$, *i*-th hash table is
  837. searched for entries distant at the most \f$\lfloor r/m \rfloor\f$ from \f$\mathbf{q}^{(i)}\f$ and a set of
  838. candidates \f$\mathcal{N}_i(\mathbf{q})\f$ is obtained. The union of sets
  839. \f$\mathcal{N}(\mathbf{q}) = \bigcup_i \mathcal{N}_i(\mathbf{q})\f$ is a superset of the *r*-neighbors
  840. of **q**. Then, last step of algorithm is computing the Hamming distance between **q** and each
  841. element in \f$\mathcal{N}(\mathbf{q})\f$, deleting the codes that are distant more that *r* from **q**.
  842. */
  843. class CV_EXPORTS_W BinaryDescriptorMatcher : public Algorithm
  844. {
  845. public:
  846. /** @brief For every input query descriptor, retrieve the best matching one from a dataset provided from user
  847. or from the one internal to class
  848. @param queryDescriptors query descriptors
  849. @param trainDescriptors dataset of descriptors furnished by user
  850. @param matches vector to host retrieved matches
  851. @param mask mask to select which input descriptors must be matched to one in dataset
  852. */
  853. CV_WRAP void match( const Mat& queryDescriptors, const Mat& trainDescriptors, CV_OUT std::vector<DMatch>& matches, const Mat& mask = Mat() ) const;
  854. /** @overload
  855. @param queryDescriptors query descriptors
  856. @param matches vector to host retrieved matches
  857. @param masks vector of masks to select which input descriptors must be matched to one in dataset
  858. (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in
  859. dataset relative to *i*-th image)
  860. */
  861. CV_WRAP_AS(matchQuery) void match( const Mat& queryDescriptors, CV_OUT std::vector<DMatch>& matches, const std::vector<Mat>& masks = std::vector<Mat>() );
  862. /** @brief For every input query descriptor, retrieve the best *k* matching ones from a dataset provided from
  863. user or from the one internal to class
  864. @param queryDescriptors query descriptors
  865. @param trainDescriptors dataset of descriptors furnished by user
  866. @param matches vector to host retrieved matches
  867. @param k number of the closest descriptors to be returned for every input query
  868. @param mask mask to select which input descriptors must be matched to ones in dataset
  869. @param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
  870. matches for a given query is not inserted in final result)
  871. */
  872. CV_WRAP void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, int k, const Mat& mask = Mat(),
  873. bool compactResult = false ) const;
  874. /** @overload
  875. @param queryDescriptors query descriptors
  876. @param matches vector to host retrieved matches
  877. @param k number of the closest descriptors to be returned for every input query
  878. @param masks vector of masks to select which input descriptors must be matched to ones in dataset
  879. (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in
  880. dataset relative to *i*-th image)
  881. @param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
  882. matches for a given query is not inserted in final result)
  883. */
  884. CV_WRAP_AS(knnMatchQuery) void knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const std::vector<Mat>& masks = std::vector<Mat>(),
  885. bool compactResult = false );
  886. /** @brief For every input query descriptor, retrieve, from a dataset provided from user or from the one
  887. internal to class, all the descriptors that are not further than *maxDist* from input query
  888. @param queryDescriptors query descriptors
  889. @param trainDescriptors dataset of descriptors furnished by user
  890. @param matches vector to host retrieved matches
  891. @param maxDistance search radius
  892. @param mask mask to select which input descriptors must be matched to ones in dataset
  893. @param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
  894. matches for a given query is not inserted in final result)
  895. */
  896. void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
  897. const Mat& mask = Mat(), bool compactResult = false ) const;
  898. /** @overload
  899. @param queryDescriptors query descriptors
  900. @param matches vector to host retrieved matches
  901. @param maxDistance search radius
  902. @param masks vector of masks to select which input descriptors must be matched to ones in dataset
  903. (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in
  904. dataset relative to *i*-th image)
  905. @param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
  906. matches for a given query is not inserted in final result)
  907. */
  908. void radiusMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance, const std::vector<Mat>& masks =
  909. std::vector<Mat>(),
  910. bool compactResult = false );
  911. /** @brief Store locally new descriptors to be inserted in dataset, without updating dataset.
  912. @param descriptors matrices containing descriptors to be inserted into dataset
  913. @note Each matrix *i* in **descriptors** should contain descriptors relative to lines extracted from
  914. *i*-th image.
  915. */
  916. void add( const std::vector<Mat>& descriptors );
  917. /** @brief Update dataset by inserting into it all descriptors that were stored locally by *add* function.
  918. @note Every time this function is invoked, current dataset is deleted and locally stored descriptors
  919. are inserted into dataset. The locally stored copy of just inserted descriptors is then removed.
  920. */
  921. void train();
  922. /** @brief Create a BinaryDescriptorMatcher object and return a smart pointer to it.
  923. */
  924. static Ptr<BinaryDescriptorMatcher> createBinaryDescriptorMatcher();
  925. /** @brief Clear dataset and internal data
  926. */
  927. void clear() CV_OVERRIDE;
  928. /** @brief Constructor.
  929. The BinaryDescriptorMatcher constructed is able to store and manage 256-bits long entries.
  930. */
  931. CV_WRAP BinaryDescriptorMatcher();
  932. /** destructor */
  933. ~BinaryDescriptorMatcher()
  934. {
  935. }
  936. private:
  937. class BucketGroup
  938. {
  939. public:
  940. /** constructor */
  941. BucketGroup(bool needAllocateGroup = true);
  942. /** destructor */
  943. ~BucketGroup();
  944. /** insert data into the bucket */
  945. void insert( int subindex, UINT32 data );
  946. /** perform a query to the bucket */
  947. UINT32* query( int subindex, int *size );
  948. /** utility functions */
  949. void insert_value( std::vector<uint32_t>& vec, int index, UINT32 data );
  950. void push_value( std::vector<uint32_t>& vec, UINT32 Data );
  951. /** data fields */
  952. UINT32 empty;
  953. std::vector<uint32_t> group;
  954. };
  955. class SparseHashtable
  956. {
  957. private:
  958. /** Maximum bits per key before folding the table */
  959. static const int MAX_B;
  960. /** Bins (each bin is an Array object for duplicates of the same key) */
  961. std::vector<BucketGroup> table;
  962. public:
  963. /** constructor */
  964. SparseHashtable();
  965. /** destructor */
  966. ~SparseHashtable();
  967. /** initializer */
  968. int init( int _b );
  969. /** insert data */
  970. void insert( UINT64 index, UINT32 data );
  971. /** query data */
  972. UINT32* query( UINT64 index, int* size );
  973. /** Bits per index */
  974. int b;
  975. /** Number of bins */
  976. UINT64 size;
  977. };
  978. /** class defining a sequence of bits */
  979. class bitarray
  980. {
  981. public:
  982. /** pointer to bits sequence and sequence's length */
  983. UINT32 *arr;
  984. UINT32 length;
  985. /** constructor setting default values */
  986. bitarray()
  987. {
  988. arr = NULL;
  989. length = 0;
  990. }
  991. /** constructor setting sequence's length */
  992. bitarray( UINT64 _bits )
  993. {
  994. arr = NULL;
  995. init( _bits );
  996. }
  997. /** initializer of private fields */
  998. void init( UINT64 _bits )
  999. {
  1000. if( arr )
  1001. delete[] arr;
  1002. length = (UINT32) ceil( _bits / 32.00 );
  1003. arr = new UINT32[length];
  1004. erase();
  1005. }
  1006. /** destructor */
  1007. ~bitarray()
  1008. {
  1009. if( arr )
  1010. delete[] arr;
  1011. }
  1012. inline void flip( UINT64 index )
  1013. {
  1014. arr[index >> 5] ^= ( (UINT32) 0x01 ) << ( index % 32 );
  1015. }
  1016. inline void set( UINT64 index )
  1017. {
  1018. arr[index >> 5] |= ( (UINT32) 0x01 ) << ( index % 32 );
  1019. }
  1020. inline UINT8 get( UINT64 index )
  1021. {
  1022. return ( arr[index >> 5] & ( ( (UINT32) 0x01 ) << ( index % 32 ) ) ) != 0;
  1023. }
  1024. /** reserve menory for an UINT32 */
  1025. inline void erase()
  1026. {
  1027. memset( arr, 0, sizeof(UINT32) * length );
  1028. }
  1029. };
  1030. class Mihasher
  1031. {
  1032. public:
  1033. /** Bits per code */
  1034. int B;
  1035. /** B/8 */
  1036. int B_over_8;
  1037. /** Bits per chunk (must be less than 64) */
  1038. int b;
  1039. /** Number of chunks */
  1040. int m;
  1041. /** Number of chunks with b bits (have 1 bit more than others) */
  1042. int mplus;
  1043. /** Maximum hamming search radius (we use B/2 by default) */
  1044. int D;
  1045. /** Maximum hamming search radius per substring */
  1046. int d;
  1047. /** Maximum results to return */
  1048. int K;
  1049. /** Number of codes */
  1050. UINT64 N;
  1051. /** Table of original full-length codes */
  1052. cv::Mat codes;
  1053. /** Counter for eliminating duplicate results (it is not thread safe) */
  1054. Ptr<bitarray> counter;
  1055. /** Array of m hashtables */
  1056. std::vector<SparseHashtable> H;
  1057. /** Volume of a b-bit Hamming ball with radius s (for s = 0 to d) */
  1058. std::vector<UINT32> xornum;
  1059. /** Used within generation of binary codes at a certain Hamming distance */
  1060. int power[100];
  1061. /** constructor */
  1062. Mihasher();
  1063. /** desctructor */
  1064. ~Mihasher();
  1065. /** constructor 2 */
  1066. Mihasher( int B, int m );
  1067. /** K setter */
  1068. void setK( int K );
  1069. /** populate tables */
  1070. void populate( cv::Mat & codes, UINT32 N, int dim1codes );
  1071. /** execute a batch query */
  1072. void batchquery( UINT32 * results, UINT32 *numres/*, qstat *stats*/, const cv::Mat & q, UINT32 numq, int dim1queries );
  1073. private:
  1074. /** execute a single query */
  1075. void query( UINT32 * results, UINT32* numres/*, qstat *stats*/, UINT8 *q, UINT64 * chunks, UINT32 * res );
  1076. };
  1077. /** retrieve Hamming distances */
  1078. void checkKDistances( UINT32 * numres, int k, std::vector<int>& k_distances, int row, int string_length ) const;
  1079. /** matrix to store new descriptors */
  1080. Mat descriptorsMat;
  1081. /** map storing where each bunch of descriptors benins in DS */
  1082. std::map<int, int> indexesMap;
  1083. /** internal MiHaser representing dataset */
  1084. Ptr<Mihasher> dataset;
  1085. /** index from which next added descriptors' bunch must begin */
  1086. int nextAddedIndex;
  1087. /** number of images whose descriptors are stored in DS */
  1088. int numImages;
  1089. /** number of descriptors in dataset */
  1090. int descrInDS;
  1091. };
  1092. /* --------------------------------------------------------------------------------------------
  1093. UTILITY FUNCTIONS
  1094. -------------------------------------------------------------------------------------------- */
  1095. /** struct for drawing options */
  1096. struct CV_EXPORTS_W_SIMPLE DrawLinesMatchesFlags
  1097. {
  1098. CV_PROP_RW enum
  1099. {
  1100. DEFAULT = 0, //!< Output image matrix will be created (Mat::create),
  1101. //!< i.e. existing memory of output image may be reused.
  1102. //!< Two source images, matches, and single keylines
  1103. //!< will be drawn.
  1104. DRAW_OVER_OUTIMG = 1,//!< Output image matrix will not be
  1105. //!< created (using Mat::create). Matches will be drawn
  1106. //!< on existing content of output image.
  1107. NOT_DRAW_SINGLE_LINES = 2//!< Single keylines will not be drawn.
  1108. };
  1109. };
  1110. /** @brief Draws the found matches of keylines from two images.
  1111. @param img1 first image
  1112. @param keylines1 keylines extracted from first image
  1113. @param img2 second image
  1114. @param keylines2 keylines extracted from second image
  1115. @param matches1to2 vector of matches
  1116. @param outImg output matrix to draw on
  1117. @param matchColor drawing color for matches (chosen randomly in case of default value)
  1118. @param singleLineColor drawing color for keylines (chosen randomly in case of default value)
  1119. @param matchesMask mask to indicate which matches must be drawn
  1120. @param flags drawing flags, see DrawLinesMatchesFlags
  1121. @note If both *matchColor* and *singleLineColor* are set to their default values, function draws
  1122. matched lines and line connecting them with same color
  1123. */
  1124. CV_EXPORTS_W void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2,
  1125. const std::vector<DMatch>& matches1to2, CV_OUT Mat& outImg, const Scalar& matchColor = Scalar::all( -1 ),
  1126. const Scalar& singleLineColor = Scalar::all( -1 ), const std::vector<char>& matchesMask = std::vector<char>(),
  1127. int flags = DrawLinesMatchesFlags::DEFAULT );
  1128. /** @brief Draws keylines.
  1129. @param image input image
  1130. @param keylines keylines to be drawn
  1131. @param outImage output image to draw on
  1132. @param color color of lines to be drawn (if set to defaul value, color is chosen randomly)
  1133. @param flags drawing flags
  1134. */
  1135. CV_EXPORTS_W void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, CV_OUT Mat& outImage, const Scalar& color = Scalar::all( -1 ),
  1136. int flags = DrawLinesMatchesFlags::DEFAULT );
  1137. //! @}
  1138. }
  1139. }
  1140. #endif