程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Opencv——將攝像頭拍攝寫成視頻文件

Opencv——將攝像頭拍攝寫成視頻文件

編輯:C++入門知識

這裡主要利用了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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved