OpenCV提供了讀視頻的類和函數,同樣也提供了將多張幀圖像或圖片生成一個視頻的類和函數。
詳細的介紹見:
http://opencv.willowgarage.com/wiki/documentation/cpp/highgui/VideoWriter
http://opencv.willowgarage.com/wiki/VideoCodecs
下面是一段讀視頻、處理視頻幀、寫/保存視頻的簡單示例代碼:
// 讀視頻文件、保存視頻文件
// Author:www.icvpr.com
// Blog: http://blog.csdn.net/icvpr
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
int main(int argc, char** argv)
{
std::string strInputName = "input.avi" ;
std::string strOutputeName = "output.avi" ;
// Input video
cv::VideoCapture capture(strInputName) ;
if( !capture.isOpened() )
{
std::cout<< "open video error happened" << std::endl ;
return -1;
}
// Info of output video
int codec = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC)) ;
int frameRate = capture.get(CV_CAP_PROP_FPS) ;
cv::Size frameSize = cv::Size((int)capture.get(CV_CAP_PROP_FRAME_WIDTH),
(int)capture.get(CV_CAP_PROP_FRAME_HEIGHT)) ;
cv::VideoWriter writer ;
writer.open(strOutputeName, CV_FOURCC('D','I','V','X') , frameRate, frameSize, true) ;
if ( !writer.isOpened())
{
std::cout<< "cannot open the output video file for write" << std::endl ;
return -1 ;
}
// process frame
cv::Mat frame ;
while ( capture.read(frame) )
{
// doSomething
// save frame
writer.write(frame) ;
// display
cv::imshow("video", frame) ;
if ( cv::waitKey(frameRate) > 0) // press any key to exit
{
break ;
}
}
return 0 ;
}
-------------------------------------------------------
< 轉載請註明:http://blog.csdn.net/icvpr >