1 #pragma once 2 #include<iostream> 3 #include<vector> 4 #include<opencv2\opencv.hpp> 5 class Config 6 { 7 public: 8 Config(); 9 ~Config(); 10 11 public: 12 13 std::vector<std::string> GetString_Vec(cv::FileStorage fs, std::string sName); 14 std::vector<cv::Point> GetPoint_Vec(cv::FileStorage fs, std::string sName); 15 std::vector<int> GetNum_Vec(cv::FileStorage fs, std::string sName); 16 cv::Point GetPoint(cv::FileStorage fs, std::string sName); 17 std::string GetString(cv::FileStorage fs, std::string sName); 18 int GetNum(cv::FileStorage fs, std::string sName); 19 };View Code
1 #include "Config.h" 2 3 Config::Config() 4 { 5 6 } 7 Config::~Config() 8 { 9 10 } 11 cv::Point Config::GetPoint(cv::FileStorage fs, std::string sName) 12 { 13 cv::Point poi; 14 cv::FileNode map_node = fs[sName]; 15 map_node >> poi; 16 return poi; 17 } 18 std::string Config::GetString(cv::FileStorage fs, std::string sName) 19 { 20 std::string str; 21 cv::FileNode map_node = fs[sName]; 22 map_node >> str; 23 return str; 24 } 25 int Config::GetNum(cv::FileStorage fs, std::string sName) 26 { 27 int iNum; 28 cv::FileNode map_node = fs[sName]; 29 map_node >> iNum; 30 return iNum; 31 } 32 std::vector<std::string> Config::GetString_Vec(cv::FileStorage fs, std::string sName) 33 { 34 std::vector<std::string> Str_vec; 35 cv::FileNode n = fs[sName]; 36 cv::FileNodeIterator it = n.begin(), it_end = n.end(); 37 for (; it != it_end; ++it) 38 Str_vec.push_back((std::string)*it); 39 return Str_vec; 40 } 41 std::vector<cv::Point> Config::GetPoint_Vec(cv::FileStorage fs, std::string sName) 42 { 43 std::vector<cv::Point> Point_vec; 44 cv::FileNode n = fs[sName]; 45 std::cout << n.size() << std::endl; 46 for (size_t i = 0; i < n.size(); ++i) 47 { 48 cv::Point2f poi; 49 n[i] >> poi; 50 Point_vec.push_back(poi); 51 } 52 return Point_vec; 53 } 54 std::vector<int> Config::GetNum_Vec(cv::FileStorage fs, std::string sName) 55 { 56 std::vector<int> Num_vec; 57 cv::FileNode n = fs[sName]; 58 cv::FileNodeIterator it = n.begin(), it_end = n.end(); // 遍历节点 59 for (; it != it_end; ++it) 60 Num_vec.push_back((int)*it); 61 return Num_vec; 62 }View Code
1 struct strYml 2 { 3 const std::string sWidth = "sWidth"; 4 5 const std::string calibPt = "sCalibPt"; 6 7 const std::string sPts = "sPts"; 8 9 const std::string sET = "sET"; 10 11 const std::string strCom = "strCom"; 12 13 const std::string sModeName = "sModeName"; 14 15 const std::string sModePath = "sModePath"; 16 17 const std::string sModeImg = "sModeImg"; 18 19 }; 20 21 struct outYmlStr 22 { 23 int iWidth; //像素宽度 24 25 cv::Point calibPt; //标定点 26 27 std::vector<cv::Point>pts; //偏移量 28 }; 29 30 struct paraSet 31 { 32 double exploreTime; //曝光时间 33 34 std::string strCom; //串口端口号 35 36 std::string sModeFileName; //模板名称 37 38 std::string sModePath; //模板文件保存路径 39 40 std::string sModeImgPath; //模板图像名称 41 };View Code
1 void sConfigExpalain(std::string sConfigPath, paraSet& paraSets, outYmlStr& modeStruct) 2 { 3 strYml sYmlConfig; 4 FileStorage fs(sConfigPath, FileStorage::READ); //FileStorage::READ 读或写 5 paraSets.exploreTime = sConfig.GetNum(fs, sYmlConfig.sET); 6 paraSets.sModeFileName = sConfig.GetString(fs, sYmlConfig.sModeName); 7 paraSets.sModeImgPath = sConfig.GetString(fs, sYmlConfig.sModeImg); 8 paraSets.sModePath = sConfig.GetString(fs, sYmlConfig.sModePath); 9 paraSets.strCom = sConfig.GetString(fs, sYmlConfig.strCom); 10 11 //读取模板图像 12 cv::Mat mMode = cv::imread(paraSets.sModeImgPath); 13 std::string sModeYml = paraSets.sModePath; 14 FileStorage fR(sModeYml, FileStorage::READ); //FileStorage::READ 读或写 15 modeStruct.calibPt = sConfig.GetPoint(fR, sYmlConfig.calibPt); 16 modeStruct.iWidth = sConfig.GetNum(fR, sYmlConfig.sWidth); 17 modeStruct.pts = sConfig.GetPoint_Vec(fR, sYmlConfig.sPts); 18 19 20 }View Code
1 template<typename T> void WArrayToYml(cv::FileStorage fs, std::string sName, T& a) 2 { 3 fs << sName << "["; 4 for (int i = 0; i < a.size(); i++) 5 { 6 fs << a[i]; 7 } 8 9 fs << "]"; 10 11 } 12 //写元素 13 template<typename T> void WValueToYml(cv::FileStorage fs, std::string sName, T& a) 14 { 15 fs << sName << a; 16 }View Code
1 void bWriteModeStruct(std::string sModePath,const outYmlStr& modeStruct) 2 { 3 FileStorage fs(sModePath, FileStorage::WRITE); //FileStorage::READ 读或写 4 strYml outStr; 5 WValueToYml(fs, outStr.sWidth, modeStruct.iWidth); 6 WValueToYml(fs, outStr.calibPt, modeStruct.calibPt); 7 WArrayToYml(fs, outStr.sPts, modeStruct.pts); 8 }View Code
以上程序基于opencv可实现windows平台下文件的解析。