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
#include "tick_meter.hpp"
19
int main(int argc, const char* argv[])
24
const std::string fname(argv[1]);
26
cv::namedWindow("CPU", cv::WINDOW_NORMAL);
27
cv::namedWindow("GPU", cv::WINDOW_OPENGL);
28
cv::cuda::setGlDevice();
31
cv::VideoCapture reader(fname);
33
cv::cuda::GpuMat d_frame;
34
cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
37
std::vector<double> cpu_times;
38
std::vector<double> gpu_times;
42
tm.reset(); tm.start();
43
if (!reader.read(frame))
46
cpu_times.push_back(tm.getTimeMilli());
48
tm.reset(); tm.start();
49
if (!d_reader->nextFrame(d_frame))
52
gpu_times.push_back(tm.getTimeMilli());
54
cv::imshow("CPU", frame);
55
cv::imshow("GPU", d_frame);
57
if (cv::waitKey(3) > 0)
61
if (!cpu_times.empty() && !gpu_times.empty())
63
std::cout << std::endl << "Results:" << std::endl;
65
std::sort(cpu_times.begin(), cpu_times.end());
66
std::sort(gpu_times.begin(), gpu_times.end());
68
double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
69
double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
71
std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
72
std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
82
std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;