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中对视频的读写操作与图像的读写操作非常相似。视频不过是一系列通常被称为帧的图像。所以,所需要做的就是在视频序列中的所有帧上循环,然后一次处理一帧。

接下来研读下:

  1. Read/Display/Write视频文件
  2. Read/Display/Write系列图片
  3. 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 重点过程分析

整合了以下章节重点过程:

    1. video_read_from_file
    1. video_write_from_webcam

8. 总结

主要通过以下三个函数API实现:

  1. videoCapture():获取数据源
  2. read():读取数据
  3. imshow():显示图像
  4. write():保存数据

其他API函数:

  • isOpened() - 数据源打开是否成功过
  • get() - 获取数据源相关信息

9. 参考资料

【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/669629.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

客户端会话技术-Cookie

一、会话技术 1.1 概述 会话&#xff1a;一次会话中包含多次**请求和响应** 一次会话&#xff1a;浏览器第一次给服务器资源发送请求&#xff0c;此时会话建立&#xff0c;直到有一方断开为止 会话的功能&#xff1a;在一次会话的范围内的多次请求间&#xff0c;共享数据 …

学习MySQL的MyISAM存储引擎

学习MySQL的MyISAM存储引擎 MySQL的MyISAM存储引擎是MySQL早期版本中默认的存储引擎&#xff0c;后来被InnoDB所取代。尽管InnoDB在许多方面提供了更高级的特性&#xff0c;如事务处理、行级锁定和外键支持&#xff0c;MyISAM仍然因其简单性、高性能以及对全文搜索的支持而被广…

Talking about your education in English

Raw Material Hi, Tim here with another 925English lesson! In today’s lesson we’re going to learn how to talk about your education. Your education is an important part of your background. And there are a lot of situations where you might talk about whe…

ubuntu上安装docker-compose踩坑记录

报错如下&#xff1a; $ docker-compose up /usr/local/lib/python3.8/dist-packages/requests/__init__.py:102: RequestsDependencyWarning: urllib3 (2.2.0) or chardet (3.0.4)/charset_normalizer (2.0.12) doesnt match a supported version!warnings.warn("urlli…

107 C++ STL 容器分类,array,vector详解

STL 的组成部分是个重要的部分&#xff0c;先回忆一下 容器&#xff0c;迭代器&#xff0c;算法&#xff08;函数&#xff09;&#xff0c;分配器&#xff08;分配内存&#xff09;&#xff0c;适配器&#xff0c;仿函数 一 容器的分类. vector &#xff0c; list&#xff0c…

Narrative Visualization: Telling Stories with Data

作者&#xff1a;Edward Segel、Jeffrey Heer 发表&#xff1a;TVCG&#xff0c; 机构&#xff1a;UW Interactive Data Lab 【原斯坦福可视化组】 1.概述 静态可视化&#xff1a;在一大串的文本描述中&#xff0c;可视化作为提供证据和细节的图表出现新兴可视化&#xff1a…

元数据驱动的思想

元数据驱动的思想 元数据驱动的思想应该不会陌生&#xff0c;但元数据驱动的实践应该会非常陌生。 因为元数据驱动架构是为了解决高频个性化的复杂业务而诞生的&#xff0c;而这种业务场景只存在2B领域。 有关元数据驱动的架构思想&#xff0c;在这里暂先简单抛几个点。&#…

精雕细琢的文档体验:Spring Boot 与 Knife4j 完美交汇

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 精雕细琢的文档体验&#xff1a;Spring Boot 与 Knife4j 完美交汇 前言Knife4j 与 Swagger 的区别1. 特性与优劣势对比&#xff1a;Knife4j&#xff1a;Swagger&#xff1a; 2. 选择 Knife4j 的理由&a…

二叉树的简单递归求解

int size 0; void btreesize(BTNode* point)//节点数 {if (point NULL){return; }else{size;}btreesize(point->left);btreesize(point->right);} 求树的节点数&#xff0c;递归思路为首先创立一个全局变量避免其在函数内部成为局部变量&#xff0c;然后当走到空树的时…

Nodejs基础6之HTTP模块的获取请求行和请求头、获取请求体、获取请求路径和查询字符串、http请求练习、设置HTTP响应报文、http响应练习

