dictionary.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 Dictionary {
  49. public:
  50. Mat bytesList; // marker code information
  51. int markerSize; // number of bits per dimension
  52. 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. * @brief Given a matrix of bits. Returns whether if marker is identified or not.
  58. * It returns by reference the correct id (if any) and the correct rotation
  59. */
  60. bool identify(const Mat &onlyBits, int &idx, int &rotation, double maxCorrectionRate) const;
  61. /**
  62. * @brief Returns the distance of the input bits to the specific id. If allRotations is true,
  63. * the four posible bits rotation are considered
  64. */
  65. int getDistanceToId(InputArray bits, int id, bool allRotations = true) const;
  66. /**
  67. * @brief Draw a canonical marker image
  68. */
  69. void drawMarker(int id, int sidePixels, OutputArray _img, int borderBits = 1) const;
  70. /**
  71. * @brief Transform matrix of bits to list of bytes in the 4 rotations
  72. */
  73. static Mat getByteListFromBits(const Mat &bits);
  74. /**
  75. * @brief Transform list of bytes to matrix of bits
  76. */
  77. static Mat getBitsFromByteList(const Mat &byteList, int markerSize);
  78. };
  79. /**
  80. * @brief Predefined markers dictionaries/sets
  81. * Each dictionary indicates the number of bits and the number of markers contained
  82. * - DICT_ARUCO: standard ArUco Library Markers. 1024 markers, 5x5 bits, 0 minimum distance
  83. */
  84. enum PREDEFINED_DICTIONARY_NAME {
  85. DICT_4X4_50 = 0,
  86. DICT_4X4_100,
  87. DICT_4X4_250,
  88. DICT_4X4_1000,
  89. DICT_5X5_50,
  90. DICT_5X5_100,
  91. DICT_5X5_250,
  92. DICT_5X5_1000,
  93. DICT_6X6_50,
  94. DICT_6X6_100,
  95. DICT_6X6_250,
  96. DICT_6X6_1000,
  97. DICT_7X7_50,
  98. DICT_7X7_100,
  99. DICT_7X7_250,
  100. DICT_7X7_1000,
  101. DICT_ARUCO_ORIGINAL
  102. };
  103. /**
  104. * @brief Returns one of the predefined dictionaries defined in PREDEFINED_DICTIONARY_NAME
  105. */
  106. CV_EXPORTS const Dictionary &getPredefinedDictionary(PREDEFINED_DICTIONARY_NAME name);
  107. /**
  108. * @brief Generates a new customizable marker dictionary
  109. *
  110. * @param nMarkers number of markers in the dictionary
  111. * @param markerSize number of bits per dimension of each markers
  112. * @param baseDictionary Include the markers in this dictionary at the beginning (optional)
  113. *
  114. * This function creates a new dictionary composed by nMarkers markers and each markers composed
  115. * by markerSize x markerSize bits. If baseDictionary is provided, its markers are directly
  116. * included and the rest are generated based on them. If the size of baseDictionary is higher
  117. * than nMarkers, only the first nMarkers in baseDictionary are taken and no new marker is added.
  118. */
  119. CV_EXPORTS Dictionary generateCustomDictionary(int nMarkers, int markerSize,
  120. const Dictionary &baseDictionary = Dictionary());
  121. //! @}
  122. }
  123. }
  124. #endif