opencv学习(十四)XML//YAML的读取和写入(同理可用txt,doc替代)//涉及FileStorage()//读写符号//FileNodeIterator()得到节点

xml,yaml介绍

这里写图片描述

如何使用

这里写图片描述
这里写图片描述

步骤一,FileStorage()打开(读/写前的准备,定义)

这里写图片描述

步骤二,读写操作函数(符号)

这里写图片描述
这里写图片描述

步骤三,vector/maps类型输入输出,FileNodeIterator读取字符串的得到节点

这里写图片描述

步骤四,关闭文件

这里写图片描述
这里写图片描述

实例,xml,yaml文件写入

#include<opencv2/opencv.hpp>
#include<time.h>
using namespace cv;

int main()
{
    //初始化
    FileStorage fs("test.yaml", FileStorage::WRITE);

    //开始文件写入
    fs << "frameCount" << 5;
    time_t rawtime; time(&rawtime);//此行何意???
    fs << "calibrationDate" << asctime(localtime(&rawtime));//此行何意???(读取时间量?)
     Mat cameraMatrix = (Mat_<double>(3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
    Mat distCoeffs = (Mat_<double>(5, 1) << 0.1, 0.01, -0.001, 0, 0);//此行何意???
    fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;//读取Mat型cameraMatrix,distcoeffs的内容
    fs << "feature" << "[";
    for (int i = 0; i < 3; i++)
    {
        int x = rand() % 640;
        int y = rand() % 480;
        uchar ibp = rand() % 256;//%何意?

        fs << "{:" << "x" << x << "y" << y << "ibp" << "[:";
        for (int j = 0; j < 8; j++)
            fs << ((ibp >> j) & 1);
        fs << "]" << "}";
    }
    fs << "]";
    fs.release();
    printf("完毕,请在工程目录下查看文件-");
    getchar();

    return 0;
}

xml,yaml文件等读取

#include<opencv2/opencv.hpp>
#include<time.h>
using namespace cv;
using namespace std;

int main()
{
    //改变consolo字体颜色
    system("color 6F");

    //初始化
    FileStorage fs2("test.yaml", FileStorage::READ);

    //开始文件读取
    //法一,对FileNode操作
    int frameCount2 = (int)fs2["framecount2"];
    std::string date;//此行何意??(定义字符串 date)

                     //法二,使用FileNode运算符>>
    fs2["calibrationDate"] >> date;

    Mat cameraMatrix2, distCoeffs2;
    fs2["cameraMatrix"] >> cameraMatrix2;
    fs2["distCoeffs"] >> distCoeffs2;//读取

    cout << "frameCount2:" << frameCount2 << endl
        << "calibration date:" << date << endl
        << "camera matrix:" << cameraMatrix2 << endl
        << "distortion coeffs:" << distCoeffs2 << endl;

    FileNode feature = fs2["feature"];
    FileNodeIterator it = feature.begin(), it_end = feature.end();//定义it
    int idx = 0;
    std::vector<uchar>ibpval;//定义向量容器ibpal?

    //使用FileNodeIterator历遍序列(读取)
    for (; it != it_end; it++, idx++)
    {
        cout << "feature#" << idx << ":";
        cout << "x=" << (int)(*it)["x"] << ",y=" << (int)(*it)["y"] << ",ibp:(";
        //也可以使用filenod>>std::vector操作符很容易读取数值阵列
        (*it)["ibp"] >> ibpval;
        for (int i = 0; i < (int)ibpval.size(); i++)
            cout << "" << (int)ibpval[i];
        cout << ")" << endl;
    }


    fs2.release();
    printf("读取完毕,请按任意键结束-");
    getchar();

    return 0;
}
}
阅读更多

扫码向博主提问

露一手吧

非学,无以致疑;非问,无以广识
去开通我的Chat快问

没有更多推荐了,返回首页