video_reader.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <iostream>
  2. #include "opencv2/opencv_modules.hpp"
  3. #if defined(HAVE_OPENCV_CUDACODEC)
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <numeric>
  8. #include <opencv2/core.hpp>
  9. #include <opencv2/core/opengl.hpp>
  10. #include <opencv2/cudacodec.hpp>
  11. #include <opencv2/highgui.hpp>
  12. int main(int argc, const char* argv[])
  13. {
  14. if (argc != 2)
  15. return -1;
  16. const std::string fname(argv[1]);
  17. cv::namedWindow("CPU", cv::WINDOW_NORMAL);
  18. #if defined(HAVE_OPENGL)
  19. cv::namedWindow("GPU", cv::WINDOW_OPENGL);
  20. cv::cuda::setGlDevice();
  21. #else
  22. cv::namedWindow("GPU", cv::WINDOW_NORMAL);
  23. #endif
  24. cv::TickMeter tm;
  25. cv::Mat frame;
  26. cv::VideoCapture reader(fname);
  27. for (;;)
  28. {
  29. if (!reader.read(frame))
  30. break;
  31. cv::imshow("CPU", frame);
  32. if (cv::waitKey(3) > 0)
  33. break;
  34. }
  35. cv::cuda::GpuMat d_frame;
  36. cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
  37. for (;;)
  38. {
  39. if (!d_reader->nextFrame(d_frame))
  40. break;
  41. #if defined(HAVE_OPENGL)
  42. cv::imshow("GPU", cv::ogl::Texture2D(d_frame));
  43. #else
  44. d_frame.download(frame);
  45. cv::imshow("GPU", frame);
  46. #endif
  47. if (cv::waitKey(3) > 0)
  48. break;
  49. }
  50. return 0;
  51. }
  52. #else
  53. int main()
  54. {
  55. std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;
  56. return 0;
  57. }
  58. #endif