1 #include<opencv2/opencv.hpp>
2 #include<iostream>
3
4 using namespace std;
5 using namespace cv;
6
7 int main() {
8 //初始化
9 FileStorage fs("test.xml", FileStorage::WRITE);
10
11 //开始文件写入
12 fs << "frameCount" << 5;
13 time_t rawtime;
14 time(&rawtime);
15 fs << "calibrationDate" << asctime(localtime(&rawtime));
16 Mat cameraMatrix = (Mat_<double>(3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
17 Mat distCoeffs = (Mat_<double>(5, 1) << 0.1, 0.01, -0.001, 0, 0);
18 fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
19 fs << "features" << "[";
20 for (int i = 0; i < 3; i++) {
21 int x = rand() % 640;
22 int y = rand() % 480;
23 uchar lbp = rand() % 256;
24
25 fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
26 for (int j = 0; j < 8; j++)
27 fs << ((lbp >> j) & 1);
28 fs << "]" << "}";
29 }
30 fs << "]";
31 fs.release();
32
33 printf("文件读写完毕,请在工程目录下查看生成的文件~");
34 getchar();
35
36 return 0;
37 }
1 #include<opencv2/opencv.hpp>
2 #include<iostream>
3
4 using namespace std;
5 using namespace cv;
6
7 int main() {
8 //改变console 字体颜色
9 system("color 6F");
10
11 //初始化
12 FileStorage fs2("test.xml", FileStorage::READ);
13
14 //第一种方法,对FileNode操作
15 int frameCount = (int)fs2["frameCount"];
16 string date;
17
18 //第二种方法,使用FileNode运算符>>
19 fs2["calibrationDate"] >> date;
20
21 Mat cameraMatrix2, distCoeffs2;
22 fs2["cameraMatrix"] >> cameraMatrix2;
23
24 fs2["distCoeffs"] >> distCoeffs2;
25
26 cout << "frameCount:" << frameCount << endl
27 << "calibration date:" << date << endl
28 << "camera matrix:" << cameraMatrix2 << endl
29 << "distortion coeffs:" << distCoeffs2 << endl;
30
31 FileNode features = fs2["features"];
32 FileNodeIterator it = features.begin(), it_end = features.end();
33 int idx = 0;
34 vector<uchar> lbpval;
35
36 //使用FileNodeIterator遍历序列
37 for (; it != it_end; ++it, idx++) {
38 cout << "feature #" << idx << ":";
39 cout << "x+" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
40
41 //我们也可以使用filenode>>vector操作符来很容易地读数值阵列
42 (*it)["lpb"] >> lbpval;
43 for (int i = 0; i < (int)lbpval.size(); i++)
44 cout << " " << (int)lbpval[i];
45 cout << ")" << endl;
46 }
47
48 fs2.release();
49
50 //程序结束,输出一些帮助文字
51 printf("\n文件读取完毕!");
52 getchar();
53
54 return 0;
55 }