3 opencv_python 摄像头&视频文件 打开 显示 保存

摄像头&视频文件 打开 显示 保存

目标

  • 学会读取视频文件,显示视频,保存视频文件
  • 学会从摄像头获取并显示视频
  • 你将会学习到这些函数:cv2.VideoCapture(),cv2.VideoWrite()

用摄像头捕获视频

1 用 cv2.VideoCapture() 创建一个捕获对象

  • 一般的笔记本电脑都有内置摄像头。所以参数就是 0。你可以通过设置成 1 或者其他的来选择别的摄像头 例如 cap = cv2.VideoCapture(0)

2 读取视频中的帧数据

ret, frame = cap.read()
ret 表示读取结果成功与否
frame 表示读取的帧数据
  • cap.read() 返回一个布尔值(True/False)。如果帧读取的是正确的,就是 True。

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

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

    • 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)

3 显示图像

cv2.imshow('cap', frame)

4 写入视频文件

  • 设置编码格式 可参考的编码格式 DIVX, XVID, MJPG, X264, WMV1, WMV2 fourcc = cv2.VideoWriter_fourcc(*'XVID')

    FourCC 码以下面的格式传给程序,以 MJPG 为例: cv2.cv.FOURCC('M','J','P','G') 或者 cv2.cv.FOURCC(*'MJPG')。

  • 设置输出参数 out = cv2.VideoWriter('output.avi',fourcc, 20.0, (800,600)) output.avi 是输出文件路径 fourcc 是视频编码格式 20.0 是帧率 (800, 600) 是一帧数据的尺寸

代码实现

import cv2
cap = cv2.VideoCapture('../mp4/5.mp4')  # 这里是从视频文件中读取的,大家可以直接改为自己的摄像头编号 如 0
fourcc = cv2.VideoWriter_fourcc(*'VXID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (544,960))  # 这里的必须要和原有的图像尺寸保持一致
while(True):
    ret, frame = cap.read()
    if ret:
        cv2.imshow('cap', frame)
        out.write(frame)
        key = cv2.waitKey(24)
    else:
        break
    if key == 'q':
        break
cap.release()
out.release()
cv2.destroyAllWindows()
print('over')

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值