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 ...
随机推荐
- 安卓第九天笔记-Activity
安卓第九天笔记-Activity 1.创建Activity 一个界面对应一个activity 创建一个Activity 1.写一个JAVA类,继承Activity publicclass CalcA ...
- Spring(六)AOP切入方式
一.接口切入方式 实现类 package com.pb.entity; /** * 实体类 */ public class Hello { private String name; private S ...
- iOS中的触摸事件和手势处理
iOS中的事件可以分为三大类: 1> 触摸事件 2> 加速计事件 3> 远程控制事件 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并 ...
- iOS之小功能模块--彩虹动画进度条学习和自主封装改进
前言: 首先展示一下这个iOS小示例的彩色进度条动画效果: 阅读本文先说说好处:对于基础不好的读者,可以直接阅读文末尾的"如何使用彩虹动画进度条"章节,然后将我封装好的这个功能模块 ...
- Windows下Node.js+Express+WebSocket 安装配置
Linux参考: Linux安装Node.js 使用Express搭建Web服务器 Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V ...
- Java 读取大文件方法
需求:实际开发中读取文本文件的需求还是很多,如读取两个系统之间FTP发送文件,读取后保存到数据库中或日志文件的数据库中保存等. 为了测试首先利用数据库SQL生成大数据文件. 规则是 编号|姓名|手机号 ...
- Effective Java 49 Prefer primitive types to boxed primitives
No. Primitives Boxed Primitives 1 Have their own values Have identities distinct from their values 2 ...
- Effective Java 61 Throw exceptions appropriate to the abstraction
Exception translation: higher layers should catch lower-level exceptions and, in their place, throw ...
- 解决easy ui两次请求服务器的问题
目前该问题已经在1.4.1版本中解决了 本文引用自:http://www.cnblogs.com/Reaver/p/4056770.html,原文博主:flyreaver 我在使用过程中遇到了easy ...
- FiddlerScript修改特定请求参数下的返回值
使用场景: api/Live/GetLiveList接口: (1)Type为1,接口返回直播列表 (2)Type为2,接口返回回放列表 现在想修改直播列表的返回值 思路: 利用FiddlerScrip ...