ubuntu22.04@laptop OpenCV Get Started: 002_reading_writing_videos
ubuntu22.04@laptop OpenCV Get Started: 002_reading_writing_videos
- 1. 源由
- 2. Read/Display/Write应用Demo
- 3 video_read_from_file
- 3.1 C++应用Demo
- 3.2 Python应用Demo
- 3.3 重点过程分析
- 3.3.1 读取视频文件
- 3.3.2 读取文件信息
- 3.3.3 帧读取&显示
- 4 video_read_from_image_sequence
- 4.1 C++应用Demo
- 4.2 Python应用Demo
- 4.3 重点过程分析
- 5 video_read_from_webcam
- 5.1 C++应用Demo
- 5.2 Python应用Demo
- 5.3 重点过程分析
- 6 video_write_from_webcam
- 6.1 C++应用Demo
- 6.2 Python应用Demo
- 6.3 重点过程分析
- 6.3.1 获取视频参数
- 6.3.2 设置保存视频参数
- 6.3.3 保存视频文件
- 7 video_write_to_file
- 7.1 C++应用Demo
- 7.2 Python应用Demo
- 7.3 重点过程分析
- 8. 总结
- 9. 参考资料
1. 源由
在OpenCV中对视频的读写操作与图像的读写操作非常相似。视频不过是一系列通常被称为帧的图像。所以,所需要做的就是在视频序列中的所有帧上循环,然后一次处理一帧。
接下来研读下:
- Read/Display/Write视频文件
- Read/Display/Write系列图片
- Read/Display/Write网络摄像头
2. Read/Display/Write应用Demo
002_reading_writing_videos是OpenCV读写、显示视频文件例程。
确认OpenCV安装路径:
$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
3 video_read_from_file
3.1 C++应用Demo
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_read_from_file$ tree .
.
├── CMakeLists.txt
├── Resources
│ └── Cars.mp4
└── video_read_from_file.cpp1 directory, 3 files
C++应用Demo工程编译执行:
$ cd video_read_from_file
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_read_from_file
3.2 Python应用Demo
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_read_from_file.py
3.3 重点过程分析
3.3.1 读取视频文件
- VideoCapture(path, apiPreference)
C++:
# Create a video capture object, in this case we are reading the video from a file
VideoCapture vid_capture("Resources/Cars.mp4");
Python:
# Create a video capture object, in this case we are reading the video from a file
vid_capture = cv2.VideoCapture('Resources/Cars.mp4')
3.3.2 读取文件信息
- vid_capture.isOpened()
- vid_capture.get()
C++:
if (!vid_capture.isOpened()){cout << "Error opening video stream or file" << endl;}
else{// Obtain fps and frame count by get() method and printint fps = vid_capture.get(5):cout << "Frames per second :" << fps;frame_count = vid_capture.get(7);cout << "Frame count :" << frame_count;}
Python:
if (vid_capture.isOpened() == False):print("Error opening the video file")
else:# Get frame rate informationfps = int(vid_capture.get(5))print("Frame Rate : ",fps,"frames per second") # Get frame countframe_count = vid_capture.get(7)print("Frame count : ", frame_count)
3.3.3 帧读取&显示
- vid_capture.read()
- cv2.imshow()
C++:
while (vid_capture.isOpened())
{// Initialize frame matrixMat frame;// Initialize a boolean to check if frames are there or notbool isSuccess = vid_capture.read(frame);// If frames are present, show itif(isSuccess == true){//display framesimshow("Frame", frame);}// If frames are not there, close itif (isSuccess == false){cout << "Video camera is disconnected" << endl;break;}
//wait 20 ms between successive frames and break the loop if key q is pressedint key = waitKey(20);if (key == 'q'){cout << "q key is pressed by the user. Stopping the video" << endl;break;}}
Python:
while(vid_capture.isOpened()):# vCapture.read() methods returns a tuple, first element is a bool # and the second is frameret, frame = vid_capture.read()if ret == True:cv2.imshow('Frame',frame)k = cv2.waitKey(20)# 113 is ASCII code for q keyif k == 113:breakelse:break
4 video_read_from_image_sequence
4.1 C++应用Demo
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_read_from_image_sequence$ tree . -L 2
.
├── CMakeLists.txt
├── Resources
│ └── Image_Sequence
└── video_read_from_image_sequence.cpp2 directories, 2 files
C++应用Demo工程编译执行:
$ cd video_read_from_image_sequence
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_read_is
4.2 Python应用Demo
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_read_from_image_sequence.py
4.3 重点过程分析
读取系列照片文件
- VideoCapture(path, apiPreference)
C++:
VideoCapture vid_capture("Resources/Image_sequence/Cars%04d.jpg");
Python:
vid_capture = cv2.VideoCapture('Resources/Image_sequence/Cars%04d.jpg')
注:Cars%04d.jpg: Cars0001.jpg, Cars0002.jpg, Cars0003.jpg, etc
5 video_read_from_webcam
5.1 C++应用Demo
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_read_from_webcam$ tree .
.
├── CMakeLists.txt
└── video_read_from_webcam.cpp0 directories, 2 files
C++应用Demo工程编译执行:
$ cd video_read_from_webcam
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_read_from_webcam
5.2 Python应用Demo
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_read_from_webcam.py
5.3 重点过程分析
- VideoCapture(path, apiPreference)
C++:
VideoCapture vid_capture(0);
Python:
vid_capture = cv2.VideoCapture(0)
注:cv2.CAP_DSHOW不要使用,有时会导致vid_capture.isOpened()
返回false。
6 video_write_from_webcam
6.1 C++应用Demo
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_write_from_webcam$ tree .
.
├── CMakeLists.txt
└── video_write_from_webcam.cpp0 directories, 2 files
C++应用Demo工程编译执行:
$ cd video_write_from_webcam
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_write_from_webcam
6.2 Python应用Demo
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_write_from_webcam.py
6.3 重点过程分析
6.3.1 获取视频参数
- vid_capture.get()
C++:
// Obtain frame size information using get() method
Int frame_width = static_cast<int>(vid_capture.get(3));
int frame_height = static_cast<int>(vid_capture.get(4));
Size frame_size(frame_width, frame_height);
int fps = 20;
Python:
# Obtain frame size information using get() method
frame_width = int(vid_capture.get(3))
frame_height = int(vid_capture.get(4))
frame_size = (frame_width,frame_height)
fps = 20
6.3.2 设置保存视频参数
- VideoWriter(filename, apiPreference, fourcc, fps, frameSize[, isColor])
- filename: pathname for the output video file
- apiPreference: API backends identifier
- fourcc: 4-character code of codec, used to compress the frames fourcc
- fps: Frame rate of the created video stream
- frame_size: Size of the video frames
- isColor: If not zero, the encoder will expect and encode color frames. Else it will work with grayscale frames (the flag is currently supported on Windows only).
C++:
//Initialize video writer object
VideoWriter output("Resources/output.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'),frames_per_second, frame_size);
Python:
# Initialize video writer object
output = cv2.VideoWriter('Resources/output_video_from_file.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 20, frame_size)
保存视频文件格式可以选择:
- AVI: cv2.VideoWriter_fourcc(‘M’,‘J’,‘P’,‘G’)
- MP4: cv2.VideoWriter_fourcc(*‘XVID’)
6.3.3 保存视频文件
- output.write()
C++:
while (vid_capture.isOpened())
{// Initialize frame matrixMat frame;// Initialize a boolean to check if frames are there or notbool isSuccess = vid_capture.read(frame);// If frames are not there, close itif (isSuccess == false){cout << "Stream disconnected" << endl;break;}// If frames are presentif(isSuccess == true){//display framesoutput.write(frame);// display framesimshow("Frame", frame);// wait for 20 ms between successive frames and break // the loop if key q is pressedint key = waitKey(20);if (key == ‘q’){cout << "Key q key is pressed by the user. Stopping the video" << endl;break;}}}
Python:
while(vid_capture.isOpened()):# vid_capture.read() methods returns a tuple, first element is a bool # and the second is frameret, frame = vid_capture.read()if ret == True:# Write the frame to the output filesoutput.write(frame)else:print(‘Stream disconnected’)break
7 video_write_to_file
7.1 C++应用Demo
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_write_to_file$ tree -L 2
.
├── CMakeLists.txt
├── Resources
│ └── Cars.mp4
└── video_write_to_file.cpp1 directory, 4 files
C++应用Demo工程编译执行:
$ cd video_write_to_file
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_write_to_file
7.2 Python应用Demo
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_write_to_file.py
7.3 重点过程分析
整合了以下章节重点过程:
-
- video_read_from_file
-
- video_write_from_webcam
8. 总结
主要通过以下三个函数API实现:
- videoCapture():获取数据源
- read():读取数据
- imshow():显示图像
- write():保存数据
其他API函数:
- isOpened() - 数据源打开是否成功过
- get() - 获取数据源相关信息
9. 参考资料
【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装
相关文章:
ubuntu22.04@laptop OpenCV Get Started: 002_reading_writing_videos
ubuntu22.04laptop OpenCV Get Started: 002_reading_writing_videos 1. 源由2. Read/Display/Write应用Demo3 video_read_from_file3.1 C应用Demo3.2 Python应用Demo3.3 重点过程分析3.3.1 读取视频文件3.3.2 读取文件信息3.3.3 帧读取&显示 4 video_read_from_image_sequ…...

Elasticsearch(ES) 简述请求操作索引下文档 增删查改操作
上文 Elasticsearch(ES) 创建带有分词器规则的索引 带着大家创建了一个带有分词功能的索引 老规矩 我们启动一下ES服务 本文 我们就来说说 关于文档的操作 我们先来添加一个文档 就像数据库加一条数据一样 这里 并不需要指定什么表结构和数据结构 它的文档结构是无模式的 添…...
Chrome扩展开发纪要
1. 开发人员模式 以Edge(Chromium)为例, 可在管理扩展页, 在左侧开发人员模式打开, 只有此项开启后才能加载未压缩的扩展, 虽然也可以打包扩展, 但是浏览器会检测, 未上线的crx包是无法被安装的. 所以不打算上架的crx只能使用 加载解压缩的扩展 安装 2. 创建扩展 2.1 建立文…...

LeetCode-第28题-找出字符串中第一个匹配项的下标
1.题目描述 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。 2.样例描述 3.思路描述 可以让字符串 …...

分享90个行业PPT,总有一款适合您
分享90个行业PPT,总有一款适合您 90个行业PPT下载链接:https://pan.baidu.com/s/1bHvhk_42-IFAjNdjPPtMZw?pwd8888 提取码:8888 Python采集代码下载链接:采集代码.zip - 蓝奏云 学习知识费力气,收集整理更不易…...

【原创 附源码】Flutter海外登录--Tiktok登录最详细流程
最近接触了几个海外登录的平台,踩了很多坑,也总结了很多东西,决定记录下来给路过的兄弟坐个参考,也留着以后留着回顾。更新时间为2024年2月7日,后续集成方式可能会有变动,所以目前的集成流程仅供参考&#…...
国内chatGPT3.5升级到chatGPT4.0的教程(24年2月更新)
最新的充值方法看这里。 通过虚拟卡 WildCard 的方式来升级 GPT 4.0 最快了,大概2分钟就可以升级完成, 而且升级 GPT 4.0 价钱也不贵,虚拟卡一年10美元,GPT4 每个月也才 20美元。如果你觉得 GPT 4.0 对你可能有帮助,那就赶快来升级…...

【python量化交易】qteasy使用教程01 - 安装方法及初始化配置
qteasy教程1 - 安装方法及初始化配置 qteasy教程1 - 安装方法及初始配置qteasy安装前的准备工作1, 创建安装环境2,安装MySQL数据库 (可选)安装pymysql 3,创建tushare账号并获取API token (可选)4,安装TA-lib (可选)WindowsMac OSL…...

UML 2.5图形库
UML 2.5图形库 drawio是一款强大的图表绘制软件,支持在线云端版本以及windows, macOS, linux安装版。 如果想在线直接使用,则直接输入网址drawon.cn或者使用drawon(桌案), drawon.cn内部完整的集成了drawio的所有功能,并实现了云端存储&#…...

分享springboot框架的一个开源的本地开发部署教程(若依开源项目开发部署过程分享持续更新二开宝藏项目PostgresSQL数据库版)
1首先介绍下若依项目: 若依是一个基于Spring Boot和Spring Cloud技术栈开发的多租户权限管理系统。该开源项目提供了一套完整的权限管理解决方案,包括用户管理、角色管理、菜单管理、部门管理、岗位管理等功能。 若依项目采用前后端分离的架构…...
打卡今天学习 Linux
过年了,祝大家过年快乐 在今天的学习中,我们涉及了一些关键的 Linux 系统管理知识点,包括 systemctl、IP 地址配置、域名解析、映射的创建、软链接等。让我们简要回顾一下这些主题。 1. systemctl systemctl 是一个强大的 Linux 系统管理工…...
单片机精进之路-3流水灯
P1代表单片机的P1口的8个io的寄存器,使用_crol_函数:将 k进行1位左位移,并将值以unsigned char类型返回,再将K的值赋给P1,这样就点亮了P1口对应的IO为低电平的led灯。 //flow light and beep #include <reg51.h>…...
c# File.WriteAllLines 和 File.WriteAllText
File.WriteAllLines 和 File.WriteAllText 都是 C# 中用于写入文本文件的方法,但它们有一些区别。 1. File.WriteAllLines 方法: File.WriteAllLines 方法用于将字符串数组的内容按行写入文本文件。每个数组元素都被写入文件的一行,且方法会…...

linux系统定时任务管理
crontab使用 一、crontab简介 crontab 这个指令所设置的工作将会循环的一直进行下去!可循环的时间为分钟、小时、每周、每月或每年等。crontab 除了可以使用指令执行外,亦可编辑 /etc/crontab 来支持。 至于让 crontab 可以生效的服务则是 crond 这个服…...
mysql的慢sql优化
为什么要优化慢sql ? 慢sql会长时间占用 数据库连接数,如果项目中有大量的慢sql,那么可用的数据库连接数就会变少,进而会影响业务。 慢sql优化 优化慢sql,最常见的就是添加索引。查询语句中不要使用select *尽量减少…...

排序算法---插入排序
原创不易,转载请注明出处。欢迎点赞收藏~ 插入排序是一种简单直观的排序算法,它的基本思想是将待排序的元素分为已排序和未排序两部分,每次从未排序部分中选择一个元素插入到已排序部分的合适位置,直到所有元素都插入到已排序部分…...

迷你世界勒索病毒,你的文件被删了吗?
前言 笔者在某恶意软件沙箱平台分析样本的时候,发现了一款比较有意思的勒索病毒MiniWorld迷你世界勒索病毒,它的解密界面与此前的WannaCry勒索病毒的界面相似,应该是作者仿冒的WannaCry的UI,如下所示: 这款勒索病毒既…...
QT styleSheet——控件设置样式表
QT开发中,需要设置多种多样的控件表现形式,QT实现的styleSheet能够满足多种多样的场景,这里简单的记录下一些我常用的 设置透明背景,鼠标悬浮时,设置背景色: pushButton->setStyleSheet("QPushBu…...
Linux学习
1 Linux的目录结构介绍 bin存放常用的命令etc存放配置文件bootlinux启动的文件home存放用户lib存放动态库,给应用程序使用lostfound一般是空的,但系统异常关机会产生文件media自动挂载,如u盘,光盘mnt手动挂载,一般自己…...
MFC研发自验用例编写应注意哪些关键测试点
MFC(Microsoft Foundation Classes)是一个用于开发Windows应用程序的C类库。在MFC应用程序的研发过程中,自验用例(自我验证测试用例)的编写是非常重要的一环,它有助于确保代码的质量、稳定性和功能正确性。…...

linux之kylin系统nginx的安装
一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源(HTML/CSS/图片等),响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址,提高安全性 3.负载均衡服务器 支持多种策略分发流量…...

工业安全零事故的智能守护者:一体化AI智能安防平台
前言: 通过AI视觉技术,为船厂提供全面的安全监控解决方案,涵盖交通违规检测、起重机轨道安全、非法入侵检测、盗窃防范、安全规范执行监控等多个方面,能够实现对应负责人反馈机制,并最终实现数据的统计报表。提升船厂…...

渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止
<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet: https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...

YSYX学习记录(八)
C语言,练习0: 先创建一个文件夹,我用的是物理机: 安装build-essential 练习1: 我注释掉了 #include <stdio.h> 出现下面错误 在你的文本编辑器中打开ex1文件,随机修改或删除一部分,之后…...
在Ubuntu中设置开机自动运行(sudo)指令的指南
在Ubuntu系统中,有时需要在系统启动时自动执行某些命令,特别是需要 sudo权限的指令。为了实现这一功能,可以使用多种方法,包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法,并提供…...
【JavaSE】绘图与事件入门学习笔记
-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角,以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向,距离坐标原点x个像素;第二个是y坐标,表示当前位置为垂直方向,距离坐标原点y个像素。 坐标体系-像素 …...

Map相关知识
数据结构 二叉树 二叉树,顾名思义,每个节点最多有两个“叉”,也就是两个子节点,分别是左子 节点和右子节点。不过,二叉树并不要求每个节点都有两个子节点,有的节点只 有左子节点,有的节点只有…...

selenium学习实战【Python爬虫】
selenium学习实战【Python爬虫】 文章目录 selenium学习实战【Python爬虫】一、声明二、学习目标三、安装依赖3.1 安装selenium库3.2 安装浏览器驱动3.2.1 查看Edge版本3.2.2 驱动安装 四、代码讲解4.1 配置浏览器4.2 加载更多4.3 寻找内容4.4 完整代码 五、报告文件爬取5.1 提…...

佰力博科技与您探讨热释电测量的几种方法
热释电的测量主要涉及热释电系数的测定,这是表征热释电材料性能的重要参数。热释电系数的测量方法主要包括静态法、动态法和积分电荷法。其中,积分电荷法最为常用,其原理是通过测量在电容器上积累的热释电电荷,从而确定热释电系数…...

【7色560页】职场可视化逻辑图高级数据分析PPT模版
7种色调职场工作汇报PPT,橙蓝、黑红、红蓝、蓝橙灰、浅蓝、浅绿、深蓝七种色调模版 【7色560页】职场可视化逻辑图高级数据分析PPT模版:职场可视化逻辑图分析PPT模版https://pan.quark.cn/s/78aeabbd92d1...