栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【0x02】opencv-python视频入门

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【0x02】opencv-python视频入门

从相机捕捉视频

从相机捕捉实时的视频流。
要捕获视频,您需要创建一个VideoCapture对象。它的参数可以为0,1,2等等,0代表第一台摄像头,1代表第二台,以此类推。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    # 从这开始逐帧的读
    # ret是true or false, 可以根据ret来判定读取成功还是失败.即, 判定是否读到了末尾
    ret, frame = cap.read()

    # Our operations on the frame come here
    # 将采集到的帧做灰度处理
    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.isOpened()

来判定摄像头是否打开。

可以使用cap.get(propId)方法访问此视频的某些功能.

VideoCapture 的propid属性代表意思:
0-视频文件的当前位置(毫秒))
• CV_CAP_PROP_POS_frameS 0-based index of the frame to be decoded/captured next.
(1-下一步要解码/捕获的帧的基于0的索引)
• CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
(2-视频文件的相对位置:0-胶片开始,1-胶片结束)
• CV_CAP_PROP_frame_WIDTH Width of the frames in the video stream.
(3-视频流中帧的宽度)
• CV_CAP_PROP_frame_HEIGHT Height of the frames in the video stream.
(4-视频流中帧的高度)
• CV_CAP_PROP_FPS frame rate.
(5-帧率)
• CV_CAP_PROP_FOURCC 4-character code of codec.
(6-编解码器的4字符代码)
• CV_CAP_PROP_frame_COUNT Number of frames in the video file.
(7-视频文件中的帧数。)
• CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
(8-返回mat对象的格式)
• CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
(9-后端特定值,指示当前捕获模式)
• CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
(10-图像的亮度–仅适用于相机)
• CV_CAP_PROP_ConTRAST Contrast of the image (only for cameras).
(11-对比度–仅用于相机)
• CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
(12-饱和度–仅用于相机)
• CV_CAP_PROP_HUE Hue of the image (only for cameras).
(13-色调–仅用于相机)
• CV_CAP_PROP_GAIN Gain of the image (only for cameras).
(14-增益–仅用于相机)
• CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
(15-曝光–仅用于相机)
• CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
(16-布尔标志,指示是否应将图像转换为RGB。)
• CV_CAP_PROP_WHITE_BALANCE Currently unsupported
(17-当前不受支持)
• CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
(18-立体摄像机的校正标志(注:目前仅受DC1394 v 2.x后端支持))
对应的,使用 cap.set(propId,value) 来修改视频属性,value 就是你想要设置成的新值。
从文件捕捉视频

跟从相机捕捉是一样的。只不过从文件捕捉参数不是0,1,2了而已。而是变成了视频文件的路径。

import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    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()
保存视频

我们捕获一个视频,逐帧进行处理后,要把它再生成一个视频。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
# 编解码器
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# 写入对象
# 20.0为帧率, 640, 480为帧的尺寸
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
# 记得release掉
cap.release()
out.release()
cv2.destroyAllWindows()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/326164.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号