Nodejs基础 HTTP模块获取请求行和请求头获取请求体获取请求路径和查询字符串方式一方式二 http请求练习设置HTTP响应报文状态码响应状态描述响应头响应体 HTTP响应练习 HTTP模块 含义语法重点掌握请求方法request.method*请求版本request.httpVersion请求路径request.url*URL …

Mac利用brew安装mysql并设置初始密码

前言 之前一直是在windows上开发后段程序&#xff0c;所以只在windows上装mysql。(我记得linux只需要适应yum之类的命令即可) 另外, linux的移步 linux安装mysql (详细步骤,初次初始化,sql小例子,可视化操作客户端推荐) 好家伙&#xff0c;我佛了&#xff0c;写完当天网上发…

机器学习聚类算法

聚类算法是一种无监督学习方法&#xff0c;用于将数据集中的样本划分为多个簇&#xff0c;使得同一簇内的样本相似度较高&#xff0c;而不同簇之间的样本相似度较低。在数据分析中&#xff0c;聚类算法可以帮助我们发现数据的内在结构和规律&#xff0c;从而为进一步的数据分析…

electron获取元素xpath、pc端网页展示获取到的xpath、websocket给两端传值

目录 需求点&#xff1a;思路&#xff1a;思路&#xff1a;一、electron获取xpath1、创建主窗口2、创建子窗口并且setBrowserView到主窗口&#xff0c;子窗口默认加载error.html3、如果获取到了url&#xff0c;就加载url4、获取xpath并传递 二、electron通过websocket传递消息三…

深度学习缝模块怎么描述创新点?(附写作模板+涨点论文)

深度学习缝了别的模块怎么描述创新点、怎么讲故事写成一篇优质论文&#xff1f; 简单框架&#xff1a;描述自己这个领域&#xff0c;该领域出现了什么问题&#xff0c;你用了什么方法解决&#xff0c;你的方法有了多大的性能提升。 其中&#xff0c;重点讲清楚这两点&#xf…

开源计算机视觉库OpenCV详细介绍

开源计算机视觉库OpenCV详细介绍 1. OpenCV简介 OpenCV&#xff08;Open Source Computer Vision Library&#xff09;是一个开源的计算机视觉和机器学习软件库。它最初由Intel开发&#xff0c;现在由一个庞大的社区维护和更新。OpenCV旨在提供一个通用、跨平台的计算机…

QtAV学习:(一)Windows下编译QtAV

QtAV 主页&#xff1a; QtAV by wang-bin 作者的编译构建说明文档&#xff1a; Build QtAV wang-bin/QtAV Wiki GitHub 我的编译环境&#xff1a; 编译环境&#xff1a;win10/msvc2015/Qt5.6.3 第一步&#xff1a;GitHub拉取代码,执行子模块初始化 地址&#xff1a; …

2024-01-07-AI 大模型全栈工程师 - 做自己的产品经理

摘要 2024-01-07 周日 杭州 阴 本节内容: a. 如何做好独立开发设计&#xff0c;实现财富自由&#xff1b; 课程内容 1. 独立开发者 英文 indie hacker&#xff0c;是指独立开发软件产品的人&#xff1b;一人承担一个项目产品的所有工作&#xff1b; 2. 创业机会 云计算设…

JavaScript 设计模式之原型模式

原型模式 一般模式 所谓原型&#xff0c;一般就类似将数据放置到原型上&#xff0c;通过原型继承模式来实现一个基类&#xff0c;然后用子类继承方式来实现 // 汽车基类 const Car function (car,sale) {this.car car;this.sale sale;this.getName function () {}this.g…

C++ 内存模型

C内存模型 - MrYun - 博客园 (cnblogs.com) 内存区域 C内存分为5个区域&#xff1a;堆 heap &#xff1a; 由new分配的内存块&#xff0c;其释放编译器不去管&#xff0c;由我们程序自己控制&#xff08;一个new对应一个delete&#xff09;。如果程序员没有释放掉&#xff0c…

abap - 发送邮件,邮件正文带表格和excel附件

发送内容 的数据获取&#xff1a; 正文部分使用cl_document_bcs>create_document静态方法实现 传入参数为html内表结构 CLEAR lo_document .lo_document cl_document_bcs>create_document(i_type HTMi_text lt_htmli_length conlengthsi_subject lv_subje…