用OpenCV將視訊分解成單幀圖片
阿新 • • 發佈:2018-11-25
轉自:https://blog.csdn.net/mr_evanchen/article/details/77733978
本文做的是基於opencv將視訊幀轉成圖片輸出,由於一個視訊包含的幀數過多,經常我們並不是需要它的全部幀轉成圖片,因此我們希望可以設定每隔多少幀再轉一次圖片(本文設定為30幀),若有人需求是隻需要前多少幀,也可以類似的改寫下程式碼即可。
#include <iostream> #include "cv.h" #include "opencv2/opencv.hpp" using namespace std; using namespace cv; // 描述:將視訊幀轉成圖片輸出 void main() { // 獲取視訊檔案 VideoCapture cap("J:\\CQH\\DLFR\\lab_face\\video\\DSC_0023.MOV"); // 獲取視訊總幀數 long totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT); cout << "total frames: " << totalFrameNumber << endl; Mat frame; bool flags = true; long currentFrame = 0; while (flags){ // 讀取視訊每一幀 cap.read(frame); stringstream str; str << "cqh" << currentFrame << ".jpg"; cout << "正在處理第" << currentFrame << "幀" << endl; printf("\n"); // 設定每30幀獲取一次幀 if (currentFrame % 30 == 0){ // 將幀轉成圖片輸出 imwrite("J:\\CQH\\DLFR\\lab_face\\videoToImages\\DSC_0023\\" + str.str(), frame); } // 結束條件 if (currentFrame >= totalFrameNumber){ flags = false; } currentFrame++; } system("pause"); }
執行結果如下: