# Include <opencv2/CORE/core. HPP> # include <iostream> # include <string> using namespace CV; using namespace STD; void help (char ** AV) {cout <Endl <AV [0] <"shows the usage of the opencv serialization functionality. "<Endl <" Usage: "<Endl <AV [0] <" outputfile.yml.gz "<Endl <" the output file may be either XML (XML) or yaml (yml/yaml ). you can even compress it by "<" specifying this I N its extension like xml.gz yaml.gz etc... "<Endl <" with filestorage you can serialize objects in opencv by using the <and> operators "<Endl <" for example: -Create a class and have it serialized "<Endl <"-use it to read and write matrices. "<Endl;} class mydata {public: mydata (): A (0), x (0), ID () {} explicit mydata (INT ): A (97), x (cv_pi), ID ("mydata1234") // explicit to avoid Implicit conversion {} void write (filestorage & FS) const // write serialization for this class {FS <"{" <"A" <"X" <x <"ID" <id <<"}";} void read (const filenode & node) // read serialization for this class {A = (INT) node ["A"]; X = (double) node ["X"]; id = (string) node ["ID"];} public: // data members int A; Double X; string ID ;}; // These write and read functions must be defined for Serialization in filestorage to workvoid write (filestorage & FS, const STD: string &, const mydata & X) {X. write (FS);} void read (const filenode & node, mydata & X, const mydata & default_value = mydata () {If (node. empty () x = default_value; else X. read (node);} // This function will print our custom class to the consoleostream & operator <(ostream & out, const mydata & M) {out <"{id =" <m. ID <","; Out <"x =" <m. x <","; out <"A =" <m. A <"}"; Return out;} int main (int ac, char ** AV) {If (AC! = 2) {help (AV); return 1;} string filename = Av [1]; // set the object {// write mat r = mat _ <uchar> :: eye (3, 3), t = mat _ <double>: zeros (3, 1); mydata M (1); filestorage FS (filename, filestorage: Write ); FS <"iterationnr" <100; FS <"strings" <"["; // text-string sequence [] FS <"image1.jpg" <"awesomeness" <"baboon.jpg"; FS <"]"; // close sequence FS <"mapping"; // text-mapping ing {} FS <"{" <"One" <1; FS <"two" <2 <"}"; FS <"R" <R; // write CV: mat FS <"T" <t; FS <"mydata" <m; // your own data structures FS. release (); // explicit close cout <"Write done. "<Endl ;}{// read cout <Endl <" reading: "<Endl; filestorage FS; FS. open (filename, filestorage: Read); int itnr; // FS ["iterationnr"]> itnr; itnr = (INT) FS ["iterationnr"]; // cout for addressing and conversion Operations <itnr; If (! FS. isopened () {cerr <"failed to open" <FILENAME <Endl; Help (AV); return 1 ;} // read string sequence-Get node filenode n = FS ["strings"]; If (N. type ()! = Filenode: SEQ) {cerr <"strings is not a sequence! Fail "<Endl; return 1 ;}// go through the node filenodeiterator it = n. Begin (), it_end = n. End (); For (; it! = It_end; ++ it) cout <(string) * It <Endl; // read mappings from a sequence n = FS ["mapping"]; cout <"two" <(INT) (N ["two"]) <";"; cout <"one" <(INT) (N ["one"]) <Endl; mydata m; mat r, T; FS ["R"]> r; // read CV :: mat FS ["T"]> T; FS ["mydata"]> m; // read your own structure _ cout <Endl <"r =" <r <Endl; cout <"t =" <t <Endl; cout <"mydata =" <Endl <m <Endl; // show default behavior for non existing nodes cout <"attempt to read nonexisting (shocould initialize the data structure with its default ). "; FS [" nonexisting "]> m; cout <Endl <" nonexisting = "<Endl <m <Endl ;} cout <Endl <"Tip: Open up" <FILENAME <"with a text editor to see the serialized data. "<Endl; return 0 ;}
Note: The mydata. xml and mydata.yaml files are created on the hard drive. You can create two text files named mydata.txt and change the suffix to. xml and. yaml respectively.