news 2026/6/10 4:54:09

opencv分别用cpu和gpu读取视频

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
opencv分别用cpu和gpu读取视频

代码

#include <iostream> #include "opencv2/opencv_modules.hpp" #include <string> #include <vector> #include <algorithm> #include <numeric> #include <opencv2/core.hpp> #include <opencv2/cudacodec.hpp> #include <opencv2/highgui.hpp> int main(int argc, const char* argv[]) { if (argc != 2) { std::cout << "not specify the input file\n"; return -1; } const std::string fname(argv[1]); cv::Mat frame; cv::VideoCapture reader; // cv::cuda::GpuMat d_frame; // cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname); cv::TickMeter tm; std::vector<double> cpu_times; // std::vector<double> gpu_times; int frnum = 0; reader.open(fname); for (;;) { tm.reset(); tm.start(); if (!reader.read(frame)) break; tm.stop(); cpu_times.push_back(tm.getTimeMilli()); // tm.reset(); tm.start(); // if (!d_reader->nextFrame(d_frame)) // break; // tm.stop(); // gpu_times.push_back(tm.getTimeMilli()); if (frame.empty()) { std::cerr << "Error capturing frame" << std::endl; break; } std::string filename = "./pic/f_" + std::to_string(frnum) + ".png"; if (cv::imwrite(filename, frame)) { std::cout << "Frame written: " << filename << std::endl; } else { std::cerr << "Error writing frame to file: " << filename << std::endl; } frnum++; } reader.release(); // d_reader.release(); // if (!cpu_times.empty() && !gpu_times.empty()) { std::cout << std::endl << "Results:" << std::endl; std::sort(cpu_times.begin(), cpu_times.end()); // std::sort(gpu_times.begin(), gpu_times.end()); double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size(); // double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size(); std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl; // std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl; } return 0; }

makefile

CROSS_COMPILER = CC = $(CROSS_COMPILER)g++ INLCUDE_DIR = -I/usr/local/opencv/include CFLAGS = LDFLAGS = -L/usr/local/opencv/lib -lopencv_cudawarping -lopencv_cudacodec LDFLAGS += -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgcodecs -lopencv_videoio -lopencv_imgproc TARGET := test all: $(TARGET) OBJS = main.o $(TARGET): $(OBJS) $(CC) -o $@ $^ $(LDFLAGS) $(OBJS) : %.o : %.cpp $(CC) $(CFLAGS) $(INLCUDE_DIR) -c $< -o $@ clean: rm -rf $(OBJS) $(TARGET)
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 4:52:05

为什么原生JavaScript是现代化Web开发的终极选择?

为什么原生JavaScript是现代化Web开发的终极选择&#xff1f; 【免费下载链接】vanilla-js-dom Vanilla JS is a fast, lightweight, cross-platform framework for building incredible, powerful JavaScript applications. 项目地址: https://gitcode.com/gh_mirrors/va/va…

作者头像 李华
网站建设 2026/6/10 4:50:06

告别复杂代码!audioMotion-analyzer让音频可视化变得如此简单

告别复杂代码&#xff01;audioMotion-analyzer让音频可视化变得如此简单 【免费下载链接】audioMotion-analyzer High-resolution real-time graphic audio spectrum analyzer JavaScript module with no dependencies. 项目地址: https://gitcode.com/gh_mirrors/au/audioM…

作者头像 李华
网站建设 2026/6/10 4:47:00

Virtualenv Burrito扩展开发:如何定制自己的环境管理工具

Virtualenv Burrito扩展开发&#xff1a;如何定制自己的环境管理工具 【免费下载链接】virtualenv-burrito One command to have a working virtualenv virtualenvwrapper environment. 项目地址: https://gitcode.com/gh_mirrors/vi/virtualenv-burrito 想要快速搭建P…

作者头像 李华
网站建设 2026/6/10 4:40:37

如何构建类型安全的HTML:Swift-HTML的设计哲学与实现原理详解

如何构建类型安全的HTML&#xff1a;Swift-HTML的设计哲学与实现原理详解 【免费下载链接】swift-html &#x1f5fa; A Swift DSL for type-safe, extensible, and transformable HTML documents. 项目地址: https://gitcode.com/gh_mirrors/sw/swift-html 在Swift开发…

作者头像 李华