opencv3.4 VideoCapture读取视频流保存图片,保存视频和视频合并操作

简单的学习了VideoCapture类并进行了如下三种应用:

  1. 从摄像头读取视频流读取视频帧保存为图片
  2. 从摄像头读取视频流读取视频帧保存为视频
  3. 多个视频合成一个视频窗口播放
    具体见代码所示:
/*
1)从摄像头读取视频流
2)读取视频帧保存为图片
3)读取视频帧保存为
*/
int readFromCamera()
{
	VideoCapture capture(0);//0表示打开默认的摄像头,自动探测
	if (!capture.isOpened())
	{
		cout << "open camera failed..\n" << endl;
		return -1;
	}
	//视频流属性
	//VideoCaptureProperties ------> CV_CAP_PROP_XXX
	//想要获取视屏对的属性信息,通过get()方法获取
	double fps = capture.get(CV_CAP_PROP_FPS);//帧信息
	cout << "每秒" << fps << "帧" << endl;

	int frameWidth = capture.get(CAP_PROP_FRAME_WIDTH);
	int frameHeight = capture.get(CAP_PROP_FRAME_HEIGHT);
	cout << frameWidth << "*" << frameHeight << endl;

	/* VideoWriter:
	 The constructors / functions initialize video writers.
	- On Linux FFMPEG is used to write videos;
	-On Windows FFMPEG or VFW is used;
	-On MacOSX QTKit is used.

	VideoWriter(const String& filename, int fourcc, double fps,
			Size frameSize, bool isColor = true);
	*/

	/*
	http://www.fourcc.org/codecs.php 网页列出相应的编码格式
	常用的有:
	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

	*/
	//写入的Size()尺寸要和前面读取到的帧尺寸大小相同,否则无法写入视频,大小一直为0KB或者6KB
	VideoWriter writer("av_1.avi", CV_FOURCC('D', 'I', 'V', 'X'), 25.0, Size(frameWidth, frameHeight), true);//构造函数方式
	//VideoWriter writer;
	//writer.open("av_2.avi", CV_FOURCC('D', 'I', 'V', 'X'), 30.0, Size(frameWidth, frameHeight0), true);//调用open函数方式

	Mat frame, bgmask;
	namedWindow("showVideo", CV_WINDOW_NORMAL);
	//Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
	//pMOG2->apply(frame, bgmask, 0.5);
	int count = 0;
	char imagePath[20];
	while (capture.read(frame))
	{
		imshow("showVideo",frame);
		
		if (count<100)
		{
			sprintf(imagePath, "E://image//img_%03d.jpg", count);
			imwrite(imagePath,frame);//保存前100帧图像
			count++;
		}
		writer.write(frame);//写入视频文件
		char a = waitKey(10);//10毫秒内是否退出,27对应键盘的ESC键是否按下
		if (a == 27)
		{
			break;
		}
	}
	writer.release();//释放资源
	capture.release();//释放资源
	return 0;
}
/*
多个视频合成视频窗口播放
*/
void mergeVideo()
{
	VideoCapture capture1;
	VideoCapture capture2;
	capture1.open("av_1_1.avi");
	capture2.open("av_1_2.avi");
	if (!capture1.isOpened() && !capture2.isOpened())
	{
		cout << "open video1 failed..\n" << endl;
		return;
	}
	Mat frame1, frame2;
	VideoWriter writer("av_3.avi", CV_FOURCC('D', 'I', 'V', 'X'), 30.0, Size(640,420), true);
	while (capture1.read(frame1) && capture2.read(frame2))
	{
		Mat result(Size(640, 420),CV_8UC3,Scalar::all(0));
		resize(frame1, frame1, Size(640, 210));
		resize(frame2, frame2, Size(640, 210));
		Mat roi1 = result(cv::Rect(0,0,640, 210));
		Mat roi2 = result(cv::Rect(0, 210,640,210));

		frame1.copyTo(roi1);
		frame2.copyTo(roi2);
		imshow("result", result);
		writer.write(result);
		char a = waitKey(10);//10毫秒内是否退出,27对应键盘的ESC键是否按下
		if (a == 27)
		{
			break;
		}

	}
	capture1.release();
	capture2.release();
	writer.release();
	waitKey(0);
	
}
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
//#include <opencv2/video/video.hpp>
#include <opencv2/videoio/videoio.hpp>

#include <iostream>

using namespace std;
using namespace cv;

int main()
{	
	//readFromCamera();
	mergeVideo();
	waitKey(0);
	return 0;

}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页