multi_cameras_calibration.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "opencv2/ccalib/omnidir.hpp"
  2. #include "opencv2/ccalib/multicalib.hpp"
  3. #include "opencv2/ccalib/randpattern.hpp"
  4. using namespace std;
  5. using namespace cv;
  6. const char * usage =
  7. "\n example command line for multi-camera calibration by using random pattern \n"
  8. " multi_cameras_calibration -nc 5 -pw 800 -ph 600 -ct 1 -fe 0 -nm 25 -v 0 multi_camera_omnidir.xml \n"
  9. "\n"
  10. " the file multi_camera_omnidir.xml is generated by imagelist_creator as \n"
  11. " imagelist_creator multi_camera_omnidir.xml *.* \n"
  12. " note the first filename in multi_camera_omnidir.xml is the pattern, the rest are photo names,\n"
  13. " photo names should be in form of cameraIdx-timestamp.*, and cameraIdx starts from 0";
  14. static void help()
  15. {
  16. printf("\n This is a sample for multi-camera calibration, so far it only support random pattern,\n"
  17. "see randomPattern.hpp for detail. Pinhole and omnidirectional cameras are both supported, \n"
  18. "for omnidirectional camera, see omnidir.hpp for detail.\n"
  19. "Usage: mutiCamCalib \n"
  20. " -nc <num_camera> # number of cameras \n"
  21. " -pw <pattern_width> # physical width of random pattern \n"
  22. " -ph <pattern_height> # physical height of random pattern \n"
  23. " -ct <camera_type> # camera type, 0 for pinhole and 1 for omnidirectional \n"
  24. " -fe # whether show feature extraction\n"
  25. " -nm # number of minimal matches of an image \n"
  26. " -v # whether show verbose information \n"
  27. " input_data # text file with pattern file names and a list of photo names, the file is generated by imagelist_creator \n");
  28. printf("\n %s", usage);
  29. }
  30. int main(int argc, char** argv)
  31. {
  32. float patternWidth = 0.0f, patternHeight = 0.0f;
  33. int nCamera = 0, nMiniMatches = 0, cameraType = 0;
  34. const char* outputFilename = "multi-camera-results.xml";
  35. const char* inputFilename = 0;
  36. int showFeatureExtraction = 0, verbose = 0;
  37. if (argc < 2)
  38. {
  39. help();
  40. return 1;
  41. }
  42. for (int i = 1; i < argc; ++i)
  43. {
  44. const char* s = argv[i];
  45. if (strcmp( s, "-nc") == 0)
  46. {
  47. if (sscanf( argv[++i], "%u", &nCamera) != 1 || nCamera <= 0)
  48. {
  49. return fprintf(stderr, "Invalid number of cameras \n"), -1;
  50. }
  51. }
  52. else if ( strcmp( s, "-pw" ) == 0 )
  53. {
  54. if (sscanf( argv[++i], "%f", &patternWidth) != 1 || patternWidth <=0 )
  55. {
  56. return fprintf(stderr, "Invalid pattern width \n"), -1;
  57. }
  58. }
  59. else if ( strcmp( s, "-ph" ) == 0 )
  60. {
  61. if (sscanf( argv[++i], "%f", &patternHeight) != 1 || patternHeight <=0 )
  62. {
  63. return fprintf(stderr, "Invalid pattern height \n"), -1;
  64. }
  65. }
  66. else if ( strcmp( s, "-ct" ) == 0 )
  67. {
  68. if (sscanf( argv[++i], "%u", &cameraType) != 1 || (cameraType !=0 && cameraType !=1 && cameraType !=2) )
  69. {
  70. return fprintf(stderr, "Invalid camera type, 0 for pinhole and 1 for omnidirectional \n"), -1;
  71. }
  72. }
  73. else if ( strcmp( s, "-fe" ) == 0 )
  74. {
  75. if (sscanf( argv[++i], "%u", &showFeatureExtraction) != 1 || (showFeatureExtraction !=1 && showFeatureExtraction !=0) )
  76. {
  77. return fprintf(stderr, "Not bool value, set to 0 or 1 \n"), -1;
  78. }
  79. }
  80. else if ( strcmp( s, "-nm" ) == 0 )
  81. {
  82. if (sscanf( argv[++i], "%u", &nMiniMatches) != 1 || nMiniMatches <=0 )
  83. {
  84. return fprintf(stderr, "Invalid number of minimal matches \n"), -1;
  85. }
  86. }
  87. else if ( strcmp( s, "-v" ) == 0 )
  88. {
  89. if (sscanf( argv[++i], "%u", &verbose) != 1 || (verbose !=1 && verbose !=0) )
  90. {
  91. return fprintf(stderr, "verbose is not bool value, set to 0 or 1 \n"), -1;
  92. }
  93. }
  94. else if( s[0] != '-')
  95. {
  96. inputFilename = s;
  97. }
  98. else
  99. {
  100. return fprintf( stderr, "Unknown option %s\n", s ), -1;
  101. }
  102. }
  103. // do multi-camera calibration
  104. multicalib::MultiCameraCalibration multiCalib(cameraType, nCamera, inputFilename, patternWidth, patternHeight, verbose, showFeatureExtraction, nMiniMatches);
  105. multiCalib.loadImages();
  106. multiCalib.initialize();
  107. multiCalib.optimizeExtrinsics();
  108. // the above three lines can be replaced by multiCalib.run();
  109. multiCalib.writeParameters(outputFilename);
  110. }