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 ...
随机推荐
- iOS设计模式-单例模式
(一)什么是单例模式(Singleton) 单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点 *最初的定义是在<设计模式>(Addison-Wesley)中 解读 1> ...
- Swift基础之闭包
内容纲要: 1.闭包基础 2.关于闭包循环引用 正文: 1.闭包 闭包是自包含的函数代码块,可以在代码中被传递和使用.Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks) ...
- Saiku 下载,安装
Saiku是一个模块化的开源分析套件,它提供轻量级的OLAP(联机分析处理),并且可嵌入.可扩展.可配置. 主页:http://community.meteorite.bi 如何安装摘自: http: ...
- Volley源码分析(2)----ImageLoader
一:imageLoader 先来看看如何使用imageloader: public void showImg(View view){ ImageView imageView = (ImageView) ...
- Effective Java 36 Consistently use the Override annotation
Principle Use the Override annotation on every method declaration that you believe to override a sup ...
- springMVC 接收数组参数,mybatis 接收数组参数,mybatis批量插入/批量删除案例
案例是给一个用户赋予多个权限,多个权限用其对应的主键 id 为参数,组成了 一个id数组,传给springMVC,然后springMVC传给mybatis,然后mybatis批量插入.其实类似的场景还 ...
- Android 渗透测试学习手册 翻译完成!
原书:Learning Pentesting for Android Devices 译者:飞龙 在线阅读 PDF格式 EPUB格式 MOBI格式 代码仓库 赞助我 协议 CC BY-NC-SA 4. ...
- 记一次Web应用CPU偏高
LZ开发的一个公司内部应用供查询HIVE数据使用.部署上线后总是会出现CPU偏高的情况,而且本地测试很难重现.之前出现几次都是通过直接重启后继续使用,因为是内部使用,重启一下也没有很大影响(当然,每次 ...
- python类方法和静态方法
C++的静态方法是用static关键字,python j是没用static的. python中实现静态方法和类方法都是依赖于python的修饰器来实现的. class MyClass: def me ...
- [转]让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
本文转自:http://www.cnblogs.com/dudu/archive/2009/11/10/1600062.html 今天下午17点左右,博客园博客站点出现这样的错误信息: Error S ...