简体   繁体   English

使用Boost序列化库序列化CvKNearest类

[英]Serialize CvKNearest class using Boost Serialization library

Is there a simple way to save a KNN classifier in OpenCV by using the C++ API? 是否有使用C ++ API在OpenCV中保存KNN分类器的简单方法?

I have tried to save a KNN classifier described here after wrapping CvKNearest class inside another class. 在将CvKNearest类包装到另一个类之后,我试图保存这里描述的KNN分类器。

It successfully saves to disk, but when I read from it running predict method gives me segmentation fault (core dumped) error. 它成功保存到磁盘,但是当我从中读取信息时,运行预测方法会给我分段错误 (核心转储)错误。

My wrapper class is as follows: 我的包装器类如下:

class KNNWrapper
{
    CvKNearest knn;
    bool train(Mat& traindata, Mat& trainclasses)
    {

    }
    void test(Mat& testdata, Mat& testclasses)
    {

    }
}

I've heard that Boost Serialization library is more robust and safe. 我听说Boost序列化库更加健壮和安全。 Can anyone point me to proper resources where I can get this done with Boost library? 谁能指出我可以使用Boost库完成此工作的适当资源?

@tisch is totally right and I'd like to correct myself. @tisch是完全正确的,我想纠正自己。 The CvKNearest doesn't override the load and save functions of the CVStatModel . CvKNearest不会覆盖负载保存的功能CVStatModel

But since a CvKNearest doesn't compute a model, there's no internal state to store. 但是由于CvKNearest不会计算模型,因此没有内部状态可存储。 Of course, you want to store the training and test cv::Mat data you have passed. 当然,您想存储已通过的培训和测试cv::Mat数据。 You can use the FileStorage class for this, a great description and tutorial is given at: 您可以为此使用FileStorage类,有关详细说明和教程,请参见

If you want to offer the same API as in the other statistical models in OpenCV (which makes sense) I suppose to subclass the CvKNearest and offer a save and load function, which respectively serialize the training/test data and deserialize it by using the FileStorage . 如果您想提供与OpenCV中其他统计模型相同的API(这很有意义),我想对CvKNearest进行子类化,并提供保存和加载功能,该功能分别序列化训练/测试数据并通过使用FileStorage反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM