SVM+HOG特征训练分类器
#1,概念
在机器学习领域,支持向量机SVM(Support Vector Machine)是一个有监督的学习模型,通常用来进行模式识别、分类、以及回归分析。
SVM的主要思想可以概括为两点:⑴它是针对线性可分情况进行分析,对于线性不可分的情况,通过使用非线性映射算法将低维输入空间线性不可分的样本转化为高维特征空间使其线性可分,从而 使得高维特征空间采用线性算法对样本的非线性特征进行线性分析成为可能;
方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子。HOG特征通过计算和统计图像局部区域的梯度方向直方图来构成特征。
#2,代码和输入文件截图
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <tchar.h>
#include <windows.h> #include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp> using namespace cv;
using namespace std; int getfilepath(string txtpath,vector<string>& img_path, vector<int>& roi_sample_class, vector<vector<Rect > >& roi_sample_rect); int main(int argc, char** argv) {
//winSize窗口大小
int ImgWidht = ;
int ImgHeight = ; //Sample总数
int num_sample_roi=; //图片路径,每张图片中的sample类别和每个sample的rect参数
vector<string> img_path;
vector<int> roi_sample_class;
vector<vector<Rect > > roi_sample_rect; //标记文件txt名称和路径
string filepath = "E:/svm/";
string txtpath = string(filepath) + "TrainingData.txt"; //获得图片路径,sample类别,每个sample的rect参数,返回Sample总数(ROI总数)
num_sample_roi=getfilepath(txtpath, img_path, roi_sample_class, roi_sample_rect); //测试img_path, roi_sample_class, roi_sample_rect
//cout << "img_path[0]= " << img_path[0] << "\n img_path[50]= " << img_path[50] << endl;
//cout << "roi_sample_class[0]= " << roi_sample_class[0] << "\n roi_sample_class[150]= " << roi_sample_class[150] << endl;
//cout << "roi_sample_rect[0][0]= " << roi_sample_rect[0][0] << endl;
//system("Pause"); //HOG特征矩阵,sample类别矩阵
//cout << "num_sample_roi= " << num_sample_roi << endl;
//system("Pause");
Mat sample_feature_mat(num_sample_roi, , CV_32FC1);//900=(win_height/8-1)*(win_width/8-1)*(2*2)*9;
Mat sample_class_mat(num_sample_roi, , CV_32SC1); //样本类别 //原图片和训练图片
Mat orig_img;
Mat train_img; //= Mat::zeros(ImgWidht, ImgHeight, CV_8UC3);//需要分析的图片 //sample指示子
unsigned long n_sample = ; //对图片循环
for( string::size_type i = ; i != img_path.size(); i++ )
{
orig_img = imread(img_path[i].c_str(), );
if(orig_img.empty()){
cout<<"Can not load the image: "<<img_path[i]<<endl;
continue;
} //端口的提示信息
cout<<"***processing***"<<img_path[i].c_str()<<endl; //每个sample都要计算hog特征
for (size_t j = ; j != roi_sample_rect[i].size(); j++){
//取ROI,归一化
Mat handle_src=orig_img(roi_sample_rect[i][j]);
resize(handle_src, train_img, Size(ImgWidht, ImgHeight)); //申明描述子,每个参数的含义见笔记
HOGDescriptor hog(Size(ImgWidht,ImgHeight),Size(,),Size(,),Size(,), ); //描述子申请内存并计算
vector<float> descriptors;
hog.compute(train_img, descriptors); //为当前sample的所有hog descriptor申请内存
//sample_feature_mat[n_sample].resize(descriptors.size(),CV_32FC1); //输出hog特征个数
//cout<<"HOG dims: "<<descriptors.size()<<endl; //每个sample的hog特征个数
for (vector<float>::size_type k = ; k != descriptors.size(); k++)
sample_feature_mat.at<float>(n_sample, k) = descriptors[k]; //int num_class=i/100;
sample_class_mat.at<int>(n_sample, ) = roi_sample_class[i];
cout<<"***end processing***"<<img_path[i].c_str()<<" "<<roi_sample_class[i]<<endl; n_sample++;
} } //SVM参数
Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::RBF);
svm->setTermCriteria(TermCriteria(CV_TERMCRIT_EPS, , FLT_EPSILON)); //SVM训练
svm->train(sample_feature_mat, ml::ROW_SAMPLE, sample_class_mat);
svm->save( filepath+"SVM_DATA.xml" ); system("Pause");
return ;
} int getfilepath(string txtpath,vector<string>& img_path, vector<int>& roi_sample_class, vector<vector<Rect > >& roi_sample_rect){
int nLine = ; //图片计数器,每个类别100张图片。
int numroi=; //sample计数器,总的roi个数 ifstream svm_data( txtpath );
char output[]; while( !svm_data.eof() ) {
svm_data >> output;//是不是>>遇见空格会自动截断?
string s0= string(output);
if( s0.length()> ){
if( nLine < ){
roi_sample_class.push_back(); size_t bufname=s0.find(" ");
string bufname1 = s0.substr(, bufname);
img_path.push_back( bufname1 ); vector<Rect> obj_list;
svm_data >> output;
int obj_num=atoi(output); numroi=numroi+obj_num;
int obj[];
for(int i=; i<obj_num;i++)
{
for(int j=;j<;j++)
{
svm_data >> output;
int p=atoi(output);
obj[j]=p;
}
Rect tmp_obj=Rect(obj[],obj[],obj[],obj[]);
obj_list.push_back(tmp_obj);
}
roi_sample_rect.push_back(obj_list);
}
else if( nLine < ){
roi_sample_class.push_back();
size_t bufname=s0.find(" ");
string bufname1 = s0.substr(, bufname);
img_path.push_back( bufname1 ); vector<Rect> obj_list;
svm_data>>output;
int obj_num=atoi(output); numroi=numroi+obj_num;
int obj[];
for(int i=; i<obj_num;i++)
{
for(int j=;j<;j++)
{
svm_data >> output;
int p=atoi(output);
obj[j]=p;
}
Rect tmp_obj=Rect(obj[],obj[],obj[],obj[]);
obj_list.push_back(tmp_obj);
}
roi_sample_rect.push_back(obj_list);
}
else if( nLine < ){
roi_sample_class.push_back();
size_t bufname=s0.find(" ");
string bufname1 = s0.substr(, bufname);
img_path.push_back( bufname1 ); vector<Rect> obj_list;
svm_data>>output;
int obj_num=atoi(output); numroi=numroi+obj_num;
int obj[];
for(int i=; i<obj_num;i++)
{
for(int j=;j<;j++)
{
svm_data >> output;
int p=atoi(output);
obj[j]=p;
}
Rect tmp_obj=Rect(obj[],obj[],obj[],obj[]);
obj_list.push_back(tmp_obj);
}
roi_sample_rect.push_back(obj_list);
}
else{//(nLine < 400)
roi_sample_class.push_back();
size_t bufname=s0.find(" ");
string bufname1 = s0.substr(, bufname);
img_path.push_back( bufname1 ); vector<Rect> obj_list;
svm_data>>output;
int obj_num=atoi(output); numroi=numroi+obj_num;
int obj[];
for(int i=; i<obj_num;i++)
{
for(int j=;j<;j++)
{
svm_data >> output;
int p=atoi(output);
obj[j]=p;
}
Rect tmp_obj=Rect(obj[],obj[],obj[],obj[]);
obj_list.push_back(tmp_obj);
}
roi_sample_rect.push_back(obj_list);
}
nLine ++; //计数
}
} svm_data.close();
//cout << "numroi= " << numroi<< endl;
return numroi;
}
SVM+HOG
我的输入文件格式:

