opencv3.2.0實現視頻抽幀,並保存成圖片


1.實現指定幀數的抽取、和全部幀數的抽取,並保存到指定目錄。
在QT新建一個控制台程序,程序源碼如下:(程序實現每十幀獲取一次幀) #include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream>
using namespace std; using namespace cv; int main() { /******** 獲取視頻文件(實例化的同時進行初始化)*******/ VideoCapture capture("/home/ttwang/out.mp4"); /********** 獲取視頻總幀數並打印*****************/
        long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT); cout << "total frames: " << totalFrameNumber << endl; Mat frame; //定義一個Mat變量,用來存放存儲每一幀圖像
        bool flags = true;     //循環標志位
        long currentFrame = 0; //定義當前幀

        while (flags) { capture.read(frame); // 讀取視頻每一幀
           stringstream str;    //stringstream字符串流,將long類型的轉換成字符型傳給對象str
           str << "f" << currentFrame << ".jpg"; cout << "正在處理第" << currentFrame << "" << endl; /***設置每10幀獲取一次幀***/
           if (currentFrame % 10 == 0) { imwrite("/home/ttwang/images/image" + str.str(), frame); // 將幀轉成圖片輸出
 } /**** 結束條件,當前幀數大於總幀數時候時,循環停止****/
            if (currentFrame >= totalFrameNumber) { flags = false; } currentFrame++; } waitKey(0); return 0; }
2.實現結果如下圖(該段視頻一共有75幀)


免責聲明!

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



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