C++ OpenCV创建xml,yml文件(图像)列表程序

        最近在学习关于相机标定的程序,标定时需要将图片打包成XML YML TXT格式,下面贴出生成xml图像序列的程序,同样适用于yml,记录一下,欢迎讨论~

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <string>
#include <iostream>
#include <io.h>

#define BMP "bmp";
#define PicFront "CapImg";//图片名字前缀,命名为CapImg*****.bmp
#define Zero4 "0000";//大于0小于10的图片前缀
#define Zero3 "000";//小于100的图片前缀
#define Zero2 "00";//小于1000的图片前缀
#define Zero1 "0";//小于10000的图片前缀

using namespace std;
using namespace cv;

//读入文件夹下指定格式的所有文件,本程序是读取文件夹下所有bmp图片
void getFiles(string path, string file_format, vector<string>& files)
{
	intptr_t   hFile = 0;//intptr_t和uintptr_t是什么类型:typedef long int/ typedef unsigned long int
	struct _finddata_t fileinfo;
	string p, file_formatName;
	if (0 != strcmp(file_format.c_str(), ""))
	{
		file_formatName = "\\*." + file_format;
	}
	else
	{
		file_formatName = "\\*";
	}
	if ((hFile = _findfirst(p.assign(path).append(file_formatName).c_str(), &fileinfo)) != -1)//assign方法可以理解为先将原字符串清空,然后赋予新的值作替换。
	{
		do
		{
			files.push_back(p.assign(path).append("\\").append(fileinfo.name));
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}
int main()
{
	string outputname = "imagelist.xml"; //图片列表保存路径,可自己设置,本程序中文本保存在.vcxproj文件夹下
	char imagename[20];
	Mat m = imread(outputname); //check if the output is an image - prevent overwrites!
	if (!m.empty())
	{
		std::cerr << "fail! Please specify an output file, don't want to overwrite you images!" << endl;
		return 1;
	}
	string filePath = "E:\\IME\\Job\\20171212vs2012dytcalibration\\20171212vs2012dytcalibration"; //正样本路径
	vector<string> files;
	//string file_format = "bmp";
	string file_format = BMP;
	getFiles(filePath, file_format, files);//读图
	int number = files.size();//文件数量
	string front_name = PicFront;
	string trans_name, end_name;
	string zero4 = Zero4;
	string zero3 = Zero3;
	string zero2 = Zero2;
	string zero1 = Zero1;
	FileStorage fs(outputname, FileStorage::WRITE); //利用FileStorage类定义对象fs,以写入方式打开文件
	fs << "ImageList" << "[";    //  "[" 此中括号意思是:表示开始写入文本系列
	for (int i = 1; i <= number; i++)  //有26张图片
	{
		if (i >= 0 && i < 10)
		{
			trans_name = zero2 + to_string(i) + "." + file_format;
			end_name = front_name + trans_name;
			strcpy(imagename, end_name.c_str());
			//sprintf(imagename, "CapImg00%d.bmp", i); //将图片名称写进imagename字符串中
		}
		else if(i < 100)
		{
			trans_name = zero1 + to_string(i) + "." + file_format;
			end_name = front_name + trans_name;
			strcpy(imagename, end_name.c_str());
			/*sprintf(imagename, "CapImg0%d.bmp", i); //将图片名称写进imagename字符串中*/
		}
		else
		{
			trans_name = to_string(i) + "." + file_format;
			end_name = front_name + trans_name;
			strcpy(imagename, end_name.c_str());
			//sprintf(imagename, "CapImg%d.bmp", i); //将图片名称写进imagename字符串中
		}

		fs << imagename;
	}
	fs << "]"; //关闭文本
	return 0;
}

得到图像列表如下:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值