opencv learning (14) XML//YAML reading and writing (similarly can be replaced by txt, doc)//Involving FileStorage()//Read and write symbols//FileNodeIterator() to get nodes

xml, yaml

write picture description here

how to use

write picture description here
write picture description here

Step 1, FileStorage() is opened (preparation before read/write, definition)

write picture description here

Step 2, read and write operation functions (symbols)

write picture description here
write picture description here

Step 3, vector/maps type input and output, FileNodeIterator reads the string to get the node

write picture description here

Step 4, close the file

write picture description here
write picture description here

Instance, xml, yaml file write

#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 files, etc. to read

#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;
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324534373&siteId=291194637