parametersController.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "parametersController.hpp"
  5. #include <iostream>
  6. template <typename T>
  7. static bool readFromNode(cv::FileNode node, T& value)
  8. {
  9. if(!node.isNone()) {
  10. node >> value;
  11. return true;
  12. }
  13. else
  14. return false;
  15. }
  16. static bool checkAssertion(bool value, const std::string& msg)
  17. {
  18. if(!value)
  19. std::cerr << "Error: " << msg << std::endl;
  20. return value;
  21. }
  22. bool calib::parametersController::loadFromFile(const std::string &inputFileName)
  23. {
  24. cv::FileStorage reader;
  25. reader.open(inputFileName, cv::FileStorage::READ);
  26. if(!reader.isOpened()) {
  27. std::cerr << "Warning: Unable to open " << inputFileName <<
  28. " Application started with default advanced parameters" << std::endl;
  29. return true;
  30. }
  31. readFromNode(reader["charuco_dict"], mCapParams.charucoDictName);
  32. if (readFromNode(reader["charuco_square_lenght"], mCapParams.charucoSquareLength)) {
  33. std::cout << "DEPRECATION: Parameter 'charuco_square_lenght' has been deprecated (typo). Use 'charuco_square_length' instead." << std::endl;
  34. }
  35. readFromNode(reader["charuco_square_length"], mCapParams.charucoSquareLength);
  36. readFromNode(reader["charuco_marker_size"], mCapParams.charucoMarkerSize);
  37. readFromNode(reader["camera_resolution"], mCapParams.cameraResolution);
  38. readFromNode(reader["calibration_step"], mCapParams.calibrationStep);
  39. readFromNode(reader["max_frames_num"], mCapParams.maxFramesNum);
  40. readFromNode(reader["min_frames_num"], mCapParams.minFramesNum);
  41. readFromNode(reader["solver_eps"], mInternalParameters.solverEps);
  42. readFromNode(reader["solver_max_iters"], mInternalParameters.solverMaxIters);
  43. readFromNode(reader["fast_solver"], mInternalParameters.fastSolving);
  44. readFromNode(reader["frame_filter_conv_param"], mInternalParameters.filterAlpha);
  45. bool retValue =
  46. checkAssertion(mCapParams.charucoDictName >= 0, "Dict name must be >= 0") &&
  47. checkAssertion(mCapParams.charucoMarkerSize > 0, "Marker size must be positive") &&
  48. checkAssertion(mCapParams.charucoSquareLength > 0, "Square size must be positive") &&
  49. checkAssertion(mCapParams.minFramesNum > 1, "Minimal number of frames for calibration < 1") &&
  50. checkAssertion(mCapParams.calibrationStep > 0, "Calibration step must be positive") &&
  51. checkAssertion(mCapParams.maxFramesNum > mCapParams.minFramesNum, "maxFramesNum < minFramesNum") &&
  52. checkAssertion(mInternalParameters.solverEps > 0, "Solver precision must be positive") &&
  53. checkAssertion(mInternalParameters.solverMaxIters > 0, "Max solver iterations number must be positive") &&
  54. checkAssertion(mInternalParameters.filterAlpha >=0 && mInternalParameters.filterAlpha <=1 ,
  55. "Frame filter convolution parameter must be in [0,1] interval") &&
  56. checkAssertion(mCapParams.cameraResolution.width > 0 && mCapParams.cameraResolution.height > 0,
  57. "Wrong camera resolution values");
  58. reader.release();
  59. return retValue;
  60. }
  61. calib::parametersController::parametersController()
  62. {
  63. }
  64. calib::captureParameters calib::parametersController::getCaptureParameters() const
  65. {
  66. return mCapParams;
  67. }
  68. calib::internalParameters calib::parametersController::getInternalParameters() const
  69. {
  70. return mInternalParameters;
  71. }
  72. bool calib::parametersController::loadFromParser(cv::CommandLineParser &parser)
  73. {
  74. mCapParams.flipVertical = parser.get<bool>("flip");
  75. mCapParams.captureDelay = parser.get<float>("d");
  76. mCapParams.squareSize = parser.get<float>("sz");
  77. mCapParams.templDst = parser.get<float>("dst");
  78. if(!checkAssertion(mCapParams.squareSize > 0, "Distance between corners or circles must be positive"))
  79. return false;
  80. if(!checkAssertion(mCapParams.templDst > 0, "Distance between parts of dual template must be positive"))
  81. return false;
  82. if (parser.has("v")) {
  83. mCapParams.source = File;
  84. mCapParams.videoFileName = parser.get<std::string>("v");
  85. }
  86. else {
  87. mCapParams.source = Camera;
  88. mCapParams.camID = parser.get<int>("ci");
  89. }
  90. std::string templateType = parser.get<std::string>("t");
  91. if(templateType.find("circles", 0) == 0) {
  92. mCapParams.board = AcirclesGrid;
  93. mCapParams.boardSize = cv::Size(4, 11);
  94. }
  95. else if(templateType.find("chessboard", 0) == 0) {
  96. mCapParams.board = Chessboard;
  97. mCapParams.boardSize = cv::Size(7, 7);
  98. }
  99. else if(templateType.find("dualcircles", 0) == 0) {
  100. mCapParams.board = DoubleAcirclesGrid;
  101. mCapParams.boardSize = cv::Size(4, 11);
  102. }
  103. else if(templateType.find("charuco", 0) == 0) {
  104. mCapParams.board = chAruco;
  105. mCapParams.boardSize = cv::Size(6, 8);
  106. mCapParams.charucoDictName = 0;
  107. mCapParams.charucoSquareLength = 200;
  108. mCapParams.charucoMarkerSize = 100;
  109. }
  110. else {
  111. std::cerr << "Wrong template name\n";
  112. return false;
  113. }
  114. if(parser.has("w") && parser.has("h")) {
  115. mCapParams.boardSize = cv::Size(parser.get<int>("w"), parser.get<int>("h"));
  116. if(!checkAssertion(mCapParams.boardSize.width > 0 || mCapParams.boardSize.height > 0,
  117. "Board size must be positive"))
  118. return false;
  119. }
  120. if(!checkAssertion(parser.get<std::string>("of").find(".xml") > 0,
  121. "Wrong output file name: correct format is [name].xml"))
  122. return false;
  123. loadFromFile(parser.get<std::string>("pf"));
  124. return true;
  125. }