Opencv 3 SVM也是自动刻度标签吗?

时间:2017-02-02 18:00:53

标签: c++ opencv svm opencv3.0

我正在使用OpenCV 3.0.0。运行OpenCV的SVM示例1时,我注意到当自动训练而不是训练时,预测值介于0和1之间。当我直接运行相同的示例时,我看不到相同的行为用libsvm。

这是一个错误还是故意缩放标签以及其他功能?这种行为似乎也没有记录。

以下是我正在运行的代码:

    // Set up training data
size_t numberOfSamples = 4;
cv::Mat1i labelsMat(numberOfSamples, 1);
labelsMat(0, 0) = 1;
labelsMat(1, 0) = -1;
labelsMat(2, 0) = -1;
labelsMat(3, 0) = -1;

cv::Mat1f trainingDataMat(numberOfSamples, 2);
// Sample 0
trainingDataMat(0, 0) = 501;
trainingDataMat(0, 1) = 10;

// Sample 1
trainingDataMat(1, 0) = 255;
trainingDataMat(1, 1) = 10;

// Sample 2
trainingDataMat(2, 0) = 501;
trainingDataMat(2, 1) = 255;

// Sample 3
trainingDataMat(3, 0) = 10;
trainingDataMat(3, 1) = 501;

// Set up SVM's parameters
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setType(cv::ml::SVM::C_SVC);
svm->setKernel(cv::ml::SVM::LINEAR);
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 100, 1e-6));

// Train the SVM with given parameters
cv::Ptr<cv::ml::TrainData> td =
    cv::ml::TrainData::create(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat);

// train the SVM
// svm->train(td);

// or auto train
svm->trainAuto(td);

// predict
// first point used for training
cv::Mat point1 = (cv::Mat_<float>(1, 2) << 501, 10);
float response1 = svm->predict(point1); 
// second point used for training
cv::Mat point2 = (cv::Mat_<float>(1, 2) << 255, 10);
float response2 = svm->predict(point2);

std::cout << "first point: " << response1 << "\n" <<
          << "second point: " << response2 << std::endl;

如果使用trainAuto运行,它将输出0和1而不是-1和1。

1 个答案:

答案 0 :(得分:2)

显然这是一个现在修复的错误:

http://code.opencv.org/issues/4464