Yml文件的读取与写入

来源:互联网 发布:设计家软件 编辑:程序博客网 时间:2024/06/02 13:53

Yml文件的读取与写入

主要描述还是要看官网:[1]

[编辑]

C++版本 based on OpenCV 2.1

/***********************************************************************

 * OpenCV2.1 example

 * ByPebbler Chung 2010

 ***********************************************************************/

 

#include "cv.h"

#include "highgui.h"

#include "iostream"

 

usingnamespace cv; //下面的所有cv相关类型不用加上前缀了

 

int main(int argc,char* argv[])

{

     FileStorage fs("test.yml", FileStorage::WRITE);//写的形式打开yml。当然也可以打开xml,主要看后缀

 

     fs <<"i" << 5 << "r" << 3.1 <<"str" << "ABCDEFGH"; //存入整型、浮点型、字符串

 

     Mat writeInImg = imread( "lena.jpg"); //载入Lena妞的图片载入

     imshow("Lena_from_jpg", writeInImg ); //看一看Lena妞是否健在

     fs <<"lena" << writeInImg;//Lena妞的图片矩阵插入test.yml

 

     fs.release();

 

     FileStoragereadfs("test.yml", FileStorage::READ);//读的形式打开yml。当然也可以打开xml,主要看后缀

 

     if(readfs.isOpened())

     {

         int i1= (int)readfs["i"];

         double r1= (double)readfs["r"];

         string str1=(string)readfs["str"];

 

         MatreadOutImg;

         readfs["lena"]>> readOutImg; //Lenayml中取出

         imshow("Lena_from_yml", readOutImg ); //看看是不是跟之前放进去的是同一个人

 

         cout<<"read out i:"<<i1<<endl<<"read out r:"<<r1<<endl<<"read out str:"<<str1<<endl;

     }

     readfs.release();

 

     waitKey();

 

     return0;

 

}

[编辑]

结果 test.yml

%YAML:1.0

i: 5

r: 3.1000000000000001e+000

str: ABCDEFGH

lena: !!opencv-matrix

   rows: 512 //lena的身高

   cols: 512 //lena的三围(正方形?惊!)

   dt:"3u"

   data: [ lena的数字版裸体。。。。。。]

 


1 0
原创粉丝点击