《学习OpenCV》练习题第二章第三题

  1 #include <highgui.h>
  2 #include <cv.h>
  3 
  4 #pragma comment (lib,"opencv_calib3d231d.lib")
  5 #pragma comment (lib,"opencv_contrib231d.lib")
  6 #pragma comment (lib,"opencv_core231d.lib")
  7 #pragma comment (lib,"opencv_features2d231d.lib")
  8 #pragma comment (lib,"opencv_flann231d.lib")
  9 #pragma comment (lib,"opencv_gpu231d.lib")
 10 #pragma comment (lib,"opencv_haartraining_engined.lib")
 11 #pragma comment (lib,"opencv_highgui231d.lib")
 12 #pragma comment (lib,"opencv_imgproc231d.lib")
 13 #pragma comment (lib,"opencv_legacy231d.lib")
 14 #pragma comment (lib,"opencv_ml231d.lib")
 15 #pragma comment (lib,"opencv_objdetect231d.lib")
 16 #pragma comment (lib,"opencv_ts231d.lib")
 17 #pragma comment (lib,"opencv_video231d.lib")
 18 
 19 /*
 20  *《学习OpenCV》第二章第三题
 21  * 从摄像机读入视频数据,并做缩放变换,将结果存入磁盘。
 22  * 完成时间:22:01 3/31 星期日 2013
 23  */
 24 
 25 IplImage * doPryDown(IplImage * in, int filter = IPL_GAUSSIAN_5x5)
 26 {
 27     IplImage * out; 
 28     if(in->width % 2 == 0 && in->height % 2 == 0)
 29     {
 30         out = cvCreateImage( cvSize( in->width/2, in->height/2 ),
 31             in->depth, in->nChannels);
 32         cvPyrDown( in, out );
 33 
 34         return out;
 35     }
 36     else
 37     {
 38         out = cvCreateImage( cvSize( (in->width+1) / 2, (in->height+1)/2 ),
 39             in->depth, in->nChannels);
 40         cvPyrDown( in, out );
 41 
 42         return out;
 43     }
 44 
 45 }
 46 
 47 
 48 int main()
 49 {
 50     CvCapture* capture;
 51 
 52     // 选择第二个摄像头
 53     capture = cvCreateCameraCapture(0);
 54     if(capture == NULL)
 55     {
 56         printf("Camera error.\texit.\n");
 57         exit(-1);
 58     }
 59 
 60     // 创建窗口
 61     cvNamedWindow("Camera", 0);
 62     
 63     IplImage *frame = cvQueryFrame(capture);
 64     IplImage *frame_pyrdown;
 65 
 66     CvVideoWriter* writer = cvCreateAVIWriter( "video_record.avi",
 67         CV_FOURCC( 'M', 'J', 'P', 'G' ), 10, cvGetSize(frame), 1);
 68     
 69     if (!frame)
 70     {
 71         return 0;
 72     }
 73 
 74     while(1)
 75     {     
 76         // 读数据
 77         frame = cvQueryFrame(capture);
 78                 
 79         if (!frame)
 80         {
 81             break;
 82         }
 83 
 84         // 原始尺寸
 85         printf("original size: {%d %d}\n", frame->width, frame->height);
 86         // 缩放变换
 87         frame_pyrdown = doPryDown( frame, 7 );
 88         // 变换后的尺寸
 89         printf("after pyrdown size: {%d %d}\n", frame_pyrdown->width, frame_pyrdown->height);
 90         // 写入视频文件
 91         cvWriteFrame( writer, frame_pyrdown );
 92     }
 93 
 94     // 释放资源
 95     cvReleaseCapture(&capture);
 96     cvReleaseImage( &frame);
 97     cvReleaseImage( &frame_pyrdown);
 98     cvReleaseVideoWriter( &writer );
 99     cvDestroyWindow("Camera");
100 
101     return 0;
102 }

程序运行结果:

保存在磁盘上的文件:

播放:

原文地址:https://www.cnblogs.com/qdsclove/p/3001882.html