videocapture_starter.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @file videocapture_starter.cpp
  3. * @brief A starter sample for using OpenCV VideoCapture with capture devices, video files or image sequences
  4. * easy as CV_PI right?
  5. *
  6. * Created on: Nov 23, 2010
  7. * Author: Ethan Rublee
  8. *
  9. * Modified on: April 17, 2013
  10. * Author: Kevin Hughes
  11. */
  12. #include <opencv2/imgcodecs.hpp>
  13. #include <opencv2/videoio.hpp>
  14. #include <opencv2/highgui.hpp>
  15. #include <iostream>
  16. #include <stdio.h>
  17. using namespace cv;
  18. using namespace std;
  19. //hide the local functions in an anon namespace
  20. namespace {
  21. void help(char** av) {
  22. cout << "The program captures frames from a video file, image sequence (01.jpg, 02.jpg ... 10.jpg) or camera connected to your computer." << endl
  23. << "Usage:\n" << av[0] << " <video file, image sequence or device number>" << endl
  24. << "q,Q,esc -- quit" << endl
  25. << "space -- save frame" << endl << endl
  26. << "\tTo capture from a camera pass the device number. To find the device number, try ls /dev/video*" << endl
  27. << "\texample: " << av[0] << " 0" << endl
  28. << "\tYou may also pass a video file instead of a device number" << endl
  29. << "\texample: " << av[0] << " video.avi" << endl
  30. << "\tYou can also pass the path to an image sequence and OpenCV will treat the sequence just like a video." << endl
  31. << "\texample: " << av[0] << " right%%02d.jpg" << endl;
  32. }
  33. int process(VideoCapture& capture) {
  34. int n = 0;
  35. char filename[200];
  36. string window_name = "video | q or esc to quit";
  37. cout << "press space to save a picture. q or esc to quit" << endl;
  38. namedWindow(window_name, WINDOW_KEEPRATIO); //resizable window;
  39. Mat frame;
  40. for (;;) {
  41. capture >> frame;
  42. if (frame.empty())
  43. break;
  44. imshow(window_name, frame);
  45. char key = (char)waitKey(30); //delay N millis, usually long enough to display and capture input
  46. switch (key) {
  47. case 'q':
  48. case 'Q':
  49. case 27: //escape key
  50. return 0;
  51. case ' ': //Save an image
  52. sprintf(filename,"filename%.3d.jpg",n++);
  53. imwrite(filename,frame);
  54. cout << "Saved " << filename << endl;
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. return 0;
  61. }
  62. }
  63. int main(int ac, char** av) {
  64. cv::CommandLineParser parser(ac, av, "{help h||}{@input||}");
  65. if (parser.has("help"))
  66. {
  67. help(av);
  68. return 0;
  69. }
  70. std::string arg = parser.get<std::string>("@input");
  71. if (arg.empty()) {
  72. help(av);
  73. return 1;
  74. }
  75. VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file or image sequence
  76. if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
  77. capture.open(atoi(arg.c_str()));
  78. if (!capture.isOpened()) {
  79. cerr << "Failed to open the video device, video file or image sequence!\n" << endl;
  80. help(av);
  81. return 1;
  82. }
  83. return process(capture);
  84. }