descriptor.hpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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 KeyLine
  89. {
  90. public:
  91. /** orientation of the line */
  92. float angle;
  93. /** object ID, that can be used to cluster keylines by the line they represent */
  94. int class_id;
  95. /** octave (pyramid layer), from which the keyline has been extracted */
  96. int octave;
  97. /** coordinates of the middlepoint */
  98. 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. float response;
  103. /** minimum area containing line */
  104. float size;
  105. /** lines's extremes in original image */
  106. float startPointX;
  107. float startPointY;
  108. float endPointX;
  109. float endPointY;
  110. /** line's extremes in image it was extracted from */
  111. float sPointInOctaveX;
  112. float sPointInOctaveY;
  113. float ePointInOctaveX;
  114. float ePointInOctaveY;
  115. /** the length of line */
  116. float lineLength;
  117. /** number of pixels covered by the line */
  118. int numOfPixels;
  119. /** Returns the start point of the line in the original image */
  120. Point2f getStartPoint() const
  121. {
  122. return Point2f(startPointX, startPointY);
  123. }
  124. /** Returns the end point of the line in the original image */
  125. 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. 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. Point2f getEndPointInOctave() const
  136. {
  137. return Point2f(ePointInOctaveX, ePointInOctaveY);
  138. }
  139. /** constructor */
  140. 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 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 reccomended.
  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. static Ptr<BinaryDescriptor> createBinaryDescriptor();
  181. static Ptr<BinaryDescriptor> createBinaryDescriptor( Params parameters );
  182. /** destructor */
  183. ~BinaryDescriptor();
  184. /** @brief Get current number of octaves
  185. */
  186. int getNumOfOctaves();/*CV_WRAP*/
  187. /** @brief Set number of octaves
  188. @param octaves number of octaves
  189. */
  190. void setNumOfOctaves( int octaves );/*CV_WRAP*/
  191. /** @brief Get current width of bands
  192. */
  193. int getWidthOfBand();/*CV_WRAP*/
  194. /** @brief Set width of bands
  195. @param width width of bands
  196. */
  197. void setWidthOfBand( int width );/*CV_WRAP*/
  198. /** @brief Get current reduction ratio (used in Gaussian pyramids)
  199. */
  200. int getReductionRatio();/*CV_WRAP*/
  201. /** @brief Set reduction ratio (used in Gaussian pyramids)
  202. @param rRatio reduction ratio
  203. */
  204. 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 );
  209. /** @brief Store parameters to a FileStorage object
  210. @param fs output FileStorage file
  211. */
  212. virtual void write( cv::FileStorage& fs ) const;
  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. 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. 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 EDLineParam
  362. {
  363. int ksize;
  364. float sigma;
  365. float gradientThreshold;
  366. float anchorThreshold;
  367. int scanIntervals;
  368. int minLineLen;
  369. 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 EDLineDetector
  383. {
  384. public:
  385. EDLineDetector();
  386. EDLineDetector( EDLineParam param );
  387. ~EDLineDetector();
  388. /*extract edges from image
  389. *image: In, gray image;
  390. *edges: Out, store the edges, each edge is a pixel chain
  391. *return -1: error happen
  392. */
  393. int EdgeDrawing( cv::Mat &image, EdgeChains &edgeChains );
  394. /*extract lines from image
  395. *image: In, gray image;
  396. *lines: Out, store the extracted lines,
  397. *return -1: error happen
  398. */
  399. int EDline( cv::Mat &image, LineChains &lines );
  400. /** extract line from image, and store them */
  401. int EDline( cv::Mat &image );
  402. cv::Mat dxImg_; //store the dxImg;
  403. cv::Mat dyImg_; //store the dyImg;
  404. cv::Mat gImgWO_; //store the gradient image without threshold;
  405. LineChains lines_; //store the detected line chains;
  406. //store the line Equation coefficients, vec3=[w1,w2,w3] for line w1*x + w2*y + w3=0;
  407. std::vector<std::vector<double> > lineEquations_;
  408. //store the line endpoints, [x1,y1,x2,y3]
  409. std::vector<std::vector<float> > lineEndpoints_;
  410. //store the line direction
  411. std::vector<float> lineDirection_;
  412. //store the line salience, which is the summation of gradients of pixels on line
  413. std::vector<float> lineSalience_;
  414. // image sizes
  415. unsigned int imageWidth;
  416. unsigned int imageHeight;
  417. /*The threshold of line fit error;
  418. *If lineFitErr is large than this threshold, then
  419. *the pixel chain is not accepted as a single line segment.*/
  420. double lineFitErrThreshold_;
  421. /*the threshold of pixel gradient magnitude.
  422. *Only those pixel whose gradient magnitude are larger than this threshold will be
  423. *taken as possible edge points. Default value is 36*/
  424. short gradienThreshold_;
  425. /*If the pixel's gradient value is bigger than both of its neighbors by a
  426. *certain threshold (ANCHOR_THRESHOLD), the pixel is marked to be an anchor.
  427. *Default value is 8*/
  428. unsigned char anchorThreshold_;
  429. /*anchor testing can be performed at different scan intervals, i.e.,
  430. *every row/column, every second row/column etc.
  431. *Default value is 2*/
  432. unsigned int scanIntervals_;
  433. int minLineLen_; //minimal acceptable line length
  434. private:
  435. void InitEDLine_();
  436. /*For an input edge chain, find the best fit line, the default chain length is minLineLen_
  437. *xCors: In, pointer to the X coordinates of pixel chain;
  438. *yCors: In, pointer to the Y coordinates of pixel chain;
  439. *offsetS:In, start index of this chain in vector;
  440. *lineEquation: Out, [a,b] which are the coefficient of lines y=ax+b(horizontal) or x=ay+b(vertical);
  441. *return: line fit error; -1:error happens;
  442. */
  443. double LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, std::vector<double> &lineEquation );
  444. /*For an input pixel chain, find the best fit line. Only do the update based on new points.
  445. *For A*x=v, Least square estimation of x = Inv(A^T * A) * (A^T * v);
  446. *If some new observations are added, i.e, [A; A'] * x = [v; v'],
  447. *then x' = Inv(A^T * A + (A')^T * A') * (A^T * v + (A')^T * v');
  448. *xCors: In, pointer to the X coordinates of pixel chain;
  449. *yCors: In, pointer to the Y coordinates of pixel chain;
  450. *offsetS:In, start index of this chain in vector;
  451. *newOffsetS: In, start index of extended part;
  452. *offsetE:In, end index of this chain in vector;
  453. *lineEquation: Out, [a,b] which are the coefficient of lines y=ax+b(horizontal) or x=ay+b(vertical);
  454. *return: line fit error; -1:error happens;
  455. */
  456. double LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int newOffsetS, unsigned int offsetE,
  457. std::vector<double> &lineEquation );
  458. /** Validate line based on the Helmholtz principle, which basically states that
  459. * for a structure to be perceptually meaningful, the expectation of this structure
  460. * by chance must be very low.
  461. */
  462. bool LineValidation_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int offsetE, std::vector<double> &lineEquation,
  463. float &direction );
  464. bool bValidate_; //flag to decide whether line will be validated
  465. int ksize_; //the size of Gaussian kernel: ksize X ksize, default value is 5.
  466. float sigma_; //the sigma of Gaussian kernal, default value is 1.0.
  467. /*For example, there two edges in the image:
  468. *edge1 = [(7,4), (8,5), (9,6),| (10,7)|, (11, 8), (12,9)] and
  469. *edge2 = [(14,9), (15,10), (16,11), (17,12),| (18, 13)|, (19,14)] ; then we store them as following:
  470. *pFirstPartEdgeX_ = [10, 11, 12, 18, 19];//store the first part of each edge[from middle to end]
  471. *pFirstPartEdgeY_ = [7, 8, 9, 13, 14];
  472. *pFirstPartEdgeS_ = [0,3,5];// the index of start point of first part of each edge
  473. *pSecondPartEdgeX_ = [10, 9, 8, 7, 18, 17, 16, 15, 14];//store the second part of each edge[from middle to front]
  474. *pSecondPartEdgeY_ = [7, 6, 5, 4, 13, 12, 11, 10, 9];//anchor points(10, 7) and (18, 13) are stored again
  475. *pSecondPartEdgeS_ = [0, 4, 9];// the index of start point of second part of each edge
  476. *This type of storage order is because of the order of edge detection process.
  477. *For each edge, start from one anchor point, first go right, then go left or first go down, then go up*/
  478. //store the X coordinates of the first part of the pixels for chains
  479. unsigned int *pFirstPartEdgeX_;
  480. //store the Y coordinates of the first part of the pixels for chains
  481. unsigned int *pFirstPartEdgeY_;
  482. //store the start index of every edge chain in the first part arrays
  483. unsigned int *pFirstPartEdgeS_;
  484. //store the X coordinates of the second part of the pixels for chains
  485. unsigned int *pSecondPartEdgeX_;
  486. //store the Y coordinates of the second part of the pixels for chains
  487. unsigned int *pSecondPartEdgeY_;
  488. //store the start index of every edge chain in the second part arrays
  489. unsigned int *pSecondPartEdgeS_;
  490. //store the X coordinates of anchors
  491. unsigned int *pAnchorX_;
  492. //store the Y coordinates of anchors
  493. unsigned int *pAnchorY_;
  494. //edges
  495. cv::Mat edgeImage_;
  496. cv::Mat gImg_; //store the gradient image;
  497. cv::Mat dirImg_; //store the direction image
  498. double logNT_;
  499. cv::Mat_<float> ATA; //the previous matrix of A^T * A;
  500. cv::Mat_<float> ATV; //the previous vector of A^T * V;
  501. cv::Mat_<float> fitMatT; //the matrix used in line fit function;
  502. cv::Mat_<float> fitVec; //the vector used in line fit function;
  503. cv::Mat_<float> tempMatLineFit; //the matrix used in line fit function;
  504. cv::Mat_<float> tempVecLineFit; //the vector used in line fit function;
  505. /** Compare doubles by relative error.
  506. The resulting rounding error after floating point computations
  507. depend on the specific operations done. The same number computed by
  508. different algorithms could present different rounding errors. For a
  509. useful comparison, an estimation of the relative rounding error
  510. should be considered and compared to a factor times EPS. The factor
  511. should be related to the accumulated rounding error in the chain of
  512. computation. Here, as a simplification, a fixed factor is used.
  513. */
  514. static int double_equal( double a, double b )
  515. {
  516. double abs_diff, aa, bb, abs_max;
  517. /* trivial case */
  518. if( a == b )
  519. return true;
  520. abs_diff = fabs( a - b );
  521. aa = fabs( a );
  522. bb = fabs( b );
  523. abs_max = aa > bb ? aa : bb;
  524. /* DBL_MIN is the smallest normalized number, thus, the smallest
  525. number whose relative error is bounded by DBL_EPSILON. For
  526. smaller numbers, the same quantization steps as for DBL_MIN
  527. are used. Then, for smaller numbers, a meaningful "relative"
  528. error should be computed by dividing the difference by DBL_MIN. */
  529. if( abs_max < DBL_MIN )
  530. abs_max = DBL_MIN;
  531. /* equal if relative error <= factor x eps */
  532. return ( abs_diff / abs_max ) <= ( RELATIVE_ERROR_FACTOR * DBL_EPSILON );
  533. }
  534. /** Computes the natural logarithm of the absolute value of
  535. the gamma function of x using the Lanczos approximation.
  536. See http://www.rskey.org/gamma.htm
  537. The formula used is
  538. @f[
  539. \Gamma(x) = \frac{ \sum_{n=0}^{N} q_n x^n }{ \Pi_{n=0}^{N} (x+n) }
  540. (x+5.5)^{x+0.5} e^{-(x+5.5)}
  541. @f]
  542. so
  543. @f[
  544. \log\Gamma(x) = \log\left( \sum_{n=0}^{N} q_n x^n \right)
  545. + (x+0.5) \log(x+5.5) - (x+5.5) - \sum_{n=0}^{N} \log(x+n)
  546. @f]
  547. and
  548. q0 = 75122.6331530,
  549. q1 = 80916.6278952,
  550. q2 = 36308.2951477,
  551. q3 = 8687.24529705,
  552. q4 = 1168.92649479,
  553. q5 = 83.8676043424,
  554. q6 = 2.50662827511.
  555. */
  556. static double log_gamma_lanczos( double x )
  557. {
  558. static double q[7] =
  559. { 75122.6331530, 80916.6278952, 36308.2951477, 8687.24529705, 1168.92649479, 83.8676043424, 2.50662827511 };
  560. double a = ( x + 0.5 ) * log( x + 5.5 ) - ( x + 5.5 );
  561. double b = 0.0;
  562. int n;
  563. for ( n = 0; n < 7; n++ )
  564. {
  565. a -= log( x + (double) n );
  566. b += q[n] * pow( x, (double) n );
  567. }
  568. return a + log( b );
  569. }
  570. /** Computes the natural logarithm of the absolute value of
  571. the gamma function of x using Windschitl method.
  572. See http://www.rskey.org/gamma.htm
  573. The formula used is
  574. @f[
  575. \Gamma(x) = \sqrt{\frac{2\pi}{x}} \left( \frac{x}{e}
  576. \sqrt{ x\sinh(1/x) + \frac{1}{810x^6} } \right)^x
  577. @f]
  578. so
  579. @f[
  580. \log\Gamma(x) = 0.5\log(2\pi) + (x-0.5)\log(x) - x
  581. + 0.5x\log\left( x\sinh(1/x) + \frac{1}{810x^6} \right).
  582. @f]
  583. This formula is a good approximation when x > 15.
  584. */
  585. static double log_gamma_windschitl( double x )
  586. {
  587. return 0.918938533204673 + ( x - 0.5 ) * log( x ) - x + 0.5 * x * log( x * sinh( 1 / x ) + 1 / ( 810.0 * pow( x, 6.0 ) ) );
  588. }
  589. /** Computes -log10(NFA).
  590. NFA stands for Number of False Alarms:
  591. @f[
  592. \mathrm{NFA} = NT \cdot B(n,k,p)
  593. @f]
  594. - NT - number of tests
  595. - B(n,k,p) - tail of binomial distribution with parameters n,k and p:
  596. @f[
  597. B(n,k,p) = \sum_{j=k}^n
  598. \left(\begin{array}{c}n\\j\end{array}\right)
  599. p^{j} (1-p)^{n-j}
  600. @f]
  601. The value -log10(NFA) is equivalent but more intuitive than NFA:
  602. - -1 corresponds to 10 mean false alarms
  603. - 0 corresponds to 1 mean false alarm
  604. - 1 corresponds to 0.1 mean false alarms
  605. - 2 corresponds to 0.01 mean false alarms
  606. - ...
  607. Used this way, the bigger the value, better the detection,
  608. and a logarithmic scale is used.
  609. @param n,k,p binomial parameters.
  610. @param logNT logarithm of Number of Tests
  611. The computation is based in the gamma function by the following
  612. relation:
  613. @f[
  614. \left(\begin{array}{c}n\\k\end{array}\right)
  615. = \frac{ \Gamma(n+1) }{ \Gamma(k+1) \cdot \Gamma(n-k+1) }.
  616. @f]
  617. We use efficient algorithms to compute the logarithm of
  618. the gamma function.
  619. To make the computation faster, not all the sum is computed, part
  620. of the terms are neglected based on a bound to the error obtained
  621. (an error of 10% in the result is accepted).
  622. */
  623. static double nfa( int n, int k, double p, double logNT )
  624. {
  625. double tolerance = 0.1; /* an error of 10% in the result is accepted */
  626. double log1term, term, bin_term, mult_term, bin_tail, err, p_term;
  627. int i;
  628. /* check parameters */
  629. if( n < 0 || k < 0 || k > n || p <= 0.0 || p >= 1.0 )
  630. CV_Error(Error::StsBadArg, "nfa: wrong n, k or p values.\n");
  631. /* trivial cases */
  632. if( n == 0 || k == 0 )
  633. return -logNT;
  634. if( n == k )
  635. return -logNT - (double) n * log10( p );
  636. /* probability term */
  637. p_term = p / ( 1.0 - p );
  638. /* compute the first term of the series */
  639. /*
  640. binomial_tail(n,k,p) = sum_{i=k}^n bincoef(n,i) * p^i * (1-p)^{n-i}
  641. where bincoef(n,i) are the binomial coefficients.
  642. But
  643. bincoef(n,k) = gamma(n+1) / ( gamma(k+1) * gamma(n-k+1) ).
  644. We use this to compute the first term. Actually the log of it.
  645. */
  646. log1term = log_gamma( (double) n + 1.0 )- log_gamma( (double ) k + 1.0 )- log_gamma( (double ) ( n - k ) + 1.0 )
  647. + (double) k * log( p )
  648. + (double) ( n - k ) * log( 1.0 - p );
  649. term = exp( log1term );
  650. /* in some cases no more computations are needed */
  651. if( double_equal( term, 0.0 ) )
  652. { /* the first term is almost zero */
  653. if( (double) k > (double) n * p ) /* at begin or end of the tail? */
  654. return -log1term / MLN10 - logNT; /* end: use just the first term */
  655. else
  656. return -logNT; /* begin: the tail is roughly 1 */
  657. }
  658. /* compute more terms if needed */
  659. bin_tail = term;
  660. for ( i = k + 1; i <= n; i++ )
  661. {
  662. /* As
  663. term_i = bincoef(n,i) * p^i * (1-p)^(n-i)
  664. and
  665. bincoef(n,i)/bincoef(n,i-1) = n-i+1 / i,
  666. then,
  667. term_i / term_i-1 = (n-i+1)/i * p/(1-p)
  668. and
  669. term_i = term_i-1 * (n-i+1)/i * p/(1-p).
  670. p/(1-p) is computed only once and stored in 'p_term'.
  671. */
  672. bin_term = (double) ( n - i + 1 ) / (double) i;
  673. mult_term = bin_term * p_term;
  674. term *= mult_term;
  675. bin_tail += term;
  676. if( bin_term < 1.0 )
  677. {
  678. /* When bin_term<1 then mult_term_j<mult_term_i for j>i.
  679. Then, the error on the binomial tail when truncated at
  680. the i term can be bounded by a geometric series of form
  681. term_i * sum mult_term_i^j. */
  682. err = term * ( ( 1.0 - pow( mult_term, (double) ( n - i + 1 ) ) ) / ( 1.0 - mult_term ) - 1.0 );
  683. /* One wants an error at most of tolerance*final_result, or:
  684. tolerance * abs(-log10(bin_tail)-logNT).
  685. Now, the error that can be accepted on bin_tail is
  686. given by tolerance*final_result divided by the derivative
  687. of -log10(x) when x=bin_tail. that is:
  688. tolerance * abs(-log10(bin_tail)-logNT) / (1/bin_tail)
  689. Finally, we truncate the tail if the error is less than:
  690. tolerance * abs(-log10(bin_tail)-logNT) * bin_tail */
  691. if( err < tolerance * fabs( -log10( bin_tail ) - logNT ) * bin_tail )
  692. break;
  693. }
  694. }
  695. return -log10( bin_tail ) - logNT;
  696. }
  697. };
  698. // Specifies a vector of lines.
  699. typedef std::vector<OctaveSingleLine> LinesVec;
  700. // each element in ScaleLines is a vector of lines
  701. // which corresponds the same line detected in different octave images.
  702. typedef std::vector<LinesVec> ScaleLines;
  703. /* compute Gaussian pyramids */
  704. void computeGaussianPyramid( const Mat& image, const int numOctaves );
  705. /* compute Sobel's derivatives */
  706. void computeSobel( const Mat& image, const int numOctaves );
  707. /* conversion of an LBD descriptor to its binary representation */
  708. unsigned char binaryConversion( float* f1, float* f2 );
  709. /* compute LBD descriptors using EDLine extractor */
  710. int computeLBD( ScaleLines &keyLines, bool useDetectionData = false );
  711. /* gathers lines in groups using EDLine extractor.
  712. Each group contains the same line, detected in different octaves */
  713. int OctaveKeyLines( cv::Mat& image, ScaleLines &keyLines );
  714. /* the local gaussian coefficient applied to the orthogonal line direction within each band */
  715. std::vector<double> gaussCoefL_;
  716. /* the global gaussian coefficient applied to each row within line support region */
  717. std::vector<double> gaussCoefG_;
  718. /* descriptor parameters */
  719. Params params;
  720. /* vector of sizes of downsampled and blurred images */
  721. std::vector<cv::Size> images_sizes;
  722. /*For each octave of image, we define an EDLineDetector, because we can get gradient images (dxImg, dyImg, gImg)
  723. *from the EDLineDetector class without extra computation cost. Another reason is that, if we use
  724. *a single EDLineDetector to detect lines in different octave of images, then we need to allocate and release
  725. *memory for gradient images (dxImg, dyImg, gImg) repeatedly for their varying size*/
  726. std::vector<Ptr<EDLineDetector> > edLineVec_;
  727. /* Sobel's derivatives */
  728. std::vector<cv::Mat> dxImg_vector, dyImg_vector;
  729. /* Gaussian pyramid */
  730. std::vector<cv::Mat> octaveImages;
  731. };
  732. /**
  733. Lines extraction methodology
  734. ----------------------------
  735. The lines extraction methodology described in the following is mainly based on @cite EDL . The
  736. extraction starts with a Gaussian pyramid generated from an original image, downsampled N-1 times,
  737. blurred N times, to obtain N layers (one for each octave), with layer 0 corresponding to input
  738. image. Then, from each layer (octave) in the pyramid, lines are extracted using LSD algorithm.
  739. Differently from EDLine lines extractor used in original article, LSD furnishes information only
  740. about lines extremes; thus, additional information regarding slope and equation of line are computed
  741. via analytic methods. The number of pixels is obtained using *LineIterator*. Extracted lines are
  742. returned in the form of KeyLine objects, but since extraction is based on a method different from
  743. the one used in *BinaryDescriptor* class, data associated to a line's extremes in original image and
  744. in octave it was extracted from, coincide. KeyLine's field *class_id* is used as an index to
  745. indicate the order of extraction of a line inside a single octave.
  746. */
  747. class CV_EXPORTS LSDDetector : public Algorithm
  748. {
  749. public:
  750. /* constructor */
  751. /*CV_WRAP*/
  752. LSDDetector()
  753. {
  754. }
  755. ;
  756. /** @brief Creates ad LSDDetector object, using smart pointers.
  757. */
  758. static Ptr<LSDDetector> createLSDDetector();
  759. /** @brief Detect lines inside an image.
  760. @param image input image
  761. @param keypoints vector that will store extracted lines for one or more images
  762. @param scale scale factor used in pyramids generation
  763. @param numOctaves number of octaves inside pyramid
  764. @param mask mask matrix to detect only KeyLines of interest
  765. */
  766. void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, int scale, int numOctaves, const Mat& mask = Mat() );
  767. /** @overload
  768. @param images input images
  769. @param keylines set of vectors that will store extracted lines for one or more images
  770. @param scale scale factor used in pyramids generation
  771. @param numOctaves number of octaves inside pyramid
  772. @param masks vector of mask matrices to detect only KeyLines of interest from each input image
  773. */
  774. void detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, int scale, int numOctaves,
  775. const std::vector<Mat>& masks = std::vector<Mat>() ) const;
  776. private:
  777. /* compute Gaussian pyramid of input image */
  778. void computeGaussianPyramid( const Mat& image, int numOctaves, int scale );
  779. /* implementation of line detection */
  780. void detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, int numOctaves, int scale, const Mat& mask ) const;
  781. /* matrices for Gaussian pyramids */
  782. std::vector<cv::Mat> gaussianPyrs;
  783. };
  784. /** @brief furnishes all functionalities for querying a dataset provided by user or internal to
  785. class (that user must, anyway, populate) on the model of @ref features2d_match
  786. Once descriptors have been extracted from an image (both they represent lines and points), it
  787. becomes interesting to be able to match a descriptor with another one extracted from a different
  788. image and representing the same line or point, seen from a differente perspective or on a different
  789. scale. In reaching such goal, the main headache is designing an efficient search algorithm to
  790. associate a query descriptor to one extracted from a dataset. In the following, a matching modality
  791. based on *Multi-Index Hashing (MiHashing)* will be described.
  792. Multi-Index Hashing
  793. -------------------
  794. The theory described in this section is based on @cite MIH . Given a dataset populated with binary
  795. codes, each code is indexed *m* times into *m* different hash tables, according to *m* substrings it
  796. has been divided into. Thus, given a query code, all the entries close to it at least in one
  797. substring are returned by search as *neighbor candidates*. Returned entries are then checked for
  798. validity by verifying that their full codes are not distant (in Hamming space) more than *r* bits
  799. from query code. In details, each binary code **h** composed of *b* bits is divided into *m*
  800. disjoint substrings \f$\mathbf{h}^{(1)}, ..., \mathbf{h}^{(m)}\f$, each with length
  801. \f$\lfloor b/m \rfloor\f$ or \f$\lceil b/m \rceil\f$ bits. Formally, when two codes **h** and **g** differ
  802. by at the most *r* bits, in at the least one of their *m* substrings they differ by at the most
  803. \f$\lfloor r/m \rfloor\f$ bits. In particular, when \f$||\mathbf{h}-\mathbf{g}||_H \le r\f$ (where \f$||.||_H\f$
  804. is the Hamming norm), there must exist a substring *k* (with \f$1 \le k \le m\f$) such that
  805. \f[||\mathbf{h}^{(k)} - \mathbf{g}^{(k)}||_H \le \left\lfloor \frac{r}{m} \right\rfloor .\f]
  806. That means that if Hamming distance between each of the *m* substring is strictly greater than
  807. \f$\lfloor r/m \rfloor\f$, then \f$||\mathbf{h}-\mathbf{g}||_H\f$ must be larger that *r* and that is a
  808. contradiction. If the codes in dataset are divided into *m* substrings, then *m* tables will be
  809. built. Given a query **q** with substrings \f$\{\mathbf{q}^{(i)}\}^m_{i=1}\f$, *i*-th hash table is
  810. searched for entries distant at the most \f$\lfloor r/m \rfloor\f$ from \f$\mathbf{q}^{(i)}\f$ and a set of
  811. candidates \f$\mathcal{N}_i(\mathbf{q})\f$ is obtained. The union of sets
  812. \f$\mathcal{N}(\mathbf{q}) = \bigcup_i \mathcal{N}_i(\mathbf{q})\f$ is a superset of the *r*-neighbors
  813. of **q**. Then, last step of algorithm is computing the Hamming distance between **q** and each
  814. element in \f$\mathcal{N}(\mathbf{q})\f$, deleting the codes that are distant more that *r* from **q**.
  815. */
  816. class CV_EXPORTS BinaryDescriptorMatcher : public Algorithm
  817. {
  818. public:
  819. /** @brief For every input query descriptor, retrieve the best matching one from a dataset provided from user
  820. or from the one internal to class
  821. @param queryDescriptors query descriptors
  822. @param trainDescriptors dataset of descriptors furnished by user
  823. @param matches vector to host retrieved matches
  824. @param mask mask to select which input descriptors must be matched to one in dataset
  825. */
  826. void match( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<DMatch>& matches, const Mat& mask = Mat() ) const;
  827. /** @overload
  828. @param queryDescriptors query descriptors
  829. @param matches vector to host retrieved matches
  830. @param masks vector of masks to select which input descriptors must be matched to one in dataset
  831. (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in
  832. dataset relative to *i*-th image)
  833. */
  834. void match( const Mat& queryDescriptors, std::vector<DMatch>& matches, const std::vector<Mat>& masks = std::vector<Mat>() );
  835. /** @brief For every input query descriptor, retrieve the best *k* matching ones from a dataset provided from
  836. user or from the one internal to class
  837. @param queryDescriptors query descriptors
  838. @param trainDescriptors dataset of descriptors furnished by user
  839. @param matches vector to host retrieved matches
  840. @param k number of the closest descriptors to be returned for every input query
  841. @param mask mask to select which input descriptors must be matched to ones in dataset
  842. @param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
  843. matches for a given query is not inserted in final result)
  844. */
  845. void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const Mat& mask = Mat(),
  846. bool compactResult = false ) const;
  847. /** @overload
  848. @param queryDescriptors query descriptors
  849. @param matches vector to host retrieved matches
  850. @param k number of the closest descriptors to be returned for every input query
  851. @param masks vector of masks to select which input descriptors must be matched to ones in dataset
  852. (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in
  853. dataset relative to *i*-th image)
  854. @param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
  855. matches for a given query is not inserted in final result)
  856. */
  857. void knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const std::vector<Mat>& masks = std::vector<Mat>(),
  858. bool compactResult = false );
  859. /** @brief For every input query descriptor, retrieve, from a dataset provided from user or from the one
  860. internal to class, all the descriptors that are not further than *maxDist* from input query
  861. @param queryDescriptors query descriptors
  862. @param trainDescriptors dataset of descriptors furnished by user
  863. @param matches vector to host retrieved matches
  864. @param maxDistance search radius
  865. @param mask mask to select which input descriptors must be matched to ones in dataset
  866. @param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
  867. matches for a given query is not inserted in final result)
  868. */
  869. void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
  870. const Mat& mask = Mat(), bool compactResult = false ) const;
  871. /** @overload
  872. @param queryDescriptors query descriptors
  873. @param matches vector to host retrieved matches
  874. @param maxDistance search radius
  875. @param masks vector of masks to select which input descriptors must be matched to ones in dataset
  876. (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in
  877. dataset relative to *i*-th image)
  878. @param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
  879. matches for a given query is not inserted in final result)
  880. */
  881. void radiusMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance, const std::vector<Mat>& masks =
  882. std::vector<Mat>(),
  883. bool compactResult = false );
  884. /** @brief Store locally new descriptors to be inserted in dataset, without updating dataset.
  885. @param descriptors matrices containing descriptors to be inserted into dataset
  886. @note Each matrix *i* in **descriptors** should contain descriptors relative to lines extracted from
  887. *i*-th image.
  888. */
  889. void add( const std::vector<Mat>& descriptors );
  890. /** @brief Update dataset by inserting into it all descriptors that were stored locally by *add* function.
  891. @note Every time this function is invoked, current dataset is deleted and locally stored descriptors
  892. are inserted into dataset. The locally stored copy of just inserted descriptors is then removed.
  893. */
  894. void train();
  895. /** @brief Create a BinaryDescriptorMatcher object and return a smart pointer to it.
  896. */
  897. static Ptr<BinaryDescriptorMatcher> createBinaryDescriptorMatcher();
  898. /** @brief Clear dataset and internal data
  899. */
  900. void clear();
  901. /** @brief Constructor.
  902. The BinaryDescriptorMatcher constructed is able to store and manage 256-bits long entries.
  903. */
  904. BinaryDescriptorMatcher();
  905. /** destructor */
  906. ~BinaryDescriptorMatcher()
  907. {
  908. }
  909. private:
  910. class BucketGroup
  911. {
  912. public:
  913. /** constructor */
  914. BucketGroup(bool needAllocateGroup = true);
  915. /** destructor */
  916. ~BucketGroup();
  917. /** insert data into the bucket */
  918. void insert( int subindex, UINT32 data );
  919. /** perform a query to the bucket */
  920. UINT32* query( int subindex, int *size );
  921. /** utility functions */
  922. void insert_value( std::vector<uint32_t>& vec, int index, UINT32 data );
  923. void push_value( std::vector<uint32_t>& vec, UINT32 Data );
  924. /** data fields */
  925. UINT32 empty;
  926. std::vector<uint32_t> group;
  927. };
  928. class SparseHashtable
  929. {
  930. private:
  931. /** Maximum bits per key before folding the table */
  932. static const int MAX_B;
  933. /** Bins (each bin is an Array object for duplicates of the same key) */
  934. std::vector<BucketGroup> table;
  935. public:
  936. /** constructor */
  937. SparseHashtable();
  938. /** destructor */
  939. ~SparseHashtable();
  940. /** initializer */
  941. int init( int _b );
  942. /** insert data */
  943. void insert( UINT64 index, UINT32 data );
  944. /** query data */
  945. UINT32* query( UINT64 index, int* size );
  946. /** Bits per index */
  947. int b;
  948. /** Number of bins */
  949. UINT64 size;
  950. };
  951. /** class defining a sequence of bits */
  952. class bitarray
  953. {
  954. public:
  955. /** pointer to bits sequence and sequence's length */
  956. UINT32 *arr;
  957. UINT32 length;
  958. /** constructor setting default values */
  959. bitarray()
  960. {
  961. arr = NULL;
  962. length = 0;
  963. }
  964. /** constructor setting sequence's length */
  965. bitarray( UINT64 _bits )
  966. {
  967. arr = NULL;
  968. init( _bits );
  969. }
  970. /** initializer of private fields */
  971. void init( UINT64 _bits )
  972. {
  973. if( arr )
  974. delete[] arr;
  975. length = (UINT32) ceil( _bits / 32.00 );
  976. arr = new UINT32[length];
  977. erase();
  978. }
  979. /** destructor */
  980. ~bitarray()
  981. {
  982. if( arr )
  983. delete[] arr;
  984. }
  985. inline void flip( UINT64 index )
  986. {
  987. arr[index >> 5] ^= ( (UINT32) 0x01 ) << ( index % 32 );
  988. }
  989. inline void set( UINT64 index )
  990. {
  991. arr[index >> 5] |= ( (UINT32) 0x01 ) << ( index % 32 );
  992. }
  993. inline UINT8 get( UINT64 index )
  994. {
  995. return ( arr[index >> 5] & ( ( (UINT32) 0x01 ) << ( index % 32 ) ) ) != 0;
  996. }
  997. /** reserve menory for an UINT32 */
  998. inline void erase()
  999. {
  1000. memset( arr, 0, sizeof(UINT32) * length );
  1001. }
  1002. };
  1003. class Mihasher
  1004. {
  1005. public:
  1006. /** Bits per code */
  1007. int B;
  1008. /** B/8 */
  1009. int B_over_8;
  1010. /** Bits per chunk (must be less than 64) */
  1011. int b;
  1012. /** Number of chunks */
  1013. int m;
  1014. /** Number of chunks with b bits (have 1 bit more than others) */
  1015. int mplus;
  1016. /** Maximum hamming search radius (we use B/2 by default) */
  1017. int D;
  1018. /** Maximum hamming search radius per substring */
  1019. int d;
  1020. /** Maximum results to return */
  1021. int K;
  1022. /** Number of codes */
  1023. UINT64 N;
  1024. /** Table of original full-length codes */
  1025. cv::Mat codes;
  1026. /** Counter for eliminating duplicate results (it is not thread safe) */
  1027. Ptr<bitarray> counter;
  1028. /** Array of m hashtables */
  1029. std::vector<SparseHashtable> H;
  1030. /** Volume of a b-bit Hamming ball with radius s (for s = 0 to d) */
  1031. std::vector<UINT32> xornum;
  1032. /** Used within generation of binary codes at a certain Hamming distance */
  1033. int power[100];
  1034. /** constructor */
  1035. Mihasher();
  1036. /** desctructor */
  1037. ~Mihasher();
  1038. /** constructor 2 */
  1039. Mihasher( int B, int m );
  1040. /** K setter */
  1041. void setK( int K );
  1042. /** populate tables */
  1043. void populate( cv::Mat & codes, UINT32 N, int dim1codes );
  1044. /** execute a batch query */
  1045. void batchquery( UINT32 * results, UINT32 *numres/*, qstat *stats*/, const cv::Mat & q, UINT32 numq, int dim1queries );
  1046. private:
  1047. /** execute a single query */
  1048. void query( UINT32 * results, UINT32* numres/*, qstat *stats*/, UINT8 *q, UINT64 * chunks, UINT32 * res );
  1049. };
  1050. /** retrieve Hamming distances */
  1051. void checkKDistances( UINT32 * numres, int k, std::vector<int>& k_distances, int row, int string_length ) const;
  1052. /** matrix to store new descriptors */
  1053. Mat descriptorsMat;
  1054. /** map storing where each bunch of descriptors benins in DS */
  1055. std::map<int, int> indexesMap;
  1056. /** internal MiHaser representing dataset */
  1057. Ptr<Mihasher> dataset;
  1058. /** index from which next added descriptors' bunch must begin */
  1059. int nextAddedIndex;
  1060. /** number of images whose descriptors are stored in DS */
  1061. int numImages;
  1062. /** number of descriptors in dataset */
  1063. int descrInDS;
  1064. };
  1065. /* --------------------------------------------------------------------------------------------
  1066. UTILITY FUNCTIONS
  1067. -------------------------------------------------------------------------------------------- */
  1068. /** struct for drawing options */
  1069. struct CV_EXPORTS DrawLinesMatchesFlags
  1070. {
  1071. enum
  1072. {
  1073. DEFAULT = 0, //!< Output image matrix will be created (Mat::create),
  1074. //!< i.e. existing memory of output image may be reused.
  1075. //!< Two source images, matches, and single keylines
  1076. //!< will be drawn.
  1077. DRAW_OVER_OUTIMG = 1,//!< Output image matrix will not be
  1078. //!< created (using Mat::create). Matches will be drawn
  1079. //!< on existing content of output image.
  1080. NOT_DRAW_SINGLE_LINES = 2//!< Single keylines will not be drawn.
  1081. };
  1082. };
  1083. /** @brief Draws the found matches of keylines from two images.
  1084. @param img1 first image
  1085. @param keylines1 keylines extracted from first image
  1086. @param img2 second image
  1087. @param keylines2 keylines extracted from second image
  1088. @param matches1to2 vector of matches
  1089. @param outImg output matrix to draw on
  1090. @param matchColor drawing color for matches (chosen randomly in case of default value)
  1091. @param singleLineColor drawing color for keylines (chosen randomly in case of default value)
  1092. @param matchesMask mask to indicate which matches must be drawn
  1093. @param flags drawing flags, see DrawLinesMatchesFlags
  1094. @note If both *matchColor* and *singleLineColor* are set to their default values, function draws
  1095. matched lines and line connecting them with same color
  1096. */
  1097. CV_EXPORTS void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2,
  1098. const std::vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor = Scalar::all( -1 ),
  1099. const Scalar& singleLineColor = Scalar::all( -1 ), const std::vector<char>& matchesMask = std::vector<char>(),
  1100. int flags = DrawLinesMatchesFlags::DEFAULT );
  1101. /** @brief Draws keylines.
  1102. @param image input image
  1103. @param keylines keylines to be drawn
  1104. @param outImage output image to draw on
  1105. @param color color of lines to be drawn (if set to defaul value, color is chosen randomly)
  1106. @param flags drawing flags
  1107. */
  1108. CV_EXPORTS void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, Mat& outImage, const Scalar& color = Scalar::all( -1 ),
  1109. int flags = DrawLinesMatchesFlags::DEFAULT );
  1110. //! @}
  1111. }
  1112. }
  1113. #endif