OpenCV获取视频帧

mac2024-07-12  56

     视频是一帧帧的图像,视频处理的是运动图像,而不是静止图像。视频资源可以是一个专用摄像机、网络摄像头、视频文件或图像文件序列。 OpenCV 中,VideoCapture 类和 VideoWriter 类为视频处理中所涉及的捕获和记录任务提供了一个易用的 C++API。     下方的 recVideo 示例是一个简短的代码片段,可以让你了解如何使用一个默认摄像机作为一个捕捉设备来抓取帧,对它们进行边缘检测,并且将新的转换视频帧作为一个文件保存。而且,创建两个窗口同时显示原始帧和处理过的帧。

#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; int main(int, char **) { Mat in_frame, out_frame; const char win1[]="Grabbing...", win2[]="Recording..."; double fps=30;//每秒的帧数 char file_out[]="recorded.avi"; VideoCapture inVid(O) ; //打开默认摄像机 if ( !inVid.isOpened () ) { //检查错误 cout << "Error! Camera not ready...\n"; return -1; } //获取输入视频的宽度和高度 int width = (int)inVid.get(CAP_PROP_FRAME_WIDTH); int height = (int)inVid.get(CAP_PR〇P_FRAME_HEIGHT); VideoWriter recVid(file out,VideoWriter::fourcc('M','S','V','C'), fps, Size(width, height)); if (!recVid.isOpened()) { cout << "Error! Video file not opened...\n"; return -1; } //为原始视频和最终视频创建两个窗口 namedWindow(win1); namedWindow(win2); while (true) { //从摄像机读取帧(抓取并解码) inVid >> in frame; //将帧转换为灰度 cvtColor(in_frame, out_frame, C0L0R_BGR2GRAY); //将幀写入视频文件(编码并保存) recVid << out_frame ? imshow (win1, in_frame);// 在窗口中显示帧 imshow(win2, out_frame); // 在窗口中显示帧 if (waitKey(1000/fps) >= 0) break; } inVid.release(); // 关闭摄像机 return 0; }

 

最新回复(0)