在OpenCV中从文件读取算法参数 [英] Reading algorithm parameters from file in OpenCV

查看:92
本文介绍了在OpenCV中从文件读取算法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从XML文件读取经过训练的Expectation Maximization模型的参数,以供以后使用.为了存储模型,我打电话

I am trying to read the parameters of a trained Expectation Maximation model from an XML file for later use. In order to store the model I call

cv::FileStorage fs("model.xml",cv::FileStorage::WRITE);
classifier.write(fs);  //classifier is of type cv::EM

这将创建包含看起来像模型数据的文件.这是文件的样子(从头开始的前几行):

this creates the file containing what looks like the data of the model. Here is what the files looks like (first few lines from the start):

StatModel.EM 1
<_ type_id="opencv-matrix"> 3 3 d
1.2159868951764311e+01 0. 0. 0. 1.9776824566023249e-01 0. 0. 0.  .2204460492503131e-16     
<_ type_id="opencv-matrix"> 3 3 d
3.2869203526862529e+00 0. 0. 0. 1.1631692248472096e+00 0. 0. 0. 2.2204460492503131e-16     
<_ type_id="opencv-matrix"> 3 3 d
2.9815870012055705e+00 0. 0. 0. 6.5049770685681069e+03 0. 0. 0. 6.8510191786605528e+03 
<_ type_id="opencv-matrix"> 3 3 d 
4.6608996548002040e+00 0. 0. 0. 3.7558131457318683e+02 0. 0. 0. 2.2204460492503131e-16 

请注意,缺少XML标头.现在为了读取我正在使用的数据

Note, that the XML header is missing. Now in order to read the data I am using

cv::FileStorage fs("model.xml",cv::FileStorage::READ);

必须使用文件节点作为参数来调用cv :: Algorithm :: read()函数.我不确定要使用哪个节点.因为我希望我尝试的文件中只有一个节点

the cv::Algorithm::read() function has to be called with a filenode as parameter. I am not sure what node to use. Since I would expect there to be only one node in the file I tried

classifier.read(fs[0]);

但是此算法之后没有经过训练.恢复原始参数需要做什么?

But the algorithm is not trained afterwards. What do I need to do in order to restore the original parameters?

推荐答案

相反,您可以使用写"来做:

Instead, of using 'write' you can do:

fs<<"my_model"<<classifier;

,然后在打开FileStorage进行读取之后,按如下方式进行读取:

and then after you open a FileStorage for reading, read it like that:

cv::EM EModel;
fs["my_model"] >> EModel;

以上内容不适用于cv :: EM,因为它不包含在重载中.

链接提供了一个很好的示例,说明了您如何向XML/YAML文件中写入和读取自定义类.据此,您为类创建了写入"和读取"方法,因此您是定义和命名节点的人.

This link provides a very good example about how you write and read a custom class to/from an XML/YAML file. According to that, you create the "write" and "read" methods for your class, so you are the one who defines and names the nodes.

如果您还没有自己编写这些方法,并且它们是cv :: Algorithm的一部分(可能这是新的,我在使用的2.2版本中找不到它),那么建议您检查xml文件查看创建的节点的名称,然后使用>>运算符或执行以下操作获取它们:

If you haven't written those methods yourself and they are part of cv::Algorithm (probably this is new, I couldn't find it in 2.2 that I am using) , then I would suggest to check the xml file to see the names of the nodes that were created and then get them either using the >> operator or doing:

FileNode myFilenode = fs["node_name"];
classifier.read(myFilenode);

根据您提供的StatModel.EM 1 <_ type_id="opencv-matrix">和链接中的xml示例,我猜想该节点的名称实际上是"_"(如果在编写时未提供任何名称,则可能是默认名称)

From the StatModel.EM 1 <_ type_id="opencv-matrix"> that you provide and the xml example in the link, I would guess that this node's name is actually "_" (maybe this is the default if you don't provide any name when you write it)

这篇关于在OpenCV中从文件读取算法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