OpenCV_讀取檔案夾下的圖片產生視頻檔案

來源:互聯網
上載者:User


下面這段代碼實現了:使用OpenCV讀取指定檔案夾下的所有圖片,然後產生avi視頻檔案。

//  讀取檔案夾下的圖片產生視頻檔案//  Author:www.icvpr.com//  Blog:  http://blog.csdn.net/icvpr  #include <iostream>#include <string>#include <io.h>#include <opencv2/opencv.hpp>using namespace std;using namespace cv;int main(int argc, char** argv){// 圖片集string fileFolderPath = "..\\images";string fileExtension = "jpg";string fileFolder = fileFolderPath + "\\*." + fileExtension;// 輸出視頻string outputVideoName = "output.avi";// openCV video writerVideoWriter writer;int codec = 0;int frameRate = 25;Size frameSize;// 遍曆檔案夾char fileName[1000];struct _finddata_t fileInfo;    // 檔案資訊結構體// 1. 第一次尋找long findResult = _findfirst(fileFolder.c_str(), &fileInfo);          if (findResult == -1){_findclose(findResult); return -1;}// 2. 迴圈尋找do {sprintf(fileName, "%s\\%s", fileFolderPath.c_str(), fileInfo.name);if ( fileInfo.attrib == _A_ARCH)  // 是存檔類型檔案{Mat frame;frame = imread(fileName);    // 讀入圖片if (!writer.isOpened()){frameSize.width  = frame.cols;frameSize.height = frame.rows;if (!writer.open(outputVideoName, CV_FOURCC('D','I','V','X') , frameRate, frameSize, true)){cout << "open writer error..." << endl;return -1;}}// 將圖片資料寫入writer.write(frame);// 顯示imshow("video", frame);waitKey(frameRate);}} while (!_findnext(findResult, &fileInfo));  _findclose(findResult); return 0;}

注意:

如果產生的avi視頻的檔案大小為0KB的話, 說明視頻產生失敗,可能的原因是編碼器沒有。下載安裝Xvid轉碼器後,再運行試一下,一般沒有問題。

: http://www.xvidmovies.com/codec/



---------------------------------------------------------
< 轉載請註明:http://blog.csdn.net/icvpr >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.