OpenCV学习笔记之课后习题练习2-3

3.使用例2-10中的视频捕捉和存储方法,结合例2-5中的doPyrDown()创建一个程序,使其从摄像机读入视频数据并将缩放变换后的彩色图像存入磁盘。

例2-10中所用的方法虽然能正常运行,但却不能生成存储的视频,百思不得其解。遂参考了一篇博文:blog.csdn.net/jacke121/article/details/54607863

利用文中的方法实现了上述功能。代码如下

 1 /*ch2_lx2_3.cpp  2017/11/29
 2   本程序是learning opencv这本书的课后习题练习
 3   欢迎提出问题一起讨论*/
 4 #include "cv.h"
 5 #include "highgui.h"
 6 using namespace std;
 7 IplImage* doPyrDown(
 8     IplImage* in,
 9     int filter = IPL_GAUSSIAN_5x5)
10 {
11 assert(in->width%2 == 0 && in->height%2 == 0);
12 
13  IplImage* out = cvCreateImage( 
14         cvSize( in->width/2, in->height/2 ),
15         in->depth,
16         in->nChannels
17     );
18     cvPyrDown(in,out);
19     return(out);
20 };
21 int main( int argc, char** argv )
22 {
23 cvNamedWindow( "Example2",CV_WINDOW_AUTOSIZE);
24 cvNamedWindow( "Example3",CV_WINDOW_AUTOSIZE);
25 CvCapture* capture = cvCreateFileCapture(argv[1]);
26 IplImage* frame;
27 IplImage* img;
28 CvVideoWriter *video=NULL;
29 int i=0;
30 int n;
31 while(1)
32 {
33   frame = cvQueryFrame( capture );
34   img = doPyrDown( frame );
35   if( !frame ) break;
36 
37   if(i==0)
38     video=cvCreateVideoWriter("camera.avi",CV_FOURCC('X', 'V', 'I', 'D'),25,cvSize(img->width,img->height)); 
39   if(video)
40   {
41             cout<<"video writer has created!"<<endl;
42   }
43   n=cvWriteFrame(video,img);
44   printf("cvWriteFrame %d\n", n);
45   i++;
46         cout<<n<<endl;
47   cvShowImage( "Example2", frame );
48   cvShowImage( "Example3", img );
49   char c = cvWaitKey(33);
50   if( c == 27 ) break;
51 
52     }
53     cvReleaseImage(&frame);
54     cvReleaseImage(&img);
55     cvReleaseVideoWriter(&video);
56     cvReleaseCapture( &capture );
57     cvDestroyWindow( "Example2" );
58     return 0;
59 } 

 

posted @ 2017-11-29 22:31  feifanren  阅读(312)  评论(0编辑  收藏  举报