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

题目叙述:结合例2-5中的doPyrDown()创建一个程序,使其从摄像机读入视频数据并将缩放变换后的彩色图像存入磁盘。

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

IplImage* doPyrDown(
  IplImage* in
)
{
    // Best to make sure input image is divisible by two.
    //
    assert( in->width%2 == 0 && in->height%2 == 0 );

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

// 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 main(  ) {
    cvNamedWindow( "Example2_10", CV_WINDOW_AUTOSIZE );
    cvNamedWindow( "Log_Polar", CV_WINDOW_AUTOSIZE );
    CvCapture* capture = cvCreateFileCapture("G:/数据/OpenCV数据/song.mp4" );
    //if (!capture){
    //    return -1;
    //}
    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)
    );

    CvSize size2 = cvSize(
        (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH)/2,
        (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)/2
    );

    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,
        size2
    );
#endif
    IplImage* logpolar_frame = cvCreateImage(
        size,
        IPL_DEPTH_8U,
        3
    );

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

    IplImage* img2 = cvCreateImage( cvSize( logpolar_frame->width/2,logpolar_frame->height/2 ), logpolar_frame->depth, logpolar_frame->nChannels);

    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 );
        img2 = doPyrDown( logpolar_frame );
        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/46347049