cvSVM

http://blog.youkuaiyun.com/firefight/article/details/6400060

 

// svm.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
using namespace std;
#include <ml.h>  

void drawCross(cv::Mat &img, cv::Point center, cv::Scalar color)
{
	int col = center.x > 2 ? center.x : 2;
	int row = center.y> 2 ? center.y : 2;

	line(img, cv::Point(col -2, row - 2), cv::Point(col + 2, row + 2), color);  
	line(img, cv::Point(col + 2, row - 2), cv::Point(col - 2, row + 2), color);  
}

int newSvmTest(int rows, int cols, int testCount,std::string dst)
{
	if(testCount > rows * cols)
		return 0;
	cv::Mat img = cv::Mat::zeros(rows, cols, CV_8UC3);
	cv::Mat testPoint = cv::Mat::zeros(rows, cols, CV_8UC1);
	cv::Mat data =cv::Mat::zeros(testCount, 2, CV_32FC1);
	cv::Mat res = cv::Mat::zeros(testCount, 1, CV_32SC1);

	//Create random test points
	for (int i= 0; i< testCount; i++) 
	{ 
		int row = rand() % rows;
		int col = rand() % cols;

		if(testPoint.at<unsigned char>(row, col) == 0)
		{
			testPoint.at<unsigned char>(row, col) = 1;
			data.at<float>(i, 0) = float (col) / cols; 
			data.at<float>(i, 1) = float (row) / rows; 
		}
		else
		{
			i--;
			continue;
		}

		if (row > ( 50 * cos(col * CV_PI/ 100) + 200) )
		{ 
			drawCross(img, cv::Point(col, row), CV_RGB(255, 0, 0));
			res.at<unsigned int>(i, 0) = 1; 
		} 
		else 
		{ 
			if (col > 200) 
			{ 
				drawCross(img, cv::Point(col, row), CV_RGB(0, 255, 0));
				res.at<unsigned int>(i, 0) = 2; 
			} 
			else 
			{ 
				drawCross(img, cv::Point(col, row), CV_RGB(0, 0, 255));
				res.at<unsigned int>(i, 0) = 3; 
			} 
		} 

	}

	imshow(dst, img);
	//
	//	/////////////START SVM TRAINNING//////////////////
	CvSVM svm = CvSVM(); 
	CvSVMParams param; 
	CvTermCriteria criteria;

	criteria= cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON); 
	param= CvSVMParams (CvSVM::C_SVC, CvSVM::RBF, 10.0, 8.0, 1.0, 10.0, 0.5, 0.1, NULL, criteria); 

	svm.train(data, res, cv::Mat(), cv::Mat(), param);

	for (int i= 0; i< rows; i++) 
	{ 
		for (int j= 0; j< cols; j++) 
		{ 
			cv::Mat m = cv::Mat::zeros(1, 2, CV_32FC1);
			m.at<float>(0,0) = float (j) / cols;
			m.at<float>(0,1) = float (i) / rows;

			float ret = 0.0; 
			ret = svm.predict(m); 
			cv::Scalar rcolor; 

			switch ((int) ret) 
			{ 
			case 1: rcolor= CV_RGB(100, 0, 0); break; 
			case 2: rcolor= CV_RGB(0, 100, 0); break; 
			case 3: rcolor= CV_RGB(0, 0, 100); break; 
			} 

			line(img, cv::Point(j,i), cv::Point(j,i), rcolor);
		} 
	}

	cvShowImage("2",&IplImage(img));
	//	//Show support vectors
	int sv_num= svm.get_support_vector_count(); 
	for (int i= 0; i< sv_num; i++) 
	{ 
		const float* support = svm.get_support_vector(i); 
		circle(img, cv::Point((int) (support[0] * cols), (int) (support[1] * rows)), 5, CV_RGB(200, 200, 200)); 
	}

	cvShowImage("3",&IplImage(img));
	cv::waitKey(0);

	return 0;
}

int main(int argc, char** argv)
{
	cv::namedWindow("a",CV_WINDOW_NORMAL);
	cv::moveWindow("a", 10, 600);
	return newSvmTest(400, 600, 100,"a");
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值