《学习OpenCV》第二章 课后题5

题目叙述:给程序加入滚动条,使得用户可以动态调节缩放比例,缩放比例的取值为2~8之间。可以跳过写入磁盘操作,但是必须将变换结果显示在窗口中。

#include "cv.h"
#include "highgui.h"
#include <stdio.h>

 int pos = 2 ;

// Convert a video to grayscale
// argv[1]: input video file
// argv[2]: name of new output file
//

//#define NOWRITE 1;   //Turn this on (removed the first comment out "//" if you can't write on linux

void ON_Change(int n)  
{   
    //printf("d%",n);
    pos = n;
}  

void main(  ) {
    cvNamedWindow( "Example2_10", CV_WINDOW_AUTOSIZE );
    cvNamedWindow( "Log_Polar", CV_WINDOW_AUTOSIZE );
    CvCapture* capture = cvCreateFileCapture("G:/数据/OpenCV数据/song.mp4" );
    //if (!capture){
    //    return -1;
    //}
    cvCreateTrackbar("scale","Example2_10",&pos,8,ON_Change);//创建滚动条 
    IplImage* bgr_frame;
    double fps = cvGetCaptureProperty (
        capture,
        CV_CAP_PROP_FPS
    );
    printf("fps=%d\n",(int)fps);

    CvSize size = cvSize(
        (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
        (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
    );

    printf("frame (w, h) = (%d, %d)\n",size.width,size.height);
#ifndef NOWRITE
 CvVideoWriter* writer = cvCreateVideoWriter(  
        "G:/数据/OpenCV数据/wind-img2.avi",                               
        CV_FOURCC('M','J','P','G'),    
        fps,
        size
    );
#endif
    IplImage* logpolar_frame = cvCreateImage(
        size,
        IPL_DEPTH_8U,
        3
    );

    IplImage* gray_frame = cvCreateImage(
        size,
        IPL_DEPTH_8U,
        1
    );

    while( (bgr_frame=cvQueryFrame(capture)) != NULL ) {
        cvShowImage( "Example2_10", bgr_frame );
       //We never make use of this gray image
        cvConvertImage(   
            bgr_frame,
            gray_frame,
            CV_RGB2GRAY
        );
        //This is just a fun conversion the mimic's the human visual system
        cvLogPolar( bgr_frame, logpolar_frame,  
                    cvPoint2D32f(bgr_frame->width/2,
                    bgr_frame->height/2), 
                    40, 
                    CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
        IplImage* img2 = cvCreateImage( cvSize( logpolar_frame->width/(pos+1),logpolar_frame->height/(pos+1)), logpolar_frame->depth, logpolar_frame->nChannels);
        cvResize(logpolar_frame, img2, CV_INTER_LINEAR); 
        cvShowImage( "Log_Polar", img2 );

        //Sigh, on linux, depending on your ffmpeg, this often won't work ...
//#ifndef NOWRITE
//       cvWriteToAVI( writer, img2 );
//#endif
        char c = cvWaitKey(10);
        if( c == 27 ) break;
    }
#ifndef NOWRITE
    cvReleaseVideoWriter( &writer );
#endif
    cvReleaseImage( &gray_frame );
    cvReleaseImage( &logpolar_frame );
    cvReleaseCapture( &capture );
}

注:不要将滚动条放在缩放的窗口中,因为效果不好,喜欢的可以试试看!

发布了19 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/windxf/article/details/46356281