CommandLineParser 函数接口
使用录制视频的功能做测试。
const std::string keys=
"{i|.../***.avi|video path}"
"{@fps|25|fps}"
"{o|.../***.avi|video saved path}";
{&&&&|xxxx|%%%%%%}
其中:
&&&&: 表示参数名称
xxxx :表示参数内容(即默认值)
%%%%%%:参数的解释说明
注意:
参数使用时需要添加 "-" 或者 ”--“,当输入指令后面没有参数,默认为true,
另外,前面有@的参数在使用时可以不添加指令名称,直接设置指令内容即可
没有输入的指令,则使用key_map中的默认值
#include<opencv2/opencv.hpp>
#include<string>
#include <iostream>
const std::string keys=
"{i |/home/images/20211515_1.mp4|video path}"
"{@fps| 25|fps}"
"{o | /home/images/example.avi|video saved path}";
int main(int argc,char** argv)
{
cv::CommandLineParser parser(argc,argv,keys);
std::string videoDir=parser.get<std::string>("i");
double fps=parser.get<double>("@fps");
std::string videoSavedDir=parser.get<std::string>("o");
cv::VideoCapture capture;
cv::VideoWriter writer;
int fourcc=cv::VideoWriter::fourcc('M','J','P','G');
// if(videoDir.size()==1)
// {
// capture.open(atoi(videoDir.c_str()));
// }
// else
// {
// capture.open(videoDir);
// }
capture.open(videoDir);
if(!capture.isOpened()){
printf("File failed to open.!!!\n");
}
// double fps=capture.get(cv::CAP_PROP_FPS);
double width=capture.get(cv::CAP_PROP_FRAME_WIDTH);
double height=capture.get(cv::CAP_PROP_FRAME_HEIGHT);
writer.open(videoSavedDir,fourcc,fps,cv::Size(width,height),true);
cv::Mat frame;
while(true)
{
capture.read(frame);
cv::imshow("test",frame);
cv::waitKey(30);
writer.write(frame);
int k=0;
if(k==27)
{
break;
}
}
capture.release();
writer.release();
return 0;
}
指令使用:
./commandLineParser_example_00 -i=/home/wang/images/drums10.mp4 -o=/home/images/00100.avi
或:
./commandLineParser_example_00 -i=/home/wang/images/drums10.mp4 10 -o=/home/wang/images/00100.avi
参考:
https://blog.csdn.net/xuelangwin/article/details/80481746 接口的源码解析