Opencv YAML和XML格式文件操作详解

前端之家收集整理的这篇文章主要介绍了Opencv YAML和XML格式文件操作详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本系列文章 @YhL_Leo 出品,转载请注明出处。
文章链接 http://www.jb51.cc/article/p-eyzsfhvb-qe.html

本文参考Opencv 2.4.11 documentation整理对YAMLXML文件实现I/O操作的方法

官网:YAML:http://www.yaml.org XML :http://www.w3c.org/XML

1.YAML与XML文件的打开和关闭

YAML格式的文件拓展名包括.yml.yaml,两个都表示YAML文件
XML格式的文件拓展名为: .xml

1.1 文件打开

在Opencv中,使用FileStorage进行文件读写。XML文件操作与YAML一样,不过存在一些细小差别。

std::string fileName  = "E:\\test.yml"; // YAML
std::string fileName2 = "E:\\test.xml"; // XML
// write file
cv::FileStorage fs(fileName,cv::FileStorage::WRITE);
// read file
cv::FileStorage fs2(fileName,cv::FileStorage::READ);
// or use: cv::FileStorage::open
fs2.open(fileName,cv::FileStorage::READ);

FileStorage文件操作模式一共分为四种:READWRITEAPPENDMEMORY

文档打开后很关心的一件事就是,进行确认是否成功。FileStorage有自己的成员函数返回文件打开状态:

// bool FileStorage::isOpened() const;
if ( !fs.isOpened() ) // Failed
{
    std::cout<<"Save File Failed!"<<std::endl;
    return ;
}
else // succeed
{
    ...
}

1.2 文件关闭

FileStorage文件关闭比较简单:

fs.release();

2.文件读写

FileStorage文件读与写的方法与C++语言中的文件流对象的使用很像,对>><<进行了重载,分别用于文件读取和写入。很棒的是,FileStorage支持一些常用格式的直接读写,例如字符、字符串、数字、cv::Mat等。对于不支持的数据结构,只能按照规则自己去写啦~

2.1 写入

fs << "frameCount" << 5;  // 字符和数字
cv::Mat_<double> cameraMat = cv::Mat_<double>::zeros(3,3); 
fs << "Camera Intrinsic Matrix" << cameraMat; // cv::Mat

注意:

  • fs << "frameCount" <<5""输出的字符串是有限制的,对于YAML有效范围是:[a-z],[A-Z],[0-9],”-“,”_”和空格。XML与YAML基本一致,但是YAML字符之间加空格是允许的,XML不允许。如果出现以下BUG,请不要慌张,检查一下输入的字符是否有效就OK~

2.2 读取

文件读取的方法有两种:

// first method: use (type) operator on FileNode.
int frameCount = (int)fs2["frameCount"];
// second second method: use cv::FileNode::operator >>
int frameCount;
fs2["frameCount"] >> frameCount;

2.3 Mat的操作

这一点真的很不错,而且与C++的输入输出方法很接近(链接常用的三种Mat类型):

cv::Mat_<double> cameraMat = cv::Mat_<double>::zeros(3,3);
cv::Mat_<double> distCoeffes = ( cv::Mat_<double>(5,1)<< 0.1,0.01,-0.001,0.0,0.0 );
// C++
std::cout<<"Camera Matrix"<<std::endl<<cv::Mat::Mat(cameraMat)<<std::endl;
std::cout<<"Distortion Coefficients"<<std::endl<<cv::Mat::Mat(distCoeffes)<<std::endl;
// cv::FileStorage
fs << "Camera Matrix" << cameraMat;
fs << "Distortion Coefficients"<<distCoeffes;

运行结果对比如下:

C++
YAML
XML

2.4 集合的操作

Opencv中将集合分为两类:映射和序列。

@H_653_301@映射集合(Mappings, 又称named collections):每个元素有一个名字或者说关键字,并且可以通过名字访问其数据,类似于Key-Value结构。使用方法为:

// Mappings write
int x(1.0),y(0.0);
fs << "features" << "["; // also can be "[:"
fs <<"{:" << "x" << x << "y" << "}" << "]";
  • "{""{:"输出的结果是不一样的,YAML使用":"后,使输出的文本具有Python的风格,映射集合会按照一行排列,不适用时,按照每个元素与其值单独一行的方法排列。XML使用":"输出结果会有不同,但基本可以视为把":"忽略。

