OpenCV4入门教程076:文件之XML读写

索引地址:系列索引

OpenCV中最常用的文件是xml格式,训练完成的模型,一些配置文件都是这种格式。

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() {
//========建立节点(写数据)=========
Mat src = (Mat_<double>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
FileStorage fswrite("stereo.xml", FileStorage::WRITE);
fswrite << "src1" << src;
fswrite.release();
cout << "Write Finished" << endl;
//========遍历节点(读数据)=========
FileStorage fsread("test.xml", FileStorage::READ);
Mat dst;
fsread["src1"] >> dst;
cout << dst << endl;
fsread.release();
cout << "Read Finished" << endl;
return 0;
}

执行输出为:

1
2
3
4
5
6
$ ./xml   
Write Finished
[1, 2, 3;
4, 5, 6;
7, 8, 9]
Read Finished

结果文件为:

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<opencv_storage>
<src1 type_id="opencv-matrix">
<rows>3</rows>
<cols>3</cols>
<dt>d</dt>
<data>
1. 2. 3. 4. 5. 6. 7. 8. 9.</data></src1>
</opencv_storage>

OpenCV4入门教程076:文件之XML读写
https://feater.top/opencv/opencv-xml-file-read-write/
作者
JackeyLea
发布于
2020年9月20日
许可协议