學習opencv 第二章 習題5答案

運行環境vc6.0 OpenCV1.0 OS:win7

//用戶可以通過滾動條動態調節縮放比例,縮放比例的取值爲2—8之間
#include "cv.h"
#include "highgui.h"
#include <iostream.h>

int g_slider_position = 0;
int flag = 0;
int pp = 0;

IplImage* doPyrDown(IplImage* in)
{
   IplImage* out = cvCreateImage( 
        cvSize( in->width/2, in->height/2 ),
        in->depth,
        in->nChannels
    );
    cvPyrDown( in, out, CV_GAUSSIAN_5x5);
    return( out );
};

void onTrackbarSlide(int pos)
{
   flag = 1;
   pp = pos;
   switch(pos){
   case 0:
      cout << "Exercise3_1中視頻畫面保持原始大小——1*1" << endl;
      flag = 0;
      break;
   case 1:
      cout << "Exercise3_1中視頻畫面縮小一半——0.5*0.5" << endl;
      break;
   case 2:
      cout << "Exercise3_1中視頻畫面縮小兩半——0.25*0.25" << endl;
      break;
   case 3:
      cout << "Exercise3_1中視頻畫面縮小三半——0.125*0.125" << endl;
      break;
   }
};

main() {
   CvCapture* capture = cvCreateCameraCapture(-1);
   cvNamedWindow( "Exercise3", CV_WINDOW_AUTOSIZE );
   cvNamedWindow( "Exercise3_1", CV_WINDOW_AUTOSIZE );
   cvCreateTrackbar(
      "Position",
      "Exercise3",
      &g_slider_position,
      3,
      onTrackbarSlide
   );
   IplImage* frame;
   IplImage* frame1 = NULL; 
   cout << "程序啓動,Exercise3_1中視頻畫面爲原始大小!" << endl;
    while(1) {
        frame = cvQueryFrame( capture );
      if( !frame ) break;
      if(flag==0)
         frame1 = frame;
        else
      {
         frame1 = frame;
         int i = pp;
         while(i!=0){
            frame1 = doPyrDown(frame1);
            i--;
         }
         cvFlip( frame1, NULL, 0);
      }
        cvShowImage( "Exercise3", frame );
        cvShowImage( "Exercise3_1", frame1 );
      char c = cvWaitKey(10);
        if( c == 27 ) break;
   }

    cvReleaseImage( &frame1 );
    cvReleaseCapture( &capture );
   cvDestroyWindow( "Exercise3_1" );
   cvDestroyWindow( "Exercise3" );
   return(0);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章