CvFileStorage. How to save our custom structures with OpenCV functions

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:

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);

7 Comments to “CvFileStorage. How to save our custom structures with OpenCV functions”

  1. piponazo 10 February 2010 at 10:33 pm #

    Have you try the new interface for reading/writting files of OpenCv 2 ?? Now it’s very easy to write/read values from/to file but I’ve not tried this with structures like your post explain.

  2. damiles 11 February 2010 at 10:10 am #

    This functions are OpenCV 2.0, in openCV 1.X there are no support for YAML. This functions are linked with opencv 2.0 wiki documentation.

  3. shervin.emami 14 February 2010 at 5:43 pm #

    Hey,
    You have a problem in your HTML code, where you call cvWriteRawData(), it should say “MyStructure->var2″ but it says “MyStructure->var2″.

  4. Edwart 11 March 2010 at 5:28 pm #

    Hi,

    I have some problem with my new cams. I worked with two Logitech webcams and my program in OpenCV showed me the option for choosing any of them and then opened one window with the real time images. But now, with my new cams, it doesn’t work. The cams are two ImagingSource like this:
    http://www.theimagingsource.com/en_US/products/cameras/usb-ccd-mono/dmk41bu02/
    If you could help me you will make me a favor.

    I write you my code:

    ————————————————–

    #include “stdafx.h”
    #include “cv.h”
    #include “highgui.h”
    #include

    int _tmain(int argc, _TCHAR* argv[])
    {
    int c;
    IplImage* frame_left=0;
    CvCapture* capture_left=NULL;
    while(capture_left==NULL) capture_left=cvCaptureFromCAM(-1);
    cvNamedWindow( “Left”, 0) ;
    cvMoveWindow( “Left”,100,60);

    while( 1 ) {
    frame_left=cvQueryFrame( capture_left) ;
    cvShowImage( “Left”,frame_left);
    c = cvWaitKey(10);
    if((c & 255) == 27) break;
    }

    cvReleaseCapture( &capture_left);
    cvDestroyAllWindows();
    return 0;

    return 0;
    }

    ————————————————–

    Tank you!

  5. damiles 11 March 2010 at 5:40 pm #

    Hi Edwart, if you are under linux sistem please, check if your camera works with v4l drivers, you can check it with xawtv.

  6. Edwart 11 March 2010 at 6:49 pm #

    Hi,

    Thanks for your so fast reply!
    I’m under Vista sistem..
    I must say that I have already a long code written in OpenCV for stereo cams and I need some change in it..but I would like that it will be some short change..Will be possible with Windows Vista?

    Tanks again!

  7. damiles 11 March 2010 at 6:52 pm #

    I suppose you can do it with windows vista, but i don’t have knowledgment about windows os. A lot of time without Microsoft software…


Leave a Reply