【opencv】示例-imagelist_creator.cpp 从命令行参数中创建一个图像文件列表(yaml格式)...

cpp 复制代码
/* 这个程序可以创建一个命令行参数列表的yaml或xml文件列表 */
// 包含必要的OpenCV头文件
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <string>
#include <iostream>


// 使用标准库中的特定标识符
using std::string;
using std::cout;
using std::endl;


// 使用OpenCV命名空间中的内容
using namespace cv;


// 帮助函数,用于展示如何使用这个程序
static void help(char** av)
{
  cout << "\nThis creates a yaml or xml list of files from the command line args\n"
      "usage:\n./" << av[0] << " imagelist.yaml *.png\n"
      << "Try using different extensions.(e.g. yaml yml xml xml.gz etc...)\n"
      << "This will serialize this list of images or whatever with opencv's FileStorage framework" << endl;
}


// main函数,程序的入口点
int main(int ac, char** av)
{
  cv::CommandLineParser parser(ac, av, "{help h||}{@output||}"); // 解析命令行参数
  if (parser.has("help")) // 如果有help参数
  {
    help(av); // 显示帮助信息
    return 0; // 退出程序
  }
  string outputname = parser.get<string>("@output"); // 获取输出文件的名称


  if (outputname.empty()) // 如果输出文件名是空的
  {
    help(av); // 显示帮助信息
    return 1; // 退出程序,并返回错误代码1
  }


  Mat m = imread(outputname); // 尝试读取输出文件名指定的图像,检查是否是一个图像文件以避免覆盖!
  if(!m.empty()){ // 如果成功读取到了图像
    std::cerr << "fail! Please specify an output file, don't want to overwrite your images!" << endl;
    help(av); // 显示帮助信息
    return 1; // 退出程序,并返回错误代码1
  }


  FileStorage fs(outputname, FileStorage::WRITE); // 创建FileStorage对象,用于将文件写入outputname
  fs << "images" << "["; // 开始写入图片列表
  for(int i = 2; i < ac; i++){ // 遍历所有命令行参数,排除了程序名和输出文件名
    fs << string(av[i]); // 将图像文件路径写入文件
  }
  fs << "]"; // 结束列表
  return 0; // 成功退出程序
}

该程序的功能是从命令行参数中创建一个图像文件列表,并将该列表序列化为yaml或xml格式的文件。该程序首先会检查用户是否指定帮助参数,如果指定了,则显示帮助信息并退出。然后程序会尝试解析命令行参数中指定的输出文件名,如果用户没有指定输出文件或输出文件已经存在并且不是为空的图像文件(避免覆盖现有图像文件),程序将显示错误信息并退出。如果输出文件名有效,程序将创建一个FileStorage对象,并循环遍历除命令行参数名和输出文件名以外的其他所有参数,将它们以列表的形式序列化到指定的yaml或xml文件中。

go 复制代码
./test.exe imagelist.yaml pic1.png pic2.png pic3.png pic4.png
相关推荐
极新12 分钟前
火山引擎解码生态型增长铁律
人工智能·火山引擎
杨景辉2 小时前
Yolov5 使用
人工智能·python·yolo
davidson14712 小时前
安装ego-planner-swarm
ubuntu·计算机视觉·无人机·px4·导航
大明者省3 小时前
AI 在课程思政的 10 大应用:从资源挖掘到效果升华
前端·人工智能·easyui
liliangcsdn6 小时前
金融领域LLM开源测试集
人工智能·金融
卓豪终端管理6 小时前
如何实现补丁管理自动化?
运维·人工智能·安全·网络安全·自动化·补丁管理·补丁自动化
llwszx7 小时前
Spring Boot 整合 Spring AI 与 MCP 开发智能体工具指南
人工智能·spring boot·spring·智能体·spring ai·mcp
xyzso1z7 小时前
飞书 MCP:AI 编码工具与飞书文档的桥梁
人工智能·飞书·mcp
Honeysea_707 小时前
目标检测相关【清晰易懂】
人工智能·计算机视觉·目标跟踪
Eric.Lee20218 小时前
数据集-目标检测系列- 杯子 数据集 bottle >> DataBall
人工智能·目标检测·计算机视觉·杯子检测·bottle detect