加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

OpenCv视频读取和保存

(2012-07-26 10:30:30)
标签:

视频文件

进度条

回调函数

命名空间

读取

杂谈



#include <iostream>

#include <cv.h>

//#include <cvaux.h>

#include <cxcore.h>

#include <highgui.h>



// 使用标准命名空间

using namespace std;



// 初始化进度条的位置

int g_slider_position1 = 0;

int g_slider_position2 = 0;



CvCapture* g_capture1 = NULL;

CvCapture* g_capture2 = NULL;



// 定义回调函数用于播放进度的控制 

void onTrackbarSlide1( int pos1 )

{

     cvSetCaptureProperty( g_capture1, CV_CAP_PROP_POS_FRAMES, pos1 );

}

void onTrackbarSlide2( int pos2 )

{

        cvSetCaptureProperty( g_capture2, CV_CAP_PROP_POS_FRAMES, pos2 );

}





int main(int argc, char** argv )

{

 // 建立播放窗口

       cvNamedWindow( "Video Test 1", CV_WINDOW_AUTOSIZE );

    cvNamedWindow( "Video Test 2", CV_WINDOW_AUTOSIZE );



    // 捕捉视频文件

       char video1[] = "C:\\TestImages\\MovingCar.avi";

        char video2[] = "C:\\TestImages\\MovingCar2.avi";

       g_capture1 = cvCreateFileCapture( video1 );

     g_capture2 = cvCreateFileCapture( video2 );



     // 读取、显示视频文件的帧数

 int frames1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FRAME_COUNT );

        cout << "frames1 = " << frames1 << endl;

      // 建立进度条

        if( frames1 != 0 )

              cvCreateTrackbar( 

             "Position", 

           "Video Test 1", 

               &g_slider_position1, 

          frames1, 

              onTrackbarSlide1

                );



      int frames2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FRAME_COUNT );

        cout << "frames2 = " << frames2 << endl;

      if( frames2 != 0 )

              cvCreateTrackbar( 

             "Position", 

           "Video Test 2", 

               &g_slider_position2, 

          frames2, 

              onTrackbarSlide2 

              );



      // 读取视频文件信息

     double fps1 = cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FPS );

      double fps2 = cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FPS );

      cout << "fps1 = " << fps1 << endl;

    cout << "fps1 = " << fps2 << endl;

    CvSize size1 = cvSize( 

                (int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_WIDTH),

         (int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_HEIGHT));

       CvSize size2 = cvSize( 

                (int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_WIDTH),

         (int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_HEIGHT));



       // 创建 VideoWriter 

     char out1[] = "C:\\TestImages\\out1.avi";

       char out2[] = "C:\\TestImages\\out2.avi";

       //CvVideoWriter* wrVideo1 = cvCreateVideoWriter(out1, CV_FOURCC('M','J','P','G'), fps1, size1);

 //CvVideoWriter* wrVideo2 = cvCreateVideoWriter(out2, CV_FOURCC('M','J','P','G'), fps2, size2);

 CvVideoWriter* wrVideo1 = cvCreateVideoWriter(out1, CV_FOURCC('X','V','I','D'), fps1, size1);

   CvVideoWriter* wrVideo2 = cvCreateVideoWriter(out2, CV_FOURCC('X','V','I','D'), fps2, size2);



   int frs = 0;



    // 开始播放并保存视频

    IplImage* frame1;

       IplImage* frame2;

       IplImage* gray1 = cvCreateImage(size1, 8, 1);

   IplImage* gray2 = cvCreateImage(size2, 8, 1);



   while( frs < frames1 && frs < frames2 )

   {

               // 获取、显示源文件的帧画面

         frame1 = cvQueryFrame( g_capture1 );

            if( !frame1 ) break;

            cvShowImage( "Video Test 1", frame1 );



          frame2 = cvQueryFrame( g_capture2 );

            if( !frame2 ) break;

            cvShowImage( "Video Test 2", frame2 );



          // 保存:将当前帧写入到目标视频文件

             cvCvtColor(frame1 ,gray1, CV_RGB2GRAY);

         cvCvtColor(frame2 ,gray2, CV_RGB2GRAY);

         cvWriteFrame( wrVideo1, gray1 );

                cvWriteFrame( wrVideo2, gray2 );



                // 若按下 ESC 键,则退出程序

              char c = cvWaitKey(33);

         if( c == 27 ) break;

    }

       // 释放内存,关闭窗口

    cvReleaseCapture( &g_capture1 );

        cvReleaseCapture( &g_capture2 );

        cvReleaseVideoWriter( &wrVideo1 );

      cvReleaseVideoWriter( &wrVideo2 );

      cvDestroyWindow( "Video Test 1" );

      cvDestroyWindow( "Video Test 2" );



      return 0;

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有