opencv API:CommandLineParser

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  接口的源码解析

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值