gpt4 book ai didi

c++ - OpenCV:如何复制 NormalBayesClassifier?

转载 作者:行者123 更新时间:2023-11-28 02:46:06 25 4
gpt4 key购买 nike

我正在训练和测试更多分类器,我想只保存其中最好的。我尝试使用 if 和 =

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

但它似乎给了我一些空指针错误:

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

编辑:

有效的代码,使用std::vector:

std::vector< cv::NormalBayesClassifier> classifiers(10);
int classifierPosition = 0;
double smallestError = 2.;
for (int i = 0; i < 10; i++)
{
// extract vocabulary from X images chosen randomly
// extract BOW descriptors for the X images used for training
// extract BOW descriptors for the N-X images used for testing (the ground truth)
classifiers[i].train(trainingData, labels);
classifiers[i].predict(evalData, &results);
double errorRate = (double) cv::countNonZero(groundTruth - results) / evalData.rows;
if (smallestError > errorRate)
{
smallestError = errorRate;
classifierPosition = i;
}
}
classifier[classifierPosition].save("name.yaml");

如果我使用 cv::Ptr,它不会工作:

cv::Ptr< cv::NormalBayesClassifier> bestClassifier;
double smallestError = 2.;
for (int i = 0; i < 10; i++)
{
// extract vocabulary from X images chosen randomly
// extract BOW descriptors for the X images used for training
// extract BOW descriptors for the N-X images used for testing (the ground truth)
cv::NormalBayesClassifier classifier;
classifier.train(trainingData, labels);
classifier.predict(evalData, &results);
double errorRate = (double) cv::countNonZero(groundTruth - results) / evalData.rows;
if (smallestError > errorRate)
{
smallestError = errorRate;
bestClassifier = &classifier;
}
}
bestClassifier->save("name.yaml"); // here it gives me that error

最佳答案

最好的办法是使用指针

其中一个应该没问题

    // opencv shared ptr
cv::Ptr<cv::NormalBayesClassifier> best_Classifier;
//or c++11 shared
shared_ptr<cv::NormalBayesClassifier> best_Classifier;
//or C raw ptr
cv::NormalBayesClassifier * best_Classifier;



if (smallestError > errorRate)
{
//raw ptr example
best_Classifier = &classifier;
}
// end for
best_Classifier->save("name");

尽管我认为将它们全部保存或使用索引技巧@berak 没有任何问题

编辑写评论,没有std::vector:

    cv::Ptr< cv::NormalBayesClassifier> bestClassifier;
double smallestError = 2.;
for (int i = 0; i < 10; i++)
{

cv::Ptr< cv::NormalBayesClassifier> classifier = new cv::NormalBayesClassifier;
classifier->train(trainingData, labels);
classifier->predict(evalData, &results);
double errorRate = (double) cv::countNonZero(groundTruth - results) / evalData.rows;
if (smallestError > errorRate)
{
smallestError = errorRate;
bestClassifier = classifier;
}
}
bestClassifier->save("name.yaml"); // no more error

关于c++ - OpenCV:如何复制 NormalBayesClassifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24368420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com