//objectKeypoints这些都是cvSeq* 即cvSeq指针
CvFileStorage *fs; fs = cvOpenFileStorage("data.xml", storage, CV_STORAGE_WRITE);//open file cvWrite(fs, "objectKeypoints", objectKeypoints); cvWrite(fs, "objectDescriptors", objectDescriptors); cvWrite(fs, "imageKeypoints", imageKeypoints); cvWrite(fs, "imageDescriptors", imageDescriptors);
cvReleaseFileStorage(&fs);
读取:
//objectKeypoints这些都是cvSeq* 即cvSeq指针
CvFileStorage *fs;
fs = cvOpenFileStorage("data.xml", storage, CV_STORAGE_READ);
objectKeypoints = (CvSeq*) cvReadByName(fs, 0, "objectKeypoints");
objectDescriptors = (CvSeq*)cvReadByName(fs, 0, "objectDescriptors",0);
imageKeypoints = (CvSeq*)cvReadByName(fs, 0, "imageKeypoints",0);
imageDescriptors = (CvSeq*)cvReadByName(fs, 0, "imageDescriptors",0);
cvReleaseFileStorage(&fs);
参考资料:http://tech.groups.yahoo.com/group/OpenCV/message/30968
(国外网站?如何打开你懂的)
优快云:http://blog.youkuaiyun.com/colorpanda/article/details/5969780
--- In OpenCV@yahoogroups.com, robert maertin <robert.maertin@g...> wrote:
> Dear Yahoo Group,
>
> I have a tiny problem with the openCV FileStorage function.
> I want to read in a number of floats and store them in triples as
> "CvPoint3D32f".
> I'd like to store these triples using FileStorage. As filestorage
cannot
> store "vector<CvPoint3D32f>"s,
> I want to put the 3D Points into a CvSeq and save this structure as
an xml.
> Problems occur during runtime, when I attempt to put the Seq into a
> fileStorage.
> I would be very thankful, if you had a look at what I did. I'm looking
> forward to your suggestions.
> If you have questions... you know ;)
>
> best & thanks
> Robert
> - - - - -
> Code Snippets:
>
> Up to here, I have got a line of interest from a file and tokenized in a
> vector<string> structure called "tokens".
> Next, I want to get the first, second and fourth token and put them in a
> "CvPoint3D32f". This point will then be pushed onto a CvSeq which later
> is put into a fileStorage.
>
>
> CvFileStorage* myFileStorage;
> CvSeq* point3DSequence; // here I want to put the points
> CvPoint3D32f newPoint; // this point will hold the current tokens
> int counter = 0; // some counter for the loop below
> // Maybe I made the mistake here (?) :
> CvMemStorage* storage = cvCreateMemStorage(0);
> point3DSequence = cvCreateSeq(CV_SEQ_ELTYPE_POINT3D,
> sizeof(CvSeq),sizeof(CvPoint3D32f), storage );
>
>
> // the first, second and fourth token are stored in new.Point's x,y,z,
> respectively.
> // before that, they're interpreted a floats.
>
> for( vector<string>::iterator myIterator = tokens.begin();
> myIterator!=tokens.end();myIterator++ )
> {
> switch (counter)
> {
> case 0: // first token
> {string buffer=*myIterator; newPoint.x=
> atof(buffer.c_str()); break;}
> case 1: // second token
> {string buffer=*myIterator; newPoint.y=
> atof(buffer.c_str()); break;}
> case 3: // fourth token
> {string buffer=*myIterator; newPoint.z=
> atof(buffer.c_str()); break;}
> default : {;} // else
> }
> counter++;
> }
>
> char* pushed = cvSeqPush( point3DSequence, &newPoint );
>
>
> [...] I do the above stuff for a lot of lines and thus end up with a
> whole lot of CvPoint3D32fs in "point3DSequence".
> Or don't I ? Because, when I try to put the cvSeq into a fileStorage, I
> receive the following message (at runtime):
> "OpenCV ERROR: Bad argument (Invalid character in the key) in function
> icvXMLWriteTag, cxpersistence.cpp(2247)
> Terminating the application..."
>
> This is how I try to put the CvSeq in a Storage:
>
> myFileStorage = cvOpenFileStorage("filename.xml",NULL,
CV_STORAGE_WRITE);
> cvWrite(myFileStorage," X,Y,Reflectance", point3DSequence);
> cvReleaseFileStorage(&myFileStorage);
>
>
>
> Anyway, a filename.xml is created and its contains the followinf two
> lines :
> " <?xml version="1.0"?>
> <opencv_storage> "
Hello,
This line causes the error:
> cvWrite(myFileStorage," X,Y,Reflectance", point3DSequence);
It is too advanced syntax to be understood by OpenCV writing
functions, namely, the tag name could not contain spaces or comma's.
If you want to store each point as a structure,
you may try the following:
cvStartWriteStruct( myFileStorage, "point3DSequence", CV_NODE_SEQ );
for( i = 0; i < point3DSequence->total; i++ )
{
CvPoint3D32f* pt=(CvPoint3D32f*)cvGetSeqElem(point3DSequence,i);
cvStartWriteStruct(myFileStorage, 0, CV_NODE_MAP+CV_NODE_FLOW);
cvWriteReal( myFileStorage, "x", pt->x );
cvWriteReal( myFileStorage, "y", pt->y );
cvWriteReal( myFileStorage, "reflectance", pt->z );
cvEndWriteStruct( myFileStorage );
}
cvEndWriteStruct( myFileStorage );
that will look nice, but a little bit bulky and then you will
have to read the elements manually from the filestorage.
Or, if the all the data could be just written as a plain array:
x0 y0 refl0 x1 y1 refl1 ...
then just use:
cvWrite( myFileStorage, "point3DSequence", point3DSequence );
Regards,
Vadim