河源网站搭建费用,天津全面解封最新通告,新浪云wordpress,鞍山怎么做平台软件/**
* file videocapture_starter.cpp
* brief 一个使用OpenCV的VideoCapture与捕获设备#xff0c;视频文件或图像序列的入门示例
* 就像CV_PI一样简单#xff0c;对吧#xff1f;
*
* 创建于: 2010年11月23日
* 作者: Ethan Rublee
*
* 修改于: 2013年4月17日
* … /**
* file videocapture_starter.cpp
* brief 一个使用OpenCV的VideoCapture与捕获设备视频文件或图像序列的入门示例
* 就像CV_PI一样简单对吧
*
* 创建于: 2010年11月23日
* 作者: Ethan Rublee
*
* 修改于: 2013年4月17日
* 作者: Kevin Hughes
*/#include opencv2/imgcodecs.hpp // 包含处理图像编解码的功能
#include opencv2/videoio.hpp // 包含处理视频读写的功能
#include opencv2/highgui.hpp // 包含GUI函数以及视频读写功能#include iostream // 包含基本输入输出功能
#include stdio.h // 包含C语言标准输入输出库using namespace cv; // 使用命名空间cv避免每次调用OpenCV功能时都要加cv::前缀
using namespace std; // 使用标准命名空间std避免每次都要加std::前缀// 在匿名命名空间中隐藏局部函数
namespace {void help(char** av) { // help函数提供了使用说明cout The program captures frames from a video file, image sequence (01.jpg, 02.jpg ... 10.jpg) or camera connected to your computer. endl Usage:\n av[0] video file, image sequence or device number endl q,Q,esc -- quit endl // 按q、Q或esc键退出程序 space -- save frame endl endl // 按空格键保存帧图片 \tTo capture from a camera pass the device number. To find the device number, try ls /dev/video* endl \texample: av[0] 0 endl // 提供了如何使用摄像头设备进行捕获的示例 \tYou may also pass a video file instead of a device number endl \texample: av[0] video.avi endl // 提供了如何打开视频文件的示例 \tYou can also pass the path to an image sequence and OpenCV will treat the sequence just like a video. endl \texample: av[0] right%%02d.jpg endl; // 提供了如何打开图像序列的示例}int process(VideoCapture capture) { // process函数用于处理视频捕获的过程int n 0;char filename[200]; // 用于存储文件名的字符数组string window_name video | q or esc to quit; // 显示窗口的名称cout press space to save a picture. q or esc to quit endl; // 提示用户按空格保存图片按q或esc退出namedWindow(window_name, WINDOW_KEEPRATIO); // 创建一个可调整大小的窗口Mat frame; // 创建一个Mat对象用于存储每一帧图像数据for (;;) {capture frame; // 从VideoCapture对象中获取一帧图像到frame中if (frame.empty()) // 如果帧为空则退出循环break;imshow(window_name, frame); // 显示当前帧char key (char)waitKey(30); // 等待30毫秒如果有按键则返回按键值switch (key) {case q:case Q:case 27: // 按esc键return 0; // 退出程序case : // 按空格键保存一幅图像snprintf(filename,sizeof(filename),filename%.3d.jpg,n);imwrite(filename,frame); // 将当前帧保存为文件cout Saved filename endl;break;default:break;}}return 0;}
}int main(int ac, char** av) {CommandLineParser parser(ac, av, {help h||}{input||}); // 命令行解析器用于解析命令行参数if (parser.has(help)) // 如果指定了help参数则显示帮助信息{help(av); // 调用help函数return 0;}string arg parser.getstring(input); // 获取输入参数可能是视频文件名、图像序列或设备号if (arg.empty()) { // 如果没有输入参数则显示帮助信息并退出help(av);return 1;}VideoCapture capture(arg); // 尝试以视频文件名或图像序列的形式打开输入if (!capture.isOpened()) // 如果打开失败则尝试将输入当作视频设备号来打开capture.open(atoi(arg.c_str()));if (!capture.isOpened()) { // 如果还是打开失败显示错误信息并显示帮助信息后退出cerr Failed to open the video device, video file or image sequence!\n endl;help(av);return 1;}return process(capture); // 调用process函数处理视频捕获过程
} 这段代码是一个使用OpenCV库进行视频捕获的C程序例子。程序可以从视频文件、图像序列如01.jpg, 02.jpg等或连接到计算机的摄像头中捕获帧。用户可以通过命令行参数指定输入源并且通过按键操作来保存帧或退出程序。程序首先定义了两个函数help函数用于显示使用方法process函数用于捕获视频并处理按键操作。在main函数中程序会根据命令行参数尝试打开视频拍摄设备、视频文件或图像序列文件并在成功打开后调用process函数来捕获和处理视频帧。如果无法打开指定的输入源则会提示错误信息并显示帮助信息。