3 #include "opencv2/opencv_modules.hpp"
5 #if defined(HAVE_OPENCV_CUDACODEC)
12 #include <opencv2/core.hpp>
13 #include <opencv2/core/opengl.hpp>
14 #include <opencv2/cudacodec.hpp>
15 #include <opencv2/highgui.hpp>
17 int main(int argc, const char* argv[])
22 const std::string fname(argv[1]);
24 cv::namedWindow("CPU", cv::WINDOW_NORMAL);
25 cv::namedWindow("GPU", cv::WINDOW_OPENGL);
26 cv::cuda::setGlDevice();
29 cv::VideoCapture reader(fname);
31 cv::cuda::GpuMat d_frame;
32 cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
35 std::vector<double> cpu_times;
36 std::vector<double> gpu_times;
40 tm.reset(); tm.start();
41 if (!reader.read(frame))
44 cpu_times.push_back(tm.getTimeMilli());
46 tm.reset(); tm.start();
47 if (!d_reader->nextFrame(d_frame))
50 gpu_times.push_back(tm.getTimeMilli());
52 cv::imshow("CPU", frame);
53 cv::imshow("GPU", d_frame);
55 if (cv::waitKey(3) > 0)
59 if (!cpu_times.empty() && !gpu_times.empty())
61 std::cout << std::endl << "Results:" << std::endl;
63 std::sort(cpu_times.begin(), cpu_times.end());
64 std::sort(gpu_times.begin(), gpu_times.end());
66 double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
67 double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
69 std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
70 std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
80 std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;