Is there a simple way to save a KNN classifier in OpenCV by using the C++ API?
I have tried to save a KNN classifier described here after wrapping CvKNearest class inside another class.
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. Can anyone point me to proper resources where I can get this done with Boost library?
@tisch is totally right and I’d like to correct myself. The CvKNearest doesn’t override the load and save functions of the CVStatModel.
But since a CvKNearest doesn’t compute a model, there’s no internal state to store. Of course, you want to store the training and test
cv::Matdata you have passed. You can use the FileStorage class for this, a great description and tutorial is given at: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.