《学习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 * 完成时间:23:05 3/31 星期日 2013 23 */ 24 25 int g_slider_position = 0; // 记录滚动条位置的全局变量 26 int g_scaling = 2; // 记录缩放比例的全局变量 27 28 void onTrackbarSlide(int pos) 29 { 30 // 改变缩放比例 31 g_scaling = pos + 2; 32 } 33 34 IplImage * doPryDown(IplImage * in, int filter = IPL_GAUSSIAN_5x5) 35 { 36 IplImage * out; 37 38 CvSize newSize = cvSize( (int)( in->width / 2 ),(int) (in->height / 2 ) ); 39 out = cvCreateImage( newSize, in->depth, in->nChannels); 40 cvPyrDown( in, out ); 41 return out; 42 } 43 44 int main() 45 { 46 CvCapture* capture; 47 48 // 选择第一个摄像头 49 capture = cvCreateCameraCapture(0); 50 if(capture == NULL) 51 { 52 printf("Camera error.\texit.\n"); 53 exit(-1); 54 } 55 56 // 创建窗口 57 cvNamedWindow("Camera", 0); 58 59 IplImage *frame = cvQueryFrame(capture); 60 IplImage *frame_pyrdown; 61 62 if (!frame) 63 { 64 return 0; 65 } 66 // 创建滑动条 67 cvCreateTrackbar( "scaling", "Camera", &g_slider_position, 68 6, onTrackbarSlide ); 69 70 while(1) 71 { 72 // 读数据 73 frame = cvQueryFrame(capture); 74 75 if (!frame) 76 { 77 break; 78 } 79 // 缩放变换(2-8之间) 80 frame_pyrdown = doPryDown( frame, 7); 81 for(int i = 2; i < g_scaling; i++) 82 { 83 frame_pyrdown = doPryDown( frame_pyrdown, 7); 84 } 85 86 printf("目前缩放比例: %d\t(%d, %d)\n", g_scaling, 87 frame_pyrdown->width, frame_pyrdown->height); 88 89 // 显示出来 90 cvShowImage("Camera", frame_pyrdown); 91 92 char c = cvWaitKey(50); 93 if (c == 27) 94 { 95 break; 96 } 97 } 98 99 // 释放资源 100 cvReleaseCapture(&capture); 101 cvReleaseImage( &frame); 102 cvReleaseImage( &frame_pyrdown); 103 cvDestroyWindow("Camera"); 104 105 return 0; 106 }
运行结果:
缩放2比例为2:
缩放比例为4:
缩放比例为5:
存在的问题:图像变模糊了,该怎么自适应地改变预览窗口呢?