YAML {
YAML {:
XML {
XML {:

// Mappings read
cv::FileNode features = fs2["features"];
// 遍历查看
cv::FileNodeIterator it = features.begin();
std::cout<<
    "x="<<(int)(*it)["x"]<<
    " y="<<(int)(*it)["y"]<<
    " z="<<(int)(*it)["z"]<<std::endl;

输出结果:

  • 编程的时候,不在Mapping的"{ }"加上"[ ]"输出效果是不一样的,而且在数据读取的时候,加上"[
    ]"
    的Mapping结构会被认为是Mapping结构,否则会出错,以上述的Mappings write代码为例: 对于 fs <<
    "fearures" << "[" << "{" << ... << "}" << "]"
    结构,用上述方法可以读取成功; 对于 fs
    << "features" << "{" << ... << "}"
    结构,用上述方法时就会出错:

@H_653_301@序列集合(Sequences,又称unnamed collections):数据没有名字名字或者关键字,一般通过序号(indices)访问数据,例如最常见的数组。

与映射类似,序列集合需要在输出开始前加"[",结束后使用"]",并且"[:""["输出风格上与映射集合类似。

// Sequences write
int mySeq[5] = {0,1,2,3,4};
fs << "mySeq" << "[";
for ( int idx=0; idx<5; idx++ )
{
    fs << mySeq[idx];
}
fs << "]";
// Sequences read
cv::FileNode mySeq2 = fs2["mySeq"];
std::vector<int> seq;
cv::FileNodeIterator it = mySeq2.begin(),it_end = mySeq2.end();
for ( ; it != it_end; it++  )
{
    seq.push_back( (int)( *it ) );
    // std::cout<<(int)(*it)<<" "<<std::endl;
}

3.Opencv documentation 源码示例

下面贴出Opencv documentation中的示例代码,可以作为参考:

// file write
#include "opencv2/opencv.hpp"
#include <time.h>

using namespace cv;
using namespace std;

int main(int,char** argv)
{
    FileStorage fs("test.yml",FileStorage::WRITE);

    fs << "frameCount" << 5;
    time_t rawtime; time(&rawtime);
    fs << "calibrationDate" << asctime(localtime(&rawtime));
    Mat cameraMatrix = (Mat_<double>(3,3) << 1000,0,320,1000,240,1);
    Mat distCoeffs = (Mat_<double>(5,1) << 0.1,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();
    return 0;
}
// results
%YAML:1.0
frameCount: 5
calibrationDate: "Fri Jun 17 14:09:29 2011\n"
cameraMatrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 1000.,0.,320.,1000.,240.,1. ]
distCoeffs: !!opencv-matrix
   rows: 5
   cols: 1
   dt: d
   data: [ 1.0000000000000001e-01,1.0000000000000000e-02,-1.0000000000000000e-03,0. ]
features:
   - { x:167,y:49,lbp:[ 1,1 ] }
   - { x:298,y:130,lbp:[ 0,1 ] }
   - { x:344,y:158,0 ] }
// file read
FileStorage fs2("test.yml",FileStorage::READ);

// first method: use (type) operator on FileNode.
int frameCount = (int)fs2["frameCount"];

std::string date;
// second method: use FileNode::operator >>
fs2["calibrationDate"] >> date;

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

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

FileNode features = fs2["features"];
FileNodeIterator it = features.begin(),it_end = features.end();
int idx = 0;
std::vector<uchar> lbpval;

// iterate through a sequence using FileNodeIterator
for( ; it != it_end; ++it,idx++ )
{
    cout << "feature #" << idx << ": ";
    cout << "x=" << (int)(*it)["x"] << ",y=" << (int)(*it)["y"] << ",lbp: (";
    // you can also easily read numerical arrays using FileNode >> std::vector operator.
    (*it)["lbp"] >> lbpval;
    for( int i = 0; i < (int)lbpval.size(); i++ )
        cout << " " << (int)lbpval[i];
    cout << ")" << endl;
}
fs.release();
原文链接:https://www.f2er.com/xml/296302.html

猜你在找的XML相关文章