1. 程式人生 > >Opencv 簡單視頻播放器

Opencv 簡單視頻播放器

mod itl break next fps ble target .get clas

[cpp] view plain copy print?
  1. // C++ header and namespace
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>
  5. using namespace std;
  6. // Opencv header and namespace
  7. #include <opencv2/core/core.hpp>
  8. #include <opencv2/highgui/highgui.hpp>
  9. #include <opencv2/imgproc/imgproc.hpp>
  10. #include <opencv2/video/video.hpp>
  11. using namespace cv;
  12. bool JumpToFrame(false);
  13. int main(int argc, char* argv[])
  14. {
  15. //!< Check out Input video
  16. if (argc != 2)
  17. {
  18. cerr << "Usage: VideoPlayer.exe VideoFilename." << endl;
  19. exit(1);
  20. }
  21. //!< Check out Open Video
  22. VideoCapture capture(argv[1]);
  23. if (!capture.isOpened())
  24. {
  25. return 1;
  26. }
  27. #pragma region InfoOfVideo
  28. long NumberOfFrame = static_cast<long>(capture.get(CV_CAP_PROP_FRAME_COUNT));
  29. double HeightOfFrame = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
  30. double WidthOfFrame = capture.get(CV_CAP_PROP_FRAME_WIDTH);
  31. double FpsOfVideo = capture.get(CV_CAP_PROP_FPS);
  32. cout << "The name of the input video is " << argv[1] << "." << endl;
  33. cout << "NumberOfFrame : " << NumberOfFrame << endl;
  34. cout << "HeightOfFrame : " << HeightOfFrame << endl;
  35. cout << "WidthOfFrame : " << WidthOfFrame << endl;
  36. cout << "FpsOfVieo : " << FpsOfVideo << endl;
  37. #pragma endregion
  38. // !< JumpToFrame function
  39. while (JumpToFrame)
  40. {
  41. double Position = 0.0;
  42. cout << "Please input the number of frame which you want jump to!" << endl;
  43. cin >> Position;
  44. capture.set(CV_CAP_PROP_POS_FRAMES, Position);
  45. }
  46. // !< Delay between each frame in ms corresponds to video frame rate(fps)
  47. Mat frame;
  48. bool stop(false);
  49. int delay = 1000 / FpsOfVideo;
  50. namedWindow("Extracted Frame");
  51. while (!stop)
  52. {
  53. //read next frame if any
  54. if (!capture.read(frame))
  55. {
  56. break;
  57. }
  58. imshow("Extracted Frame", frame);
  59. //introduce a delay or press key to stop
  60. if (waitKey(delay) >= 0)
  61. {
  62. stop = true;
  63. }
  64. }
  65. // !< Close the video file.
  66. // Not required since called by destructor
  67. capture.release();
  68. return 0;
  69. }

Opencv 簡單視頻播放器