C++ OpenCV创建xml,yml文件(图像)列表程序
生活随笔
收集整理的這篇文章主要介紹了
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 intstruct _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;
}
得到圖像列表如下:
總結
以上是生活随笔為你收集整理的C++ OpenCV创建xml,yml文件(图像)列表程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Opencv 读取指定路径中的所
- 下一篇: error C4996: 'fopen'