当前位置:网站首页>OpenCV笔记整理【视频处理】
OpenCV笔记整理【视频处理】
2022-08-09 14:20:00 【武尚发的博客】
视频信号由一系列图像组成(帧),帧是以固定的时间间隔从视频中获取的,获取播放帧的速度被称为(帧速率),单位用 “帧/秒” 表示,代表一秒钟取图像的图像数量,英文为“FramePerSecord”,也就是FPS。
1. VideoCapture类:
此类下面常用的函数包括:初始化、打开、帧捕获、释放、属性设置等。
初始化:
捕获对象 = cv2.VideoCapture(index) 如果index传入0则表示初始化第1个摄像头,传入1则表示初始化第2个摄像头,传入-1表示随机。
捕获对象 = cv2.VideoCapture(filename) 直接传入视频文件的路径即可。打开:
返回状态 = cv2.VideoCapture.Open(index或filename) 当摄像头或者视频文件被成功打开返回true
返回状态 = cv2.VideoCapture.isOpened() 判断当前初始化是否成功,返回true或者false。帧捕获:
returnValue,Image = cv2.VideoCapture.read()
returnValue表示捕获状态,成功为true,失败为false。
Image表示捕获到的帧图像,如果没有捕获到返回空。释放:
在不用摄像头时,需要将其对象释放,这一定很重要!!!
cv2.VideoCapture.release()属性获取 / 设置:
returnValue = cv2.VideoCapture.get(propID) 获取视频属性
returnValue = cv2.VideoCapture.set(propID,value) 设置视频属性值
关于propID可以直接传入对应的index,也可以直接写属性值

上代码:
import cv2
capture = cv2.VideoCapture(0) # 初始化笔记本内置摄像头
while (capture.isOpened()): # 判断是否可用
retval, image = capture.read() # 实时读取视频从摄像头
cv2.imshow("Video", image) # 在窗口中显示读取到的视频
key = cv2.waitKey(1) # 窗口的图像刷新时间为1毫秒
if key == 32: # 如果按下空格键
break
print(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) # 获取视频的行分辨率
print(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 获取视频的列分辨率
capture.release() # 关闭笔记本内置摄像头
cv2.destroyAllWindows() # 销毁显示摄像头视频的窗口
运行:
2. VideoWriter类:
此类可以实现保存视频、修改视频、转换视频类型等。。。
obj = cv2.VideoWriter(fileName,fourcc,fps,frameSize)
fileName :保存视频的路径。
fourcc:用四个字符表示的编码格式。
使用 cv2.VideoWriter_fourcc() 指定视频格式。
fps:帧速率。
frameSize:每一帧的大小。
上代码【1】:保存一段摄像头录制的视频到文件。
import cv2
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 初始化默认摄像头
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D') # 确定视频被保存后的编码格式
output = cv2.VideoWriter("output.avi", fourcc, 30, (640, 480)) # 创建VideoWriter类对象
while (capture.isOpened()): # 判断是否可以用
retval, frame = capture.read() # 从摄像头中实时读取视频
if retval == True: # 如果图像不为空
output.write(frame) # 将当前一帧图像写入
cv2.imshow("frame", frame) # 在窗口中显示摄像头视频
key = cv2.waitKey(1) # 窗口的图像刷新时间为1毫秒
if key == 27: # 如果按下Esc键
break
capture.release() # 关闭默认摄像头
output.release() # 释放VideoWriter类对象
cv2.destroyAllWindows() # 销毁显示摄像头视频的窗口
运行:
上代码【2】:将视频文件进行Canny处理并显示
import numpy as np
import cv2
cap = cv2.VideoCapture('viptrain.avi') # 初始化
cap.set(5,10) # 设置视频帧速为10
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('Original',frame)
frame=cv2.Canny(frame,100,200)
cv2.imshow('Canny',frame)
c = cv2.waitKey(1)
if c==27: #ESC键
break
cap.release()
cv2.destroyAllWindows()
运行:
拜了个拜,走过路过点个关注,谢谢。。。
边栏推荐
猜你喜欢
随机推荐
*2-3 Upgraded version of OJ 1164 missile interceptor
shell提取ip地址
Assembly language learning (8)
小程序程序开发怎么做?应以突出功能为主
从零开始Blazor Server(9)--修改Layout
在Word中如何调整编号和文字之间的间距?
同事的接口文档我每次看着就头大,毛病是真的多多多。。。
网站小程序开发有哪些步骤?
从TRPO到PPO(理论分析与数学证明)
spacedesk-notebook, tablet, extended screen-solve the problem that the tablet font is too small
Which applet making software is easy to use?how to choose?
SQL Server查询优化
宁夏等保测评机构有哪些?如何选择?
What are the steps website small program development?
VMWare不使用简易安装,手动安装ISO操作手册
深圳堡垒机厂家有哪些?重点推荐哪家?
Minesweeper game
IK学习笔记(2)——TwoBones IK
Meta 发布 1750 亿聊天机器人,亿万富翁老板小扎被「他」疯狂吐槽!
The title icon favicon.icon on the browser tab









