stitching.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "opencv2/imgcodecs.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include "opencv2/stitching.hpp"
  4. #include <iostream>
  5. using namespace std;
  6. using namespace cv;
  7. bool divide_images = false;
  8. Stitcher::Mode mode = Stitcher::PANORAMA;
  9. vector<Mat> imgs;
  10. string result_name = "result.jpg";
  11. void printUsage(char** argv);
  12. int parseCmdArgs(int argc, char** argv);
  13. int main(int argc, char* argv[])
  14. {
  15. int retval = parseCmdArgs(argc, argv);
  16. if (retval) return EXIT_FAILURE;
  17. //![stitching]
  18. Mat pano;
  19. Ptr<Stitcher> stitcher = Stitcher::create(mode);
  20. Stitcher::Status status = stitcher->stitch(imgs, pano);
  21. if (status != Stitcher::OK)
  22. {
  23. cout << "Can't stitch images, error code = " << int(status) << endl;
  24. return EXIT_FAILURE;
  25. }
  26. //![stitching]
  27. imwrite(result_name, pano);
  28. cout << "stitching completed successfully\n" << result_name << " saved!";
  29. return EXIT_SUCCESS;
  30. }
  31. void printUsage(char** argv)
  32. {
  33. cout <<
  34. "Images stitcher.\n\n" << "Usage :\n" << argv[0] <<" [Flags] img1 img2 [...imgN]\n\n"
  35. "Flags:\n"
  36. " --d3\n"
  37. " internally creates three chunks of each image to increase stitching success\n"
  38. " --mode (panorama|scans)\n"
  39. " Determines configuration of stitcher. The default is 'panorama',\n"
  40. " mode suitable for creating photo panoramas. Option 'scans' is suitable\n"
  41. " for stitching materials under affine transformation, such as scans.\n"
  42. " --output <result_img>\n"
  43. " The default is 'result.jpg'.\n\n"
  44. "Example usage :\n" << argv[0] << " --d3 --mode scans img1.jpg img2.jpg\n";
  45. }
  46. int parseCmdArgs(int argc, char** argv)
  47. {
  48. if (argc == 1)
  49. {
  50. printUsage(argv);
  51. return EXIT_FAILURE;
  52. }
  53. for (int i = 1; i < argc; ++i)
  54. {
  55. if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
  56. {
  57. printUsage(argv);
  58. return EXIT_FAILURE;
  59. }
  60. else if (string(argv[i]) == "--d3")
  61. {
  62. divide_images = true;
  63. }
  64. else if (string(argv[i]) == "--output")
  65. {
  66. result_name = argv[i + 1];
  67. i++;
  68. }
  69. else if (string(argv[i]) == "--mode")
  70. {
  71. if (string(argv[i + 1]) == "panorama")
  72. mode = Stitcher::PANORAMA;
  73. else if (string(argv[i + 1]) == "scans")
  74. mode = Stitcher::SCANS;
  75. else
  76. {
  77. cout << "Bad --mode flag value\n";
  78. return EXIT_FAILURE;
  79. }
  80. i++;
  81. }
  82. else
  83. {
  84. Mat img = imread(samples::findFile(argv[i]));
  85. if (img.empty())
  86. {
  87. cout << "Can't read image '" << argv[i] << "'\n";
  88. return EXIT_FAILURE;
  89. }
  90. if (divide_images)
  91. {
  92. Rect rect(0, 0, img.cols / 2, img.rows);
  93. imgs.push_back(img(rect).clone());
  94. rect.x = img.cols / 3;
  95. imgs.push_back(img(rect).clone());
  96. rect.x = img.cols / 2;
  97. imgs.push_back(img(rect).clone());
  98. }
  99. else
  100. imgs.push_back(img);
  101. }
  102. }
  103. return EXIT_SUCCESS;
  104. }