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

Opencv && 把视频裁剪成指定帧率的图像集

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

Opencv && 把视频裁剪成指定帧率的图像集

目的:

视频 FPS=30,需要把视频裁剪成 FPS=3 的图像集,即两分钟的视频裁剪出360张图像。

FPS:

  • 每秒传输帧数(Frames Per Second)
  • FPS 也可以理解为我们常说的“刷新率(单位为Hz)”,例如我们常在游戏里说的“FPS值”。
  • FPS 是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。FPS 是测量用于保存、显示动态视频的信息数量。每秒钟帧数越多,所显示的动作就会越流畅。通常,要避免动作不流畅的最低是30。某些计算机视频格式,每秒只能提供15帧。

示例代码(含注释):

import cv2
from tqdm import tqdm  # 进度条


def main():
    # 读取视频文件
    video_caputre = cv2.VideoCapture(r"G:CSDNpythonvideos2imagesvideosIMG_3389.MP4")

    # 获取视频流的参数
    fps = video_caputre.get(cv2.CAP_PROP_FPS)  # 帧率
    width = video_caputre.get(cv2.CAP_PROP_FRAME_WIDTH)  # 宽度
    height = video_caputre.get(cv2.CAP_PROP_FRAME_HEIGHT)  # 高度
    all_frames = video_caputre.get(cv2.CAP_PROP_FRAME_COUNT)  # 总帧数

    print("fps:", fps, "n", "width:", width, "n", "height:", height, "n", "all_frames:", all_frames)

    # 读取视频流(返回参数:是否读到视频流、图像帧)
    whether, frame = video_caputre.read()

    i = 0
    for _ in tqdm(range(int(all_frames))):
        # cv2.imshow("image", frame)
        # cv2.waitKey(0)
        i += 1

        # 取余数(每10帧保存一张图像)
        if i % 10 == 0:
            cv2.imwrite(r"G:CSDNpythonvideos2imagessave_image_3_person4_{}.png".format(i), frame)

        # 读取下一帧图像
        whether, frame = video_caputre.read()

    # 释放视频流
    video_caputre.release()

    print("video to images done!")


if __name__ == "__main__":
    main()

 >>>output

fps: 29.996961074122165
width: 1920.0
height: 1080.0
all_frames: 5429.0
100%|██████████| 5429/5429 [02:27<00:00, 36.79it/s]
video to images done!

   >>>如有疑问,欢迎评论区一起探讨  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917967.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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