利用opencv將視訊轉為影象幀
阿新 • • 發佈:2018-12-30
本文做的是基於opencv將視訊幀轉成圖片輸出,由於一個視訊包含的幀數過多,經常我們並不是需要它的全部幀轉成圖片,因此我們希望可以設定每隔多少幀再轉一次圖片(本文設定為5幀),若有人需求是隻需要前多少幀,也可以類似的改寫下程式碼即可。
本文程式碼如下:
#include <iostream> #include "cv.h" #include "opencv2/opencv.hpp" using namespace std; using namespace cv; // 描述:將視訊幀轉成圖片輸出 void main() { // 獲取視訊檔案 VideoCapture cap("C:\\Users\\zdq\\Desktop\\video process\\car.flv"); // 獲取視訊總幀數 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"); // 設定每5幀獲取一次幀 if (currentFrame % 5 == 0) { // 將幀轉成圖片輸出 imwrite("C:\\Users\\zdq\\Desktop\\video process\\pictures" + str.str(), frame); } // 結束條件 if (currentFrame >= totalFrameNumber) { flags = false; } currentFrame++; } system("pause"); }
參考:https://blog.csdn.net/Mr_EvanChen/article/details/77733978