對XML和YAML檔案實現I O操作

2022-11-24 15:11:28 字數 2235 閱讀 2142

xml\yaml檔案在opencv中的資料結構為filestorage,開啟操作例如:

string filename = "

i.xml";

filestorage fs(filename, filestorage::write);

\\...

fs.open(filename, filestorage::read);

檔案關閉操作會在filestorage結構銷燬時自動進行,但也可呼叫如下函式實現

fs.release();
寫入檔案使用  <

fs << "

iterationnr

"<< 100;

讀取檔案,使用 >> 運算子,例如

int

itnr;

fs["

iterationnr

"] >>itnr;

itnr = (int) fs["

iterationnr

"];

mat r = mat_::eye (3, 3

),t = mat_::zeros(3, 1

);fs

<< "r"

<< r; //

write cv::mat

fs << "t"

<

fs["

r"] >> r; //

read cv::mat

fs["

t"] >> t;

vector要注意在第一個元素前加上“[”,在最後一個元素前加上"]"。例如:

fs << "

strings

"<< "

["; //

text - string sequence

fs << "

image1.jpg

"<< "

awesomeness

"<< "

baboon.jpg";

fs << "

]"; //

close sequence

對於map結構的操作使用的符號是"",例如

fs << "

"; //

fs << "

";

讀取這些結構的時候,會用到filenode和filenodeiterator資料結構。對filestorage類的操作符會返回filenode資料型別,對於一連串的node,可以使用filenodeiterator結構,例如:

filenode n = fs["

strings

"]; //

read string sequence – get node

if (n.type() !=filenode::seq)

filenodeiterator it = n.begin(), it_end = n.end(); //

go through the node

for (; it != it_end; ++it)

cout

<< (string)*it << endl;

if

(filename.empty())

cv::filestorage fs(filename,cv::filestorage::write);

if(!fs.isopened())

fs<

prefiltercap"fs

<

sadwindowsize"fs

<

mindisparity"fs

<

numberofdisparities"fs

<

texturethreshold"fs

<

uniquenessratio"fs

<

specklewindowsize"fs

<

specklerange"fs

<

leftvalidarea";

fs<

fs<

rightvalidarea";

fs<

fs.release();