当前位置:网站首页>CV2 read video - and save image or video
CV2 read video - and save image or video
2022-07-08 01:52:00 【step step】
title: cv2 Read video , And save images or videos
date: 2022-07-02 18:10:24
tags: opencv
@TOC
cv2 Read video , And save images or videos
cv2 The general process of reading video
- Get video cap = cv2.VideoCapture()
- Judge whether the obtained video is successful , If the video object is successfully read, it returns True. cap.isOpened()
- Read by frame ret, frame = cap.read()
- Show the image cv2.waitKey(1)
Read the local file video and show
from turtle import color
import numpy as np
import cv2 as cv
import cv2
def read_video_show(videoName):
''' opencv Read video cv2.VideoCapture(filename) # Read local video cv2.VideoCapture(index) # Get the camera '''
#1. Read video
cap = cv2.VideoCapture(videoName) # If the parameter is 0, Local camera
#2. Judge whether the read video stream is successful
while cap.isOpened(): # When successful
#3. Get images per frame
ret, frame = cap.read() # If successful ,ret by True, Otherwise False;frame It's an image
if ret: # Successfully obtained image
cv2.imshow('frame', frame) # Two parameters , One is the name of the display , One is pixel content
key = cv2.waitKey(25) # Stop 25ms, When it comes to 0 The jam will not continue in the first frame
if key == ord(' ') or key == ord('q'): # When typing a space or q when , The exit while loop
break
cap.release() # Release video
cv2.destroyAllWindows() # Release all windows that display images
def read_video_save(videoName):
''' opencv Save the video cv2.VideoWriter(filename, fourcc, fps, frameSize, [isColor]) '''
cap = cv2.VideoCapture(videoName)
# Video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # Get the width of the original video
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Get the original video
fps = int(cap.get(cv2.CAP_PROP_FPS)) # Frame rate
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) # Video coding
# Output of video object
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) # Write video
if key == ord('q'):
break
cap.release() # Release video
out.release()
cv2.destroyAllWindows() # Release all display windows
def read_video_write(videoName):
''' Read the video and save the screenshot cv2.imwrite(filename, frame) '''
cap = cv2.VideoCapture(videoName)
# Video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # Get the width of the original video
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Get the original video
fps = int(cap.get(cv2.CAP_PROP_FPS)) # Frame rate
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) # Video coding
n, i = 0, 0 # Total frames , Save the second i A picture
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) # Save to snapshot
cv2.imshow('frame', frame)
key = cv2.waitKey(25)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
边栏推荐
- QT -- package the program -- don't install qt- you can run it directly
- MySQL数据库(2)
- 《ClickHouse原理解析与应用实践》读书笔记(7)
- nacos-微服务网关Gateway组件 +Swagger2接口生成
- 项目经理有必要考NPDP吗?我告诉你答案
- Version 2.0 of tapdata, the open source live data platform, has been released
- ROS problems (topic types do not match, topic datatype/md5sum not match, MSG XXX have changed. rerun cmake)
- Qt - - Packaging Programs - - Don't install Qt - can run directly
- Codeforces Round #649 (Div. 2)——A. XXXXX
- Matlab method is good~
猜你喜欢

城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布

子矩阵的和

I don't know. The real interest rate of Huabai installment is so high

QT build with built-in application framework -- Hello World -- use min GW 32bit

给刚入门或者准备转行网络工程师的朋友一些建议

ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original

Anaconda3 download address Tsinghua University open source software mirror station

Working principle of stm32gpio port

能力贡献 GBASE三大解决方案入选“金融信创生态实验室-金融信创解决方案(第一批)”

Write a pure handwritten QT Hello World
随机推荐
MATLAB R2021b 安装libsvm
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
云原生应用开发之 gRPC 入门
Codeforces Round #649 (Div. 2)——A. XXXXX
Euler Lagrange equation
Voice of users | understanding of gbase 8A database learning
Mouse event - event object
系统测试的类型有哪些,我给你介绍
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Android 创建的sqlite3数据存放位置
快速熟知XML解析
Redission源码解析
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
MySQL数据库(2)
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
C语言-模块化-Clion(静态库,动态库)使用
Version 2.0 of tapdata, the open source live data platform, has been released
Cross modal semantic association alignment retrieval - image text matching
Why does the updated DNS record not take effect?
Anaconda3 download address Tsinghua University open source software mirror station