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

来源:互联网 发布:华晨宇 知乎 编辑:程序博客网 时间:2024/04/28 09:00

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 cameraCvCapture *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 imagepSaveimage = 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)  //ESCbreak;}cvReleaseVideoWriter(&video);cvReleaseCapture(&capture);cvDestroyAllWindows();return 0;}


0 0