Hello~洛基在上一篇關於人體檢測的文章末尾提到了自舉法,這裏科普一下,所謂自舉法,即在一個容量爲n的原始樣本中重複抽取一系列容量也是n的隨機樣本,並保證每次抽樣中每一樣本觀察值被抽取的概率都爲1/n。好像不是很通俗易懂,說人話就是——應用於行人檢測中的自舉方法是,對於訓練好的HOG檢測器,將原來進行訓練的負樣本當做檢測樣本,利用檢測器進行檢測,檢測出來的圖像必定是不包含人體的樣本,這些不包含人體的樣本集合便成爲了自舉訓練樣本,將自舉訓練樣本處理之後重新作爲負樣本,輸入到SVM中進行再一次訓練,如此一來得到的檢測器會“進化”爲錯檢率更低的進化版檢測器。
Of course,自舉訓練可以進行多次,這樣分類的效果可能會更好,不過也不排除,在進行了多次自舉訓練之後,檢測器出現大量漏檢情況哦- -所以自舉訓練的次數,可能並不是越多越好,這是我在進行試驗的小總結(認真臉)。
Okay進入正題,也就是代碼部分。
#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>
using namespace std;
using namespace cv;
#define str "E:\\INRIAPerson\\HardExample\\"
int hardExampleCount = 0;
//因爲在生成setSVMDetector中用到的檢測子參數時要用到訓練好的SVM的decision_func的protected類型參數alpha和rho,故只能通過繼承之後利用函數訪問了
class MySVM : public CvSVM
{
public:
//獲得SVM的決策函數中的alpha數組
double * get_alpha_vector()
{
return this->decision_func->alpha;
}
//獲得SVM的決策函數中的rho參數,即偏移量
float get_rho()
{
return this->decision_func->rho;
}
};
int main()
{
Mat src;
char saveName[256];//剪裁出來的hard example圖片的文件名
string ImgName;
//ifstream fin_detector("HOGDetectorParagram.txt");
ifstream fin_imgList("neg.txt");//打開原始負樣本圖片文件列表
//每次在載入計算好的HOG描述子,然後設置SVM檢測器時,都會報錯——Assertion failed checkDetectorSize。令人抓狂的一個錯誤
//於是只能每次都重新根據訓練好的SVM.xml,計算HOG描述子,然後再設置SVM檢測器了。這個問題留到後面再解決!
cout<<"載入訓練好的SVM分類器"<<endl;
int DescriptorDim;//特徵向量維數,即HOG描述子的維數
MySVM svm;
svm.load("SVM_HOG.xml");
DescriptorDim = svm.get_var_count();
cout<<"描述子維數:"<<DescriptorDim<<endl;
int supportVectorNum = svm.get_support_vector_count();//支持向量的個數
cout<<"支持向量個數:"<<supportVectorNum<<endl;
//Mat alphaMat = Mat::zeros(1, supportVectorNum, CV_32FC1);//alpha向量,長度等於支持向量個數
Mat alphaMat = Mat::zeros(1, supportVectorNum, CV_32FC1);
Mat supportVectorMat = Mat::zeros(supportVectorNum, DescriptorDim, CV_32FC1);//支持向量矩陣
Mat resultMat = Mat::zeros(1, DescriptorDim, CV_32FC1);//alpha向量乘以支持向量矩陣的結果
//將支持向量的數據複製到supportVectorMat矩陣中,共有supportVectorNum個支持向量,每個支持向量的數據有DescriptorDim維(種)
for(int i=0; i<supportVectorNum; i++)
{
const float * pSVData = svm.get_support_vector(i);//返回第i個支持向量的數據指針
for(int j=0; j<DescriptorDim; j++)
supportVectorMat.at<float>(i,j) = pSVData[j];//第i個向量的第j維數據
}
//將alpha向量的數據複製到alphaMat中
//double * pAlphaData = svm.get_alpha_vector();//返回SVM的決策函數中的alpha向量
double * pAlphaData = svm.get_alpha_vector();
for(int i=0; i<supportVectorNum; i++)
{
alphaMat.at<float>(0,i) = pAlphaData[i];//alpha向量,長度等於支持向量個數
}
//計算-(alphaMat * supportVectorMat),結果放到resultMat中
resultMat = -1 * alphaMat * supportVectorMat;//上一篇博文中有解釋爲什麼是負號,這裏不贅述
//得到最終的setSVMDetector(const vector<float>& detector)參數中可用的檢測子
vector<float> myDetector;
//將resultMat中的數據複製到數組myDetector中
for(int i=0; i<DescriptorDim; i++)
{
myDetector.push_back(resultMat.at<float>(0,i));
}
myDetector.push_back(svm.get_rho());//最後添加偏移量rho,得到檢測子
cout<<"檢測子維數:"<<myDetector.size()<<endl;
//設置HOGDescriptor的檢測子
HOGDescriptor hog(Size(64,128),Size(16,16),Size(8,8),Size(8,8),9);
hog.setSVMDetector(myDetector);
//從文件中讀入自己訓練的SVM參數
//float temp;
//vector<float> myDetector;//自己的檢測器數組
//while(!fin_detector.eof())
//{
//fin_detector >> temp;
//myDetector.push_back(temp);
//}
//cout<<"檢測子維數:"<<myDetector.size()<<endl;
//設置檢測器參數爲自己訓練的SVM參數
//HOGDescriptor hog;
//hog.setSVMDetector(myDetector);//上面說過了,如果直接讀入之前保存好的HOG描述子txt文件,會一直報錯= =no idea to solve it
while(getline(fin_imgList,ImgName))
{
cout<<"處理:"<<ImgName<<endl;
string fullName = "E:\\INRIAPerson\\Negjpg_undesign\\" + ImgName;
src = imread(fullName);
Mat img = src.clone();
vector<Rect> found;
hog.detectMultiScale(src, found, 0, Size(8,8),Size(32,32), 1.05, 2);
//處理得到的矩形框
for(int i=0; i < found.size(); i++)
{
//將矩形框輪廓限定在圖像內部,r的x、y座標是相對於源圖像src來定義的
Rect r = found[i];
if(r.x < 0)
r.x = 0;
if(r.y < 0)
r.y = 0;
if(r.x + r.width > src.cols)
r.width = src.cols - r.x;
if(r.y + r.height > src.rows)
r.height = src.rows - r.y;
//將矩形框框出的圖片保存爲難例
Mat hardExampleImg = src(r);//從原圖上截取矩形框大小的圖片
resize(hardExampleImg,hardExampleImg,Size(64,128));//將剪裁出來的圖片縮放爲64*128大小
sprintf(saveName,"hardexample%09d.jpg",hardExampleCount++);//生成hard example圖片的文件名
imwrite(saveName, hardExampleImg);//保存框出的圖片部分,這裏沒辦法添加一個路徑名稱,所以全部保存在VS項目裏了,好亂= =
//畫矩形框,因爲hog檢測出的矩形框比實際人體框要稍微大些,所以這裏需要做一些調整
r.x += cvRound(r.width*0.1);
r.width = cvRound(r.width*0.8);
rectangle(img, r.tl(), r.br(), Scalar(0,255,0), 2);
}
imwrite(str+ImgName,img);//將處理過的圖像保存下來,這裏保存的就是原始負樣本加上各種框框之後的結果圖片
imshow("src",src);
waitKey(20);
}
system("pause");
}
這個是經過自舉法之後得到的新檢測器的成果,是不是很Duang Duang Duang?來對比一下沒有通過自舉法得到的檢測結果:
其實我只利用了一次自舉訓練,便得到了以上的良好檢測效果,因爲檢測的正負樣本基數不夠大,所以不敢進行多次自舉訓練,擔心會造成大量漏檢。最後值得一提的是,利用原始1000多張負樣本進行自舉檢測,竟然得到了7000+張新的負樣本,這說明原來的檢測器檢測效果堪憂。。。不過好在擁有了自舉訓練這個關鍵的idea,使得我們的檢測器功能優化了很多~
Last but not least,洛基祝大家新年快樂!大家在刻苦敲代碼的同時也別忘了去鍛鍊身體哦!現在是成都時間2017年1月3日凌晨12點38分
Nighty night~ :-D