得到的分类器xml文件和输入的数据文件TrainingData.txt是放在同一个文件夹下:

图片源文件是给的绝对目录,看代码就知道了。
SVM+HOG特征训练分类器的更多相关文章
- HOG参数简介及Hog特征维数的计算(转)
HOG构造函数 CV_WRAP HOGDescriptor() :winSize(64,128), blockSize(16,16), blockStride(8,8), cellSize( ...
- 利用Hog特征和SVM分类器进行行人检测
在2005年CVPR上,来自法国的研究人员Navneet Dalal 和Bill Triggs提出利用Hog进行特征提取,利用线性SVM作为分类器,从而实现行人检测.而这两位也通过大量的测试发现,Ho ...
- Opencv学习之路—Opencv下基于HOG特征的KNN算法分类训练
在计算机视觉研究当中,HOG算法和LBP算法算是基础算法,但是却十分重要.后期很多图像特征提取的算法都是基于HOG和LBP,所以了解和掌握HOG,是学习计算机视觉的前提和基础. HOG算法的原理很多资 ...
- 如何正确训练一个 SVM + HOG 行人检测器
这几个月一直在忙着做大论文,一个基于 SVM 的新的目标检测算法.为了做性能对比,我必须训练一个经典的 Dalal05 提出的行人检测器,我原以为这个任务很简单,但是我错了. 为了训练出一个性能达标的 ...
- SVM中图像常用的HOG特征描述及实现
转摘网址:http://www.cnblogs.com/tiandsp/archive/2013/05/24/3097503.html Hog参考网址:http://www.cnblogs.com/t ...
- NLP用CNN分类Mnist,提取出来的特征训练SVM及Keras的使用(demo)
用CNN分类Mnist http://www.bubuko.com/infodetail-777299.html /DeepLearning Tutorials/keras_usage 提取出来的特征 ...
- opencv学习笔记(七)SVM+HOG
opencv学习笔记(七)SVM+HOG 一.简介 方向梯度直方图(Histogram of Oriented Gradient,HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子 ...
- paper 80 :目标检测的图像特征提取之(一)HOG特征
1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的 ...
- HOG特征(Histogram of Gradient)总结(转载)
整理一下我个人觉得比较好的HOG博文 博文1:OpenCV HOGDescriptor: 参数与图解 http://blog.csdn.NET/raodotcong/article/details/6 ...
随机推荐
- Android线程管理(二)——ActivityThread
线程通信.ActivityThread及Thread类是理解Android线程管理的关键. 线程,作为CPU调度资源的基本单位,在Android等针对嵌入式设备的操作系统中,有着非常重要和基础的作用. ...
- windows 我的电脑右键 无法打开管理窗口
保存成reg文件,执行一下就好了. Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{20D04FE0-3AEA-1069- ...
- 关于报malformed or corrupted AST file: 'Unable to load module 的错~
今天早上 一运行程序 居然报错,我都惊呆了,昨天明明好好的-但是百度是强大的- 报错内容: malformed or corrupted AST file: 'Unable to load modul ...
- 《慕客网:IOS-动画入门》学习笔记
新建Cocoa Touch Class,语言是swift 然后继续为界面添加一个普通的View Controller,并且添加前面视图的静态table的转向剪头指向这个View Controller, ...
- Effective Java 58 Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
Three kinds of throwables Throwables Checked Recoverable checked exceptions Y Y runtime exceptions N ...
- Bootstrap 类解析
Bootstrap 类解析 元素 Bootstrap 类 定义 <div> container 内容容器 <table> table 表格 <table> tabl ...
- Windows环境下JDK安装与环境变量配置详细的图文教程
原文作者:souvc博文出处:http://www.cnblogs.com/liuhongfeng/p/4177568.html 本节内容:JDK安装与环境变量配置 以下是详细步骤 一.准备工具: 1 ...
- 烂泥:【解决】Ubuntu下使用SSH连接centos系统很慢
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 这几天在Ubuntu下使用SSH连接centos系统,发现连接很慢.建议一个连接大约需要30s.很是坑爹,如下: 后来查询相关资料,发现这个是Ubunt ...
- Python 元组知识点
1.元组是一个有序的集合,2.元组和列表一样可以使用索引.切片来取值.3.创建元组后不能在原地进行修改替换等操作.4.元组支持嵌套,可以包含列表.字典和不同元组.5.元组支持一般序列的操作,例如:+. ...
- 17110 Divisible(basic)
17110 Divisible 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description Given n + m integers, I1,I2,. ...