opencv 從攝像頭中讀取視頻並保存(c++版)


原文:http://blog.csdn.net/zhongshijunacm/article/details/68947890

 

OpenCV中的視頻操作函數如下表所列:

VideoCapture 
VideoCapture::VideoCapture 
VideoCapture::open 
VideoCapture::isOpened 
VideoCapture::release 
VideoCapture::grab 
VideoCapture::retrieve 
VideoCapture::read 
VideoCapture::get 
VideoCapture::set 
VideoWriter 
VideoWriter::VideoWriter 
ReleaseVideoWriter 
VideoWriter::open 
VideoWriter::isOpened 
VideoWriter::write

感興趣的可以自己查看官方文檔。 
這里主要介紹幾個本文中使用到的函數。

C++: VideoWriter::VideoWriter() 
C++: VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)

Parameters: 
filename – Name of the output video file. 
fourcc – 4-character code of codec used to compress the frames. For example, CV_FOURCC(‘P’,’I’,’M’,’1’) is a MPEG-1 codec, CV_FOURCC(‘M’,’J’,’P’,’G’) is a motion-jpeg codec etc. List of codes can be obtained at Video Codecs by FOURCC page. (如果,這里輸入-1.在windows下會彈出一個對話框讓你選擇視頻解壓格式。) 
fps – Framerate of the created video stream. 
frameSize – Size of the video frames. 
isColor – If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).

    //視頻保存位置 string outputVideoPath = "..\\images\\test.avi"; //打開攝像頭 VideoCapture capture0(0); VideoWriter outputVideo; //獲取當前攝像頭的視頻信息 cv::Size S = cv::Size((int)capture0.get(CV_CAP_PROP_FRAME_WIDTH), (int)capture0.get(CV_CAP_PROP_FRAME_HEIGHT)); //打開視頻路勁,設置基本信息 open函數中你參數跟上面給出的VideoWriter函數是一樣的 outputVideo.open(outputVideoPath, -1, 30.0, S, true); //CV_FOURCC('P','I','M','1') if (!outputVideo.isOpened()) { cout << "fail to open!" << endl; return -1; } cv::Mat frameImage; int count = 0; while(true) { //讀取當前幀 capture0 >> frameImage; if (frameImage.empty()) break; ++count; //輸出當前幀 cv::imshow("output", frameImage); //保存當前幀 outputVideo << frameImage; if (char(waitKey(1)) == 'q') break; } std::cout << "TotalFrame: " << count << std::endl;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM