OpenCV 二、视频文件的逐帧读取和任意帧读取
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
本文主要介绍在OpenCV中如何以逐帧方式和任意帧方式读取一个视频文件。
一、视频信息的读取
OpenCV中,VideoCapture类用于视频文件的读取,其方法get用于视频信息的获取。
比如获取,视频的宽高,帧数,原始视频的帧频。
VideoCapture cap;
cap.open("./test.mp4");
if (!cap.isOpened())//如果视频不能正常打开则返回
return 0;
int frameRate = static_cast<int>(cap.get(CV_CAP_PROP_FPS)); //帧率 x frames/s
int totalFrames = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_COUNT)); //总帧数
int width = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH)); //帧宽度
int height = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT)); //帧高度
cout << "视频宽度=" << width << endl;
cout << "视频高度=" << height << endl;
cout << "视频总帧数=" << totalFrames << endl;
cout << "帧率=" << frameRate << endl;
二、视频的逐帧去读
OpenCV中,从一个视频文件中按顺序逐帧获取图像是非常简单的,这里提供三种方法:
1、流操作 >> 方法
2、read()方法
3、grap()和retrieve()方法
其实,read()方法就是将grab和retrieve整合到了一个调用函数中而已。
VideoCapture cap;
cap.open("./test.mp4");
if (!cap.isOpened())//如果视频不能正常打开则返回
return 0;
Mat frame;
double t = 0, fps = 0;
while (1)
{
t = (double)cv::getTickCount();
//方法1:
//cap >> frame;
//方法2:
//cap.read(frame);
//方法3:
cap.grab();
cap.retrieve(frame);
if (frame.empty())//如果某帧为空则退出循环
break;
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
fps = 1.0 / t;
cout << " 帧频是: " << fps << endl;
}
三、视频的任意帧获取
视频的任意位置获取,需要首先设置任意帧的位置,
VideoCaputure 通过其方法set设置要获取帧的位置,具体有以下几种方法:
set(CAP_PROP_POS_MSEC, 毫秒为单位的时间位置)
set(CAP_PROP_POS_FRAMES , 任意帧的索引位置)
set(CAP_PROP_POS_AVI_RATIO , 任意帧的位置在整个视频中的位置比例, 数值为0-1)
比如:下面的例子是以6帧的间隔读取视频,不过亲测,读取速度较慢!
int timeStame = 0;
while (1)
{
timeStame += 6;
t = (double)cv::getTickCount();
cap.set(CAP_PROP_POS_FRAMES, timeStame);
cap.read(frame);
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
fps = 1.0 / t;
cout << " 帧频是: " << fps << endl;
//cap >> frame;//等价于cap.read(frame);
if (frame.empty())//如果某帧为空则退出循环
break;
}
GitHub 加速计划 / opencv31 / opencv
166
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
c623a5af
Android camera refactoring #26646
This patch set does not contain any functional changes. It just cleans up the code structure to improve readability and to prepare for future changes.
* videoio(Android): Use 'unique_ptr' instead of 'shared_ptr'
Using shared pointers for unshared data is considered an antipattern.
* videoio(Android): Make callback functions private static members
Don't leak internal functions into global namespace. Some member
variables are now private as well.
* videoio(Android): Move resolution matching into separate function
Also make internally used member functions private.
* videoio(Android): Move ranges query into separate function
Also remove some unneccessary initialisations from initCapture().
* videoio(Android): Wrap extremly long source code lines
* videoio(Android): Rename members of 'RangeValue'
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
4 小时前
7728dd33
Fix potential READ memory access #26782
This fixes https://oss-fuzz.com/testcase-detail/4923671881252864 and https://oss-fuzz.com/testcase-detail/5048650127966208
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
6 小时前
更多推荐
已为社区贡献1条内容
所有评论(0)