当前位置:网站首页>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()
边栏推荐
猜你喜欢

用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟

Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos

Gnuradio3.9.4 create OOT module instances

Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"

In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!

Kindle operation: transfer downloaded books and change book cover

Introduction to grpc for cloud native application development

From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run

Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings

C language - modularization -clion (static library, dynamic library) use
随机推荐
如何制作企业招聘二维码?
Gnuradio transmits video and displays it in real time using VLC
QT -- package the program -- don't install qt- you can run it directly
The persistence mode of redis - RDB and AOF persistence mechanisms
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
Dataworks duty table
nacos-微服务网关Gateway组件 +Swagger2接口生成
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
body有8px的神秘边距
Codeforces Round #649 (Div. 2)——A. XXXXX
Codeforces Round #649 (Div. 2)——A. XXXXX
Plot function drawing of MATLAB
Introduction to natural language processing (NLP) based on transformers
About snake equation (2)
Remote Sensing投稿經驗分享
The difference between distribution function and probability density function of random variables
滑环使用如何固定
正则表达式
Grey correlation analysis link (portal) matlab
Redux usage