videocapture_image_sequence.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/videoio.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <iostream>
  5. using namespace cv;
  6. using namespace std;
  7. static void help(char** argv)
  8. {
  9. cout << "\nThis sample shows you how to read a sequence of images using the VideoCapture interface.\n"
  10. << "Usage: " << argv[0] << " <image_mask> (example mask: example_%02d.jpg)\n"
  11. << "Image mask defines the name variation for the input images that have to be read as a sequence. \n"
  12. << "Using the mask example_%02d.jpg will read in images labeled as 'example_00.jpg', 'example_01.jpg', etc."
  13. << endl;
  14. }
  15. int main(int argc, char** argv)
  16. {
  17. help(argv);
  18. cv::CommandLineParser parser(argc, argv, "{@image| ../data/left%02d.jpg |}");
  19. string first_file = parser.get<string>("@image");
  20. if(first_file.empty())
  21. {
  22. return 1;
  23. }
  24. VideoCapture sequence(first_file);
  25. if (!sequence.isOpened())
  26. {
  27. cerr << "Failed to open the image sequence!\n" << endl;
  28. return 1;
  29. }
  30. Mat image;
  31. namedWindow("Image sequence | press ESC to close", 1);
  32. for(;;)
  33. {
  34. // Read in image from sequence
  35. sequence >> image;
  36. // If no image was retrieved -> end of sequence
  37. if(image.empty())
  38. {
  39. cout << "End of Sequence" << endl;
  40. break;
  41. }
  42. imshow("Image sequence | press ESC to close", image);
  43. if(waitKey(500) == 27)
  44. break;
  45. }
  46. return 0;
  47. }