如何使用OpenCV Python从视频获取图像

编程入门 行业动态 更新时间:2024-10-27 14:22:07
本文介绍了如何使用OpenCV Python从视频获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从视频中获取图像并将其以'.jpeg'或'.png'格式存储,请帮助我如何使用opencv做到这一点.我的代码是

I want to get image from video and store it in '.jpeg' or '.png' format please help me how to do this with opencv My code is

import cv2 vidcap = cv2.VideoCapture('video1.mp4') success,image = vidcap.read() count = 0; print "I am in success" while success: success,image = vidcap.read() cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file if cv2.waitKey(10) == 27: # exit if Escape is hit break count += 1

在这里,我试图逐帧从视频中获取图像并将其另存为frame1,frame2,frame3

Here i am trying to get the image from video frame by frame and save it as frame1,frame2,frame3

推荐答案

这是我用来读取视频并保存帧的方法:

This is what I use to read in a video and save off the frames:

import cv2 import os def video_to_frames(video, path_output_dir): # extract frames from a video and save to directory as 'x.png' where # x is the frame index vidcap = cv2.VideoCapture(video) count = 0 while vidcap.isOpened(): success, image = vidcap.read() if success: cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, image) count += 1 else: break cv2.destroyAllWindows() vidcap.release() video_to_frames('../somepath/myvid.mp4', '../somepath/out')

更多推荐

如何使用OpenCV Python从视频获取图像

本文发布于:2023-05-26 09:50:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/252277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   图像   视频   Python   OpenCV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!