CvNormalBayesClassifier训练错误

CvNormalBayesClassifier train error

本文关键字:错误 CvNormalBayesClassifier      更新时间:2023-10-16

我使用OpenCv来制作CvNormalBayesClassifier,但是当我尝试训练它时,我得到了一个错误。这是我的代码:

CvMat train=cvMat(1, 32, CV_32FC1, val);
CvMat res=cvMat(1, 1, CV_32FC1, type);
M1.train(&train, &res);

,其中val是双精度数组,type是只有一个int元素的数组。M1是我的CvNormalBayesClassifier

我得到的错误是:

OpenCV Error: Bad argument (There is only a single class) in cvPreprocessCategoricalResponses, file /build/buildd/opencv-2.1.0/src/ml/ml_inner_functions.cpp, line 731 terminate called after throwing an instance of 'cv::Exception' what(): /build/buildd/opencv-2.1.0/src/ml/ml_inner_functions.cpp:731: error: (-5) There is only a single class in function cvPreprocessCategoricalResponses

流产

谢谢你的建议

该错误意味着您的响应数组res是完全统一的(它必须是,因为它只有元素)。要么添加一个不同的值来允许训练师进行训练,要么告诉它你正在进行单课训练:

CvSVMParams params;
params.svm_type = CvSVM::ONE_CLASS;
M1.train(train, res, Mat(), Mat(), params);

我没有使用"ONE_CLASS",所以你可能需要先给"nu"参数一个值(见这个OpenCV SVM文档页)。此外,我认为没有必要使用&号

首先,最后一个参数cvMat函数具有void*类型,因此它不知道数组的实际类型。因此,不可能进行数据转换。但是你正在使用CV_32FC1常量,它假设float类型和trainres矩阵只是重新解释你的int和双精度浮点数。你的CvNormalBayesClassifier只看到一个垃圾,而不是真正的数据。