opencv/samples/gpu/video_reader.cpp

72 lines
1.4 KiB
C++
Raw Normal View History

2012-10-17 15:12:04 +08:00
#include <iostream>
#include "opencv2/opencv_modules.hpp"
2013-07-23 18:29:21 +08:00
#if defined(HAVE_OPENCV_CUDACODEC)
2012-10-17 15:12:04 +08:00
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <opencv2/core.hpp>
#include <opencv2/core/opengl.hpp>
2013-07-23 18:29:21 +08:00
#include <opencv2/cudacodec.hpp>
#include <opencv2/highgui.hpp>
2012-10-17 15:12:04 +08:00
int main(int argc, const char* argv[])
{
if (argc != 2)
return -1;
const std::string fname(argv[1]);
cv::namedWindow("CPU", cv::WINDOW_NORMAL);
#if defined(HAVE_OPENGL)
2012-10-17 15:12:04 +08:00
cv::namedWindow("GPU", cv::WINDOW_OPENGL);
2013-08-28 19:45:13 +08:00
cv::cuda::setGlDevice();
#else
cv::namedWindow("GPU", cv::WINDOW_NORMAL);
#endif
2012-10-17 15:12:04 +08:00
cv::TickMeter tm;
2012-10-17 15:12:04 +08:00
cv::Mat frame;
cv::VideoCapture reader(fname);
for (;;)
{
if (!reader.read(frame))
break;
cv::imshow("CPU", frame);
if (cv::waitKey(3) > 0)
break;
}
2012-10-17 15:12:04 +08:00
cv::cuda::GpuMat d_frame;
cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
for (;;)
{
if (!d_reader->nextFrame(d_frame))
2012-10-17 15:12:04 +08:00
break;
#if defined(HAVE_OPENGL)
cv::imshow("GPU", cv::ogl::Texture2D(d_frame));
#else
d_frame.download(frame);
cv::imshow("GPU", frame);
#endif
2012-10-17 15:12:04 +08:00
if (cv::waitKey(3) > 0)
break;
}
return 0;
}
#else
int main()
{
2013-07-24 17:55:18 +08:00
std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;
return 0;
}
#endif