用摄像头捕获视频
cv2.VideoCapture(设备索引号):创建一个VideoCapture 对象,他的参数可以是设备的索引号,或者是一个视频文件。设备索引号就是在指定要使用的摄像头。 一般的笔记本电脑都有内置摄像头。参数是 0。
cap.read(): 返回一个布尔值(True/False)。如果帧读取的是正确的,就是 True。所以最后你可以通过检查他的返回值来查看视频文件是否已经到 了结尾。
cap.isOpened(): 返回一个布尔值(True/False)。用于检查摄像头是否初始化成功,Ture表示成功,否则要使用cap.open()进行初始化
cap.get(参数):用于获得视频的一些参数信息
cap.set(参数,值):用于设置视频的一些参数,参数值如下
参数名 | 对应值 | 含义 |
CV_CAP_PROP_POS_MSEC | 0 | Current position of the video file in milliseconds. |
CV_CAP_PROP_POS_FRAMES | 1 | 0-based index of the frame to be decoded/captured next. |
CV_CAP_PROP_POS_AVI_RATIO | 2 | Relative position of the video file: 0 - start of the film, 1 - end of the film. |
CV_CAP_PROP_FRAME_WIDTH | 3 | Width of the frames in the video stream. |
CV_CAP_PROP_FRAME_HEIGHT | 4 | Height of the frames in the video stream. |
CV_CAP_PROP_FPS | 5 | Frame rate. |
CV_CAP_PROP_FOURCC | 6 | 4-character code of codec. |
CV_CAP_PROP_FRAME_COUNT | 7 | Number of frames in the video file. |
CV_CAP_PROP_FORMAT | 8 | Format of the Mat objects returned by retrieve() . |
CV_CAP_PROP_MODE | 9 | Backend-specific value indicating the current capture mode. |
CV_CAP_PROP_BRIGHTNESS | 10 | Brightness of the image (only for cameras). |
CV_CAP_PROP_CONTRAST | 11 | Contrast of the image (only for cameras). |
CV_CAP_PROP_SATURATION | 12 | Saturation of the image (only for cameras). |
CV_CAP_PROP_HUE | 13 | Hue of the image (only for cameras). |
CV_CAP_PROP_GAIN | 14 | Gain of the image (only for cameras). |
CV_CAP_PROP_EXPOSURE | 15 | Exposure (only for cameras). |
CV_CAP_PROP_CONVERT_RGB | 16 | Boolean flags indicating whether images should be converted to RGB. |
CV_CAP_PROP_WHITE_BALANCE | 17 | Currently unsupported |
CV_CAP_PROP_RECTIFICATION | 18 | Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently) |
一个例子:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret = cap.set(3, 320) # 设置帧宽
ret = cap.set(4, 240) # 设置帧高
if cap.isOpened() is True:
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转换为灰色通道
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
else:
print('cap is not opened!')
保存视频
cv2.VideoWriter_fourcc(视频格式):设置保存视频的格式,有两种方式,以MJPG为例:('M', 'J', 'P', 'G') ,或者(*'MJPG'),windows可用DIVX,输出avi格式
cv2.VideoWriter(视频文件名,格式,帧率,帧大小,是否为彩色):设置一个输出流out = cv2.VideoWriter()
out.write(frame):向输出流内写入一帧
一个例子
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID') # 设置视频编码格式
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) # 名称, 格式, 帧率, 帧大小
if cap.isOpened() is True: # 如果摄像头已被初始化返回True
while(True):
ret, frame = cap.read() # 如果帧读取正确则返回True
if ret is True:
frame = cv2.flip(frame, 1) # 反转图像,0:垂直反转,1:水平翻转,2:水平垂直反转
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cap.release()
cv2.destroyAllWindows()
else:
print('cap is not opened!')
播放视频
播放视频只需将cv2.VideoCapture()参数改为视频即可,播放视频的本质就是图片的连续显示,在播放每一帧时,使用 cv2.waiKey() 设置适当的持续时间就可以控制视频的播放速度。
一个例子
import numpy as np
import cv2
cap = cv2.VideoCapture('output.avi') # 选择要播放的视频
if cap.isOpened() is True:
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 输出灰色图像
cv2.imshow('frame', gray)
if cv2.waitKey(25) & 0xFF == ord('q'): # 改变cv2.waitKey()中的值可以改变播放速度
break
cap.release()
cv2.destroyAllWindows()
else:
print('cap is not opened!')