OpenCV

In this tutorial we go to show how to save our custom structures with opencv functions.

We go to imagine we have this structure in our program.

typedef struct
{
float var1;
unsigned char var2[255];
}MyStructure;

Opencv can save data in two formats: XML or YAML. This formats are markup languages.

For write or read a file, we go to use cvOpenFileStorage function, the functions returns a CvFileStorage structure pointer. This pointer we use to read or write values into.

We can open a file in two ways: for read or write. The function to open file is cvOpenFileStorage

CvFileStorage* storage = cvOpenFileStorage( file, 0, CV_STORAGE_WRITE_TEXT );

One time we have our CvFileStorage initialized then we can save our data. To save the data we need create Nodes. XML and YAML are markup languages, and they are defined with nodes.

First we go to create one node that contains all structure data. And inside of this node we need create one node for each variable.

For create a node we need open and close this node, as we do with xml, then we use cvStartWriteStruct and cvEndWriteStruct.

cvStartWriteStruct(storage, "MyStructure", CV_NODE_MAP, NULL, cvAttrList(0,0));
...
cvEndWriteStruct(storage);

Between the start and end node we go to insert each new node with asigned variable. The variable we must save as some of this types: int, raw (array), real or string with this functions:

cvWriteInt cvWriteRawData cvWriteReal cvWriteString Then for save our structure we go to create this code:

//Open file
CvFileStorage* storage = cvOpenFileStorage( file, 0, CV_STORAGE_WRITE_TEXT );
//Create our node for structure
cvStartWriteStruct(storage, "MyStructure", CV_NODE_MAP, NULL, cvAttrList(0,0));
//Write our float
cvWriteReal( storage, "var1", MyStructure->var1 );

//Write our array
cvStartWriteStruct(storage, "var2", CV_NODE_SEQ, NULL, cvAttrList(0,0));
cvWriteRawData(storage, MyStructure->var2,255,"u");
cvEndWriteStruct(storage);

//End node structure
cvEndWriteStruct(storage);