Opencv——将摄像头拍摄写成视频文件

这里主要利用了Opencv打开摄像头的代码,以及写入视频的函数,只是这里要注意的是摄像头好像没有帧率,在cvCreateVideoWriter,时要自己设置

 1 #include"cv.h"

 2 #include"highgui.h"

 3 

 4 int main(int argc, char** argv)

 5 {

 6     cvNamedWindow("camera", CV_WINDOW_AUTOSIZE);

 7     argc = 1;

 8     CvCapture* capture;

 9     capture = cvCreateCameraCapture(1);//如果不行换成-1,0,1试试

10     assert(capture != NULL);

11     IplImage* frame;

12     CvSize size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),

13         (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));

14 

15     double fps = cvGetCaptureProperty(

16                 capture,

17                 CV_CAP_PROP_FPS//帧率

18                 );

19     CvVideoWriter* writer = cvCreateVideoWriter(  // On linux Will only work if you've installed ffmpeg development files correctly, 

20                 "D:\\Opencv\\testData\\camera.avi",                               // otherwise segmentation fault.  Windows probably better.

21                 CV_FOURCC('D', 'X', '5', '0'),

22                 15,//摄像头好像帧率获取出来为0,故设为15

23                 size

24                 );

25     IplImage* imgWrite = cvCreateImage(size, 8, 3);

26     while (1){

27         frame = cvQueryFrame(capture);

28         if (!frame)

29             break;

30         char c = cvWaitKey(10);

31             if (c == 27) break;

32         cvCopy(frame, imgWrite);

33         cvWriteToAVI(writer, imgWrite);

34         cvShowImage("camera", frame);

35     }

36 

37     cvReleaseCapture(&capture);

38     cvReleaseVideoWriter(&writer);

39     cvDestroyWindow("camera");

40     return 0;

41 }

 

你可能感兴趣的:(opencv)