OpenCV-python 学习笔记 OpenCV视频操作
1. 使用摄像头捕获视频
cv2.VedioCapture()
输入参数为0为“计算机默认摄像头“,1可以更换来源。
例如:
当代码报错时,可以使用 cap.isOpend() 来检查是否成功初始化,若返回值为
True ,说明摄像头正常打开,否则需要使用 cap.open() .
获取视频的参数信息,使用 cap.get(propld) 来获取视频的一些参数信息,
propld 可以是0~18的任何数,每个数代表一个属性。
修改某些值,可以使用 cap.set(propld, value) 来修改,例如 cap.get(3) 和
cap.get(4) 来查看每一帧的宽和高,默认是 640x480 。我们可以使用
r et=cap.set(3,320) 和ret = cap.set(4,240) 来把宽和高改成 320x240 .
2. 从文件中播放视频
cv2.VedioCapture()
import numpy as np
import cv2
cap = cv2.VideoCapture(0) # 计算机摄像头
while (True):
# 逐帧捕捉图像
ret, frame = cap.read()
# our operation on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转换为灰
度图像
# display the resulting frame
cv2.imshow('frame', gray) # 展示当前帧
if cv2.waitKey(1) & 0xFF == ord('q'): # 按q键退出
break
# when everything done , release the capture
cap.release()
cv2.destroyAllWindows() # 关闭所有窗口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
评论0