#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特征训练分类器的更多相关文章

  1. HOG参数简介及Hog特征维数的计算(转)

    HOG构造函数 CV_WRAP HOGDescriptor() :winSize(64,128), blockSize(16,16), blockStride(8,8),      cellSize( ...

  2. 利用Hog特征和SVM分类器进行行人检测

    在2005年CVPR上,来自法国的研究人员Navneet Dalal 和Bill Triggs提出利用Hog进行特征提取,利用线性SVM作为分类器,从而实现行人检测.而这两位也通过大量的测试发现,Ho ...

  3. Opencv学习之路—Opencv下基于HOG特征的KNN算法分类训练

    在计算机视觉研究当中,HOG算法和LBP算法算是基础算法,但是却十分重要.后期很多图像特征提取的算法都是基于HOG和LBP,所以了解和掌握HOG,是学习计算机视觉的前提和基础. HOG算法的原理很多资 ...

  4. 如何正确训练一个 SVM + HOG 行人检测器

    这几个月一直在忙着做大论文,一个基于 SVM 的新的目标检测算法.为了做性能对比,我必须训练一个经典的 Dalal05 提出的行人检测器,我原以为这个任务很简单,但是我错了. 为了训练出一个性能达标的 ...

  5. SVM中图像常用的HOG特征描述及实现

    转摘网址:http://www.cnblogs.com/tiandsp/archive/2013/05/24/3097503.html Hog参考网址:http://www.cnblogs.com/t ...

  6. NLP用CNN分类Mnist,提取出来的特征训练SVM及Keras的使用(demo)

    用CNN分类Mnist http://www.bubuko.com/infodetail-777299.html /DeepLearning Tutorials/keras_usage 提取出来的特征 ...

  7. opencv学习笔记(七)SVM+HOG

    opencv学习笔记(七)SVM+HOG 一.简介 方向梯度直方图(Histogram of Oriented Gradient,HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子 ...

  8. paper 80 :目标检测的图像特征提取之(一)HOG特征

    1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的 ...

  9. HOG特征(Histogram of Gradient)总结(转载)

    整理一下我个人觉得比较好的HOG博文 博文1:OpenCV HOGDescriptor: 参数与图解 http://blog.csdn.NET/raodotcong/article/details/6 ...

随机推荐

  1. ListView嵌套出现的问题

    项目中一个列表子项中也需要用到列表,这就不由得使我想到ListView的嵌套,其实这个东西想想也只是复杂了一点,并没有什么难的地方,可是却依然在这里狠狠滴栽个跟头.问题出在子列表动态展开的操作上.可能 ...

  2. iOS开发之网络编程--使用NSURLConnection实现大文件下载

    主要思路(实现下载数据分段写入缓存中) 1.使用NSURLConnectionDataDelegate以及代理方法.2.在成功获取响应的代理方法中,获得沙盒全路径,并在该路径下创建空文件和文件句柄.3 ...

  3. Animated progress view with CAGradientLayer(带翻译)<待更新>

    原文网址:使用CAGradientLayer的动画精度条View Modern software design is getting flatter and thinner all the time. ...

  4. iOS中倒计时

    方法一:使用NSTimer来实现(比较适用于发送短信验证码倒计时) 主要是利用NSTimer的scheduledTimerWithTimeInterval方法来每秒执行一次changeTime方法 / ...

  5. js事件执行顺序

    http://ejohn.org/blog/how-javascript-timers-work/

  6. HTTP/HTTPs要点

    Http(超文本传输协议)是一个属于应用层的面向对象的协议. HTTP结构 HTTP请求: 请求行:Method-Request URI HTTP-Version(CRLF),eg:GET /form ...

  7. JavaScript Patterns 3.3 Patterns for Enforcing new

    When your constructor has something like  this.member and you invoke the constructor without  new,  ...

  8. centos7 拨号之后添加路由

    问题:拨号主机再自动拨号(/sbin/ifdown ppp0;/sbin/ifup ppp0)之后,无法上网(没有添加路由) 思路:在拨号程序中添加路由代码 vim /sbin/ifup { slee ...

  9. Backbone模型

    现在进入最关键的组件 - 模型.模型用来存储应用的所有数据,以及直接和数据操作相关的逻辑.Backbone中的模型类是Backbone.Model,它包含了数据存储,数据验证,以及数据发生变动时触发相 ...

  10. 【转】LINUX 5 常用ftp telnet配置

    LINUX 5 常用ftp telnet配置 一.解决远程登陆乱码问题 目标:在xwindow和其console中使用中文界面,在纯console中使用英文 在/etc/profile最后加上一行 e ...