【opencv基础】获取视频帧保存视频或图像帧
前言
对已有的视频进行解帧,得到视频图像帧,便于之后的图像处理。
c++/opencv代码如下:

#include<highgui.h> #include<cv.h> #include<iostream> using namespace std; using namespace cv; int main() { CvCapture* capture = cvCaptureFromFile(".\\input_path\\input_video.mp4"); int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); long nFrame = (long)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT); // 获取总帧数 int width = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH); int height = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); IplImage* image = NULL; char save_path[100]; int i = 0; int start = 1; int end = 1000; if (capture) { while (1) { image = cvQueryFrame(capture); if (image) { i++; cout << "i=" << i << endl; if (i >= start && i <= end) { //cout << "save " << i << " th image..." << endl; sprintf(save_path, ".\\output_path\\frames\\%d.jpg", i); cvSaveImage(save_path, image); } if (i > end) { return 0; } } } } }
注意:
c++代码中的路径必须是反双斜杠!!!
从camera获取视频并保存

/* Copyright(C) 2019 ABC. ALL rights reserved. File : save_video.cpp Brief: save video every 10 minutes. Coder: happy Email: xxx.zzzz@abc.com Date : 20191224 ChLog: */ #include "opencv2/opencv.hpp" #include "opencv2/highgui.hpp" #include "opencv2/core.hpp" #include <iostream> #include <string> #include <time.h> #include <stdio.h> const int video_len = 18000; std::string time_now() { time_t t = time(0); char tmp[64]; strftime(tmp, sizeof(tmp), "%Y%m%dT%H%M%S", localtime(&t)); return tmp; } int main() { // std::string output_dir = "./dms_video/"; std::string output_dir = "../output/"; int camera_id = 0; cv::VideoCapture cap(camera_id); if(!cap.isOpened()) { std::cerr << "ERROR! Unable to open camera.\n"; return -1; } int fps = (int)cap.get(CV_CAP_PROP_FPS); int width = (int)cap.get(CV_CAP_PROP_FRAME_WIDTH); int height = (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT); cv::Size frame_size(width, height); // cv::Size frame_size(640, 480); std::cout << "fps: " << fps << "width: " << width << "height: " << height << std::endl; // double begin = 0.0; // double end = 0.0; // double cost_time = 0.0; while(true) { cv::Mat frame; int cnt = video_len; cv::VideoWriter writer; std::string video_name = output_dir + "TFL_" + time_now() + ".avi"; std::cout << "video_name: " << video_name << std::endl; writer.open(video_name, CV_FOURCC('D', 'I', 'V', '3'), fps, frame_size); while(cnt--) { // begin = (double)cv::getTickCount(); cap >> frame; // end = (double)cv::getTickCount(); // cost_time = (end-begin)*1000 / cv::getTickFrequency(); // printf("capture one frame cost time: %fms...\n", cost_time); if(frame.empty()) { std::cerr << "ERROR! Blank frame grabbed.\n"; break; } // cv::resize(frame, frame, cv::Size(640, 480), 0, 0, CV_INTER_LINEAR); cv::imshow("frame", frame); // begin = (double)cv::getTickCount(); writer << frame; // end = (double)cv::getTickCount(); // cost_time = (end-begin)*1000 / cv::getTickFrequency(); // printf("write one frame cost time: %f ms...\n", cost_time); if(char(cv::waitKey(1)) == 'q') { writer.release(); frame.release(); return 0; } } writer.release(); frame.release(); if(char(cv::waitKey(1)) == 'q') return 0; } if(char(cv::waitKey(1)) == 'q') return 0; return 0; }
从camera获取图像并通过手动保存图像

* Copyright(C) 2019 ABC. ALL rights reserved. File : get_frame.cpp Brief: save frame when click 's'. Coder: happy Email: xxx.zzz@abc.com Date : 20191224 ChLog: */ #include "opencv2/opencv.hpp" #include "opencv2/highgui.hpp" #include "opencv2/core.hpp" #include <iostream> #include <string> #include <time.h> #include <stdio.h> // const int video_len = 180; std::string time_now() { time_t t = time(0); char tmp[64] = {0}; strftime(tmp, sizeof(tmp), "%Y%m%dT%H%M%S", localtime(&t)); return tmp; } int main() { std::string output_dir = "../output/"; int camera_id = 0; cv::VideoCapture cap(camera_id); if(!cap.isOpened()) { std::cerr << "ERROR! Unable to open camera.\n"; return -1; } int fps = (int)cap.get(CV_CAP_PROP_FPS); int width = (int)cap.get(CV_CAP_PROP_FRAME_WIDTH); int height = (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT); cv::Size frame_size(width, height); // cv::Size frame_size(640, 480); std::cout << "fps: " << fps << "width: " << width << "height: " << height << std::endl; int cnt = 0; cv::Mat frame; char strnum[64] = {0}; while(true) { cap >> frame; if(frame.empty()) { std::cerr << "ERROR! Blank frame grabbed.\n"; break; } cv::imshow("frame", frame); if(char(cv::waitKey(1)) == 's') { sprintf(strnum, "%05d_", cnt++); std::string imgname = output_dir + "TFL_" + strnum + time_now() + ".png"; cv::imwrite(imgname, frame); } else if(char(cv::waitKey(1)) == 'q') { frame.release(); cap.release(); return 0; } } cap.release(); return 0; }
CMakeLists.txt

cmake_minimum_required(VERSION 2.8) set(CMAKE_CXX_COMPILER "g++") set(CMAKE_CXX_FLAGS "-std=c++11 -O3 -DNDEBUG -fopenmp -ffast-math -Wall") project(get_video) # set_property(GLOBAL PROPERTY USE_FOLDERS ON) include_directories(include) aux_source_directory(src DIR_SRCS) find_package(OpenCV 3.3.1 REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) add_executable(${PROJECT_NAME} ${DIR_SRCS}) target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) # # build shared lib. # set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib) # list(APPEND CURRENT_INCLUDE ${OpenCV_INCLUDE_DIRS} include) # add_library(${PROJECT_NAME} SHARED ${DIR_SRCS} ${CURRENT_INCLUDE}) message("project_name: ${PROJECT_NAME}") message("DIR_SRCS: ${DIR_SRCS}") message("OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}") message("OpenCV_LIBS: ${OpenCV_LIBS}")
完
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】