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; }
