大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C++技巧 > Opencv——将摄像头拍摄写成视频文件

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

关键词:视频文件写成摄像头拍摄Opencv  阅读(745) 赞(16)

[摘要]本文是对Opencv——将摄像头拍摄写成视频文件的讲解,对学习C++编程技术有所帮助,与大家分享。

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

 #include"cv.h"
 #include"highgui.h"
 
 int main(int argc, char** argv)
 {
     cvNamedWindow("camera", CV_WINDOW_AUTOSIZE);
     argc = 1;
     CvCapture* capture;
     capture = cvCreateCameraCapture(1);//如果不行换成-1,0,1试试
     assert(capture != NULL);
     IplImage* frame;
     CvSize size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
         (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
 
     double fps = cvGetCaptureProperty(
                 capture,
                 CV_CAP_PROP_FPS//帧率
                 );
     CvVideoWriter* writer = cvCreateVideoWriter(  // On linux Will only work if you've installed ffmpeg development files correctly, 
                 "D:\\Opencv\\testData\\camera.avi",                               // otherwise segmentation fault.  Windows probably better.
                 CV_FOURCC('D', 'X', '5', '0'),
                 15,//摄像头好像帧率获取出来为0,故设为15
                 size
                 );
     IplImage* imgWrite = cvCreateImage(size, 8, 3);
     while (1){
         frame = cvQueryFrame(capture);
         if (!frame)
             break;
         char c = cvWaitKey(10);
             if (c == 27) break;
         cvCopy(frame, imgWrite);
         cvWriteToAVI(writer, imgWrite);
         cvShowImage("camera", frame);
     }
 
     cvReleaseCapture(&capture);
     cvReleaseVideoWriter(&writer);
     cvDestroyWindow("camera");
     return 0;
 }


相关评论