Powered by Blogger.

Wednesday, February 10, 2010

Tag: ,

CvFileStorage. How to save our custom structures with OpenCV functions

[caption id="attachment_311" align="aligncenter" width="645" caption="OpenCV"]OpenCV[/caption]

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.

[c language="++"]typedef struct
{
float var1;
unsigned char var2[255];
}MyStructure;[/c]

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

[c language="++"]CvFileStorage* storage = cvOpenFileStorage( file, 0, CV_STORAGE_WRITE_TEXT );[/c]

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.

[c language="++"]cvStartWriteStruct(storage, "MyStructure", CV_NODE_MAP, NULL, cvAttrList(0,0));
...
cvEndWriteStruct(storage);[/c]

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:

[c language="++"]
//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);

[/c]

About David Millán Escrivá

David completed his studies in Universidad Politecnica de Valencia in IT with a Master's degree in artificial intelligence, computer graphics, and pattern recognition, focusing on pattern recognition and Computer Vision. David has more than 15 years of experience in IT, with more than ten years of experience in Computer Vision, computer graphics, and pattern recognition, working on different projects and start-ups, applying his knowledge of Computer Vision, optical character recognition, and augmented reality. Co-Author of two OpenCV books and reviewer of few more.

8 comments:

  1. 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.

    ReplyDelete
  2. 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.

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

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

    ReplyDelete
  5. 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!

    ReplyDelete
  6. 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...

    ReplyDelete
  7. please tell me the file storage code of reading and writing of a histogramm

    ReplyDelete
  8. [...] CvFileStorage. How to save our custom structures with OpenCV functions 【http://blog.damiles.com/2010/02/cvfilestorage-how-to-save-our-custom-structures-with-opencv-function...】 [...]

    ReplyDelete