支持向量机

#include <stdio.h>  
#include <time.h>  
#include <opencv2/opencv.hpp>  
#include <opencv/cv.h>  
#include <iostream> 
#include <opencv2/core/core.hpp>   
#include <io.h>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"

using namespace std;
using namespace cv;
using namespace cv::ml;

int main(int argc, char** argv)
{
	double time0 = static_cast<double>(getTickCount());
	Mat train = Mat::zeros(50, 19, CV_32FC1);//创建Mat类矩阵,定义初始化值全部是0,矩阵大小和xml一致,double
	Mat label = Mat::zeros(50, 1, CV_32SC1);//同理,int
	//读入mat
	FileStorage fs("C:\\Users\\Administrator\\Desktop\\xun\\data.xml", FileStorage::READ);
	fs["data"] >> train;
	FileStorage fs1("C:\\Users\\Administrator\\Desktop\\xun\\label.xml", FileStorage::READ);
	fs1["label"] >> label;
	/*for (int i = 0; i < train.rows;i++)
		for (int j = 0; j < train.cols; j++)
		{
			cout <<train.at<float>(i,j) << endl;
		}
	cout << " 训练数据行数:" << train.rows << "训练数据列数:" << train.cols << endl;
	cout << " 训练标签行数:" << label.rows << "训练数据列数: " << label.cols << endl;*/

	// 
	Ptr<SVM> svm = SVM::create();
	//svm->setType(SVM::C_SVC);
	//svm->setC(100);
	//svm->setKernel(SVM::RBF);
	//svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
	//svm->train(train, ROW_SAMPLE, label);
	svm->trainAuto(train, ROW_SAMPLE, label);

	cout << "训练好了!!!" << endl;
	svm->save("C:\\Users\\Administrator\\Desktop\\xun\\svm.xml");
	cout << "baocun" << endl;
	time0 = ((double)getTickCount() - time0) / getTickFrequency();
	cout << "shijian wei" << time0 << "秒" << endl;

	while (char(waitKey(1) != 'q')) {}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值