学OpenCV

================================================

读写XML和YML文件。

================================================

用处?比方说标定完相机后,相关参数要保存一下,以便未来使用。

================================================

  1 #include <iostream>
  2 
  3 #include <opencv2/opencv.hpp>
  4 #include <opencv2/core/utils/logger.hpp>
  5 
  6 
  7 void Test_write(std::string pathfile)
  8 {
  9     cv::FileStorage file(pathfile, cv::FileStorage::WRITE);
 10     
 11     cv::Mat mat=cv::Mat::eye(3,3,CV_16UC2);
 12 
 13     file << "eye"<<mat;
 14 
 15     file.write("i", 100);
 16     file.write("d", 100.0);
 17     file.write("dd", 100.100);//dd: 100.09999999999999
 18 
 19     float d1 = 101.0;//不区分float和double
 20     file << "d1" << d1;
 21     double d2 = 101.0;
 22     file << "d2" << d1;
 23 
 24     std::string str1 = "str1";//不区分string和字符串常量
 25     file << "str1" << str1;
 26     file << "str2" << "str2";
 27 
 28     file << "arr1" << "["<<1 <<2 <<3 << "]";//不区分数组中数据类型
 29     file << "arr2" << "[" << 1 << "2" << 3 << "]";
 30 
 31     file << "yyyymmddarr" 
 32                      << "{" 
 33                      << "yyyy"<<2024
 34                      << "mm"<<8
 35                      << "dd"<<19
 36                      << "arr"<<"["
 37                                 <<12<<34<<56
 38                              <<"]"
 39                      << "}";
 40 
 41     file.release();
 42 }
 43 
 44 void Test_read(std::string pathfile)
 45 {
 46     cv::FileStorage file(pathfile, cv::FileStorage::READ);
 47     if (file.isOpened() == false)
 48     {
 49         std::cout << "open file failed."  << std::endl;
 50     }
 51 
 52     cv::Mat mat;
 53     file["eye"] >> mat;
 54     
 55     int i;
 56     file["i"] >> i;
 57 
 58     float f_d;
 59     double d_d;
 60     float f_dd;
 61     double d_dd;
 62 
 63     file["d"] >> f_d;
 64     file["d"] >> d_d;
 65     file["dd"] >> f_dd;
 66     file["dd"] >> d_dd;
 67 
 68     float d1;
 69     double d2;
 70     file["d1"] >> d1;
 71     file["d2"] >> d2;
 72 
 73     std::string str1;
 74     std::string str2;
 75     file["str1"] >> str1;
 76     file["str2"] >> str2;
 77 
 78     cv::FileNode arr1;
 79     arr1 = file["arr1"];
 80     for (cv::FileNodeIterator it = arr1.begin(); it != arr1.end(); it++)
 81     {
 82         int i = 0;
 83         int j = 0;
 84         
 85         *it >> i;
 86         j = *it;
 87 
 88         std::cout << i <<" " << j << std::endl;
 89     }
 90 
 91     int l = arr1.size();
 92     for (int i = 0; i < l; i++)
 93     {
 94         int v = 0;
 95         
 96         v=file["arr1"][i];
 97 
 98         std::cout << v << std::endl;
 99     }
100 
101     //这个有点特别,写入时不区分,怎么读取呢?
102     //经实验,可以强行解释转换,但数据是不对的。
103     cv::FileNode arr2;
104     arr2 = file["arr2"];
105     for (cv::FileNodeIterator it = arr2.begin(); it != arr2.end(); it++)
106     {
107         int i = 0;
108         int j = 0;
109 
110         *it >> i;
111         j = *it;
112 
113         std::cout << i << " " << j << std::endl;
114     }
115 
116     int i1=file["arr2"][0];
117     std::string stra2=file["arr2"][1];
118     int i2=file["arr2"][2];
119     std::cout <<i1<<" "<<stra2 <<" " <<i2 << std::endl;
120     
121     cv::FileNode ext=file["yyyymmddarr"];
122     int iyyyy=ext["yyyy"];
123     int imm = ext["mm"];
124     int idd = ext["dd"];
125     int hh = ext["arr"][0];
126     int min = ext["arr"][1];
127     int sec = ext["arr"][2];
128     std::cout << iyyyy << "-" << imm << "-" << idd <<" "<<hh<<":"<<min<<":"<<sec << std::endl;
129 
130     cv::waitKey();
131 }
132 
133 
134 int main()
135 {
136     cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);
137     
138     Test_write(R"(C:\ProjectHome\test\LeanOpenCV\Test_XML_YML\test.yml)");
139     Test_read(R"(C:\ProjectHome\test\LeanOpenCV\Test_XML_YML\test.yml)");
140     std::cout <<"======" << std::endl;
141     Test_write(R"(C:\ProjectHome\test\LeanOpenCV\Test_XML_YML\test.xml)");
142     Test_read(R"(C:\ProjectHome\test\LeanOpenCV\Test_XML_YML\test.xml)");
143 
144     cv::waitKey(0);
145 
146     return 0;
147 }
148 
149 
150 #if 0
151 % YAML:1.0
152 -- -
153 eye : !!opencv - matrix
154 rows : 3
155 cols : 3
156 dt : "2w"
157 data : [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0]
158 i : 100
159 d : 100.
160 dd : 100.09999999999999
161 d1 : 101.
162 d2 : 101.
163 str1 : str1
164 str2 : str2
165 arr1 :
166 -1
167 - 2
168 - 3
169 arr2 :
170     -1
171     - "2"
172     - 3
173     yyyymmddarr :
174     yyyy : 2024
175     mm : 8
176     dd : 19
177     arr :
178     -12
179     - 34
180     - 56
181 
182 #endif
183 
184 #if 0
185 < ? xml version = "1.0" ? >
186 <opencv_storage>
187 <eye type_id = "opencv-matrix">
188 < rows>3 < / rows >
189 < cols>3 < / cols >
190 < dt>"2w" < / dt >
191 < data>
192 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 < / data > < / eye>
193 < i>100 < / i >
194 < d>100. < / d >
195 < dd>100.09999999999999 < / dd >
196 < d1>101. < / d1 >
197 < d2>101. < / d2 >
198 <str1>str1< / str1>
199 <str2>str2< / str2>
200 < arr1>
201 1 2 3 < / arr1 >
202 < arr2>
203 1 "2" 3 < / arr2 >
204 <yyyymmddarr>
205 < yyyy>2024 < / yyyy >
206 < mm>8 < / mm >
207 < dd>19 < / dd >
208 < arr>
209 12 34 56 < / arr > < / yyyymmddarr>
210 < / opencv_storage>
211 
212 #endif
213 
214 
215 #if 0
216 1 1
217 2 2
218 3 3
219 1
220 2
221 3
222 1 1
223 2147483647 2147483647
224 3 3
225 1 2 3
226 2024 - 8 - 19 12:34 : 56
227 ======
228 1 1
229 2 2
230 3 3
231 1
232 2
233 3
234 1 1
235 2147483647 2147483647
236 3 3
237 1 2 3
238 2024 - 8 - 19 12:34 : 56
239 
240 C : \ProjectHome\test\LeanOpenCV\x64\Debug\Test_XML_YML.exe(进程 7568)已退出,代码为 0 (0x0)。
241 要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
242 按任意键关闭此窗口. . .
243 #endif