#include "train_classifier.h"
#include <fstream>
#define DEBUG_OUT 1
#define ERROR 1
TrainClassifier::TrainClassifier()
{
//正样本数:
_positive_sample_num = 0;
//负样本数:
_negative_sample_num = 0;
//hog描述子参数:
_nbins = 9; //hog梯度统计方向
_block_size = Size(16, 16); //滑动块大小
_block_stride = Size(8, 8); //滑动歩长
_cell_size = Size(8, 8); //hog统计块大小
}
int TrainClassifier::getHogDescriptorLength(Size image_size)
{
return (size_t)_nbins *
(_block_size.width / _cell_size.width) *
(_block_size.height / _cell_size.height) *
((image_size.width - _block_size.width) / _block_stride.width + 1) *
((image_size.height - _block_size.height) / _block_stride.height + 1);
}
int TrainClassifier::generateClassifier(string save_classifier_path, string positive_sample_path,string negative_sample_path, Size image_size)
{
_hog_descriptor_length = getHogDescriptorLength(image_size);
_image_size = image_size;
//获取样本数:
_positive_sample_num = getSampleNum(positive_sample_path);
_negative_sample_num = getSampleNum(negative_sample_path);
//样本总数:
int total_sample_num = _positive_sample_num + _negative_sample_num;
_sample_feature_mat = cvCreateMat(total_sample_num , _hog_descriptor_length, CV_32FC1);
cvSetZero(_sample_feature_mat);
//64*128的训练样本,该矩阵将是totalSample*3780,64*64的训练样本,该矩阵将是totalSample*1764
_sample_label_mat = cvCreateMat(total_sample_num, 1, CV_32FC1);//样本标识
cvSetZero(_sample_label_mat);
//训练正样本:
train(positive_sample_path, 1);
//训练负样本:
train(negative_sample_path, -1);
//生成分类器:
generator(save_classifier_path);
return 0;
}
//获取样本数:
int TrainClassifier::getSampleNum(string sample_path)
{
ifstream in_f;
in_f.open(sample_path.c_str(), ios_base::in);
if(in_f.is_open() == false)
{
#if DEBUG_OUT && ERROR
cout<<"can't find file!"<<endl;
#endif //DEBUG_OUT && ERROR
return 1;
}
int sample_num = 0;
string temp = "";
while(getline(in_f, temp))
{
sample_num++;
}
in_f.close();
return sample_num;
}
//训练正负样本:
int TrainClassifier::train(string sample_path, int label)
{
//从文件中读取数据:
ifstream in_f;
in_f.open(sample_path.c_str(), ios_base::in);
if(in_f.is_open() == false)
{
#if DEBUG_OUT && ERROR
cout<<"can't find file!"<<endl;
#endif //DEBUG_OUT && ERROR
return 1;
}
string image_path;
static int i = 0;
while(getline(in_f, image_path))
{
cv::Mat image = imread(image_path);
if(image.data != NULL)
{
cv::resize(image, image, _image_size, 0, 0, CV_INTER_LINEAR);
cv::HOGDescriptor hog(_image_size, _block_size, _block_stride, _cell_size, _nbins);
vector<float> featureVec;
hog.compute(image, featureVec, _cell_size);
int featureVecSize = featureVec.size();
for (int j=0; j<featureVecSize; j++)
{
CV_MAT_ELEM( *_sample_feature_mat, float, i, j ) = featureVec[j];
}
_sample_label_mat->data.fl[i] = label;
i++;
}
}
in_f.close();
return 0;
}
//产生分类器:
int TrainClassifier::generator(string save_classifier_path)
{
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON);
params.C = 0.01;
CvSVM svm;
svm.train( _sample_feature_mat, _sample_label_mat, NULL, NULL, params ); //用SVM线性分类器训练
svm.save(save_classifier_path.c_str(), 0);
cvReleaseMat(&_sample_feature_mat);
cvReleaseMat(&_sample_label_mat);
int supportVectorSize = svm.get_support_vector_count();
#if DEBUG_OUT
cout<<"support num :"<<supportVectorSize<<endl;
#endif //DEBUG_OUT
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
训练SVM分类器 VS2013 C++
共49个文件
tlog:12个
pdb:4个
cpp:4个
需积分: 10 27 下载量 190 浏览量
2018-04-17
17:32:58
上传
评论 1
收藏 7.48MB ZIP 举报
温馨提示
程序一:修改文件名的程序,可以用来批处理文件将文件名修改为数字命名并写到文本文件中 程序二:SVM训练程序。
资源推荐
资源详情
资源评论
收起资源包目录
训练SVM分类器.zip (49个子文件)
训练SVM分类器
训练分类器程序
TrainClassifier
ConsoleApplication2.v12.suo 29KB
ConsoleApplication2.sln 1003B
Debug
TrainClassifier.pdb 1.6MB
TrainClassifier.exe 118KB
TrainClassifier.ilk 773KB
ConsoleApplication2.sdf 11.63MB
ConsoleApplication2
ConsoleApplication2.vcxproj 4KB
train_classifier.cpp 4KB
PropertySheet.props 1KB
Debug
vc120.pdb 956KB
ConsoleApplication2.log 2KB
vc120.idb 587KB
TrainClassifier.tlog
CL.write.1.tlog 2KB
TrainClassifier.lastbuildstate 173B
link.command.1.tlog 5KB
CL.read.1.tlog 18KB
link.write.1.tlog 732B
cl.command.1.tlog 3KB
link.read.1.tlog 5KB
train_classifier.obj 378KB
main.obj 211KB
left_phone_svm.xml 30KB
ConsoleApplication2.vcxproj.filters 1KB
main.cpp 811B
train_classifier.h 1KB
修改文件名程序
ChangeSamplePath
ChangeSamplePath.sdf 7MB
ChangeSamplePath
ChangeSamplePath.vcxproj.filters 1KB
ChangeSamplePath.vcxproj 4KB
Debug
ChangeSamplePath.log 1KB
vc120.pdb 364KB
ChangeSamplePath.tlog
ChangeSamplePath.lastbuildstate 174B
CL.write.1.tlog 2KB
link.command.1.tlog 1KB
CL.read.1.tlog 13KB
link.write.1.tlog 730B
cl.command.1.tlog 1KB
link.read.1.tlog 3KB
源.obj 136KB
ChangeSamplePath.Build.CppClean.log 1KB
ChangeSamplePath.obj 345KB
vc120.idb 347KB
ChangeSamplePath.h 1KB
ChangeSamplePath.cpp 3KB
源.cpp 192B
Debug
ChangeSamplePath.ilk 631KB
ChangeSamplePath.pdb 851KB
ChangeSamplePath.exe 113KB
ChangeSamplePath.v12.suo 31KB
ChangeSamplePath.sln 994B
共 49 条
- 1
资源评论
jekcai
- 粉丝: 15
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功