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应用程序的研发过程中,自验用例(自我验证测试用例)的编写是非常重要的一环,它有助于确保代码的质量、稳定性和功能正确性。…...
GME-Qwen2-VL-2B效果实测:抽象文字如何匹配具体图片?
GME-Qwen2-VL-2B效果实测:抽象文字如何匹配具体图片? 1. 多模态搜索的突破性体验 想象一下,你脑海中浮现出一句富有哲理的句子:"人生不是裁决书",却想找一张能表达这种意境的图片。传统搜索引擎会怎么做&a…...
CMake+vcpkg环境配置避坑指南:从命令行到GUI的完整流程
CMakevcpkg环境配置避坑指南:从命令行到GUI的完整流程 刚接触C/C开发的工程师们,往往会在环境配置阶段经历"从入门到放弃"的心路历程。面对复杂的依赖库管理、跨平台编译问题,以及各种晦涩的错误提示,不少开发者甚至还没…...
Keil5主题配色进阶:不只是好看,更要好用!详解如何区分函数、变量、宏定义的颜色
Keil5主题配色进阶:不只是好看,更要好用!详解如何区分函数、变量、宏定义的颜色 作为一名嵌入式开发者,每天面对Keil5的默认编辑器界面,你是否也感到视觉疲劳?那些单调的配色不仅影响编码心情,更…...
指尖藏趣,抽享惊喜——扭蛋机抽赏盲盒小程序前端功能详解
抽赏盲盒所带来的未知惊喜与收集乐趣,深受不同年龄段用户的喜爱,扭蛋机抽赏盲盒小程序则打破线下场景限制,让这份乐趣随时可及。该小程序前端功能以“简约操作、趣味体验”为核心,聚焦用户可直接操作的功能板块,简化流…...
大数据产品实战:用户画像系统的设计与实现
大数据产品实战:用户画像系统的设计与实现 关键词:用户画像、标签体系、大数据平台、精准营销、数据挖掘 摘要:用户画像系统是大数据时代企业实现“以用户为中心”运营的核心工具,它通过给用户“贴标签”的方式,将复杂的用户行为转化为可量化、可分析的数字特征。本文将从…...
拒了一个只要1.8万的45岁大佬
因公众号更改推送规则,请点“在看”并加“星标”第一时间获取精彩技术分享点击关注#互联网架构师公众号,领取架构师全套资料 都在这里0、2T架构师学习资料干货分上一篇:2T架构师学习资料干货分享大家好,我是互联网架构师ÿ…...
开局掌控者:EdB Prepare Carefully - RimWorld自定义体验革命
开局掌控者:EdB Prepare Carefully - RimWorld自定义体验革命 【免费下载链接】EdBPrepareCarefully EdB Prepare Carefully, a RimWorld mod 项目地址: https://gitcode.com/gh_mirrors/ed/EdBPrepareCarefully 副标题:如何告别随机开局…...
OpenClaw多通道管理:GLM-4.7-Flash同时对接飞书与钉钉的配置技巧
OpenClaw多通道管理:GLM-4.7-Flash同时对接飞书与钉钉的配置技巧 1. 为什么需要多通道管理? 上周我接到一个技术咨询需求:一个小型内容团队需要同时在飞书和钉钉两个平台上接收AI助手服务。他们的编辑用飞书,运营用钉钉…...
告别性能瓶颈:如何用NVIDIA Profile Inspector释放显卡90%潜能?
告别性能瓶颈:如何用NVIDIA Profile Inspector释放显卡90%潜能? 【免费下载链接】nvidiaProfileInspector 项目地址: https://gitcode.com/gh_mirrors/nv/nvidiaProfileInspector 为什么官方显卡控制面板永远像个"锁着的工具箱"&#…...
Comsol异构电池力电热耦合模型:探索电池的多场奥秘
comsol异构电池力电热耦合模型 采用椭圆型电极颗粒模拟锂离子正负极的电极颗粒,还原真实电池的3D介观结构,耦合电化学场-热场-力学场,可模拟电流,浓度,温度,应力等多场结果在电池研究领域,深入理…...
