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应用程序的研发过程中,自验用例(自我验证测试用例)的编写是非常重要的一环,它有助于确保代码的质量、稳定性和功能正确性。…...
装饰模式(Decorator Pattern)重构java邮件发奖系统实战
前言 现在我们有个如下的需求,设计一个邮件发奖的小系统, 需求 1.数据验证 → 2. 敏感信息加密 → 3. 日志记录 → 4. 实际发送邮件 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其…...
C++实现分布式网络通信框架RPC(3)--rpc调用端
目录 一、前言 二、UserServiceRpc_Stub 三、 CallMethod方法的重写 头文件 实现 四、rpc调用端的调用 实现 五、 google::protobuf::RpcController *controller 头文件 实现 六、总结 一、前言 在前边的文章中,我们已经大致实现了rpc服务端的各项功能代…...
ubuntu搭建nfs服务centos挂载访问
在Ubuntu上设置NFS服务器 在Ubuntu上,你可以使用apt包管理器来安装NFS服务器。打开终端并运行: sudo apt update sudo apt install nfs-kernel-server创建共享目录 创建一个目录用于共享,例如/shared: sudo mkdir /shared sud…...
【力扣数据库知识手册笔记】索引
索引 索引的优缺点 优点1. 通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度(创建索引的主要原因)。3. 可以加速表和表之间的连接,实现数据的参考完整性。4. 可以在查询过程中,…...
STM32标准库-DMA直接存储器存取
文章目录 一、DMA1.1简介1.2存储器映像1.3DMA框图1.4DMA基本结构1.5DMA请求1.6数据宽度与对齐1.7数据转运DMA1.8ADC扫描模式DMA 二、数据转运DMA2.1接线图2.2代码2.3相关API 一、DMA 1.1简介 DMA(Direct Memory Access)直接存储器存取 DMA可以提供外设…...
【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验
系列回顾: 在上一篇中,我们成功地为应用集成了数据库,并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了!但是,如果你仔细审视那些 API,会发现它们还很“粗糙”:有…...
多种风格导航菜单 HTML 实现(附源码)
下面我将为您展示 6 种不同风格的导航菜单实现,每种都包含完整 HTML、CSS 和 JavaScript 代码。 1. 简约水平导航栏 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport&qu…...
USB Over IP专用硬件的5个特点
USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中,从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备(如专用硬件设备),从而消除了直接物理连接的需要。USB over IP的…...
R 语言科研绘图第 55 期 --- 网络图-聚类
在发表科研论文的过程中,科研绘图是必不可少的,一张好看的图形会是文章很大的加分项。 为了便于使用,本系列文章介绍的所有绘图都已收录到了 sciRplot 项目中,获取方式: R 语言科研绘图模板 --- sciRplothttps://mp.…...
深度剖析 DeepSeek 开源模型部署与应用:策略、权衡与未来走向
在人工智能技术呈指数级发展的当下,大模型已然成为推动各行业变革的核心驱动力。DeepSeek 开源模型以其卓越的性能和灵活的开源特性,吸引了众多企业与开发者的目光。如何高效且合理地部署与运用 DeepSeek 模型,成为释放其巨大潜力的关键所在&…...
