之前找到一些代码都是2.X版本的,很多都不能运行,我大致修改了一下,全部能跑动了,2.x版本都注释了,下面替换为新的,可以对照修改。
/*******************************
** 作者: 周小小
** 描述:
struct CV_EXPORTS_W_MAP CvSVMParams
{
CvSVMParams();
CvSVMParams(
int svm_type, //SVM类型
int kernel_type,//核函数类型
double degree,//核函数中的参数degree,针对多项式核函数;
double coef0,//核函数中的参数,针对多项式/SIGMOID核函数;
double Cvalue,//SVM类型(C_SVC/ EPS_SVR/ NU_SVR)的参数C。
double p,
CvMat* class_weights,
CvTermCriteria term_crit );
CV_PROP_RW int svm_type;
CV_PROP_RW int kernel_type;
CV_PROP_RW double degree; // for poly
CV_PROP_RW double gamma; // for poly/rbf/sigmoid
CV_PROP_RW double coef0; // for poly/sigmoid
CV_PROP_RW double C; // for CV_SVM_C_SVC, CV_SVM_EPS_SVR and CV_SVM_NU_SVR
CV_PROP_RW double nu; // for CV_SVM_NU_SVC, CV_SVM_ONE_CLASS, and CV_SVM_NU_SVR
CV_PROP_RW double p; // for CV_SVM_EPS_SVR
CvMat* class_weights; // for CV_SVM_C_SVC
CV_PROP_RW CvTermCriteria term_crit; // termination criteria
};
SVM_params.c:SVM最优问题参数,设置C-SVC,EPS_SVR和NU_SVR的参数;
SVM_params.nu:SVM最优问题参数,设置NU_SVC, ONE_CLASS 和NU_SVR的参数;
SVM_params.p:SVM最优问题参数,设置EPS_SVR 中损失函数p的值.
*******************************/
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"
//using namespace cv;
//using namespace cv::ml;
int main(int argc, char** argv)
{
// visual representation
int width = 512;
int height = 512;
cv::Mat image = cv::Mat::zeros(height, width, CV_8UC3);
// training data
int labels[4] = { 1, -1, 1, -1 };//样本数据
float trainingData[4][2] = { { 501, 10 }, { 255, 10 }, { 501, 255 }, { 10, 501 } };//Mat结构特征数据
cv::Mat trainingDataMat(4, 2, CV_32FC1, trainingData);//Mat结构标签
cv::Mat labelsMat(4, 1, CV_32SC1, labels); //样本标签
// initial SVM初始化
cv::Ptr svm = cv::ml::SVM::create();
svm->setType(cv::ml::SVM::Types::C_SVC);//类型
svm->setKernel(cv::ml::SVM::KernelTypes::LINEAR);//核函数类型
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 100, 1e-6));//算法终止条件
// train operation
svm->train(trainingDataMat, cv::ml::SampleTypes::ROW_SAMPLE, labelsMat);
// prediction
cv::Vec3b green(0, 255, 0);
cv::Vec3b blue(255, 0, 0);
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
cv::Mat sampleMat = (cv::Mat_(1, 2) << j, i);
float respose = svm->predict(sampleMat);
if (respose == 1)
image.at(i, j) = green;
else if (respose == -1)
image.at(i, j) = blue;
}
}
int thickness = -1;
int lineType = cv::LineTypes::LINE_8;
//给点上色
cv::circle(image, cv::Point(501, 10), 5, cv::Scalar(0, 0, 0), thickness, lineType);
cv::circle(image, cv::Point(255, 10), 5, cv::Scalar(255, 255, 255), thickness, lineType);
cv::circle(image, cv::Point(501, 255), 5, cv::Scalar(0, 0, 0), thickness, lineType);
cv::circle(image, cv::Point(10, 501), 5, cv::Scalar(255, 255, 255), thickness, lineType);
thickness = 2;
lineType = cv::LineTypes::LINE_8;
cv::Mat sv = svm->getSupportVectors();
for (int i = 0; i < sv.rows; i++)
{
const float* v = sv.ptr(i);
cv::circle(image, cv::Point((int)v[0], (int)v[1]), 6, cv::Scalar(128, 128, 128), thickness, lineType);
}
cv::imshow("SVM Simple Example", image);
cv::waitKey(0);
return 0;
}