opencv 将视频流转换成帧图像(支持asf,mp4,avi)
生活随笔
收集整理的這篇文章主要介紹了
opencv 将视频流转换成帧图像(支持asf,mp4,avi)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C++版
#include <opencv2/opencv.hpp> #include <iostream>using namespace cv; using namespace std;int main() {//打開視頻路徑VideoCapture cap("G:\\video\\chan4 2020-01-08 11.50.00 - 12.09.26.asf");//獲取視頻總幀數long totalFrameNum = cap.get(CV_CAP_PROP_FRAME_COUNT);Mat frame;bool flags = true; long currentFrame = 0; //當前的幀數while(flags){cap.read(frame);stringstream str;str << "frame" << currentFrame << ".jpg";//30幀取一張if (currentFrame % 30 == 0){//輸出文件imwrite("F:\\pic\\" + str.str(), frame);}//是否讀圖結束if (currentFrame >= totalFrameNum){flags = false;}currentFrame ++;//遞增幀數}}python版:
import cv2 import oscap = cv2.VideoCapture('C:\\Users\\sazass\\Desktop\\classroom_data\\素材2.mp4') out_dir = 'C:\\Users\\sazass\\Desktop\\classroom_data\\images\\source3' skip_num = 5*16 # 跳幀頻率if not os.path.exists(out_dir):os.makedirs(out_dir)count=0frame_count = 0all_frames=[] while(True):ret, frame = cap.read()if ret is False:breakframe_count = frame_count + 1if frame_count % skip_num == 0:print(frame_count)frame_name = str(frame_count) + '.jpg'path = os.path.join(out_dir,frame_name)cv2.imwrite(path,frame)總結
以上是生活随笔為你收集整理的opencv 将视频流转换成帧图像(支持asf,mp4,avi)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蒙氏三段卡---马三段卡
- 下一篇: 小写字母转大写代码HTML,字母大小写转