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 ...
随机推荐
- OC中的复合
#import <Foundation/Foundation.h> #import "Car.h" int main(int argc, const char * ar ...
- iOS9 HTTP 不能正常使用的解决办法
Google后查证,iOS9引入了新特性App Transport Security (ATS).详情:App Transport Security (ATS) 新特性要求App内访问的网络必须使用H ...
- OC语言-04-OC语言-核心语法
一.点语法 1> 基本使用 点语法本质上是set方法/get方法的调用 2> 使用注意 若出现在赋值操作符的右边,在执行时会转换成get方法 若出现在赋值操作符的左边,在执行时会转换成se ...
- 测试管理_测试人员招聘[持续更新ing]
招聘之难,难于上青天. 如何招聘到一位称心如意的员工想必是每个公司和管理者都要面临而且头疼的问题.尤其在初建团队或团队缺人的情况下问题会显得更加严重. 作为一个测试管理者,如何招聘到合适的测试人员是必 ...
- 第一章 Mysql 简介及安装和配置
Mysql是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一. ...
- R语言画图布局摆放(layout)
require(ggplot2) require(Cairo) require(grid) p = ggplot(iris,aes(x = Species,y = Sepal.Length,colou ...
- [部署]CentOS配置IP地址
环境 虚拟机:VMWare10.0.1 build-1379776 操作系统:CentOS7 64位 简介 CentOS7最小化安装(Minimal)时,是不带ifconfig指令的,该指令在net- ...
- Java zip and unzip demo
目录结构如下: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import ...
- VMware + OpenStack: 从 Plugin 到 VIO (VMware Integrated OpenStack)的演进
VMware 做为实际上的企业虚拟化领导者,对 OpenStack 的态度一直在变化.一开始,VMware 表达出与 OpenStack 的竞争态度.随着 OpenStack 的逐步壮大并且一步一步进 ...
- gre网络细节
一.OpenStack网络设备的命名规律: 1.TenantA的router和Linux网络命名空间qrouter名称 root@controller:~# neutron --os-tenant-n ...