GPU 抽帧

#include <iostream>

#include "opencv2/opencv_modules.hpp"

#if defined(HAVE_OPENCV_CUDACODEC)
#include <fstream>
#include <string>
#include <vector>
#include<stdio.h>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/core/opengl.hpp>
#include <opencv2/cudacodec.hpp>
#include <opencv2/highgui.hpp>
// #include <cuda_runtime.h>
// cudaGetDeviceCount(&num_devices);
// cudaSetDevice(cuda_device);

using namespace std;

int main(int argc, const char* argv[])
{
    if (argc != 2)
        return -1;

    const std::string fname(argv[1]);

    cv::namedWindow("GPU", cv::WINDOW_OPENGL);
    cv::cuda::setGlDevice();


    cv::cuda::GpuMat d_frame;
    cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);

    cv::TickMeter tm;
    std::vector<double> gpu_times;
    int i = 0;
    for (;;)
    {
        i++;
        if(i%5!=0)
        {
            continue;
        }
        tm.reset(); tm.start();
        if (!d_reader->nextFrame(d_frame))
            break;
        tm.stop();
        gpu_times.push_back(tm.getTimeMilli());
   //     cv::imshow("GPU", d_frame);

        cv::Mat frame2;
        d_frame.download(frame2);
        stringstream ss;
        ss<<i; 
		string savepath = "images"+ss.str()+".jpg";
        cv::imwrite(savepath, frame2);

        if (cv::waitKey(3) > 0)
            break;
    }

    // if ( !gpu_times.empty())
    // {
    //     std::cout << std::endl << "Results:" << std::endl;

    //     std::sort(gpu_times.begin(), gpu_times.end());

    //     double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();

    //     std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
    // }

    return 0;
}

#else

int main()
{
    std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;
    return 0;
}

#endif

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值