捕获视频
- cap=cv2.VideoCapture(0) ——解释:获取视频。参数为设备索引号或者视频文件,内置摄像头取0。(按帧捕获视频)——实例化
- cap.read() ——解释:返回布尔值,如果帧数读取正确返回True。
- cap.isOpened() ——解释:检查对象是否成功初始化摄像头设备。
- cap.open() ——解释:初始化摄像头设备。
- cap.get(propId) ——解释:获得视频的一些参数信息,参数取值范围:0-18。
5.1 CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
5.2 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
5.3 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
5.4 CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
5.5 CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
5.6 CV_CAP_PROP_FPS Frame rate.
5.7 CV_CAP_PROP_FOURCC 4-character code of codec.
5.8 CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
5.9 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
5.10 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
5.11 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
5.12 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
5.13 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
5.14 CV_CAP_PROP_HUE Hue of the image (only for cameras).
5.15 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
5.16 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
5.17 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
5.18 CV_CAP_PROP_WHITE_BALANCE Currently unsupported
5.19 CV_CAP_PROP_RECTIFICATION Rectificationflagforstereo - cap.set(propId,value) ——解释:对propid属性修改成相应的value值。
捕获:代码演示
# coding=utf-8
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
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()
播放视频——cv2.imshow()
- cv2.imshow('窗口名’,帧名) ——解释:显示保存的帧。
播放:代码演示
# coding=utf-8
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))
while(cap.isOpened()):
ret,frame=cap.read()
if ret=True:
frame=cv2.flip(frame,0)
out.write(frame)
cv2.imshow('frame',frame)
if waitKey(1) &0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
保存视频
- 创建VideoWriter的对象。
- 确定输出文件的名字。
- 指定FourCC编码。
- 确定播放帧率和帧大小。
- 确定isColor标签。True:彩色图,否则灰度图。
- FourCC 就是一个 4 字节码用来确定的编码格式。
6.1 In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
6.2 In Windows: DIVX (More to be tested and added)
6.3 In OSX : (I dont have access to OSX. Can some one fill this?)
- FourCC 码以下的格式传给程序以 MJPG 为例 ——cv2.cv.FOURCC(‘M’,‘J’,‘P’,‘G’)或者cv2.cv.FOURCC(*‘MJPG’)。 下面的代码是从摄像头中捕获沿水平方向旋转每一帧并保存它。
保存:代码演示
# coding=utf-8
import numpy as np
import cv2
cap=cv2.VideoCapture(0)
fourcc=cv2.cvFOURCC(*'XVID')
out=cv2.VideoWriter('output.avi',fourcc,20,0,(640,480))
while(cap,isOpened()):
ret,frame=cap.read()
if ret = True:
frame=cv2.flip(frame,0)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) &0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()