计算机视觉
图像处理

新手学习opencv:svm

新手学习OpencvSVM

1.opencv中svm参数和函数

svm参数:

CvSVMParams::CvSVMParams() :

    svm_type(CvSVM::C_SVC), kernel_type(CvSVM::RBF), degree(0),

    gamma(1), coef0(0), C(1), nu(0), p(0), class_weights(0)

{

    term_crit = cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, FLT_EPSILON );

}

 

svm_type表示SVM类型:

{C_SVC=100, NU_SVC=101, ONE_CLASS=102, EPS_SVR=103, NU_SVR=104 };//SVC是SVM分类器,SVR是SVM回归

kernel_type表示核函数的类型:

 { LINEAR=0, POLY=1, RBF=2, SIGMOID=3 }; //提供四种核函数,分别是线性,多项式,径向基,sigmoid型函数。

degree针对多项式核函数degree的设置,

gamma针对多项式/rbf/sigmoid核函数的设置

oef0针对多项式/sigmoid核函数的设置

默认值degree = 0,gamma = 1,coef0 = 0

svm训练函数

bool CvSVM::train( const CvMat* _train_data, const CvMat* _responses,

const CvMat* _var_idx, const CvMat* _sample_idx, CvSVMParams _params )

用的时候:svm.train(dataMat, labelMat, NULL, NULL, param);

svm预测函数

svm.predict(testMat);

支持向量机的个数

svm.get_support_vector_count();

支持向量机

svm.get_support_vector(i)

保存训练结果

svm.save(“svmResult.txt”);

用法流程:

1.设置svm参数

2.输入训练样本矩阵

3.训练样本

4.输入测试数据,根据训练结果分类

/////////svm/////////
#include "cv.h"
#include 
#include "ml.h"
#include "iostream"
using namespace std;
double inputArr[10][13] = 
{
	1,0.708333,1,1,-0.320755,-0.105023,-1,1,-0.419847,-1,-0.225806,0,1, 
	-1,0.583333,-1,0.333333,-0.603774,1,-1,1,0.358779,-1,-0.483871,0,-1,
	1,0.166667,1,-0.333333,-0.433962,-0.383562,-1,-1,0.0687023,-1,-0.903226,-1,-1,
	-1,0.458333,1,1,-0.358491,-0.374429,-1,-1,-0.480916,1,-0.935484,0,-0.333333,
	-1,0.875,-1,-0.333333,-0.509434,-0.347032,-1,1,-0.236641,1,-0.935484,-1,-0.333333,
	-1,0.5,1,1,-0.509434,-0.767123,-1,-1,0.0534351,-1,-0.870968,-1,-1,
	1,0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,
	1,0.25,1,1,-0.698113,-0.484018,-1,1,0.0839695,1,-0.612903,0,-0.333333,
	1,0.291667,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333,
	1,0.416667,-1,1,0.0566038,0.283105,-1,1,0.267176,-1,0.290323,0,1
};
double testArr[]=
{
	//0.25,1,1,-0.226415,-0.506849,-1,-1,0.374046,-1,-0.83871,0,-1
	1.29,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333
};
int main()
{
	//1.svm参数设置
	CvSVM svm;
	CvSVMParams param;
	param.svm_type = 100;
	param.kernel_type = 1;
	param.degree = 2;
	param.gamma = 1;
	param.coef0 = 0;
	//2.输入样本矩阵
	CvMat *trainMat = cvCreateMat(10, 12, CV_32FC1);
	CvMat *labelMat = cvCreateMat(10, 1, CV_32SC1);//标签要有符号,代表正负
	for (int i=0; i<10; i++)
	{
		for (int j=0; j<12; j++)
		{
			cvSetReal2D(trainMat, i, j, inputArr[i][j+1]);
		}
		cvSetReal2D(labelMat, i, 0, inputArr[i][0]);
	}
	//3训练样本
	//计算时间
	double t = (double)cvGetTickCount();
	svm.train(trainMat, labelMat, NULL, NULL, param);
	t = (double)cvGetTickCount() - t;
	double timecost = t/(cvGetTickFrequency()*1000); 
	cout<<"svm train耗时:"<<timecost<<"ms"<<endl;
	//保存训练结果
	svm.save("svmResult.txt");
	CvMat *testMat = cvCreateMat(1, 12, CV_32FC1);
	for (int i=0; i<12; i++)
	{
		cvSetReal2D(testMat, 0, i, testArr[i]);
	}

	//4预测分类
	float flag = 0;
	flag = svm.predict(testMat);
	int c = svm.get_support_vector_count();
	cout<<"svm个数"<<c<<endl;
	for (int i=0; i<c; i++)
	{
		const float* v = svm.get_support_vector(i);
		for(int i=0;i<12;i++)
		{
			cout<<*(v+i)<<",";
		}
		cout<<endl;
	}
	
	cout<<"testMat分类结果:"<<flag<<endl;
	system("pause");
	cvReleaseMat(&trainMat);
	cvReleaseMat(&labelMat);
	cvReleaseMat(&testMat);
	return 0;

}

 

转载注明来源:CV视觉网 » 新手学习opencv:svm

分享到:更多 ()
扫描二维码,给作者 打赏
pay_weixinpay_weixin

请选择你看完该文章的感受:

0不错 0超赞 0无聊 0扯淡 0不解 0路过

评论 3

评论前必须登录!