• python-opencv 视频处理


    使用python-opencv针对视频进行处理,读取视频操作。

    import cv2
    # 创建读取视频的类
    capture = cv2.VideoCapture("images-6/vtest.avi")
    # 得到视频的高度
    height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
    # 得到视频的宽度
    width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
    # 得到视频的帧数
    count = capture.get(cv2.CAP_PROP_FRAME_COUNT)
    # 得到视频的帧速
    fps = capture.get(cv2.CAP_PROP_FPS)
    
    print("video height:",height)
    print("video ",width)
    print("video frame count:",count)
    print("video fps:",fps)
    
    # 对视频中的每一帧图像做处理的函数
    def process_fun(image, opt=1):
        if opt == 0: # 若操作数为 0,则选取 OTSU 阈值化操作
            gray_img=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
            ret,dst = cv2.threshold(gray_img,0,255,cv2.THRESH_OTSU)
        if opt == 1: # 若操作数为 1,则选取高斯模糊操作
            dst = cv2.GaussianBlur(image, (0, 0), 15)
        if opt == 2: # 若操作数为 2,则选取 Canny 操作
            dst = cv2.Canny(image, 100, 200)
        return dst
    
    # 定义写入视频的编码格式
    fourcc = cv2.VideoWriter_fourcc(*'XVID')  #  *'XVID' or *'MJPG'  写入视频类的编码格式
    # 创建写入视频的类
    out_video = cv2.VideoWriter("processed_video.avi", fourcc, int(fps), (int(width), int(height)),False)
    while(True):
        # 读取视频中的每一帧
        ret, frame = capture.read()
        # 如果该帧存在则进行操作
        if ret is True:
            # 对输入的每一帧图像处理
            result = process_fun(frame, opt=2)
            # 保存已经处理后的每一帧图像
            out_video.write(result)
            cv2.imwrite("precessed.jpg",result)
        # 如果该帧不存在则循环结束
        else:
            break
    out_video.release()
  • 相关阅读:
    Pymsql
    MySQL基础操/下
    MySQL基础操作
    前端学习之jquery/下
    前端学习之jquery
    Python之异常处理
    Python之模块和包导入
    Python之模块
    Python之面向对象上下文管理协议
    Python之面向对象slots与迭代器协议
  • 原文地址:https://www.cnblogs.com/dzswise/p/14844642.html
Copyright © 2020-2023  润新知