dictionary.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. */
  31. #ifndef __OPENCV_DICTIONARY_HPP__
  32. #define __OPENCV_DICTIONARY_HPP__
  33. #include <opencv2/core.hpp>
  34. namespace cv {
  35. namespace aruco {
  36. //! @addtogroup aruco
  37. //! @{
  38. /**
  39. * @brief Dictionary/Set of markers. It contains the inner codification
  40. *
  41. * bytesList contains the marker codewords where
  42. * - bytesList.rows is the dictionary size
  43. * - each marker is encoded using `nbytes = ceil(markerSize*markerSize/8.)`
  44. * - each row contains all 4 rotations of the marker, so its length is `4*nbytes`
  45. *
  46. * `bytesList.ptr(i)[k*nbytes + j]` is then the j-th byte of i-th marker, in its k-th rotation.
  47. */
  48. class CV_EXPORTS_W Dictionary {
  49. public:
  50. CV_PROP_RW Mat bytesList; // marker code information
  51. CV_PROP_RW int markerSize; // number of bits per dimension
  52. CV_PROP_RW int maxCorrectionBits; // maximum number of bits that can be corrected
  53. /**
  54. */
  55. Dictionary(const Mat &_bytesList = Mat(), int _markerSize = 0, int _maxcorr = 0);
  56. /**
  57. Dictionary(const Dictionary &_dictionary);
  58. */
  59. /**
  60. */
  61. Dictionary(const Ptr<Dictionary> &_dictionary);
  62. /**
  63. * @see generateCustomDictionary
  64. */
  65. CV_WRAP_AS(create) static Ptr<Dictionary> create(int nMarkers, int markerSize, int randomSeed=0);
  66. /**
  67. * @see generateCustomDictionary
  68. */
  69. CV_WRAP_AS(create_from) static Ptr<Dictionary> create(int nMarkers, int markerSize,
  70. const Ptr<Dictionary> &baseDictionary, int randomSeed=0);
  71. /**
  72. * @see getPredefinedDictionary
  73. */
  74. CV_WRAP static Ptr<Dictionary> get(int dict);
  75. /**
  76. * @brief Given a matrix of bits. Returns whether if marker is identified or not.
  77. * It returns by reference the correct id (if any) and the correct rotation
  78. */
  79. bool identify(const Mat &onlyBits, int &idx, int &rotation, double maxCorrectionRate) const;
  80. /**
  81. * @brief Returns the distance of the input bits to the specific id. If allRotations is true,
  82. * the four posible bits rotation are considered
  83. */
  84. int getDistanceToId(InputArray bits, int id, bool allRotations = true) const;
  85. /**
  86. * @brief Draw a canonical marker image
  87. */
  88. CV_WRAP void drawMarker(int id, int sidePixels, OutputArray _img, int borderBits = 1) const;
  89. /**
  90. * @brief Transform matrix of bits to list of bytes in the 4 rotations
  91. */
  92. CV_WRAP static Mat getByteListFromBits(const Mat &bits);
  93. /**
  94. * @brief Transform list of bytes to matrix of bits
  95. */
  96. CV_WRAP static Mat getBitsFromByteList(const Mat &byteList, int markerSize);
  97. };
  98. /**
  99. * @brief Predefined markers dictionaries/sets
  100. * Each dictionary indicates the number of bits and the number of markers contained
  101. * - DICT_ARUCO_ORIGINAL: standard ArUco Library Markers. 1024 markers, 5x5 bits, 0 minimum
  102. distance
  103. */
  104. enum PREDEFINED_DICTIONARY_NAME {
  105. DICT_4X4_50 = 0,
  106. DICT_4X4_100,
  107. DICT_4X4_250,
  108. DICT_4X4_1000,
  109. DICT_5X5_50,
  110. DICT_5X5_100,
  111. DICT_5X5_250,
  112. DICT_5X5_1000,
  113. DICT_6X6_50,
  114. DICT_6X6_100,
  115. DICT_6X6_250,
  116. DICT_6X6_1000,
  117. DICT_7X7_50,
  118. DICT_7X7_100,
  119. DICT_7X7_250,
  120. DICT_7X7_1000,
  121. DICT_ARUCO_ORIGINAL,
  122. DICT_APRILTAG_16h5, ///< 4x4 bits, minimum hamming distance between any two codes = 5, 30 codes
  123. DICT_APRILTAG_25h9, ///< 5x5 bits, minimum hamming distance between any two codes = 9, 35 codes
  124. DICT_APRILTAG_36h10, ///< 6x6 bits, minimum hamming distance between any two codes = 10, 2320 codes
  125. DICT_APRILTAG_36h11 ///< 6x6 bits, minimum hamming distance between any two codes = 11, 587 codes
  126. };
  127. /**
  128. * @brief Returns one of the predefined dictionaries defined in PREDEFINED_DICTIONARY_NAME
  129. */
  130. CV_EXPORTS Ptr<Dictionary> getPredefinedDictionary(PREDEFINED_DICTIONARY_NAME name);
  131. /**
  132. * @brief Returns one of the predefined dictionaries referenced by DICT_*.
  133. */
  134. CV_EXPORTS_W Ptr<Dictionary> getPredefinedDictionary(int dict);
  135. /**
  136. * @see generateCustomDictionary
  137. */
  138. CV_EXPORTS_AS(custom_dictionary) Ptr<Dictionary> generateCustomDictionary(
  139. int nMarkers,
  140. int markerSize,
  141. int randomSeed=0);
  142. /**
  143. * @brief Generates a new customizable marker dictionary
  144. *
  145. * @param nMarkers number of markers in the dictionary
  146. * @param markerSize number of bits per dimension of each markers
  147. * @param baseDictionary Include the markers in this dictionary at the beginning (optional)
  148. * @param randomSeed a user supplied seed for theRNG()
  149. *
  150. * This function creates a new dictionary composed by nMarkers markers and each markers composed
  151. * by markerSize x markerSize bits. If baseDictionary is provided, its markers are directly
  152. * included and the rest are generated based on them. If the size of baseDictionary is higher
  153. * than nMarkers, only the first nMarkers in baseDictionary are taken and no new marker is added.
  154. */
  155. CV_EXPORTS_AS(custom_dictionary_from) Ptr<Dictionary> generateCustomDictionary(
  156. int nMarkers,
  157. int markerSize,
  158. const Ptr<Dictionary> &baseDictionary,
  159. int randomSeed=0);
  160. //! @}
  161. }
  162. }
  163. #endif