当前位置:网站首页>cv2读取视频-并保存图像或视频
cv2读取视频-并保存图像或视频
2022-07-08 00:23:00 【step step】
title: cv2读取视频,并保存图像或视频
date: 2022-07-02 18:10:24
tags: opencv
@TOC
cv2读取视频,并保存图像或视频
cv2读取视频的一般流程
- 获取视频 cap = cv2.VideoCapture()
- 判断获取的视频是否成功,成功读取视频对象则返回True。 cap.isOpened()
- 按帧读取 ret, frame = cap.read()
- 展示图像 cv2.waitKey(1)
读取本地文件视频并展示
from turtle import color
import numpy as np
import cv2 as cv
import cv2
def read_video_show(videoName):
''' opencv 读取视频 cv2.VideoCapture(filename) #读取本地视频 cv2.VideoCapture(index) #获取摄像头 '''
#1. 读取视频
cap = cv2.VideoCapture(videoName) #若参数为0, 则是本地摄像头
#2. 判断读的视频流是否成功
while cap.isOpened(): #当成功时
#3. 获取每帧图像
ret, frame = cap.read() #若获取成功,ret为True,否则为False;frame是图像
if ret: #成功获取图像
cv2.imshow('frame', frame) #两个参数,一个是展示画面的名字,一个是像素内容
key = cv2.waitKey(25) # 停留25ms,当为0的时候则堵塞在第一帧不会继续下去
if key == ord(' ') or key == ord('q'): #当键入空格或者q时,则退出while循环
break
cap.release() #释放视频
cv2.destroyAllWindows() #释放所有显示图像的窗口
def read_video_save(videoName):
''' opencv 保存视频 cv2.VideoWriter(filename, fourcc, fps, frameSize, [isColor]) '''
cap = cv2.VideoCapture(videoName)
#视频属性
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #获取原视频的宽
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #获取原视频的搞
fps = int(cap.get(cv2.CAP_PROP_FPS)) #帧率
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) #视频的编码
#视频对象的输出
out = cv2.VideoWriter('video_output.avi', fourcc, 20.0, (width, height))
# out = cv2.VideoWriter('out.avi', fourcc, 20.0, (width, height))
while cap.isOpened():
ret, frame = cap.read()
cv2.imshow('fame', frame)
key = cv2.waitKey(25)
out.write(frame) #写入视频
if key == ord('q'):
break
cap.release() #释放视频
out.release()
cv2.destroyAllWindows() #释放所有的显示窗口
def read_video_write(videoName):
''' 读取视频并保存截帧 cv2.imwrite(filename, frame) '''
cap = cv2.VideoCapture(videoName)
#视频属性
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #获取原视频的宽
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #获取原视频的搞
fps = int(cap.get(cv2.CAP_PROP_FPS)) #帧率
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) #视频的编码
n, i = 0, 0 #总的帧数,保存的第i张图片
while cap.isOpened():
ret, frame = cap.read()
if ret:
n += 1
if n % fps == 0:
i += 1
filename = '{:0>4}.jpg'.format(str(i))
cv2.imwrite(filename, frame) #存入快照
cv2.imshow('frame', frame)
key = cv2.waitKey(25)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
边栏推荐
- Tencent game client development interview (unity + cocos) double bombing social recruitment 6 rounds of interviews
- The function of carbon brush slip ring in generator
- What kind of MES system is a good system
- Different methods for setting headers of different pages in word (the same for footer and page number)
- Gnuradio operation error: error thread [thread per block [12]: < block OFDM_ cyclic_ prefixer(8)>]: Buffer too small
- 小金额炒股,在手机上开户安全吗?
- The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
- The method of using thread in PowerBuilder
- Dataworks duty table
- Redux usage
猜你喜欢
如何让导电滑环信号更好
Sum of submatrix
Problems of font legend and time scale display of MATLAB drawing coordinate axis
Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos
break net
3. Multi agent reinforcement learning
How to make the conductive slip ring signal better
Why does the updated DNS record not take effect?
Version 2.0 of tapdata, the open source live data platform, has been released
滑环在直驱电机转子的应用领域
随机推荐
How does Matplotlib and PIL image integrate and save multiple pictures into one picture
Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"
About snake equation (1)
How to make enterprise recruitment QR code?
批次管控如何实现?MES系统给您答案
The difference between distribution function and probability density function of random variables
什么样的MES系统才是好系统
正则表达式
After modifying the background of jupyter notebook and adding jupyterthemes, enter 'JT -l' and the error 'JT' is not an internal or external command, nor a runnable program
Mysql database (2)
Grey correlation analysis link (portal) matlab
Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
body有8px的神秘边距
第七章 行为级建模
Get familiar with XML parsing quickly
Redux使用
GBASE观察 | 数据泄露频发 信息系统安全应如何守护
DataWorks值班表
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!