Object Recognition and Scene Understanding(六)OpenCV中HOG+SVM目標檢測

來源:互聯網
上載者:User

參考:利用Hog特徵和SVM分類器進行行人檢測http://blog.csdn.net/carson2005/article/details/7841443

 

HOG+SVM由於其特性,對行人檢測有非常好的效果,但是對其他目標檢測也有好效果。這裡就把範圍擴大些。

 

carson2005的博文中介紹了利用opencv實現樣本訓練和目標檢測。利用Libsvm也可以進行處理。

1.使用libsvm求取權重

http://www.opencv.org.cn/forum/viewtopic.php?f=1&t=9146

 

--------使用libsvm求取權重,即OpenCv中的detector[]----------

直接使用libsvm,需要按它的格式構造資料,下面簡述在matlab下使用libsvm

下載libsvm-mat-2.9-1
方法1:
切換到libsvm-mat-2.9-1所在的目錄下,開啟MATLAB鍵入:
mex -setup

方法2:matlab菜單 File-->set path 將libsvm-mat-2.9-1所在路徑添加進來。
----------------------
下面以libsvm-mat-2.9-1內建的heart_scale為例進行介紹

-----------kernel_type為線性----------------------------------
load heart_scale.mat
train_data = heart_scale_inst(1:150,:);
train_label = heart_scale_label(1:150,:);
test_data = heart_scale_inst(151:270,:);
test_label = heart_scale_label(151:270,:);
model_linear = svmtrain(train_label, train_data, '-t 0');
[predict_label_L, accuracy_L, dec_values_L] = svmpredict(test_label, test_data,model_linear);
----------訓練後得到模型-------

model_linear =

Parameters: [5x1 double]
nr_class: 2
totalSV: 58
rho: -1.1848
Label: [2x1 double]
ProbA: []
ProbB: []
nSV: [2x1 double]
sv_coef: [58x1 double]
SVs: [58x13 double]
-----------如何從模型中求取權重係數----
參考以下網站,可知 
http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f804

對於2類問題,可如下求解線性問題(y=wx+b)的權重係數w和b

w = model_linear.SVs' * model_linear.sv_coef;
b = -model_linear.rho;

---------

求出的w即是OpenCv中的detector[]

 

下面直接把carson2005的文章貼過來吧。

2.OpenCV求detector

之前介紹過Hog特徵(http://blog.csdn.net/carson2005/article/details/7782726),也介紹過SVM分類器(http://blog.csdn.net/carson2005/article/details/6453502
);而本文的目的在於介紹利用Hog特徵和SVM分類器來進行行人檢測。

        在2005年CVPR上,來自法國的研究人員Navneet Dalal和Bill
Triggs提出利用Hog進行特徵提取,利用線性SVM作為分類器,從而實現行人檢測。而這兩位也通過大量的測試發現,Hog+SVM是速度和效果綜合平衡效能較好的一種行人檢測方法。後來,雖然很多研究人員也提出了很多改進的行人檢測演算法,但基本都以該演算法為基礎架構。因此,Hog+SVM也成為一個里程錶式的演算法被寫入到OpenCV中。在OpenCV2.0之後的版本,都有Hog特徵描述運算元的API,而至於SVM,早在OpenCV1.0版本就已經整合進去了;OpenCV雖然提供了Hog和SVM的API,也提供了行人檢測的sample,遺憾的是,OpenCV並沒有提供樣本訓練的sample。這也就意味著,很多人只能用OpenCV內建的已經訓練好的分類器來進行行人檢測。然而,OpenCV內建的分類器是利用Navneet
Dalal和Bill Triggs提供的樣本進行訓練的,不見得能適用於你的應用場合。因此,針對你的特定應用情境,很有必要進行重新訓練得到適合你的分類器。本文的目的,正在於此。

重新訓練行人檢測的流程:

(1)準備訓練樣本集合;包括正樣本集和負樣本集;根據機器學習的基礎知識我們知道,要利用機器學習演算法進行樣本訓練,從而得到一個效能優良的分類器,訓練樣本應該是無限多的,而且訓練樣本應該覆蓋實際應用過程中可能發生的各種情況。(很多朋友,用10來個正樣本,10來個負樣本進行訓練,之後,就進行測試,發現效果沒有想象中的那麼好,就開始發牢騷,抱怨。。。對於這些人,我只能抱歉的說,對於機器學習、模式識別的認識,你還處於沒有入門的階段);實際應用過程中,訓練樣本不可能無限多,但無論如何,三五千個正樣本,三五千個負樣本,應該不是什麼難事吧?(如果連這個都做不到,建議你別搞機器學習,模式識別了;訓練素材都沒有,怎麼讓機器學習到足夠的資訊呢?)

