1. 程式人生 > >《隨堂小記》 OPENCV+獲取視訊中某一幀的影象並儲存

《隨堂小記》 OPENCV+獲取視訊中某一幀的影象並儲存

配置:
opencv3.2
VS2017
Windows10

2.程式碼部分

// GetApictureOfVideos.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include "string"
#include <iostream>//io流  cout
#include <opencv2/highgui/highgui.hpp>//新增此句不出錯說明安裝配置成功
#include <time.h>

using namespace std;
using namespace cv;
void Video_to_Image(string
filename) { VideoCapture capture(filename);//獲取VideoCapture 物件 if (!capture.isOpened()) { cout << "open video error"; } //capture.get十分強大 可以獲得幀的很多資訊 int frame_width = (int)capture.get(CAP_PROP_FRAME_WIDTH); int frame_height = (int)capture.get(CAP_PROP_FRAME_HEIGHT); int
frame_number = capture.get(CAP_PROP_FRAME_COUNT); cout << "frame_width is " << frame_width << endl; cout << "frame_height is " << frame_height << endl; cout << "frame_number is " << frame_number << endl; srand((unsigned)time(NULL));#時間點
long frameToStart = rand() % frame_number;#取 最大幀數之內的 隨機數 cout <<"幀開始的地方"<< frameToStart << endl; capture.set(CAP_PROP_POS_FRAMES, frameToStart);//從此時的幀數開始獲取幀 Mat frame; #Mat物件 其實就是影象物件 char image_name[20]; if (!capture.read(frame)) { cout << "讀取視訊失敗" << endl; } imshow("che", frame);//顯示 sprintf(image_name, "%s%s", "image", ".jpg");//儲存的圖片名 imwrite(image_name, frame);#寫入 前面是 path+name不要忘了字尾 後面是 幀 capture.release();#釋放物件 } int main() { string filename = "F:/1.mp4"; Video_to_Image(filename); waitKey(0); return 0; }