create_diamond.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <opencv2/highgui.hpp>
  2. #include <opencv2/objdetect/charuco_detector.hpp>
  3. #include <vector>
  4. #include <iostream>
  5. #include "aruco_samples_utility.hpp"
  6. using namespace std;
  7. using namespace cv;
  8. namespace {
  9. const char* about = "Create a ChArUco marker image";
  10. const char* keys =
  11. "{@outfile | res.png | Output image }"
  12. "{sl | 100 | Square side length (in pixels) }"
  13. "{ml | 60 | Marker side length (in pixels) }"
  14. "{cd | | Input file with custom dictionary }"
  15. "{d | 10 | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
  16. "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
  17. "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
  18. "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
  19. "{ids |0, 1, 2, 3 | Four ids for the ChArUco marker: id1,id2,id3,id4 }"
  20. "{m | 0 | Margins size (in pixels) }"
  21. "{bb | 1 | Number of bits in marker borders }"
  22. "{si | false | show generated image }";
  23. }
  24. int main(int argc, char *argv[]) {
  25. CommandLineParser parser(argc, argv, keys);
  26. parser.about(about);
  27. int squareLength = parser.get<int>("sl");
  28. int markerLength = parser.get<int>("ml");
  29. string idsString = parser.get<string>("ids");
  30. int margins = parser.get<int>("m");
  31. int borderBits = parser.get<int>("bb");
  32. bool showImage = parser.get<bool>("si");
  33. string out = parser.get<string>(0);
  34. aruco::Dictionary dictionary = readDictionatyFromCommandLine(parser);
  35. if(!parser.check()) {
  36. parser.printErrors();
  37. return 0;
  38. }
  39. istringstream ss(idsString);
  40. vector<string> splittedIds;
  41. string token;
  42. while(getline(ss, token, ','))
  43. splittedIds.push_back(token);
  44. if(splittedIds.size() < 4) {
  45. throw std::runtime_error("Incorrect ids format\n");
  46. }
  47. Vec4i ids;
  48. for(int i = 0; i < 4; i++)
  49. ids[i] = atoi(splittedIds[i].c_str());
  50. //! [generate_diamond]
  51. vector<int> diamondIds = {ids[0], ids[1], ids[2], ids[3]};
  52. aruco::CharucoBoard charucoBoard(Size(3, 3), (float)squareLength, (float)markerLength, dictionary, diamondIds);
  53. Mat markerImg;
  54. charucoBoard.generateImage(Size(3*squareLength + 2*margins, 3*squareLength + 2*margins), markerImg, margins, borderBits);
  55. //! [generate_diamond]
  56. if(showImage) {
  57. imshow("board", markerImg);
  58. waitKey(0);
  59. }
  60. if (out != "")
  61. imwrite(out, markerImg);
  62. return 0;
  63. }