• Open Source Computer Vision Library

Yml文件的读取与写入

Wikipedia,自由的百科全书

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

C++版本 based on OpenCV 2.1

/***********************************************************************
 * OpenCV 2.1 example
 * By Pebbler Chung 2010
 ***********************************************************************/
 
#include "cv.h"
#include "highgui.h"
#include "iostream"
 
using namespace 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();
 
	FileStorage readfs("test.yml", FileStorage::READ); //读的形式打开yml。当然也可以打开xml,主要看后缀
 
	if(readfs.isOpened() )
	{
		int i1 = (int)readfs["i"];
		double r1 = (double)readfs["r"];
		string str1 = (string)readfs["str"];
 
		Mat readOutImg;
		readfs["lena"] >> readOutImg; //把Lena从yml中取出
		imshow( "Lena_from_yml", readOutImg ); //看看是不是跟之前放进去的是同一个人
 
		cout<<"read out i:"<<i1<<endl<<"read out r:"<<r1<<endl<<"read out str:"<<str1<<endl;
	}
	readfs.release();
 
	waitKey();
 
	return 0;
 
}

结果 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的数字版裸体。。。。。。]
Views
Personal tools