Python+OpenCV 图像处理系列(2)—— 视频捕获、播放和保存

1.视频捕获

为了获取视频,首先需要创建一个 VideoCapture 类对象。它的参数可以是设备的索引号,或者是一个视频文件。设备索引号就是在指定要使用的摄像头。一般的笔记本电脑都有内置摄像头。所以参数就是 0。你可以通过设置成 1 或者其他的来选择别的摄像头。

import cv2
cap = cv2.VideoCapture()
flag = cap.isOpened()

if flag:
    while 1:
        # Capture frame-by-frame
        ret, frame = cap.read()

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Display the resulting frame
        cv2.imshow('frame', gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

cap.read() 返回一个布尔值(True/False)。如果帧读取的是正确的,就是True。

有时 cap 可能不能成功的初始化摄像头设备。这种情况下上面的代码会报错。你可以使用cap.isOpened(),来检查是否成功初始化了。如果返回值是 True,那就没有问题。否则就要使用函数 cap.open()。

可以使用函数 cap.get(propId) 来获得视频的一些参数信息。这里 propId 可以是 0 到 18 之间的任何整数。每一个数代表视频的一个属性,见下表。

属性说明
CV_CAP_PROP_POS_MSECCurrent position of the video file in milliseconds.
CV_CAP_PROP_POS_FRAMES0-based index of the frame to be decoded/captured next.
CV_CAP_PROP_POS_AVI_RATIORelative position of the video file: 0 - start of the film, 1 - end of the film.
CV_CAP_PROP_FRAME_WIDTHWidth of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHTHeight of the frames in the video stream.
CV_CAP_PROP_FPSFrame rate.
CV_CAP_PROP_FOURCC4-character code of codec.
CV_CAP_PROP_FRAME_COUNTNumber of frames in the video file.
CV_CAP_PROP_FORMATFormat of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODEBackend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESSBrightness of the image (only for cameras).
CV_CAP_PROP_CONTRASTContrast of the image (only for cameras).
CV_CAP_PROP_SATURATIONSaturation of the image (only for cameras).
CV_CAP_PROP_HUEHue of the image (only for cameras).
CV_CAP_PROP_GAINGain of the image (only for cameras).
CV_CAP_PROP_EXPOSUREExposure (only for cameras).
CV_CAP_PROP_CONVERT_RGBBoolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCECurrently unsupported
CV_CAP_PROP_RECTIFICATIONRectification flag for stereo (note: only supported by DC1394 v 2.x backend currently)

其中的一些值可以使用 cap.set(propId, value) 来修改,value 就是你想要设置成的新值。

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

2.视频播放

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

import cv2

cap = cv2.VideoCapture("2018-12-16_11-33.mp4")
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc('XVID') # cv3 的写法,cv2 需要修改
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)
        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

如果安装的是 OpenCV 2 的版本会出现如下错误:

AttributeError: 'module' object has no attribute 'VideoWriter_fourcc'

查看 OpenCV 的版本号

>>> import cv2
>>> cv2.__version__
'2.4.9.1'

cv2.VideoWriter_fourcc() 是 Opencv3.0 所用,而 cv2.cv.CV_FOURCC() 用于 Opencv2.4.*

修改该行为

fourcc = cv2.cv.CV_FOURCC('M', 'J', 'P', 'G')

3.视频保存

要创建一个 VideoWriter 的对象。我们应该确定一个输出文件的名字。接下来指定 FourCC 编码(下面会介绍)。播放频率和帧的大小也都需要确定。最后一个是 isColor 标签。如果是 True,每一帧就是彩色图,否则就是灰度图。

FourCC 就是一个4 字节码,用来确定视频的编码格式。

• In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)

• In Windows: DIVX (More to be tested and added)

• In OSX : (I don’t have access to OSX. Can some one fill this?)

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

	cv2.cv.FOURCC('M','J','P','G') 或者 cv2.cv.FOURCC(*'MJPG')

下面的代码是从摄像头中捕获视频,沿水平方向旋转每一帧并保存它。

import cv2

cap = cv2.VideoCapture("2018-12-16_11-33.mp4")
# Define the codec and create VideoWriter object

# 此处fourcc的在 ubuntu 16.04 上有效,如果视频保存为空,那么可以改一下这个参数试试, 也可以是-1
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')

fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
size = (
    int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
    int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
    )

out = cv2.VideoWriter('output.avi', fourcc, fps, size)

while cap.isOpened():
    ret, frame = cap.read()
    if ret == True:
        # frame = cv2.flip(frame, 0) 该行表示翻转视频,播放已经存在的视频时注释改行
        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

编码器常用的几种:

cv2.VideoWriter_fourcc(“I”, “4”, “2”, “0”)
压缩的yuv颜色编码器,4:2:0色彩度子采样 兼容性好,产生很大的视频 avi

cv2.VideoWriter_fourcc(“P”, I", “M”, “1”)
采用mpeg-1编码,文件为avi

cv2.VideoWriter_fourcc(“X”, “V”, “T”, “D”)
采用mpeg-4编码,得到视频大小平均 拓展名avi

cv2.VideoWriter_fourcc(“T”, “H”, “E”, “O”)
Ogg Vorbis, 拓展名为ogv

cv2.VideoWriter_fourcc(“F”, “L”, “V”, “1”)
FLASH视频,拓展名为.flv

4.总结

以下代码在 OpenCV 3.4.4 中测试通过,

import cv2
import numpy as np

video_path = "video/test.mp4"
cap = cv2.VideoCapture(video_path)

height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = cap.get(cv2.CAP_PROP_FPS)
print "height is {}, width is {}, count is {}, fps is {}".format(height, width, count, fps)
# height is 720.0, width is 1280.0, count is 3599.0, fps is 11.9941808935
out_video = cv2.VideoWriter("video/out_video.mp4", cv2.VideoWriter_fourcc('D', 'I', 'V', 'X'),
                            15, (np.int(width), np.int(height)), True)

while True:
    ret, frame = cap.read()
    print "ret is {}, frame is {}, frame's shape is {}".format(ret, frame, frame.shape)

    if ret is True:
        cv2.imshow("video_out", frame)
        out_video.write(frame)
        c = cv2.waitKey(50)
        if c & 0xFF == 27:
            break
    else:
        break

cap.release()
out_video.release()

部分输出结果:

ret is True, frame is [[[209 190 180]
  [209 190 180]
  [209 190 180]
  ...
  [ 60  39  47]
  [ 57  39  49]
  [ 62  44  54]]

 [[209 190 180]
  [209 190 180]
  [209 190 180]
  ...
  [ 60  39  47]
  [ 61  43  53]
  [ 57  39  49]]

 [[209 190 180]
  [209 190 180]
  [209 190 180]
  ...
  [ 74  43  56]
  [ 74  45  52]
  [ 66  37  44]]

 ...

 [[108 102 119]
  [108 102 119]
  [107 101 118]
  ...
  [163 158 147]
  [163 158 147]
  [163 158 147]]

 [[108 102 119]
  [108 102 119]
  [107 101 118]
  ...
  [168 157 146]
  [168 157 146]
  [168 157 146]]

 [[108 102 119]
  [108 102 119]
  [107 101 118]
  ...
  [161 150 139]
  [161 150 139]
  [161 150 139]]], frame's shape is (720, 1280, 3)
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值