《学习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:18 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 int main()
 48 {
 49     CvCapture* capture;
 50 
 51     // 选择第二个摄像头
 52     capture = cvCreateCameraCapture(0);
 53     if(capture == NULL)
 54     {
 55         printf("Camera error.\texit.\n");
 56         exit(-1);
 57     }
 58 
 59     // 创建窗口
 60     cvNamedWindow("Camera", 0);
 61     
 62     IplImage *frame = cvQueryFrame(capture);
 63     IplImage *frame_pyrdown;
 64     
 65     if (!frame)
 66     {
 67         return 0;
 68     }
 69 
 70     while(1)
 71     {     
 72         // 读数据
 73         frame = cvQueryFrame(capture);
 74                 
 75         if (!frame)
 76         {
 77             break;
 78         }
 79 
 80         // 原始尺寸
 81         printf("original size: {%d %d}\n", frame->width, frame->height);
 82         // 缩放变换
 83         frame_pyrdown = doPryDown( frame, 7 );
 84         // 变换后的尺寸
 85         printf("after pyrdown size: {%d %d}\n", frame_pyrdown->width, frame_pyrdown->height);
 86         // 显示出来
 87         cvShowImage("Camera", frame_pyrdown);
 88 
 89         char c = cvWaitKey(50);
 90         if (c == 27)
 91         {
 92             break;
 93         }
 94     }
 95 
 96     // 释放资源
 97     cvReleaseCapture(&capture);
 98     cvReleaseImage( &frame);
 99     cvReleaseImage( &frame_pyrdown);
100     cvDestroyWindow("Camera");
101 
102     return 0;
103 }

运行结果:

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