OpenCV将多幅BMP压缩成AVI文件

最近需要同时采集多个摄像头的视频,一般的屏幕录制软件使用不了,只能自己把一幅幅图片保存下来,再转成AVI视频。

OpenCV正好提供了这类函数,所以自己做了一个简单的转换工具。主要利用的函数:

CvVideoWriter* cvCreateVideoWriter(const char* filename, int fourcc, double fps,CvSize frame_size, intis_color=1)

创建一个视频文件Writer

Parameters:
  • filename – Name of the output video file.
  • fourcc – 4-character code of codec used to compress the frames. For example,CV_FOURCC('P','I','M,'1') is a MPEG-1 codec,CV_FOURCC('M','J','P','G') is a motion-jpeg codec etc. Under Win32 it is possible to pass -1 in order to choose compression method and additional compression parameters from dialog. Under Win32 if 0 is passed while using an avi filename it will create a video writer that creates an uncompressed avi file.
  • fps – Framerate of the created video stream.
  • frame_size – Size of the video frames.
  • is_color – If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).

encs[CV_FOURCC('H','F','Y','U')]=(char*)"ffenc_huffyuv";
encs[CV_FOURCC('D','R','A','C')]=(char*)"diracenc";
encs[CV_FOURCC('X','V','I','D')]=(char*)"xvidenc";
encs[CV_FOURCC('X','2','6','4')]=(char*)"x264enc";
encs[CV_FOURCC('M','P','1','V')]=(char*)"mpeg2enc";

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

On Windows HighGui uses Video for Windows (VfW), on Linux ffmpeg is used and on Mac OS X the back end is QuickTime.

 

void cvReleaseVideoWriter(CvVideoWriter** writer)

释放Writer

 

int cvWriteFrame(CvVideoWriter* writer, constIplImage* image)

将读取的图像写入Writer


由于不知道自己电脑上安装的编码器在OpenCV中要如何用CV_FOURCC的宏表示,所以采用-1的方式选择一种编码方式,然后debug进入opencv源代码得到相应的编码器1935959654。不知道有没有什么方式可以直接获取这些对应的编码方式。

// VideoSaver.cpp

#include <stdio.h>

#include <cv.h>   
#include <cvaux.h>   
#include <highgui.h>   

#pragma comment(lib, "ml.lib")   
#pragma comment(lib, "cv.lib")   
#pragma comment(lib, "cvaux.lib")   
#pragma comment(lib, "cvcam.lib")   
#pragma comment(lib, "cxcore.lib")   
#pragma comment(lib, "cxts.lib")   
#pragma comment(lib, "highgui.lib")   
#pragma comment(lib, "cvhaartraining.lib") 

#define VIDEO_FRAME_WIDTH	320
#define VIDEO_FRAME_HEIGHT	240
#define VIDEO_FPS			20

int main()
{
	CvCapture *pLeftCapture = NULL;
	CvCapture *pRightCapture = NULL;

	int lImgWidth, lImgHeight;

	IplImage *pLeftCurImg = NULL;
	IplImage *pRightCurImg = NULL;

	CvVideoWriter *pLeftVideoWriter = NULL;
	CvVideoWriter *pRightVideoWriter = NULL;

	int lFrameIndex = 0;
	int wait_key, wt = 1;
	int lSaveVideo = 0;

	pLeftCapture = cvCreateCameraCapture(0);
	pRightCapture = cvCreateCameraCapture(1);

	if (pLeftCapture == NULL || pRightCapture == NULL)
	{
		printf("can not open camera!\n");
		return -1;
	}

	cvSetCaptureProperty(pLeftCapture, CV_CAP_PROP_FRAME_WIDTH, VIDEO_FRAME_WIDTH);
	cvSetCaptureProperty(pLeftCapture, CV_CAP_PROP_FRAME_HEIGHT, VIDEO_FRAME_HEIGHT);
	cvSetCaptureProperty(pLeftCapture, CV_CAP_PROP_FPS, VIDEO_FPS);

	cvSetCaptureProperty(pRightCapture, CV_CAP_PROP_FRAME_WIDTH, VIDEO_FRAME_WIDTH);
	cvSetCaptureProperty(pRightCapture, CV_CAP_PROP_FRAME_HEIGHT, VIDEO_FRAME_HEIGHT);
	cvSetCaptureProperty(pRightCapture, CV_CAP_PROP_FPS, VIDEO_FPS);

	cvNamedWindow("Left Camera");
	cvNamedWindow("Right Camera");
	cvMoveWindow("Left Camera", 0, 0);
	cvMoveWindow("Right Camera", VIDEO_FRAME_WIDTH+10, 0);


	lFrameIndex = 0;
	for (;;)
	{
		pLeftCurImg = cvQueryFrame(pLeftCapture);
		pRightCurImg = cvQueryFrame(pRightCapture);

		if (pLeftCurImg == NULL || pRightCurImg == NULL)
		{
			break;
		}

		lFrameIndex++;

		lImgWidth = pLeftCurImg->width;
		lImgHeight = pLeftCurImg->height;

		cvShowImage("Left Camera", pLeftCurImg);
		cvShowImage("Right Camera", pRightCurImg);

		if (lSaveVideo)
		{
			if (!pLeftVideoWriter)
			{
				pLeftVideoWriter = cvCreateVideoWriter("LeftCapture.avi", 
					1935959654,//-1,
					VIDEO_FPS, cvSize(lImgWidth, lImgHeight), 1);
			}

			if (!pRightVideoWriter)
			{
				pRightVideoWriter = cvCreateVideoWriter("RightCapture.avi", 
					1935959654,//-1,
					VIDEO_FPS, cvSize(lImgWidth, lImgHeight), 1);
			}

			cvWriteFrame(pLeftVideoWriter, pLeftCurImg);
			cvWriteFrame(pRightVideoWriter, pRightCurImg);
		}

		wait_key = cvWaitKey(wt);
		if (wait_key == 27) // ESC
			break;
		if (wait_key == 's')
		{
			lSaveVideo = 1;
		}
	}

	cvDestroyWindow("Left Camera");
	cvDestroyWindow("Right Camera");
	if (pLeftCapture) cvReleaseCapture(&pLeftCapture);
	pLeftCapture = NULL;
	if (pRightCapture) cvReleaseCapture(&pRightCapture);
	pRightCapture = NULL;

	if (pLeftVideoWriter) cvReleaseVideoWriter(&pLeftVideoWriter);
	pLeftVideoWriter = NULL;
	if (pRightVideoWriter) cvReleaseVideoWriter(&pRightVideoWriter);
	pRightVideoWriter = NULL;

	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值