C++ OpenCV 获取视频当前帧图像并处理
可以使用 OpenCV 中的 VideoCapture 类来读取视频,并使用该类的 read() 函数来获取当前帧图像。具体代码如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
VideoCapture capture('test.mp4'); // 打开视频文件
if (!capture.isOpened()) { // 判断是否成功打开
cout << "Error opening video file" << endl;
return -1;
}
Mat frame;
while (capture.read(frame)) { // 循环读取每一帧
// 在这里对 frame 进行处理,例如显示、保存等
// ...
imshow('Frame', frame); // 显示当前帧图像
waitKey(30); // 等待一段时间,以便显示图像
}
capture.release(); // 释放资源
destroyAllWindows(); // 关闭所有窗口
return 0;
}
在上述代码中,每次循环调用 capture.read(frame) 可以获取视频的下一帧图像,并将其存储在名为 frame 的 Mat 对象中。在循环中对 frame 进行处理即可实现对当前帧图像的操作。
原文地址: https://www.cveoy.top/t/topic/oduf 著作权归作者所有。请勿转载和采集!