【OpenCV】CvSVM分类器进行车牌识别(C++)

资源下载地址:http://download.csdn.net/detail/taily_duan/9633850

// SVM_Train.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include 
#include 

using namespace std;
using namespace cv;


void getFiles( string path, vector& files )
{
	//文件句柄
	long   hFile   =   0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)
	{
		do
		{
			//如果是目录,迭代之
			//如果不是,加入列表
			if((fileinfo.attrib &  _A_SUBDIR))
			{
				if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)
					getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
			}
		}while(_findnext(hFile, &fileinfo)  == 0);
		_findclose(hFile);
	}
}

void getPlate(Mat& trainingImages, vector& trainingLabels)
{

	char * filePath = "..\\data\\HasPlate";
	vector files;

	////获取该路径下的所有文件
	getFiles(filePath, files );

	int size = files.size();
	if (0 == size)
		cout << "No File Found in train HasPlate!" << endl;

	for (int i = 0;i < size;i++)
	{
		cout << files[i].c_str() << endl;
		Mat img = imread(files[i].c_str());
		//img= img.reshape(1, 1);
		img= img.reshape(1, 1);
		trainingImages.push_back(img);
		trainingLabels.push_back(1);//打上标签1
	}
}

void getNoPlate(Mat& trainingImages, vector& trainingLabels)
{

	char * filePath = "..\\data\\NoPlate";
	vector files;

	////获取该路径下的所有文件
	getFiles(filePath, files );
	int size = files.size();
	if (0 == size)
		cout << "No File Found in train NoPlate!" << endl;

	for (int i = 0;i < size;i++)
	{
		cout << files[i].c_str() << endl;
		Mat img = imread(files[i].c_str());
		//img= img.reshape(1, 1);
		img= img.reshape(1, 1);
		trainingImages.push_back(img);
		trainingLabels.push_back(0);//打上标签0
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	double start1 = GetTickCount();
	Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1),车牌与非车牌标签集合;
	Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );两种训练合并起来的集合

	Mat trainingImages;
	vector trainingLabels;

	getPlate(trainingImages, trainingLabels);
	getNoPlate(trainingImages, trainingLabels);

	Mat(trainingImages).copyTo(trainingData);
	trainingData.convertTo(trainingData, CV_32FC1);
	Mat(trainingLabels).copyTo(classes);

	//参数设置
	CvSVMParams SVM_params;
	SVM_params.svm_type = CvSVM::C_SVC;//2分类器
	SVM_params.kernel_type = CvSVM::LINEAR; //CvSVM::LINEAR;
	SVM_params.degree = 0;
	SVM_params.gamma = 1;
	SVM_params.coef0 = 0;
	SVM_params.C = 1;
	SVM_params.nu = 0;
	SVM_params.p = 0;
	SVM_params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, 0.01);//迭代1000,CV_TERMCRIT_ITER:精度小于0.01算法停止
	//Train SVM

	cout << "Begin to generate svm" << endl;
	CvSVM svmClassifier(trainingData, classes, Mat(), Mat(), SVM_params);

	cout << "Svm generate done!" << endl;

	FileStorage fsTo("..\\out\\svm.xml", cv::FileStorage::WRITE);//训练数据存档
	svmClassifier.write(*fsTo, "svm");
	double end1 = GetTickCount();
	cout << "训练时间时间:" << (end1 - start1) / 1000 << "s" << endl;

	int a;
	cin >> a;

	return 0;
}


你可能感兴趣的:(OpenCV)