openCV svm分类 基础
简而言之,SVM训练部分的目标就是通过一批数据,然后生成一个代表我们模型的xml文件。
1.Pretreatment(预处理)
a.分类图片:收集大量图片将图片分类好放入不同的文件夹(破损或者完好)中,取出一部分(40%)来做测试图片放入测试文件夹中。
b.重命名:将完好文件夹中图片同意重命名为1,破损文件夹中图片同意重命名为2,测试文件夹中图片同意重命名为3。
2.加载待训练的车牌数据
void getok(Mat& trainingImages, vector<int>& trainingLabels)
{
int n = 1;
while (n <= NUM) //30 NUM图片数量
{
string ImgName;
ImgName = "完好/1";
stringstream ss;
string str;
ss << n; ss >> str;
ImgName = ImgName + " (" + str + ")"; //图像文件明格式:ImgName(n)
ImgName = ImgName + ".bmp";//图片格式bmp,可修改
cout << "处理:" << ImgName << endl;
Mat src_test = imread(ImgName);//读取图片
resize(src_test, src_test, Size(200, 200));//统一图片尺寸
src_test = src_test.reshape(1, 1);//将图片拉成一条线
trainingImages.push_back(src_test);
trainingLabels.push_back(1);//给予标签
n++;
}
}
void getno(Mat& trainingImages, vector<int>& trainingLabels)
{
int n1 = 1;
while (n1 <= NUM) //30 NUM图片数量
{
string ImgName;
ImgName = "破损/2";
stringstream ss;
string str;
ss << n1; ss >> str;
ImgName = ImgName + " (" + str + ")"; //图像文件明格式:ImgName(n)
ImgName = ImgName + ".bmp";//图片格式bmp,可修改
cout << "处理:" << ImgName << endl;
Mat src_test = imread(ImgName);//读取图片
resize(src_test, src_test, Size(200, 200));//统一图片尺寸
src_test = src_test.reshape(1, 1);//将图片拉成一条线
trainingImages.push_back(src_test);
trainingLabels.push_back(-1);//给予标签
n1++;
}
}
2.train (训练数据->模型)
设置svm参数并训练模型:
CvSVM SVM;;
CvSVMParams param;CvTermCriteria criteria;//新建一个SVM
criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
param = CvSVMParams(CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria); //svm参数
SVM.train(trainingData, classes, Mat(), Mat(), param);cout << "训练中。。。。" << endl;//trainingData训练数据classes训练标签
SVM.save("svm.xml");//保存训练模型
3.测试图片分类
while (n <= 20) //20测试图片数
{
string ImgName;
ImgName = "测试/3";
stringstream ss;
string str;
ss << n; ss >> str;
ImgName = ImgName + " (" + str + ")"; //图像文件明格式:ImgName(n)
ImgName = ImgName + ".bmp";//图片格式bmp,可修改
cout << "处理:" << ImgName << endl;
Mat src_test = imread(ImgName);//读取图片
Mat src_test1 = src_test;
resize(src_test, src_test, Size(200, 200));////统一图片尺寸
src_test = src_test.reshape(1, 1);//将图片拉成一条线
src_test.convertTo(src_test, CV_32FC1);
float response = SVM.predict(src_test);//获得测试得分
if (response == 1)
imshow("完好", src_test1);
if (response == -1)
imshow("缺陷", src_test1);
waitKey(0);
n++;
}
完整程序:
#include "cv.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
using namespace cv;
using namespace std;
#define NUM 35
void getok(Mat& trainingImages, vector<int>& trainingLabels);
void getno(Mat& trainingImages, vector<int>& trainingLabels);
int main(int argc, char** argv)
{
Mat classes,trainingData,trainingImages;
CvMat* results;
vector<int> trainingLabels;
getok(trainingImages, trainingLabels);
getno(trainingImages, trainingLabels);
Mat(trainingImages).copyTo(trainingData);
trainingData.convertTo(trainingData, CV_32FC1);
Mat(trainingLabels).copyTo(classes);
CvSVM SVM;; CvSVMParams param;CvTermCriteria criteria;//新建一个SVM //这里是参数
criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
param = CvSVMParams(CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria);
SVM.train(trainingData, classes, Mat(), Mat(), param);
cout << "训练中。。。。" << endl;
SVM.save("svm.xml");
int n = 1;
while (n <= 20) //30
{
string ImgName;
ImgName = "测试/3";
stringstream ss;
string str;
ss << n; ss >> str;
ImgName = ImgName + " (" + str + ")"; //图像文件明格式:ImgName(n)
ImgName = ImgName + ".bmp";
cout << "处理:" << ImgName << endl;
Mat src_test = imread(ImgName);//读取图片
Mat src_test1 = src_test;
resize(src_test, src_test, Size(200, 200));
src_test = src_test.reshape(1, 1);
src_test.convertTo(src_test, CV_32FC1);
float response = SVM.predict(src_test);
if (response == 1)
imshow("完好", src_test1);
if (response == -1)
imshow("缺陷", src_test1);
waitKey(0);
n++;
}
}
void getok(Mat& trainingImages, vector<int>& trainingLabels)
{
int n = 1;
while (n <= NUM) //30
{
string ImgName;
ImgName = "完好/1";
stringstream ss;
string str;
ss << n; ss >> str;
ImgName = ImgName + " (" + str + ")"; //图像文件明格式:ImgName(n)
ImgName = ImgName + ".bmp";
cout << "处理:" << ImgName << endl;
Mat src_test = imread(ImgName);//读取图片
resize(src_test, src_test, Size(200, 200));
src_test = src_test.reshape(1, 1);
trainingImages.push_back(src_test);
trainingLabels.push_back(1);
n++;
}
}
void getno(Mat& trainingImages, vector<int>& trainingLabels)
{
int n1 = 1;
while (n1 <= NUM) //30
{
string ImgName;
ImgName = "破损/2";
stringstream ss;
string str;
ss << n1; ss >> str;
ImgName = ImgName + " (" + str + ")"; //图像文件明格式:ImgName(n)
ImgName = ImgName + ".bmp";
cout << "处理:" << ImgName << endl;
Mat src_test = imread(ImgName);//读取图片
resize(src_test, src_test, Size(200, 200));
src_test = src_test.reshape(1, 1);
trainingImages.push_back(src_test);
trainingLabels.push_back(-1);
n1++;
}
}
运行截图:
项目下载:
http://download.****.net/detail/a1111h/9847398