opencv 摄像头录取视频保存图像

1,API

CvVideoWriter* cvCreateVideoWriter(const char* filename, int fourcc, double fps, CvSize frame_size, int is_color=1 )
 isColor – 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)

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. 参数为-1时,运行时会弹出一个框叫你选择。

IplImage* cvQueryFrame(CvCapture* capture)
The methods/functions combine VideoCapture::grab() and VideoCapture::retrieve() in one call.
If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.

int cvSaveImage(const char* filename, const CvArr* image, const int* params=0 )
params: Format-specific save parameters encoded as pairs paramId_1, paramValue_1, paramId_2, paramValue_2, 

2,测试代码:

#include "cv.h"
#include "highgui.h"
#include "conio.h"
#include <stdio.h>

using namespace cv;

int main(int argc, char** argv)
{
	IplImage *pSaveimage= NULL;
	char filename[20];
	int   i=0;
	//start camera
	CvCapture *capture = cvCreateCameraCapture(0);
	if(!capture)
	{
		printf("cannot open camera\n");
		system("pause");
		return -1;
	}
	IplImage *frame;
	frame = cvQueryFrame(capture);
	CvSize  size =cvGetSize(frame);
	double fps =10;
	CvVideoWriter *video =  cvCreateVideoWriter("video_camera.avi",CV_FOURCC('M','J','P','G'),fps,size,1);
	cvNamedWindow("haha",1);
	while((frame=cvQueryFrame(capture))!=NULL)
	{
		cvShowImage("haha",frame);
		cvWriteFrame(video,frame);
		printf("one more picture\n");
		//save image
		pSaveimage = cvCreateImage(size,frame->depth,frame->nChannels);
		sprintf(filename ,"%d.jpg",i);
		i++;

		cvResize(frame,pSaveimage,CV_INTER_LINEAR);
		cvSaveImage(filename,pSaveimage);
		char key =cvWaitKey(30);
		if(key==27)  //ESC
			break;

	}
	cvReleaseVideoWriter(&video);
	cvReleaseCapture(&capture);
	cvDestroyAllWindows();
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值