概述
本篇是对opencv的svm学习笔记,基于对opencv官方svm教程的修改和记录。opencv的svm教程如下: 官网原版:http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html#introductiontosvms 汉语翻译版:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html#introductiontosvms
效果演示
本文代码运行效果如下:
代码讲解
具体代码
具体代码如下:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv/cv.h> #include <stdio.h> #include <opencv2/ml/ml.hpp> using namespace cv; int main() { // Data for visual representation int width = 512, height = 512; Mat image = Mat::zeros(height, width, CV_8UC3); float labels[3]; Mat labelsMat(3, 1, CV_32FC1, labels); labelsMat.rowRange(0,1).setTo(1); labelsMat.rowRange(1,2).setTo(2); labelsMat.rowRange(2,3).setTo(3); float trainingData[3][2] = { {501, 10}, {255, 10}, {501, 255} }; Mat trainingDataMat(3, 2, CV_32FC1, trainingData); // Set up SVM's parameters CvSVMParams params; params.svm_type = CvSVM::C_SVC; params.kernel_type = CvSVM::LINEAR; params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6); // Train the SVM CvSVM SVM; SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params); Vec3b green(0,255,0), blue (255,0,0), red(0,0,255); // Show the decision regions given by the SVM for (int i = 0; i < image.rows; ++i){ for (int j = 0; j < image.cols; ++j) { Mat sampleMat = (Mat_<float>(1,2) << i,j); float response = SVM.predict(sampleMat); if (response == 1) image.at<Vec3b>(j, i) = green; else if (response == 2) image.at<Vec3b>(j, i) = blue; else if (response == 3) image.at<Vec3b>(j, i) = red; } } // Show the training data 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, 0, 255), thickness, lineType); circle( image, Point(501, 255), 5, Scalar(255, 255, 0), thickness, lineType); imshow("SVM Simple Example", image); // show it to the user waitKey(0); }
建立训练样本
float labels[3]; Mat labelsMat(3, 1, CV_32FC1, labels); labelsMat.rowRange(0,1).setTo(1); labelsMat.rowRange(1,2).setTo(2); labelsMat.rowRange(2,3).setTo(3); float trainingData[3][2] = { {501, 10}, {255, 10}, {501, 255} }; Mat trainingDataMat(3, 2, CV_32FC1, trainingData);
这里对labelsMat设置了分类标签1、2、3,对应到训练数据中,也就是将trainingData中的三组数据正好分成了三类。
训练支持向量机
用函数SVM.train进行训练。
// Set up SVM's parameters CvSVMParams params; params.svm_type = CvSVM::C_SVC; params.kernel_type = CvSVM::LINEAR; params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6); // Train the SVM CvSVM SVM; SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params);
SVM区域分割
使用CvSVM::predict 通过重建训练完毕的支持向量机来将输入的样本分类。
Vec3b green(0,255,0), blue (255,0,0), red(0,0,255); // Show the decision regions given by the SVM for (int i = 0; i < image.rows; ++i){ for (int j = 0; j < image.cols; ++j) { Mat sampleMat = (Mat_<float>(1,2) << i,j); float response = SVM.predict(sampleMat); if (response == 1) image.at<Vec3b>(j, i) = green; else if (response == 2) image.at<Vec3b>(j, i) = blue; else if (response == 3) image.at<Vec3b>(j, i) = red; } }
这是一个大循环,遍历整个imgge图像,在每一次循环中:首先创建一个sampleMat,将本次循环中对应的i、j,以float的形式作为作为sampleMat的初始化参数。 接着使用SVM.predict对这个sampleMat进行分类。通过response返回数据,判断出本次的sampleMat属于第一、第二还是第三类,然后分别用红、绿、蓝在图像对应 位置,对分类进行标记。
时间: 2024-10-12 11:08:39