openCV-Python笔记二:视频捕获

来源:互联网 发布:网络u盾讀取服务器 编辑:程序博客网 时间:2024/05/29 08:51

一、从摄像头捕获视频

从摄像头捕获视频,首先需要创建VideoCapture对象,参数为设备索引号,例如:对于笔记本电脑,传0表示使用其内置摄像头。

import numpy as np import cv2cap = cv2.VideoCapture(0)while(cap.isOpened()):    # 从摄像头读取一帧,ret是表明成功与否    ret, frame = cap.read()     if ret:        #处理得到的帧,这里将其翻转        frame = cv2.flip(frame,0)        cv2.imshow('frame',frame)    else:        break    # 监听键盘,按下q键退出    if cv2.waitKey(1) & 0xFF == ord('q'):         break ##释放cap.release()cv2.destroyAllWindows()

可以使用函数 cap.get(propId) 来获得视频的一些参数信息。这里 propId 可以是 0 到 18 之间的任何整数。每一个数代表视频的一个属性,其中的一些值可以使用 cap.set(propId,value) 来修改,value 就是 你想要设置成的新值。

例如,可以使用 cap.get(3) 和 cap.get(4) 来查看每一帧的宽和高。 默认情况下得到的值是 640X480。但是可以使用 cap.set(3,320) 和 cap.set(4,240) 来把宽和高改成 320X240。

propId为以下值:

  • CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
  • CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
  • CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
  • CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
  • CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
  • CV_CAP_PROP_FPS Frame rate.
  • CV_CAP_PROP_FOURCC 4-character code of codec.
  • CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
  • CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
  • CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
  • CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
  • CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
  • CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
  • CV_CAP_PROP_HUE Hue of the image (only for cameras).
  • CV_CAP_PROP_GAIN Gain of the image (only for cameras).
  • CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
  • CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
  • CV_CAP_PROP_WHITE_BALANCE Currently unsupported
  • CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend cur- rently)

二、保存从摄像头捕获的视频

保存视频,首先需要创建一个VideoWriter对象,它的参数依次为视频文件名,视频编码格式,帧率,大小,是否彩色。

其中,编码为FourCC编码,可用格式可从http://www.fourcc.org/codecs.php查到

FourCC 码以下面的格式传给程序,以XVID 为例:

cv2.cv.CV_FOURCC(‘X’,’V’,’I’,’D’) 或者cv2.cv.CV_FOURCC(*’XVID’)。

import numpy as np import cv2cap = cv2.VideoCapture(0)#视频编码格式fourcc = cv2.cv.CV_FOURCC(*'XVID')#VideoWriter对象out = cv2.VideoWriter('cam.avi',fourcc,20.0,(640,480))while(cap.isOpened()):    # 从摄像头读取一帧,ret是表明成功与否    ret, frame = cap.read()     if ret:        #处理得到的帧,然后保存        frame = cv2.flip(frame,0)        out.write(frame)        cv2.imshow('frame',frame)    else:        break    # 监听键盘,按下q键退出    if cv2.waitKey(1) & 0xFF == ord('q'):         break ##释放cap.release()out.release()cv2.destroyAllWindows()

三、读取视频文件

与从摄像头中捕获一样,只需要把设备索引号改成视频文件的名字。在播放每一帧时,使用cv2.waiKey() 设置适当的持续时间。如果设置的太低视频就会播放的非常快,如果设置的太高就会播放的很慢(可以使用这种方法控制视频的播放速度)。

如果获取失败的话,可能是因为缺少ffmpeg的dll,可以把opencv文件夹中的 \3rdparty\ffmpeg里的opecv_ffmpeg.dll文件(64位系统下是opecv_ffmpeg_64.dll)复制到python文件夹目录(我的是C:\python27)下,同时重命名为opecv_ffmpegXXX.dll,XXX为opencv版本,我使用的2.4.13则重命名为opecv_ffmpeg2413.dll

import numpy as np import cv2cap = cv2.VideoCapture('1.avi')while(cap.isOpened()):    # 从摄像头读取一帧,ret是表明成功与否    ret, frame = cap.read()     if ret:        #处理得到的帧,这里将其翻转        frame = cv2.flip(frame,0)        cv2.imshow('frame',frame)    else:        break    # 监听键盘,按下q键退出    if cv2.waitKey(25) & 0xFF == ord('q'):         break ##释放cap.release()cv2.destroyAllWindows()
0 0
原创粉丝点击