diff --git a/modules/aruco/samples/calibrate_camera.cpp b/modules/aruco/samples/calibrate_camera.cpp index 45b4dc376..c4b6a8443 100644 --- a/modules/aruco/samples/calibrate_camera.cpp +++ b/modules/aruco/samples/calibrate_camera.cpp @@ -49,59 +49,31 @@ using namespace std; using namespace cv; -/** - */ -static void help() { - cout << "Calibration using a ArUco Planar Grid board" << endl; - cout << "How to Use:" << endl; - cout << "To capture a frame for calibration, press 'c'," << endl; - cout << "If input comes from video, press any key for next frame" << endl; - cout << "To finish capturing, press 'ESC' key and calibration starts." << endl; - cout << "Parameters: " << endl; - cout << "-w # Number of markers in X direction" << endl; - cout << "-h # Number of markers in Y direction" << endl; - cout << "-l # Marker side lenght (in meters)" << endl; - cout << "-s # Separation between two consecutive" - << "markers in the grid (in meters)" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "-o # Output file with calibrated camera parameters" << endl; - cout << "[-v ] # Input from video file, if ommited, input comes from camera" << endl; - cout << "[-ci ] # Camera id if input doesnt come from video (-v). Default is 0" << endl; - cout << "[-dp ] # File of marker detector parameters" << endl; - cout << "[-rs] # Apply refind strategy" << endl; - cout << "[-zt] # Assume zero tangential distortion" << endl; - cout << "[-a ] # Fix aspect ratio (fx/fy)" << endl; - cout << "[-p] # Fix the principal point at the center" << endl; +namespace { +const char* about = + "Calibration using a ArUco Planar Grid board\n" + " To capture a frame for calibration, press 'c',\n" + " If input comes from video, press any key for next frame\n" + " To finish capturing, press 'ESC' key and calibration starts.\n"; +const char* keys = + "{w | | Number of squares in X direction }" + "{h | | Number of squares in Y direction }" + "{l | | Marker side lenght (in meters) }" + "{s | | Separation between two consecutive markers in the grid (in meters) }" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{@outfile | | Output file with calibrated camera parameters }" + "{v | | Input from video file, if ommited, input comes from camera }" + "{ci | 0 | Camera id if input doesnt come from video (-v) }" + "{dp | | File of marker detector parameters }" + "{rs | false | Apply refind strategy }" + "{zt | false | Assume zero tangential distortion }" + "{a | | Fix aspect ratio (fx/fy) to this value }" + "{pc | false | Fix the principal point at the center }"; } - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; -} - - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; -} - - - - /** */ static bool readDetectorParameters(string filename, aruco::DetectorParameters ¶ms) { @@ -177,55 +149,65 @@ static bool saveCameraParams(const string &filename, Size imageSize, float aspec /** */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-w", argc, argv) || !isParam("-h", argc, argv) || !isParam("-l", argc, argv) || - !isParam("-s", argc, argv) || !isParam("-d", argc, argv) || !isParam("-o", argc, argv)) { - help(); + if(argc < 6) { + parser.printMessage(); return 0; } - int markersX = atoi(getParam("-w", argc, argv).c_str()); - int markersY = atoi(getParam("-h", argc, argv).c_str()); - float markerLength = (float)atof(getParam("-l", argc, argv).c_str()); - float markerSeparation = (float)atof(getParam("-s", argc, argv).c_str()); - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); - string outputFile = getParam("-o", argc, argv); + int markersX = parser.get("w"); + int markersY = parser.get("w"); + float markerLength = parser.get("l"); + float markerSeparation = parser.get("s"); + int dictionaryId = parser.get("d"); + string outputFile = parser.get(0); int calibrationFlags = 0; float aspectRatio = 1; - if(isParam("-a", argc, argv)) { + if(parser.has("a")) { calibrationFlags |= CALIB_FIX_ASPECT_RATIO; - aspectRatio = (float)atof(getParam("-a", argc, argv).c_str()); + aspectRatio = parser.get("a"); } - if(isParam("-zt", argc, argv)) calibrationFlags |= CALIB_ZERO_TANGENT_DIST; - if(isParam("-p", argc, argv)) calibrationFlags |= CALIB_FIX_PRINCIPAL_POINT; + if(parser.get("zt")) calibrationFlags |= CALIB_ZERO_TANGENT_DIST; + if(parser.get("pc")) calibrationFlags |= CALIB_FIX_PRINCIPAL_POINT; aruco::DetectorParameters detectorParams; - if(isParam("-dp", argc, argv)) { - bool readOk = readDetectorParameters(getParam("-dp", argc, argv), detectorParams); + if(parser.has("dp")) { + bool readOk = readDetectorParameters(parser.get("dp"), detectorParams); if(!readOk) { cerr << "Invalid detector parameters file" << endl; return 0; } } - bool refindStrategy = false; - if(isParam("-rs", argc, argv)) refindStrategy = true; + bool refindStrategy = parser.get("rs"); + int camId = parser.get("ci"); + String video; + + if(parser.has("v")) { + video = parser.get("v"); + } + + if(!parser.check()) { + parser.printErrors(); + return 0; + } VideoCapture inputVideo; int waitTime; - if(isParam("-v", argc, argv)) { - inputVideo.open(getParam("-v", argc, argv)); + if(!video.empty()) { + inputVideo.open(video); waitTime = 0; } else { - int camId = 0; - if(isParam("-ci", argc, argv)) camId = atoi(getParam("-ci", argc, argv).c_str()); inputVideo.open(camId); waitTime = 10; } + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); + // create board object aruco::GridBoard board = aruco::GridBoard::create(markersX, markersY, markerLength, markerSeparation, dictionary); diff --git a/modules/aruco/samples/calibrate_camera_charuco.cpp b/modules/aruco/samples/calibrate_camera_charuco.cpp index ba54cbcc1..fb763f571 100644 --- a/modules/aruco/samples/calibrate_camera_charuco.cpp +++ b/modules/aruco/samples/calibrate_camera_charuco.cpp @@ -48,60 +48,32 @@ the use of this software, even if advised of the possibility of such damage. using namespace std; using namespace cv; - -/** - */ -static void help() { - cout << "Calibration using a ChArUco board" << endl; - cout << "How to Use:" << endl; - cout << "To capture a frame for calibration, press 'c'," << endl; - cout << "If input comes from video, press any key for next frame" << endl; - cout << "To finish capturing, press 'ESC' key and calibration starts." << endl; - cout << "Parameters: " << endl; - cout << "-w # Number of markers in X direction" << endl; - cout << "-h # Number of squares in Y direction" << endl; - cout << "-sl # Square side lenght (in meters)" << endl; - cout << "-ml # Marker side lenght (in meters)" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "-o # Output file with calibrated camera parameters" << endl; - cout << "[-v ] # Input from video file, if ommited, input comes from camera" << endl; - cout << "[-ci ] # Camera id if input doesnt come from video (-v). Default is 0" << endl; - cout << "[-dp ] # File of marker detector parameters" << endl; - cout << "[-rs] # Apply refind strategy" << endl; - cout << "[-zt] # Assume zero tangential distortion" << endl; - cout << "[-a ] # Fix aspect ratio (fx/fy)" << endl; - cout << "[-p] # Fix the principal point at the center" << endl; - cout << "[-sc] # Show detected chessboard corners after calibration" << endl; -} - - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; -} - - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; +namespace { +const char* about = + "Calibration using a ChArUco board\n" + " To capture a frame for calibration, press 'c',\n" + " If input comes from video, press any key for next frame\n" + " To finish capturing, press 'ESC' key and calibration starts.\n"; +const char* keys = + "{w | | Number of squares in X direction }" + "{h | | Number of squares in Y direction }" + "{sl | | Square side lenght (in pixels) }" + "{ml | | Marker side lenght (in pixels) }" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{@outfile | | Output file with calibrated camera parameters }" + "{v | | Input from video file, if ommited, input comes from camera }" + "{ci | 0 | Camera id if input doesnt come from video (-v) }" + "{dp | | File of marker detector parameters }" + "{rs | false | Apply refind strategy }" + "{zt | false | Assume zero tangential distortion }" + "{a | | Fix aspect ratio (fx/fy) to this value }" + "{pc | false | Fix the principal point at the center }" + "{sc | false | Show detected chessboard corners after calibration }"; } - - - /** */ static bool readDetectorParameters(string filename, aruco::DetectorParameters ¶ms) { @@ -177,57 +149,67 @@ static bool saveCameraParams(const string &filename, Size imageSize, float aspec /** */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-w", argc, argv) || !isParam("-h", argc, argv) || !isParam("-sl", argc, argv) || - !isParam("-ml", argc, argv) || !isParam("-d", argc, argv) || !isParam("-o", argc, argv)) { - help(); + if(argc < 7) { + parser.printMessage(); return 0; } - int squaresX = atoi(getParam("-w", argc, argv).c_str()); - int squaresY = atoi(getParam("-h", argc, argv).c_str()); - float squareLength = (float)atof(getParam("-sl", argc, argv).c_str()); - float markerLength = (float)atof(getParam("-ml", argc, argv).c_str()); - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); - string outputFile = getParam("-o", argc, argv); + int squaresX = parser.get("w"); + int squaresY = parser.get("h"); + float squareLength = parser.get("sl"); + float markerLength = parser.get("ml"); + int dictionaryId = parser.get("d"); + string outputFile = parser.get(0); - bool showChessboardCorners = isParam("-sc", argc, argv); + bool showChessboardCorners = parser.get("sc"); int calibrationFlags = 0; float aspectRatio = 1; - if(isParam("-a", argc, argv)) { + if(parser.has("a")) { calibrationFlags |= CALIB_FIX_ASPECT_RATIO; - aspectRatio = (float)atof(getParam("-a", argc, argv).c_str()); + aspectRatio = parser.get("a"); } - if(isParam("-zt", argc, argv)) calibrationFlags |= CALIB_ZERO_TANGENT_DIST; - if(isParam("-p", argc, argv)) calibrationFlags |= CALIB_FIX_PRINCIPAL_POINT; + if(parser.get("zt")) calibrationFlags |= CALIB_ZERO_TANGENT_DIST; + if(parser.get("pc")) calibrationFlags |= CALIB_FIX_PRINCIPAL_POINT; aruco::DetectorParameters detectorParams; - if(isParam("-dp", argc, argv)) { - bool readOk = readDetectorParameters(getParam("-dp", argc, argv), detectorParams); + if(parser.has("dp")) { + bool readOk = readDetectorParameters(parser.get("dp"), detectorParams); if(!readOk) { cerr << "Invalid detector parameters file" << endl; return 0; } } - bool refindStrategy = false; - if(isParam("-rs", argc, argv)) refindStrategy = true; + bool refindStrategy = parser.get("rs"); + int camId = parser.get("ci"); + String video; + + if(parser.has("v")) { + video = parser.get("v"); + } + + if(!parser.check()) { + parser.printErrors(); + return 0; + } VideoCapture inputVideo; int waitTime; - if(isParam("-v", argc, argv)) { - inputVideo.open(getParam("-v", argc, argv)); + if(!video.empty()) { + inputVideo.open(video); waitTime = 0; } else { - int camId = 0; - if(isParam("-ci", argc, argv)) camId = atoi(getParam("-ci", argc, argv).c_str()); inputVideo.open(camId); waitTime = 10; } + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); + // create charuco board object aruco::CharucoBoard board = aruco::CharucoBoard::create(squaresX, squaresY, squareLength, markerLength, dictionary); diff --git a/modules/aruco/samples/create_board.cpp b/modules/aruco/samples/create_board.cpp index f65ceb5ac..7aeb2ccca 100644 --- a/modules/aruco/samples/create_board.cpp +++ b/modules/aruco/samples/create_board.cpp @@ -39,92 +39,63 @@ the use of this software, even if advised of the possibility of such damage. #include #include -#include -using namespace std; using namespace cv; - -/** - */ -static void help() { - cout << "Create an ArUco grid board image" << endl; - cout << "Parameters: " << endl; - cout << "-o # Output image" << endl; - cout << "-w # Number of markers in X direction" << endl; - cout << "-h # Number of markers in Y direction" << endl; - cout << "-l # Marker side lenght (in pixels)" << endl; - cout << "-s # Separation between two consecutive" - << "markers in the grid (in pixels)" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "[-m ] # Margins size (in pixels)" - << "Default is marker separation" << endl; - cout << "[-bb ] # Number of bits in marker borders. Default is 1" << endl; - cout << "[-si] # show generated image" << endl; +namespace { +const char* about = "Create an ArUco grid board image"; +const char* keys = + "{@outfile | | Output image }" + "{w | | Number of markers in X direction }" + "{h | | Number of markers in Y direction }" + "{l | | Marker side lenght (in pixels) }" + "{s | | Separation between two consecutive markers in the grid (in pixels)}" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{m | | Margins size (in pixels). Default is marker separation (-s) }" + "{bb | 1 | Number of bits in marker borders }" + "{si | false | show generated image }"; } - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; -} - - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; -} - - -/** - */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-w", argc, argv) || !isParam("-h", argc, argv) || !isParam("-l", argc, argv) || - !isParam("-s", argc, argv) || !isParam("-d", argc, argv) || !isParam("-o", argc, argv)) { - help(); + if(argc < 7) { + parser.printMessage(); return 0; } - int markersX = atoi(getParam("-w", argc, argv).c_str()); - int markersY = atoi(getParam("-h", argc, argv).c_str()); - int markerLength = atoi(getParam("-l", argc, argv).c_str()); - int markerSeparation = atoi(getParam("-s", argc, argv).c_str()); - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); - + int markersX = parser.get("w"); + int markersY = parser.get("h"); + int markerLength = parser.get("l"); + int markerSeparation = parser.get("s"); + int dictionaryId = parser.get("d"); int margins = markerSeparation; - if(isParam("-m", argc, argv)) { - margins = atoi(getParam("-m", argc, argv).c_str()); + if(parser.has("m")) { + margins = parser.get("m"); } - int borderBits = 1; - if(isParam("-bb", argc, argv)) { - borderBits = atoi(getParam("-bb", argc, argv).c_str()); - } + int borderBits = parser.get("bb"); + bool showImage = parser.get("si"); - bool showImage = false; - if(isParam("-si", argc, argv)) showImage = true; + String out = parser.get(0); + + if(!parser.check()) { + parser.printErrors(); + return 0; + } Size imageSize; imageSize.width = markersX * (markerLength + markerSeparation) - markerSeparation + 2 * margins; imageSize.height = markersY * (markerLength + markerSeparation) - markerSeparation + 2 * margins; + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); + aruco::GridBoard board = aruco::GridBoard::create(markersX, markersY, float(markerLength), float(markerSeparation), dictionary); @@ -137,7 +108,7 @@ int main(int argc, char *argv[]) { waitKey(0); } - imwrite(getParam("-o", argc, argv), boardImage); + imwrite(out, boardImage); return 0; } diff --git a/modules/aruco/samples/create_board_charuco.cpp b/modules/aruco/samples/create_board_charuco.cpp index 3b33df834..75f8f3ab7 100644 --- a/modules/aruco/samples/create_board_charuco.cpp +++ b/modules/aruco/samples/create_board_charuco.cpp @@ -39,85 +39,57 @@ the use of this software, even if advised of the possibility of such damage. #include #include -#include -using namespace std; using namespace cv; - -/** - */ -static void help() { - cout << "Create a ChArUco board image" << endl; - cout << "Parameters: " << endl; - cout << "-o # Output image" << endl; - cout << "-w # Number of squares in X direction" << endl; - cout << "-h # Number of squares in Y direction" << endl; - cout << "-sl # Square side lenght (in pixels)" << endl; - cout << "-ml # Marker side lenght (in pixels)" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "[-m ] # Margins size (in pixels)" - << "Default is (squareLength-markerLength)" << endl; - cout << "[-bb ] # Number of bits in marker borders. Default is 1" << endl; - cout << "[-si] # show generated image" << endl; -} - - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; +namespace { +const char* about = "Create a ChArUco board image"; +const char* keys = + "{@outfile | | Output image }" + "{w | | Number of squares in X direction }" + "{h | | Number of squares in Y direction }" + "{sl | | Square side lenght (in pixels) }" + "{ml | | Marker side lenght (in pixels) }" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{m | | Margins size (in pixels). Default is (squareLength-markerLength) }" + "{bb | 1 | Number of bits in marker borders }" + "{si | false | show generated image }"; } - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; -} - - -/** - */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-w", argc, argv) || !isParam("-h", argc, argv) || !isParam("-sl", argc, argv) || - !isParam("-ml", argc, argv) || !isParam("-d", argc, argv) || !isParam("-o", argc, argv)) { - help(); + if(argc < 7) { + parser.printMessage(); return 0; } - int squaresX = atoi(getParam("-w", argc, argv).c_str()); - int squaresY = atoi(getParam("-h", argc, argv).c_str()); - int squareLength = atoi(getParam("-sl", argc, argv).c_str()); - int markerLength = atoi(getParam("-ml", argc, argv).c_str()); - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); - + int squaresX = parser.get("w"); + int squaresY = parser.get("h"); + int squareLength = parser.get("sl"); + int markerLength = parser.get("ml"); + int dictionaryId = parser.get("d"); int margins = squareLength - markerLength; - if(isParam("-m", argc, argv)) { - margins = atoi(getParam("-m", argc, argv).c_str()); + if(parser.has("m")) { + margins = parser.get("m"); } - int borderBits = 1; - if(isParam("-bb", argc, argv)) { - borderBits = atoi(getParam("-bb", argc, argv).c_str()); + int borderBits = parser.get("bb"); + bool showImage = parser.get("si"); + + String out = parser.get(0); + + if(!parser.check()) { + parser.printErrors(); + return 0; } - bool showImage = false; - if(isParam("-si", argc, argv)) showImage = true; + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); Size imageSize; imageSize.width = squaresX * squareLength + 2 * margins; @@ -135,7 +107,7 @@ int main(int argc, char *argv[]) { waitKey(0); } - imwrite(getParam("-o", argc, argv), boardImage); + imwrite(out, boardImage); return 0; } diff --git a/modules/aruco/samples/create_diamond.cpp b/modules/aruco/samples/create_diamond.cpp index 315c2d704..5cb7f0bda 100644 --- a/modules/aruco/samples/create_diamond.cpp +++ b/modules/aruco/samples/create_diamond.cpp @@ -45,65 +45,50 @@ the use of this software, even if advised of the possibility of such damage. using namespace std; using namespace cv; - -/** - */ -static void help() { - cout << "Create a ChArUco marker image" << endl; - cout << "Parameters: " << endl; - cout << "-o # Output image" << endl; - cout << "-sl # Square side lenght (in pixels)" << endl; - cout << "-ml # Marker side lenght (in pixels)" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "-ids # Four ids for the ChArUco marker" << endl; - cout << "[-m ] # Margins size (in pixels). Default is 0" << endl; - cout << "[-bb ] # Number of bits in marker borders. Default is 1" << endl; - cout << "[-si] # show generated image" << endl; -} - - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; -} - - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; +namespace { +const char* about = "Create a ChArUco marker image"; +const char* keys = + "{@outfile | | Output image }" + "{sl | | Square side lenght (in pixels) }" + "{ml | | Marker side lenght (in pixels) }" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{ids | | Four ids for the ChArUco marker: id1,id2,id3,id4 }" + "{m | 0 | Margins size (in pixels) }" + "{bb | 1 | Number of bits in marker borders }" + "{si | false | show generated image }"; } - /** */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); + + if(argc < 4) { + parser.printMessage(); + return 0; + } - if(!isParam("-sl", argc, argv) || !isParam("-ml", argc, argv) || !isParam("-d", argc, argv) || - !isParam("-ids", argc, argv)) { - help(); + int squareLength = parser.get("sl"); + int markerLength = parser.get("ml"); + int dictionaryId = parser.get("d"); + string idsString = parser.get("ids"); + int margins = parser.get("m"); + int borderBits = parser.get("bb"); + bool showImage = parser.get("si"); + String out = parser.get(0); + + if(!parser.check()) { + parser.printErrors(); return 0; } - int squareLength = atoi(getParam("-sl", argc, argv).c_str()); - int markerLength = atoi(getParam("-ml", argc, argv).c_str()); - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); - string idsString = getParam("-ids", argc, argv); istringstream ss(idsString); vector< string > splittedIds; string token; @@ -111,26 +96,13 @@ int main(int argc, char *argv[]) { splittedIds.push_back(token); if(splittedIds.size() < 4) { cerr << "Incorrect ids format" << endl; - help(); + parser.printMessage(); return 0; } Vec4i ids; for(int i = 0; i < 4; i++) ids[i] = atoi(splittedIds[i].c_str()); - int margins = 0; - if(isParam("-m", argc, argv)) { - margins = atoi(getParam("-m", argc, argv).c_str()); - } - - int borderBits = 1; - if(isParam("-bb", argc, argv)) { - borderBits = atoi(getParam("-bb", argc, argv).c_str()); - } - - bool showImage = false; - if(isParam("-si", argc, argv)) showImage = true; - Mat markerImg; aruco::drawCharucoDiamond(dictionary, ids, squareLength, markerLength, markerImg, margins, borderBits); @@ -140,7 +112,7 @@ int main(int argc, char *argv[]) { waitKey(0); } - imwrite(getParam("-o", argc, argv), markerImg); + imwrite(out, markerImg); return 0; } diff --git a/modules/aruco/samples/create_marker.cpp b/modules/aruco/samples/create_marker.cpp index e83df3e1b..922b23835 100644 --- a/modules/aruco/samples/create_marker.cpp +++ b/modules/aruco/samples/create_marker.cpp @@ -39,78 +39,48 @@ the use of this software, even if advised of the possibility of such damage. #include #include -#include -using namespace std; using namespace cv; - -/** - */ -static void help() { - cout << "Create an ArUco marker image" << endl; - cout << "Parameters: " << endl; - cout << "-o # Output image" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "-id # Marker id in the dictionary" << endl; - cout << "[-ms ] # Marker size in pixels. Default is 200" << endl; - cout << "[-bb ] # Number of bits in marker borders. Default is 1" << endl; - cout << "[-si] # show generated image" << endl; -} - - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; -} - - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; +namespace { +const char* about = "Create an ArUco marker image"; +const char* keys = + "{@outfile | | Output image }" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{id | | Marker id in the dictionary }" + "{ms | 200 | Marker size in pixels }" + "{bb | 1 | Number of bits in marker borders }" + "{si | false | show generated image }"; } -/** - */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-d", argc, argv) || !isParam("-o", argc, argv)) { - help(); + if(argc < 4) { + parser.printMessage(); return 0; } - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); + int dictionaryId = parser.get("d"); + int markerId = parser.get("id"); + int borderBits = parser.get("bb"); + int markerSize = parser.get("ms"); + bool showImage = parser.get("si"); - int markerId = atoi(getParam("-id", argc, argv).c_str()); - - int borderBits = 1; - if(isParam("-bb", argc, argv)) { - borderBits = atoi(getParam("-bb", argc, argv).c_str()); - } + String out = parser.get(0); - int markerSize = 200; - if(isParam("-ms", argc, argv)) { - markerSize = atoi(getParam("-ms", argc, argv).c_str()); + if(!parser.check()) { + parser.printErrors(); + return 0; } - bool showImage = false; - if(isParam("-si", argc, argv)) showImage = true; + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); Mat markerImg; aruco::drawMarker(dictionary, markerId, markerSize, markerImg, borderBits); @@ -120,7 +90,7 @@ int main(int argc, char *argv[]) { waitKey(0); } - imwrite(getParam("-o", argc, argv), markerImg); + imwrite(out, markerImg); return 0; } diff --git a/modules/aruco/samples/detect_board.cpp b/modules/aruco/samples/detect_board.cpp index 73c9da436..534bd3c8c 100644 --- a/modules/aruco/samples/detect_board.cpp +++ b/modules/aruco/samples/detect_board.cpp @@ -45,52 +45,25 @@ the use of this software, even if advised of the possibility of such damage. using namespace std; using namespace cv; - -/** - */ -static void help() { - cout << "Pose estimation using a ArUco Planar Grid board" << endl; - cout << "Parameters: " << endl; - cout << "-w # Number of markers in X direction" << endl; - cout << "-h # Number of markers in Y direction" << endl; - cout << "-l # Marker side lenght (in meters)" << endl; - cout << "-s # Separation between two consecutive" - << "markers in the grid (in meters)" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "-c # Camera intrinsic parameters file" << endl; - cout << "[-v ] # Input from video file, if ommited, input comes from camera" << endl; - cout << "[-ci ] # Camera id if input doesnt come from video (-v). Default is 0" << endl; - cout << "[-dp ] # File of marker detector parameters" << endl; - cout << "[-rs] # Apply refind strategy" << endl; - cout << "[-r] # show rejected candidates too" << endl; -} - - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; +namespace { +const char* about = "Pose estimation using a ArUco Planar Grid board"; +const char* keys = + "{w | | Number of squares in X direction }" + "{h | | Number of squares in Y direction }" + "{l | | Marker side lenght (in pixels) }" + "{s | | Separation between two consecutive markers in the grid (in pixels)}" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{c | | Output file with calibrated camera parameters }" + "{v | | Input from video file, if ommited, input comes from camera }" + "{ci | 0 | Camera id if input doesnt come from video (-v) }" + "{dp | | File of marker detector parameters }" + "{rs | | Apply refind strategy }" + "{r | | show rejected candidates too }"; } - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; -} - - /** */ static bool readCameraParameters(string filename, Mat &camMatrix, Mat &distCoeffs) { @@ -137,27 +110,27 @@ static bool readDetectorParameters(string filename, aruco::DetectorParameters &p /** */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-w", argc, argv) || !isParam("-h", argc, argv) || !isParam("-l", argc, argv) || - !isParam("-s", argc, argv) || !isParam("-d", argc, argv) || !isParam("-c", argc, argv)) { - help(); + if(argc < 7) { + parser.printMessage(); return 0; } - int markersX = atoi(getParam("-w", argc, argv).c_str()); - int markersY = atoi(getParam("-h", argc, argv).c_str()); - float markerLength = (float)atof(getParam("-l", argc, argv).c_str()); - float markerSeparation = (float)atof(getParam("-s", argc, argv).c_str()); - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); + int markersX = parser.get("w"); + int markersY = parser.get("h"); + float markerLength = parser.get("l"); + float markerSeparation = parser.get("s"); + int dictionaryId = parser.get("d"); + bool showRejected = parser.has("r"); + bool refindStrategy = parser.has("rs"); + int camId = parser.get("ci"); - bool showRejected = false; - if(isParam("-r", argc, argv)) showRejected = true; Mat camMatrix, distCoeffs; - if(isParam("-c", argc, argv)) { - bool readOk = readCameraParameters(getParam("-c", argc, argv), camMatrix, distCoeffs); + if(parser.has("c")) { + bool readOk = readCameraParameters(parser.get("c"), camMatrix, distCoeffs); if(!readOk) { cerr << "Invalid camera file" << endl; return 0; @@ -165,8 +138,8 @@ int main(int argc, char *argv[]) { } aruco::DetectorParameters detectorParams; - if(isParam("-dp", argc, argv)) { - bool readOk = readDetectorParameters(getParam("-dp", argc, argv), detectorParams); + if(parser.has("dp")) { + bool readOk = readDetectorParameters(parser.get("dp"), detectorParams); if(!readOk) { cerr << "Invalid detector parameters file" << endl; return 0; @@ -174,17 +147,25 @@ int main(int argc, char *argv[]) { } detectorParams.doCornerRefinement = true; // do corner refinement in markers - bool refindStrategy = false; - if(isParam("-rs", argc, argv)) refindStrategy = true; + String video; + if(parser.has("v")) { + video = parser.get("v"); + } + + if(!parser.check()) { + parser.printErrors(); + return 0; + } + + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); VideoCapture inputVideo; int waitTime; - if(isParam("-v", argc, argv)) { - inputVideo.open(getParam("-v", argc, argv)); + if(!video.empty()) { + inputVideo.open(video); waitTime = 0; } else { - int camId = 0; - if(isParam("-ci", argc, argv)) camId = atoi(getParam("-ci", argc, argv).c_str()); inputVideo.open(camId); waitTime = 10; } diff --git a/modules/aruco/samples/detect_board_charuco.cpp b/modules/aruco/samples/detect_board_charuco.cpp index 36524bd01..4c72e2627 100644 --- a/modules/aruco/samples/detect_board_charuco.cpp +++ b/modules/aruco/samples/detect_board_charuco.cpp @@ -46,50 +46,25 @@ using namespace std; using namespace cv; -/** - */ -static void help() { - cout << "Pose estimation using a ChArUco board" << endl; - cout << "Parameters: " << endl; - cout << "-w # Number of markers in X direction" << endl; - cout << "-h # Number of squares in Y direction" << endl; - cout << "-sl # Square side lenght (in meters)" << endl; - cout << "-ml # Marker side lenght (in meters)" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "[-c ] # Camera intrinsic parameters file" << endl; - cout << "[-v ] # Input from video file, if ommited, input comes from camera" << endl; - cout << "[-ci ] # Camera id if input doesnt come from video (-v). Default is 0" << endl; - cout << "[-dp ] # File of marker detector parameters" << endl; - cout << "[-rs] # Apply refind strategy" << endl; - cout << "[-r] # show rejected candidates too" << endl; -} - - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; -} - - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; +namespace { +const char* about = "Pose estimation using a ChArUco board"; +const char* keys = + "{w | | Number of squares in X direction }" + "{h | | Number of squares in Y direction }" + "{sl | | Square side lenght (in pixels) }" + "{ml | | Marker side lenght (in pixels) }" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{c | | Output file with calibrated camera parameters }" + "{v | | Input from video file, if ommited, input comes from camera }" + "{ci | 0 | Camera id if input doesnt come from video (-v) }" + "{dp | | File of marker detector parameters }" + "{rs | | Apply refind strategy }" + "{r | | show rejected candidates too }"; } - /** */ static bool readCameraParameters(string filename, Mat &camMatrix, Mat &distCoeffs) { @@ -135,27 +110,31 @@ static bool readDetectorParameters(string filename, aruco::DetectorParameters &p /** */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-w", argc, argv) || !isParam("-h", argc, argv) || !isParam("-sl", argc, argv) || - !isParam("-ml", argc, argv) || !isParam("-d", argc, argv)) { - help(); + if(argc < 6) { + parser.printMessage(); return 0; } - int squaresX = atoi(getParam("-w", argc, argv).c_str()); - int squaresY = atoi(getParam("-h", argc, argv).c_str()); - float squareLength = (float)atof(getParam("-sl", argc, argv).c_str()); - float markerLength = (float)atof(getParam("-ml", argc, argv).c_str()); - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); - - bool showRejected = false; - if(isParam("-r", argc, argv)) showRejected = true; + int squaresX = parser.get("w"); + int squaresY = parser.get("h"); + float squareLength = parser.get("sl"); + float markerLength = parser.get("ml"); + int dictionaryId = parser.get("d"); + bool showRejected = parser.has("r"); + bool refindStrategy = parser.has("rs"); + int camId = parser.get("ci"); + + String video; + if(parser.has("v")) { + video = parser.get("v"); + } Mat camMatrix, distCoeffs; - if(isParam("-c", argc, argv)) { - bool readOk = readCameraParameters(getParam("-c", argc, argv), camMatrix, distCoeffs); + if(parser.has("c")) { + bool readOk = readCameraParameters(parser.get("c"), camMatrix, distCoeffs); if(!readOk) { cerr << "Invalid camera file" << endl; return 0; @@ -163,25 +142,28 @@ int main(int argc, char *argv[]) { } aruco::DetectorParameters detectorParams; - if(isParam("-dp", argc, argv)) { - bool readOk = readDetectorParameters(getParam("-dp", argc, argv), detectorParams); + if(parser.has("dp")) { + bool readOk = readDetectorParameters(parser.get("dp"), detectorParams); if(!readOk) { cerr << "Invalid detector parameters file" << endl; return 0; } } - bool refindStrategy = false; - if(isParam("-rs", argc, argv)) refindStrategy = true; + if(!parser.check()) { + parser.printErrors(); + return 0; + } + + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); VideoCapture inputVideo; int waitTime; - if(isParam("-v", argc, argv)) { - inputVideo.open(getParam("-v", argc, argv)); + if(!video.empty()) { + inputVideo.open(video); waitTime = 0; } else { - int camId = 0; - if(isParam("-ci", argc, argv)) camId = atoi(getParam("-ci", argc, argv).c_str()); inputVideo.open(camId); waitTime = 10; } @@ -251,7 +233,7 @@ int main(int argc, char *argv[]) { if(interpolatedCorners > 0) { Scalar color; - color = Scalar(0, 0, 255); + color = Scalar(255, 0, 0); aruco::drawDetectedCornersCharuco(imageCopy, charucoCorners, charucoIds, color); } diff --git a/modules/aruco/samples/detect_diamonds.cpp b/modules/aruco/samples/detect_diamonds.cpp index f350b2f6a..d19418ef7 100644 --- a/modules/aruco/samples/detect_diamonds.cpp +++ b/modules/aruco/samples/detect_diamonds.cpp @@ -46,51 +46,26 @@ using namespace std; using namespace cv; -/** - */ -static void help() { - cout << "Detect ChArUco markers" << endl; - cout << "Parameters: " << endl; - cout << "-sl # Square side lenght (in meters)" << endl; - cout << "-ml # Marker side lenght (in meters)" << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "[-c ] # Camera intrinsic parameters file" << endl; - cout << "[-as ] # Automatic scale. The provided number is multiplied by the last " - << "diamond id becoming an indicator of the square length. In this case, the -sl and " - << "-ml are only used to know the relative length relation between " - << "squares and markers" << endl; - cout << "[-v ] # Input from video file, if ommited, input comes from camera" << endl; - cout << "[-ci ] # Camera id if input doesnt come from video (-v). Default is 0" << endl; - cout << "[-dp ] # File of marker detector parameters" << endl; - cout << "[-r] # show rejected candidates too" << endl; -} - - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; -} - - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; +namespace { +const char* about = "Detect ChArUco markers"; +const char* keys = + "{sl | | Square side lenght (in pixels) }" + "{ml | | Marker side lenght (in pixels) }" + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{c | | Output file with calibrated camera parameters }" + "{as | | Automatic scale. The provided number is multiplied by the last" + "diamond id becoming an indicator of the square length. In this case, the -sl and " + "-ml are only used to know the relative length relation between squares and markers }" + "{v | | Input from video file, if ommited, input comes from camera }" + "{ci | 0 | Camera id if input doesnt come from video (-v) }" + "{dp | | File of marker detector parameters }" + "{rs | | Apply refind strategy }" + "{r | | show rejected candidates too }"; } - /** */ static bool readCameraParameters(string filename, Mat &camMatrix, Mat &distCoeffs) { @@ -136,56 +111,61 @@ static bool readDetectorParameters(string filename, aruco::DetectorParameters &p /** */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-sl", argc, argv) || !isParam("-ml", argc, argv) || !isParam("-d", argc, argv)) { - help(); + if(argc < 4) { + parser.printMessage(); return 0; } - float squareLength = (float)atof(getParam("-sl", argc, argv).c_str()); - float markerLength = (float)atof(getParam("-ml", argc, argv).c_str()); - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); - - bool showRejected = false; - if(isParam("-r", argc, argv)) showRejected = true; + float squareLength = parser.get("sl"); + float markerLength = parser.get("ml"); + int dictionaryId = parser.get("d"); + bool showRejected = parser.has("r"); + bool estimatePose = parser.has("c"); + bool autoScale = parser.has("as"); + float autoScaleFactor = autoScale ? parser.get("as") : 1.f; - bool estimatePose = false; - Mat camMatrix, distCoeffs; - if(isParam("-c", argc, argv)) { - bool readOk = readCameraParameters(getParam("-c", argc, argv), camMatrix, distCoeffs); + aruco::DetectorParameters detectorParams; + if(parser.has("dp")) { + bool readOk = readDetectorParameters(parser.get("dp"), detectorParams); if(!readOk) { - cerr << "Invalid camera file" << endl; + cerr << "Invalid detector parameters file" << endl; return 0; } - estimatePose = true; } - bool autoScale = false; - float autoScaleFactor = 1.; - if(isParam("-as", argc, argv)) { - autoScaleFactor = (float)atof(getParam("-as", argc, argv).c_str()); - autoScale = true; + int camId = parser.get("ci"); + String video; + + if(parser.has("v")) { + video = parser.get("v"); } - aruco::DetectorParameters detectorParams; - if(isParam("-dp", argc, argv)) { - bool readOk = readDetectorParameters(getParam("-dp", argc, argv), detectorParams); + if(!parser.check()) { + parser.printErrors(); + return 0; + } + + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); + + Mat camMatrix, distCoeffs; + if(estimatePose) { + bool readOk = readCameraParameters(parser.get("c"), camMatrix, distCoeffs); if(!readOk) { - cerr << "Invalid detector parameters file" << endl; + cerr << "Invalid camera file" << endl; return 0; } } VideoCapture inputVideo; int waitTime; - if(isParam("-v", argc, argv)) { - inputVideo.open(getParam("-v", argc, argv)); + if(!video.empty()) { + inputVideo.open(video); waitTime = 0; } else { - int camId = 0; - if(isParam("-ci", argc, argv)) camId = atoi(getParam("-ci", argc, argv).c_str()); inputVideo.open(camId); waitTime = 10; } @@ -224,7 +204,7 @@ int main(int argc, char *argv[]) { for(unsigned int i = 0; i < diamondCorners.size(); i++) { float autoSquareLength = autoScaleFactor * float(diamondIds[i].val[3]); vector< vector< Point2f > > currentCorners; - vector< Mat > currentRvec, currentTvec; + vector< Vec3d > currentRvec, currentTvec; currentCorners.push_back(diamondCorners[i]); aruco::estimatePoseSingleMarkers(currentCorners, autoSquareLength, camMatrix, distCoeffs, currentRvec, currentTvec); diff --git a/modules/aruco/samples/detect_markers.cpp b/modules/aruco/samples/detect_markers.cpp index ffae81312..02882bffa 100644 --- a/modules/aruco/samples/detect_markers.cpp +++ b/modules/aruco/samples/detect_markers.cpp @@ -39,56 +39,26 @@ the use of this software, even if advised of the possibility of such damage. #include #include -#include #include using namespace std; using namespace cv; - -/** - */ -static void help() { - cout << "Basic marker detection" << endl; - cout << "Parameters: " << endl; - cout << "-d # DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, " - << "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " - << "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," - << "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16" << endl; - cout << "[-v ] # Input from video file, if ommited, input comes from camera" << endl; - cout << "[-ci ] # Camera id if input doesnt come from video (-v). Default is 0" << endl; - cout << "[-c ] # Camera intrinsic parameters. Needed for camera pose" << endl; - cout << "[-l ] # Marker side lenght (in meters). Needed for correct" - << "scale in camera pose, default 0.1" << endl; - cout << "[-dp ] # File of marker detector parameters" << endl; - cout << "[-r] # show rejected candidates too" << endl; -} - - - -/** - */ -static bool isParam(string param, int argc, char **argv) { - for(int i = 0; i < argc; i++) - if(string(argv[i]) == param) return true; - return false; +namespace { +const char* about = "Basic marker detection"; +const char* keys = + "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2," + "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, " + "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12," + "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}" + "{v | | Input from video file, if ommited, input comes from camera }" + "{ci | 0 | Camera id if input doesnt come from video (-v) }" + "{c | | Camera intrinsic parameters. Needed for camera pose }" + "{l | 0.1 | Marker side lenght (in meters). Needed for correct scale in camera pose }" + "{dp | | File of marker detector parameters }" + "{r | | show rejected candidates too }"; } - -/** - */ -static string getParam(string param, int argc, char **argv, string defvalue = "") { - int idx = -1; - for(int i = 0; i < argc && idx == -1; i++) - if(string(argv[i]) == param) idx = i; - if(idx == -1 || (idx + 1) >= argc) - return defvalue; - else - return argv[idx + 1]; -} - - - /** */ static bool readCameraParameters(string filename, Mat &camMatrix, Mat &distCoeffs) { @@ -136,49 +106,59 @@ static bool readDetectorParameters(string filename, aruco::DetectorParameters &p /** */ int main(int argc, char *argv[]) { + CommandLineParser parser(argc, argv, keys); + parser.about(about); - if(!isParam("-d", argc, argv)) { - help(); + if(argc < 2) { + parser.printMessage(); return 0; } - int dictionaryId = atoi(getParam("-d", argc, argv).c_str()); - aruco::Dictionary dictionary = - aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); - - bool showRejected = false; - if(isParam("-r", argc, argv)) showRejected = true; + int dictionaryId = parser.get("d"); + bool showRejected = parser.has("r"); + bool estimatePose = parser.has("c"); + float markerLength = parser.get("l"); - bool estimatePose = false; - Mat camMatrix, distCoeffs; - if(isParam("-c", argc, argv)) { - bool readOk = readCameraParameters(getParam("-c", argc, argv), camMatrix, distCoeffs); + aruco::DetectorParameters detectorParams; + if(parser.has("dp")) { + bool readOk = readDetectorParameters(parser.get("dp"), detectorParams); if(!readOk) { - cerr << "Invalid camera file" << endl; + cerr << "Invalid detector parameters file" << endl; return 0; } - estimatePose = true; } - float markerLength = (float)atof(getParam("-l", argc, argv, "0.1").c_str()); + detectorParams.doCornerRefinement = true; // do corner refinement in markers + + int camId = parser.get("ci"); + + String video; + if(parser.has("v")) { + video = parser.get("v"); + } - aruco::DetectorParameters detectorParams; - if(isParam("-dp", argc, argv)) { - bool readOk = readDetectorParameters(getParam("-dp", argc, argv), detectorParams); + if(!parser.check()) { + parser.printErrors(); + return 0; + } + + aruco::Dictionary dictionary = + aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId)); + + Mat camMatrix, distCoeffs; + if(estimatePose) { + bool readOk = readCameraParameters(parser.get("c"), camMatrix, distCoeffs); if(!readOk) { - cerr << "Invalid detector parameters file" << endl; + cerr << "Invalid camera file" << endl; return 0; } } - detectorParams.doCornerRefinement = true; // do corner refinement in markers VideoCapture inputVideo; int waitTime; - if(isParam("-v", argc, argv)) { - inputVideo.open(getParam("-v", argc, argv)); + if(!video.empty()) { + inputVideo.open(video); waitTime = 0; } else { - int camId = 0; - if(isParam("-ci", argc, argv)) camId = atoi(getParam("-ci", argc, argv).c_str()); inputVideo.open(camId); waitTime = 10; }