你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

opencv中XML文件的读与写

2022/8/16 19:18:57

opencv中XML文件的读与写

OpenCV中有相关的函数对XML文件进行写与读的操作,

FileStorage fs("../xml/read.xml", FileStorage::WRITE);

FileStorage fs2("../xml/read.xml",FileStorage::READ);

 

#include<opencv2/opencv.hpp>
#include<vector>

using namespace std;
using namespace cv;

void other_way_xml();
int main()
{
    //xml写入
    FileStorage fs("../xml/read.xml", FileStorage::WRITE);
    fs << "one" << 1;

    fs << "array1" << "[" << 1 << 2 << 3 << "]";

    fs << "array2" << "[";
    float a = 1.0; float b = 2.0; float c = 3.0;
    fs << a << b << c << "]";
    
    fs.release();

    FileStorage fs2("../xml/read.xml",FileStorage::READ);

    int d;
    fs2["one"] >> d;
    cout << d << endl;

    vector<float> array1, array2;
    fs2["array1"] >> array1;
    cout << array1[0] << "," << array1[1] << "," << array1[2] << endl;

    fs2["array2"] >> array2;
    cout << array2[0] << "," << array2[1] << "," << array2[2] << endl;
    fs2.release();

    other_way_xml();
    return 0;
}

void other_way_xml()
{
    //
    FileStorage fs("../xml/read2.xml", FileStorage::WRITE);

    float a = 1.0; float b = 2.0; float c = 3.0;
    fs << "big_class" << "[";  //大类中的细分小类
    fs << "{:" << "class1" << a << "class2" << b << "class3" << c << "}" << "]";
    fs.release();

    //
    FileStorage fs2("../xml/read2.xml", FileStorage::READ);
    FileNode hand_point = fs2["big_class"];
    FileNodeIterator it = hand_point.begin(), it_end = hand_point.end();
    cout << "class1:" << (float)(*it)["class1"] << ",class2:" << (float)(*it)["class2"] << ",class3:" << (float)(*it)["class3"] << endl;

    fs2.release();

    return;
}

 

写入的xml文件如下图所示:

read.xml:

 

read2.xml: 

 

 运行结果: