create_board.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include <opencv2/highgui.hpp>
  32. #include <opencv2/aruco.hpp>
  33. #include <iostream>
  34. #include "aruco_samples_utility.hpp"
  35. using namespace cv;
  36. namespace {
  37. const char* about = "Create an ArUco grid board image";
  38. const char* keys =
  39. "{@outfile |<none> | Output image }"
  40. "{w | | Number of markers in X direction }"
  41. "{h | | Number of markers in Y direction }"
  42. "{l | | Marker side length (in pixels) }"
  43. "{s | | Separation between two consecutive markers in the grid (in pixels)}"
  44. "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
  45. "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
  46. "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
  47. "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
  48. "{cd | | Input file with custom dictionary }"
  49. "{m | | Margins size (in pixels). Default is marker separation (-s) }"
  50. "{bb | 1 | Number of bits in marker borders }"
  51. "{si | false | show generated image }";
  52. }
  53. int main(int argc, char *argv[]) {
  54. CommandLineParser parser(argc, argv, keys);
  55. parser.about(about);
  56. if(argc < 7) {
  57. parser.printMessage();
  58. return 0;
  59. }
  60. int markersX = parser.get<int>("w");
  61. int markersY = parser.get<int>("h");
  62. int markerLength = parser.get<int>("l");
  63. int markerSeparation = parser.get<int>("s");
  64. int margins = markerSeparation;
  65. if(parser.has("m")) {
  66. margins = parser.get<int>("m");
  67. }
  68. int borderBits = parser.get<int>("bb");
  69. bool showImage = parser.get<bool>("si");
  70. String out = parser.get<String>(0);
  71. if(!parser.check()) {
  72. parser.printErrors();
  73. return 0;
  74. }
  75. Size imageSize;
  76. imageSize.width = markersX * (markerLength + markerSeparation) - markerSeparation + 2 * margins;
  77. imageSize.height =
  78. markersY * (markerLength + markerSeparation) - markerSeparation + 2 * margins;
  79. Ptr<aruco::Dictionary> dictionary;
  80. if (parser.has("d")) {
  81. int dictionaryId = parser.get<int>("d");
  82. dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
  83. }
  84. else if (parser.has("cd")) {
  85. FileStorage fs(parser.get<std::string>("cd"), FileStorage::READ);
  86. bool readOk = aruco::Dictionary::readDictionary(fs.root(), dictionary);
  87. if(!readOk)
  88. {
  89. std::cerr << "Invalid dictionary file" << std::endl;
  90. return 0;
  91. }
  92. }
  93. else {
  94. std::cerr << "Dictionary not specified" << std::endl;
  95. return 0;
  96. }
  97. Ptr<aruco::GridBoard> board = aruco::GridBoard::create(markersX, markersY, float(markerLength),
  98. float(markerSeparation), dictionary);
  99. // show created board
  100. Mat boardImage;
  101. board->draw(imageSize, boardImage, margins, borderBits);
  102. if(showImage) {
  103. imshow("board", boardImage);
  104. waitKey(0);
  105. }
  106. imwrite(out, boardImage);
  107. return 0;
  108. }