資源簡介
自己訓(xùn)練SVM分類器進(jìn)行HOG行人檢測.
環(huán)境為VS2010 + OpenCV2.4.4.
使用時請自行修改工程的include目錄和lib目錄配置。
正樣本來源是INRIA數(shù)據(jù)集中的96*160大小的人體圖片,使用時上下左右都去掉16個像素,截取中間的64*128大小的人體。
負(fù)樣本是從不包含人體的圖片中隨機(jī)裁取的,大小同樣是64*128。
SVM使用的是OpenCV自帶的CvSVM類。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
using?namespace?cv;
#define?PosSamNO?2400????//正樣本個數(shù)
#define?NegSamNO?12000????//負(fù)樣本個數(shù)
#define?TRAIN?false????//是否進(jìn)行訓(xùn)練true表示重新訓(xùn)練,false表示讀取xml文件中的SVM模型
#define?CENTRAL_CROP?true???//true:訓(xùn)練時,對96*160的INRIA正樣本圖片剪裁出中間的64*128大小人體
//HardExample:負(fù)樣本個數(shù)。如果HardExampleNO大于0,表示處理完初始負(fù)樣本集后,繼續(xù)處理HardExample負(fù)樣本集。
//不使用HardExample時必須設(shè)置為0,因為特征向量矩陣和特征類別矩陣的維數(shù)初始化時用到這個值
#define?HardExampleNO?4435??
//繼承自CvSVM的類,因為生成setSVMDetector()中用到的檢測子參數(shù)時,需要用到訓(xùn)練好的SVM的decision_func參數(shù),
//但通過查看CvSVM源碼可知decision_func參數(shù)是protected類型變量,無法直接訪問到,只能繼承之后通過函數(shù)訪問
class?MySVM?:?public?CvSVM
{
public:
//獲得SVM的決策函數(shù)中的alpha數(shù)組
double?*?get_alpha_vector()
{
return?this->decision_func->alpha;
}
//獲得SVM的決策函數(shù)中的rho參數(shù)即偏移量
float?get_rho()
{
return?this->decision_func->rho;
}
};
int?main()
{
//檢測窗口(64128)塊尺寸(1616)塊步長(88)cell尺寸(88)直方圖bin個數(shù)9
HOGDescriptor?hog(Size(64128)Size(1616)Size(88)Size(88)9);//HOG檢測器,用來計算HOG描述子的
int?DescriptorDim;//HOG描述子的維數(shù),由圖片大小、檢測窗口大小、塊大小、細(xì)胞單元中直方圖bin個數(shù)決定
MySVM?svm;//SVM分類器
//若TRAIN為true,重新訓(xùn)練分類器
if(TRAIN)
{
string?ImgName;//圖片名(絕對路徑)
ifstream?finPos(“INRIAPerson96X160PosList.txt“);//正樣本圖片的文件名列表
//ifstream?finPos(“PersonFromVOC2012List.txt“);//正樣本圖片的文件名列表
ifstream?finNeg(“NoPersonFromINRIAList.txt“);//負(fù)樣本圖片的文件名列表
Mat?sampleFeatureMat;//所有訓(xùn)練樣本的特征向量組成的矩陣,行數(shù)等于所有樣本的個數(shù),列數(shù)等于HOG描述子維數(shù)
Mat?sampleLabelMat;//訓(xùn)練樣本的類別向量,行數(shù)等于所有樣本的個數(shù),列數(shù)等于1;1表示有人,-1表示無人
//依次讀取正樣本圖片,生成HOG描述子
for(int?num=0;?num {
cout<<“處理:“< //ImgName?=?“D:\\DataSet\\PersonFromVOC2012\\“?+?ImgName;//加上正樣本的路徑名
ImgName?=?“D:\\DataSet\\INRIAPerson\\INRIAPerson\\96X160H96\\Train\\pos\\“?+?ImgName;//加上正樣本的路徑名
Mat?src?=?imread(ImgName);//讀取圖片
if(CENTRAL_CROP)
src?=?src(Rect(161664128));//將96*160的INRIA正樣本圖片剪裁為64*128,即剪去上下左右各16個像素
//resize(srcsrcSize(64128));
vector?descriptors;//HOG描述子向量
hog.compute(srcdescriptorsSize(88));//計算HOG描述子,檢測窗口移動步長(88)
//cout<<“描述子維數(shù):“<riptors.size()<
//處理第一個樣本時初始化特征向量矩陣和類別矩陣,因為只有知道了特征向量的維數(shù)才能初始化特征向量矩陣
if(?0?==?num?)
{
DescriptorDim?=?descriptors.size();//HOG描述子的維數(shù)
//初始化所有訓(xùn)練樣本的特征向量組成的矩陣,行數(shù)等于所有樣本的個數(shù),列數(shù)等于HOG描述子維數(shù)sampleFeatureMat
sampleFeatureMat?=?Mat::zeros(PosSamNO+NegSamNO+HardExampleNO?DescriptorDim?CV_32FC1);
//初始化訓(xùn)練樣本的類別向量,行數(shù)等于所有樣本的個數(shù),列數(shù)等于1;1表示有人,0表示無人
sampleLabelMat?=?Mat::zeros(PosSamNO+NegSamNO+HardExampleNO?1?CV_32FC1);
}
//將計算好的HOG描述子復(fù)制到樣本特征矩陣sampleFeatureMat
for(int?i=0;?iriptorDim;?i++)
sampleFeatureMat.at(numi)?=?descriptors[i];//第num個樣本的特征向量中的第i個元素
sampleLabelMat.at(num0)?=?1;//正樣本類別為1,有人
}
//依次讀取負(fù)樣本圖片,生成HOG描述子
f
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????144896??2013-11-13?17:14??SVM_Train_Predict_HOG\Debug\SVM_Train_Predict_HOG.exe
?????文件?????910680??2013-11-13?17:14??SVM_Train_Predict_HOG\Debug\SVM_Train_Predict_HOG.ilk
?????文件????1747968??2013-11-13?17:14??SVM_Train_Predict_HOG\Debug\SVM_Train_Predict_HOG.pdb
?????文件?????174277??2013-10-22?16:53??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\00000.jpg
?????文件?????788547??2013-10-21?21:44??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\1.png
?????文件?????813015??2013-10-21?21:47??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\2.png
?????文件??????90689??2007-01-10?01:37??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\2007_000423.jpg
?????文件?????818604??2013-10-21?21:48??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\3.png
?????文件????1610478??2013-10-21?21:48??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\4.png
?????文件?????911894??2013-10-21?21:49??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\5.png
?????文件????????762??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\cl.command.1.tlog
?????文件??????14186??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\CL.read.1.tlog
?????文件????????470??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\CL.write.1.tlog
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
?????文件??????????2??2013-11-13?17:14??SVM_Train_Predict_HOG\SVM_Train_Predict_HOG\Debug\li
............此處省略96個文件信息
評論
共有 條評論