标签:
#include "stdafx.h" //预编译头文件 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/ml/ml.hpp> using namespace cv; int main() { //定义一个512*512像素大小的图像进行SVM可视化呈现 int width = 512, height = 512; Mat image = Mat::zeros(height, width, CV_8UC3); //设置训练数据。训练数据有两个类1和-1 这两个类分别有一个和三个数据。 float labels[4] = {1.0, -1.0, -1.0, -1.0}; Mat labelsMat(4, 1, CV_32FC1, labels); float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} }; Mat trainingDataMat(4, 2, CV_32FC1, trainingData); //设置SVM参数 CvSVMParams params; params.svm_type = CvSVM::C_SVC; params.kernel_type = CvSVM::LINEAR; params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6); // 训练SVM CvSVM SVM; SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params); Vec3b green(0,255,0), blue (255,0,0); // 将图像中的每个点进行SVM分类并着色 for (int i = 0; i < image.rows; ++i) for (int j = 0; j < image.cols; ++j) { Mat sampleMat = (Mat_<float>(1,2) << j,i); float response = SVM.predict(sampleMat); if (response == 1) image.at<Vec3b>(i,j) = green; else if (response == -1) image.at<Vec3b>(i,j) = blue; } //显示训练数据 int thickness = -1; int lineType = 8; circle( image, Point(501, 10), 5, Scalar( 0, 0, 0), thickness, lineType); circle( image, Point(255, 10), 5, Scalar(255, 255, 255), thickness, lineType); circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType); circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType); //显示支持向量 thickness = 2; lineType = 8; int c = SVM.get_support_vector_count(); for (int i = 0; i < c; ++i) { const float* v = SVM.get_support_vector(i); circle( image, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thickness, lineType); } imwrite("result.png", image); // save the image imshow("SVM Simple Example", image); // show it to the user waitKey(0); }运行截图:
OpenCV2.4.10之samples_cpp_tutorial-code_learn-----ml(SVM支持向量机一)
标签:
原文地址:http://blog.csdn.net/u013018721/article/details/51280341