Ask Your Question
0

How to copy NormalBayesClassifier?

asked Jun 23 '14

thdrksdfthmn gravatar image

updated Jun 25 '14

berak gravatar image

I am training and testing more classifiers and I want to save just the best of them. I have tried to use an if and =

// for each train/eval
if (smallestError > errorRate)
{
  best_Classifier = classifier;
}
// end for
best_Classifier.save("name");

but it seems that it gives me some null pointer error:

OpenCV Error: Null pointer (Null pointer to the written object) in cvWrite, file /home/me/opencv/modules/core/src/persistence.cpp, line 5011
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/me/opencv/modules/core/src/persistence.cpp:5011: error: (-27) Null pointer to the written object in function cvWrite

Can you suggest me some other way if this is not possible?


EDIT:

I have also tried using Ptr, but it still does not work:

cv::Ptr< cv::NormalBayesClassifier > bestClassifier;
// for loop where train and evaluate the classifier cv::NormalBayesClassifier classifier;
if (smallestError > errorRate)
{
  best_Classifier = &classifier;
  smallesError = errorRate;
}

// end loop

best_Classifier->save("name");

And it gives me the following error:

OpenCV Error: The function/feature is not implemented () in CvStatModel::write, file /home/me/opencv/modules/ml/src/inner_functions.cpp, line 114
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/me/opencv/modules/ml/src/inner_functions.cpp:114: error: (-213)  in function CvStatModel::write
Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Jun 27 '14

thdrksdfthmn gravatar image

updated Jun 27 '14

The answer is by using cv::Ptr< cv::NormalBayesClassifier >, but do not forget to allocate memory with new:

cv::Ptr< cv::NormalBayesClassifier > bestClassifier;
for (int i = 0; i < 10; i++)
{
  // define pointer to classifier and allocate memory
  cv::Ptr< cv::NormalBayesClassifier > classifier = new cv::NormalBayesClassifier;
  // train, test
  if (smallestError > errorRate)
  {
    bestClassifier = classifier;
    smallesError = errorRate;
  }
}
bestClassifier->save("name");
Preview: (hide)

Comments

You should release your classifier if it isn't the best, and also the previous best classifier.

Mathieu Barnachon gravatar imageMathieu Barnachon (Jun 27 '14)edit

@MathieuBarnachon So, you mean that I shall add bestClassifier-&gt;clear(); before bestClassifier = classifier; and classifier-&gt;clear(); before closing the for loop?

thdrksdfthmn gravatar imagethdrksdfthmn (Jun 27 '14)edit

Sorry, I just miss the cv::Ptr which is a smart pointer ;-)

Mathieu Barnachon gravatar imageMathieu Barnachon (Jun 27 '14)edit

Anyway, I have tried it and there were errors...

thdrksdfthmn gravatar imagethdrksdfthmn (Jun 28 '14)edit

Question Tools

Stats

Asked: Jun 23 '14

Seen: 281 times

Last updated: Jun 27 '14