opencv特征点cvSeq保存到文件,读写成功

本文介绍了如何使用OpenCV将特征点的cvSeq结构保存到文件,并成功进行读取操作。参考了特定的论坛和优快云博客文章来详细阐述这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了保存特征下次运行时直接读取,研究了半天,贴出来分享下


保存:

//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->);
cvWriteReal( myFileStorage, "y", pt->);
cvWriteReal( myFileStorage, "reflectance", pt->);
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值