(2)收集到足夠的訓練樣本之後,你需要手動裁剪樣本。例如,你想用Hog+SVM來對商業步行街的監控畫面中進行行人檢測,那麼,你就應該用收集到的訓練樣本集合,手動裁剪畫面中的行人(可以寫個簡單程式,只需要滑鼠框選一下,就將框選地區儲存下來)。

(3)裁剪得到訓練樣本之後,將所有正樣本放在一個檔案夾中;將所有負樣本放在另一個檔案夾中;並將所有訓練樣本縮放到同樣的尺寸大小。OpenCV內建的例子在訓練時,就是將樣本縮放為64*128進行訓練的;

(4)提取所有正樣本的Hog特徵;

(5)提取所有負樣本的Hog特徵;

(6)對所有正負樣本賦予樣本標籤;例如,所有正樣本標記為1,所有負樣本標記為0;

(7)將正負樣本的Hog特徵,正負樣本的標籤,都輸入到SVM中進行訓練;Dalal在論文中考慮到速度問題,建議採用線性SVM進行訓練。這裡,不妨也採用線性SVM;

(8)SVM訓練之後,將結果儲存為文字檔。

(9)線性SVM進行訓練之後得到的文字檔裡面,有一個數組,叫做support vector,還有一個數組,叫做alpha,有一個浮點數,叫做rho;將alpha矩陣同support
vector相乘,注意,alpha*supportVector,將得到一個列向量。之後,再該列向量的最後添加一個元素rho。如此,變得到了一個分類器,利用該分類器,直接替換opencv中行人檢測預設的那個分類器(cv::HOGDescriptor::setSVMDetector()),就可以利用你的訓練樣本訓練出來的分類器進行行人檢測了。

下面給出樣本訓練的參考代碼:

 

 

 

