一、图片视频流的读取(调用VideoCapture类)
Class for video capturing from video files, image sequences or cameras. The class provides C++ API for capturing video from cameras or for reading video files and image sequences. (VideoCapture类从视频文件,图像序列或者相机中捕捉帧),示例代码如下:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // 打开摄像头,open the default camera 初始化方法1
VideoCapture cap; // 初始化方法2
capture.open(0);
或者
VideoCapture capture; // open ".mp4" file
capture.open("test.mp4");
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
二、图片视频流的写入(VideoWriter类)
(转载Form链接:https://www.jianshu.com/p/581108baa71e)
用于创建一个可写的视频对象,可以往进写入图像,常用的构造函数如下:VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true);
第一个是视频名字,第二个参数是要选择的编码方式,opencv里有很多宏定义可选,这里我们给-1的话会弹出对话框让我们来选,这样也是可以的,具体宏定义可以参见opencv官方文档。
还可用open函数来初始化一个写入视频对象:bool open(const String& filename, int fourcc, double fps,Size frameSize, bool isColor = true);
和上面的参数意义是一致的,还有其他参数形式的open函数和构造函数,需要用的话参见文档。
最后一个最重要的就是写入了。
视频流一般图像的定义和写入方式为:
VideoWriter videowriter(tObjReconParam.strDstVideo.c_str(), CV_FOURCC('D', 'I', 'V', 'X'), 25.0, Size(640, 480));
for(int i = 0; i < files.size(); i++)
{
path = files[i];
Mat img = imread(path,0);
videowriter << img;
}
下面贴上写的剪视频的一段小代码:
#include<opencv2/core/core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\/imgproc\imgproc.hpp>
#include<iostream>
#include<string>
using namespace std;
using namespace cv;
void Image_To_Video(int frame_start, int frame_end, VideoCapture video);
int main()
{
VideoCapture video("data2.mp4");
Image_To_Video(1000, 3700, video);
return 0;
}
void Image_To_Video(int frame_start,int frame_end,VideoCapture video) //输入应该是起始和结束帧以及原视频对象
{
string video_name = "data_cut.avi"; //视频名字
int hight = video.get(CAP_PROP_FRAME_HEIGHT); //宽和高保持不变
int wed = video.get(CAP_PROP_FRAME_WIDTH);
VideoWriter writer; //
writer = VideoWriter(video_name, -1, video.get(CAP_PROP_FPS), Size(wed,hight));
//写入的对象,保持原来的帧率和大小。帧率也可以取5,10,15,20...
//writer.open(video_name,-1, video.get(CAP_PROP_FPS), Size(wed, hight), true);
Mat image;
for (unsigned i = 0; i < video.get(CAP_PROP_FRAME_COUNT); i++)
{
cout << i << endl;
video.read(image);
if (i >= frame_start&&i < frame_end)
{
imshow("test", image);
waitKey(10);
writer.write(image);
}
if (i == frame_end)
{
cout << "transform task was done!" << endl;
break;
}
}
}
不同的编码方式如下:
CV_FOURCC('P','I','M','1') = MPEG-1 codec
CV_FOURCC('M','J','P','G') = motion-jpeg codec
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
三、两个视频写入对比
两个视频整合成一个视频(实验数据结果对比)
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace cv;
#define WIDTH 640
#define HEIGHT 480
int main()
{
cv::VideoCapture cap1, cap2, cap3, cap4;
VideoWriter videowriter("C:\\Users\\sunlj4\\Desktop\\EstiCompare_A.avi", CV_FOURCC('D', 'I', 'V', 'X'), 5.0, Size(1280, 480));
cap1.open("C:\\Users\\sunlj4\\Desktop\\video_PoseBin_matching17000.avi");
int frame_num = cap1.get(cv::CAP_PROP_FRAME_COUNT);
cap2.open("C:\\Users\\sunlj4\\Desktop\\video_PoseBin_matching17000After.avi");
if (frame_num != cap2.get(cv::CAP_PROP_FRAME_COUNT))
return -1;
Mat result;
for (size_t i = 0; i < frame_num; i++)
{
cv::Mat frame1, frame2;
cap1 >> frame1; cap2 >> frame2;
putText(frame1, "video_PoseBin_matching17000", Point(30, 30), cv::FONT_HERSHEY_COMPLEX,
0.5, cv::Scalar(0, 255, 255), 1, 8, 0);
putText(frame2, "video_PoseBin_matching17000After", Point(30, 30), cv::FONT_HERSHEY_COMPLEX,
0.5, cv::Scalar(0, 255, 255), 1, 8, 0);
hconcat(frame1, frame2, result);
char raw_name[256] = { 0 };
sprintf(raw_name, "%d/%d", i, frame_num);
putText(result, raw_name, Point(620, 240), cv::FONT_HERSHEY_COMPLEX,
0.5, cv::Scalar(0, 255, 255), 1, 8, 0);
imshow("a", result);
waitKey(1);
videowriter << result;
}
return 0;
}