OpenCV視頻讀寫的代碼,用的是其C++形式的API。輸入文件為完全無壓縮的avi格式視頻文件。
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#undef RC_OPENCV_2_1_0
#ifdef _DEBUG
#ifdef RC_OPENCV_2_1_0
#pragma comment( lib, "cxcore210d.lib" )
#pragma comment( lib, "cv210d.lib" )
#pragma comment( lib, "highgui210d.lib" )
#else
#pragma comment( lib, "opencv_core220d.lib" )
#pragma comment( lib, "opencv_highgui220d.lib" )
#pragma comment( lib, "opencv_imgproc220d.lib" )
#endif
#else
#ifdef RC_OPENCV_2_1_0
#pragma comment( lib, "cxcore210.lib" )
#pragma comment( lib, "cv210.lib" )
#pragma comment( lib, "highgui210.lib" )
#else
#pragma comment( lib, "opencv_core220.lib" )
#pragma comment( lib, "opencv_highgui220.lib" )
#pragma comment( lib, "opencv_imgproc220.lib" )
#endif
#endif
void main()
{
/** 打開輸入視頻文件 */
cv::VideoCapture vc;
vc.open( ".//test.avi" );
if ( vc.isOpened() )
{
/** 打開輸出視頻文件 */
cv::VideoWriter vw;
vw.open( ".//output.avi", // 輸出視頻文件名
(int)vc.get( CV_CAP_PROP_FOURCC ), // 也可設為CV_FOURCC_PROMPT,在運行時選取
(double)vc.get( CV_CAP_PROP_FPS ), // 視頻幀率
cv::Size( (int)vc.get( CV_CAP_PROP_FRAME_WIDTH ), (int)vc.get( CV_CAP_PROP_FRAME_HEIGHT ) ), // 視頻大小
true ); // 是否輸出彩色視頻
/** 如果成功打開輸出視頻文件 */
if ( vw.isOpened() )
{
while ( true )
{
/** 讀取當前視頻幀 */
cv::Mat frame;
vc >> frame;
/** 若視頻讀取完畢,跳出循環 */
if ( frame.empty() )
{
break;
}
/** 將視頻寫入文件 */
vw << frame;
}
}
}
/** 手動釋放視頻捕獲資源 */
vc.release();
}
運行結果
--------------------------------------分割線 --------------------------------------
Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm
Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm
--------------------------------------分割線 --------------------------------------
OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