opencv对xml和yaml文件的读写操作

来源:互联网 发布:java中类和对象的区别 编辑:程序博客网 时间:2024/05/18 02:10

一.xml和yaml的简单介绍

       所谓的xml,就是eXtensible Markup Language, 翻译成中文就是“可扩展标识语言“。首先XML是一种元标记语言,所谓“元标记”就是开发者可以根据自己的需要定义自己的标记,比如开发者可以定义如下标记<book> <name>,任何满足xml命名规则的名称都可以标记,这就为不同的应用程序打开了的大门。 第二xml是一种语义/结构化语言。它描述了文档的结构和语义。

     XML可利用于数据交换 主要是因为XML表示的信息独立于平台的,这里的平台即可以理解为不同的应用程序也可以理解为不同的操作系统;它描述了一种规范,利用它Microsoft的word文档可以和Adobe 的Acrobat交换信息,可以和数据库交换信息。

   XML表示的结构化数据。 
  对于大型复杂的文档,xml 是一种理想语言,不仅允许指定文档中的词汇,还允许指定元素之间的关系。比如可以规定一个author元素必须有一个name子元素。可以规定企业的业务必须有包括什么子业务。 
XML文档。 XML文档有DTD和XML文本组成,所谓DTD(Document Type Definition ),简单的说就是一组标记符的语法规则.,表明XML文本是怎么样组织的,比如DTD可以表示一个<book>必须有一个子标记<author>, 可以有或者没有子标记<pages> 等等。当然一个简单的XML文本可以没有DTD。下面是一个简单的xml文本。 <? Xml version=”1.0” standalone=”yes”> <book> haha </book> 其中以?开始并结尾的是进程说明。Standalone表示外围设备。这里外围设备可以理解为该XML文本没有应用其他的文件。因为XML文件可以外部应用DTD等外部数据。更多信息请参考:点击打开链接


     和GNU一样,YAML是一个递归着说“不”的名字。不同的是,GNU对UNIX说不,YAML说不的对象是XML。YAML不是XML。

为什么不是XML呢?因为:

  • YAML的可读性好。
  • YAML和脚本语言的交互性好。
  • YAML使用实现语言的数据类型。
  • YAML有一个一致的信息模型。
  • YAML易于实现。

上面5条也就是XML不足的地方。同时,YAML也有XML的下列优点:

  • YAML可以基于流来处理;
  • YAML表达能力强,扩展性好。

总之,YAML试图用一种比XML更敏捷的方式,来完成XML所完成的任务。

更多的内容及规范参见http://www.yaml.org。以及点击打开链接


二.opencv中对xml 和yaml文件的读写数据结构介绍

    XML\YAML文件在OpenCV中的数据结构为FileStorage,更多参考:点击打开链接

   打开写操作:

[cpp] view plaincopy
  1. FileStorage fs("test.yml", FileStorage::WRITE);  

打开读操作:

[cpp] view plaincopy
  1. FileStorage fs2("test.yml", FileStorage::READ);  

三.对xml和yaml文件的读写操作及其示例

     写操作示例只是对yaml做演示,xml的操作也一样:

示例代码:

[cpp] view plaincopy
  1. #include "opencv2/opencv.hpp"  
  2. #include <time.h>  
  3.   
  4. using namespace cv;  
  5.   
  6. int main(intchar** argv)  
  7. {  
  8.     FileStorage fs("test.yml", FileStorage::WRITE);  
  9.   
  10.     fs << "frameCount" << 5;  
  11.     time_t rawtime; time(&rawtime);  
  12.     fs << "calibrationDate" << asctime(localtime(&rawtime));  
  13.     Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);  
  14.     Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);  
  15.     fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;  
  16.     fs << "features" << "[";  
  17.     forint i = 0; i < 3; i++ )  
  18.     {  
  19.         int x = rand() % 640;  
  20.         int y = rand() % 480;  
  21.         uchar lbp = rand() % 256;  
  22.   
  23.         fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";  
  24.         forint j = 0; j < 8; j++ )  
  25.             fs << ((lbp >> j) & 1);  
  26.         fs << "]" << "}";  
  27.     }  
  28.     fs << "]";  
  29.     fs.release();  
  30.     return 0;  
  31. }  

写入的xml文档内容:

[html] view plaincopy
  1. <?xml version="1.0"?>  
  2. <opencv_storage>  
  3. <frameCount>5</frameCount>  
  4. <calibrationDate>"Fri May 03 11:34:09 2013  
  5. "</calibrationDate>  
  6. <cameraMatrix type_id="opencv-matrix">  
  7.   <rows>3</rows>  
  8.   <cols>3</cols>  
  9.   <dt>d</dt>  
  10.   <data>  
  11.     1000. 0. 320. 0. 1000. 240. 0. 0. 1.</data></cameraMatrix>  
  12. <distCoeffs type_id="opencv-matrix">  
  13.   <rows>5</rows>  
  14.   <cols>1</cols>  
  15.   <dt>d</dt>  
  16.   <data>  
  17.     1.0000000000000001e-001 1.0000000000000000e-002  
  18.     -1.0000000000000000e-003 0. 0.</data></distCoeffs>  
  19. <features>  
  20.   <_><x>41</x>  
  21.     <y>227</y>  
  22.     <lbp>  
  23.       0 1 1 1 1 1 0 1</lbp></_>  
  24.   <_><x>260</x>  
  25.     <y>449</y>  
  26.     <lbp>  
  27.       0 0 1 1 0 1 1 0</lbp></_>  
  28.   <_><x>598</x>  
  29.     <y>78</y>  
  30.     <lbp>  
  31.       0 1 0 0 1 0 1 0</lbp></_></features>  
  32. </opencv_storage>  

读操作:

[cpp] view plaincopy
  1. FileStorage fs2("test.yml", FileStorage::READ);  
  2.   
  3. // first method: use (type) operator on FileNode.  
  4. int frameCount = (int)fs2["frameCount"];  
  5.   
  6. std::string date;  
  7. // second method: use FileNode::operator >>  
  8. fs2["calibrationDate"] >> date;  
  9.   
  10. Mat cameraMatrix2, distCoeffs2;  
  11. fs2["cameraMatrix"] >> cameraMatrix2;  
  12. fs2["distCoeffs"] >> distCoeffs2;  
  13.   
  14. cout << "frameCount: " << frameCount << endl  
  15.      << "calibration date: " << date << endl  
  16.      << "camera matrix: " << cameraMatrix2 << endl  
  17.      << "distortion coeffs: " << distCoeffs2 << endl;  
  18.   
  19. FileNode features = fs2["features"];  
  20. FileNodeIterator it = features.begin(), it_end = features.end();  
  21. int idx = 0;  
  22. std::vector<uchar> lbpval;  
  23.   
  24. // iterate through a sequence using FileNodeIterator  
  25. for( ; it != it_end; ++it, idx++ )  
  26. {  
  27.     cout << "feature #" << idx << ": ";  
  28.     cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";  
  29.     // you can also easily read numerical arrays using FileNode >> std::vector operator.  
  30.     (*it)["lbp"] >> lbpval;  
  31.     forint i = 0; i < (int)lbpval.size(); i++ )  
  32.         cout << " " << (int)lbpval[i];  
  33.     cout << ")" << endl;  
  34. }  
  35. fs.release();  

读取示例:


0 0