OpenCV SVM Training Data

依然范特西╮ 提交于 2019-12-04 15:32:57

The type of responses cannot be float or double.

Change

float labels[4] = { 1.0, -1.0, -1.0, -1.0 };
Mat labelsMat(4, 1, CV_32FC1, labels);

to

int labels[4] = { 1, -1, -1, -1 };
Mat labelsMat(4, 1, CV_32S, labels);

BTW, if you are using Linear kernel, the only parameter is C, so you do not need to setGamma.


Another problem is the way to get the predicted response. Since each time there is only one sample to predict, if you want to use the return value as the response, you should not pass res to predict.

You can change

float response = svm->predict(sampleMat, res);

to

float response = svm->predict(sampleMat);

Otherwise, if you want to use res, then the return value is no longer the response value. But you can get the response from res instead.

You can change

if (response == 1)
    image.at<Vec3b>(i, j) = green;
else if (response == -1)
    image.at<Vec3b>(i, j) = blue;
}

to

if (res.at<float>(0) == 1)
    image.at<Vec3b>(i, j) = green;
else if (res.at<float>(0) == -1)
    image.at<Vec3b>(i, j) = blue;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!