OpenCV xml与yaml文件使用

本文是基于毛星云主编的opencv 3编程入门(顺便推荐一下,这本书写的浅显易懂,看完之后,基本操作也都学的差不多了。Opencv3.3.1是本人使用的版本,其中的操作api有些已经更改,现在这里提一下),也算是自己画的重点吧。

XML(eXtensible Markup Language):元标记语言
YAML:出现是为了尝试比XML更为便捷,完成XML的任务

写入或读取XML或YAML文件的过程

  1. 实例化FileStorage类对象(两种解决方案)
  2. 使用流操作符<<进行文件写入,>>进行文件读取操作
  3. FileStorage::release()函数析构类对象,同时关闭文件

实例化类对象

方法1:

FileStorage fs(fileName,FileStorage::WRITE)//实例化对象fs
//fs设定为写入操作
//读取操作时,实例化对象方式写为FileStorage::READ

方法2:

FileStorage fs//实例化对象fs
fs.open(fileName,FileStorage::WRITE)

文件读写操作

文本和数字写入方法:

fs<<"youSetNameHere"<<data;
//data可为数字或文本
//也可为各类数据结构
//读取时,注意各类youSetNameHere是对应的

读取时:

int itNr;//当读取int数据时
fs["youSetNameHere"]>>itNr;//一种读取方式
itNr = (int) fs["youSetNameHere"];//另一种读取方式
//注意数据类型准确
//youSetNameHere在存储数据的xml,yml文件中不存在时
//读取数据为空

vector(arrays)和maps的输入和输出
vector结构的输入与输出,需要在第一个元素前加上”[“,在最后一个元素后加入”]”。
map结构需要使用的是” { “和” } “。

文件关闭

调用析构函数

fs.release();

实例

#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 << "calibrationData" << asctime(localtime(&rawtime));
    Mat cameraMatrix = (Mat_<double>(3, 3) << 1000.0, 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;
    fs << "features" << "[";
    for (int i = 0; i < 3; i++)
    {
        int x = rand() % 640;
        int y = rand() % 480;
        uchar lbp = rand() % 256;
        fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
        for (int j = 0; j < 8; j++)fs << ((lbp >> j) & 1);
        fs << "]" << "}";
    }
    fs << "]";
    fs.release();
    /***********************写入*********************/
    /***********************读取*********************/
    system("color 6F");
    FileStorage fs2("test.yaml", FileStorage::READ);
    int frameCount = (int)fs2["frameCount"];

    std::string data;
    fs2["calibrationData"] >> data;

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

    std::cout << "frameCount:\t" << frameCount
        << "\ncalibration data:\t" << data
        << "\ncamera matrix:\n" << cameraMatrix2
        << "\ndistortion coeffs:\t" << distCoeffs2 << std::endl;

    FileNode feature = fs2["features"];
    FileNodeIterator it = feature.begin(), it_end = feature.end();
    int idx = 0;
    std::vector<uchar> lbpval;
    for (; it != it_end; ++it, idx++)
    {
        std::cout << "features #" << idx << ":";
        std::cout << "x = " << (int)(*it)["x"] << ", y = " << (int)(*it)["y"]
            << ", lbp : (";
        (*it)["lbp"] >> lbpval;
        for (int i = 0; i < (int)lbpval.size(); i++)
            std::cout << "  " << (int)lbpval[i];
        std::cout << ")" << std::endl;
    }
    fs2.release();

    return 0;
}

猜你喜欢

转载自blog.csdn.net/id00000/article/details/80144873