前言
对已有的视频进行解帧,得到视频图像帧,便于之后的图像处理。
c++/opencv代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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获取视频并保存
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* 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获取图像并通过手动保存图像
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
* Copyright(C) 2019 UISEE. 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; }
完