You can download this from here or find it in the samples/cpp/tutorial_code/core/file_input_output/file_input_output.cpp of the OpenCV source code library.
Here's a sample code of how to achieve all the stuff enumerated at the goal list.
<< "Tip: Open up " << filename << " with a text editor to see the serialized data." << endl;
return 0;
}
Explanation
Here we talk only about XML and YAML file inputs. Your output (and its respective input) file may have only one of these extensions and the structure coming from this. They are two kinds of data structures you may serialize: mappings (like the STL map) and element sequence (like the STL vector). The difference between these is that in a map every element has a unique name through what you may access it. For sequences you need to go through them to query a specific item.
XML/YAML File Open and Close. Before you write any content to such file you need to open it and at the end to close it. The XML/YAML data structure in OpenCV is cv::FileStorage . To specify that this structure to which file binds on your hard drive you can use either its constructor or the open() function of this:
Either one of this you use the second argument is a constant specifying the type of operations you'll be able to on them: WRITE, READ or APPEND. The extension specified in the file name also determinates the output format that will be used. The output may be even compressed if you specify an extension such as *.xml.gz*.
The file automatically closes when the cv::FileStorage objects is destroyed. However, you may explicitly call for this by using the release function:
Input and Output of text and numbers. The data structure uses the same << output operator that the STL library. For outputting any type of data structure we need first to specify its name. We do this by just simply printing out the name of this. For basic types you may follow this with the print of the value :
fs << "iterationNr" << 100;
Reading in is a simple addressing (via the [] operator) and casting operation or a read via the >> operator :
int itNr;
fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];
Input/Output of OpenCV Data structures. Well these behave exactly just as the basic C++ types:
Input/Output of vectors (arrays) and associative maps. As I mentioned beforehand, we can output maps and sequences (array, vector) too. Again we first print the name of the variable and then we have to specify if our output is either a sequence or map.
For sequence before the first element print the "[" character and after the last one the "]" character:
Read and write your own data structures. Suppose you have a data structure such as:
class MyData
{
public:
MyData() : A(0), X(0), id() {}
public: // Data Members
int A;
double X;
string id;
};
It's possible to serialize this through the OpenCV I/O XML/YAML interface (just as in case of the OpenCV data structures) by adding a read and a write function inside and outside of your class. For the inside part:
voidwrite(FileStorage& fs) const//Write serialization for this class
{
fs << "{" << "A" << A << "X" << X << "id" << id << "}";
}
voidread(const FileNode& node) //Read serialization for this class
{
A = (int)node["A"];
X = (double)node["X"];
id = (string)node["id"];
}
Then you need to add the following functions definitions outside the class:
Here you can observe that in the read section we defined what happens if the user tries to read a non-existing node. In this case we just return the default initialization value, however a more verbose solution would be to return for instance a minus one value for an object ID.
Once you added these four functions use the >> operator for write and the << operator for read:
MyData m(1);
fs << "MyData" << m; // your own data structures
fs["MyData"] >> m; // Read your own structure_
Or to try out reading a non-existing read:
fs["NonExisting"] >> m; // Do not add a fs << "NonExisting" << m command for this to work