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相關(guān)類型不用加上前綴了
int main(int argc, char* argv[])
{
FileStorage fs("test.yml", FileStorage::WRITE); //寫的形式打開yml。當(dāng)然也可以打開xml,主要看后綴
fs << "i" << 5 << "r" << 3.1 << "str" << "ABCDEFGH"; //存入整型、浮點(diǎn)型、字符串
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。當(dāng)然也可以打開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 ); //看看是不是跟之前放進(jìn)去的是同一個(gè)人
cout<<"read out i:"<<i1<<endl<<"read out r:"<<r1<<endl<<"read out str:"<<str1<<endl;
}
readfs.release();
waitKey();
return 0;
} 結(jié)果 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的數(shù)字版裸體。。。。。。]
|