class Mysvm: public CvSVM{public:int get_alpha_count(){return this->sv_total;}int get_sv_dim(){return this->var_all;}int get_sv_count(){return this->decision_func->sv_count;}double* get_alpha(){return this->decision_func->alpha;}float** get_sv(){return this->sv;}float get_rho(){return this->decision_func->rho;}};void Train(){char classifierSavePath[256] = "c:/pedestrianDetect-peopleFlow.txt";string positivePath = "E:\\pictures\\train1\\pos\\";string negativePath = "E:\\pictures\\train1\\neg\\";int positiveSampleCount = 4900;int negativeSampleCount = 6192;int totalSampleCount = positiveSampleCount + negativeSampleCount;cout<<"//////////////////////////////////////////////////////////////////"<<endl;cout<<"totalSampleCount: "<<totalSampleCount<<endl;cout<<"positiveSampleCount: "<<positiveSampleCount<<endl;cout<<"negativeSampleCount: "<<negativeSampleCount<<endl;CvMat *sampleFeaturesMat = cvCreateMat(totalSampleCount , 1764, CV_32FC1);//64*128的訓練樣本,該矩陣將是totalSample*3780,64*64的訓練樣本,該矩陣將是totalSample*1764cvSetZero(sampleFeaturesMat);  CvMat *sampleLabelMat = cvCreateMat(totalSampleCount, 1, CV_32FC1);//樣本標識  cvSetZero(sampleLabelMat);  cout<<"************************************************************"<<endl;cout<<"start to training positive samples..."<<endl;char positiveImgName[256];string path;for(int i=0; i<positiveSampleCount; i++)  {  memset(positiveImgName, '\0', 256*sizeof(char));sprintf(positiveImgName, "%d.jpg", i);int len = strlen(positiveImgName);string tempStr = positiveImgName;path = positivePath + tempStr;cv::Mat img = cv::imread(path);if( img.data == NULL ){cout<<"positive image sample load error: "<<i<<" "<<path<<endl;system("pause");continue;}cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);vector<float> featureVec; hog.compute(img, featureVec, cv::Size(8,8));  int featureVecSize = featureVec.size();for (int j=0; j<featureVecSize; j++)  {  CV_MAT_ELEM( *sampleFeaturesMat, float, i, j ) = featureVec[j]; }  sampleLabelMat->data.fl[i] = 1;}cout<<"end of training for positive samples..."<<endl;cout<<"*********************************************************"<<endl;cout<<"start to train negative samples..."<<endl;char negativeImgName[256];for (int i=0; i<negativeSampleCount; i++){  memset(negativeImgName, '\0', 256*sizeof(char));sprintf(negativeImgName, "%d.jpg", i);path = negativePath + negativeImgName;cv::Mat img = cv::imread(path);if(img.data == NULL){cout<<"negative image sample load error: "<<path<<endl;continue;}cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);  vector<float> featureVec; hog.compute(img,featureVec,cv::Size(8,8));//計算HOG特徵int featureVecSize = featureVec.size();  for ( int j=0; j<featureVecSize; j ++)  {  CV_MAT_ELEM( *sampleFeaturesMat, float, i + positiveSampleCount, j ) = featureVec[ j ];}  sampleLabelMat->data.fl[ i + positiveSampleCount ] = -1;}  cout<<"end of training for negative samples..."<<endl;cout<<"********************************************************"<<endl;cout<<"start to train for SVM classifier..."<<endl;CvSVMParams params;  params.svm_type = CvSVM::C_SVC;  params.kernel_type = CvSVM::LINEAR;  params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON);params.C = 0.01;Mysvm svm;svm.train( sampleFeaturesMat, sampleLabelMat, NULL, NULL, params ); //用SVM線性分類器訓練svm.save(classifierSavePath);cvReleaseMat(&sampleFeaturesMat);cvReleaseMat(&sampleLabelMat);int supportVectorSize = svm.get_support_vector_count();cout<<"support vector size of SVM:"<<supportVectorSize<<endl;cout<<"************************ end of training for SVM ******************"<<endl;CvMat *sv,*alp,*re;//所有樣本特徵向量 sv  = cvCreateMat(supportVectorSize , 1764, CV_32FC1);alp = cvCreateMat(1 , supportVectorSize, CV_32FC1);re  = cvCreateMat(1 , 1764, CV_32FC1);CvMat *res  = cvCreateMat(1 , 1, CV_32FC1);cvSetZero(sv);cvSetZero(re);  for(int i=0; i<supportVectorSize; i++){memcpy( (float*)(sv->data.fl+i*1764), svm.get_support_vector(i), 1764*sizeof(float));}double* alphaArr = svm.get_alpha();int alphaCount = svm.get_alpha_count();for(int i=0; i<supportVectorSize; i++){        alp->data.fl[i] = alphaArr[i];}cvMatMul(alp, sv, re);int posCount = 0;for (int i=0; i<1764; i++){re->data.fl[i] *= -1;}FILE* fp = fopen("c:/hogSVMDetector-peopleFlow.txt","wb");if( NULL == fp ){return 1;}for(int i=0; i<1764; i++){fprintf(fp,"%f \n",re->data.fl[i]);}float rho = svm.get_rho();fprintf(fp, "%f", rho);cout<<"c:/hogSVMDetector.txt 儲存完畢"<<endl;//儲存HOG能識別的分類器fclose(fp);return 1;}

 

接著,再給出利用訓練好的分類器進行行人檢測的參考代碼:

void Detect(){CvCapture* cap = cvCreateFileCapture("E:\\02.avi");if (!cap){cout<<"avi file load error..."<<endl;system("pause");exit(-1);}vector<float> x;ifstream fileIn("c:/hogSVMDetector-peopleFlow.txt", ios::in);float val = 0.0f;while(!fileIn.eof()){fileIn>>val;x.push_back(val);}fileIn.close();vector<cv::Rect>  found;cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);hog.setSVMDetector(x);IplImage* img = NULL;cvNamedWindow("img", 0);while(img=cvQueryFrame(cap)){hog.detectMultiScale(img, found, 0, cv::Size(8,8), cv::Size(32,32), 1.05, 2);if (found.size() > 0){for (int i=0; i<found.size(); i++){CvRect tempRect = cvRect(found[i].x, found[i].y, found[i].width, found[i].height);cvRectangle(img, cvPoint(tempRect.x,tempRect.y),cvPoint(tempRect.x+tempRect.width,tempRect.y+tempRect.height),CV_RGB(255,0,0), 2);}}}cvReleaseCapture(&cap);}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.