代码
#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)