您的位置:首页 > 运维架构

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

2014-11-23 13:12 471 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: