OpenCV笔记3:视频文件的读取与保存

这里主要是依照《Learning OpenCV》一书的例程修改实现的,其功能是读取2个视频文件,分别在两个窗口中播放,每个窗口都加入一个进度条,可以自行用鼠标控制播放进度。
来源:http://blog.csdn.net/chenyusiyuan/article/details/4640827

 在菜单Project -> Properties -> Configuration Properties -> Linker –> Input 的 additional dependencies中加入 cxcore200.lib cv200.lib highgui200.lib opencv_ffmpeg200.lib 等库。opencv_ffmpeg200.lib 库可以支持多数主流视频文件格式(包括 rm、rmvb、flv 等)。 

由于我是vc6.0+opencv1.0,因此我加入的库为cxcore.lib cv.lib highgui.lib ,加入opencv_ffmpeg200.lib编译通不过,提示LINK : fatal error LNK1104: cannot open file "opencv_ffmpeg200.lib"。不加opencv_ffmpeg200.lib编译编译可以通过,然后进入到dos窗口,输入以下命令:video.exe a.avi b.avi回车即可,这时就可以看到相应的程序运行结果。

源代码:
// video.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"  
#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 );  
  
 // 捕捉视频文件  
 g_capture1 = cvCreateFileCapture( argv[1] );  
 g_capture2 = cvCreateFileCapture( argv[2] );  
  
 // 读取、显示视频文件的帧数  
 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 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FPS );  
 double fps2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FPS );  
 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   
 CvVideoWriter* wrVideo1 = cvCreateVideoWriter(argv[3], CV_FOURCC('M','J','P','G'), fps1, size1);  
 CvVideoWriter* wrVideo2 = cvCreateVideoWriter(argv[4], CV_FOURCC('M','J','P','G'), fps2, size2);  
  
 int frs = 0;  
  
 // 开始播放并保存视频  
 IplImage* frame1;  
 IplImage* frame2;  
   
 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 );  
  
  // 保存:将当前帧写入到目标视频文件  
  
  cvWriteFrame( wrVideo1, frame1 );  
  cvWriteFrame( wrVideo2, frame2 );  
    
  // 若按下 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;  
}  


运行结果:

视频无法正常显示,原因不明


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值