日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

SVM+HOG:利用训练好的XML进行行人检测(检测效果)

發(fā)布時(shí)間:2025/3/21 asp.net 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SVM+HOG:利用训练好的XML进行行人检测(检测效果) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
說明:HOG+SVM生成的.xml文件不能用人臉的代碼進(jìn)行測(cè)試效果,必須用下面的代碼才能測(cè)試代碼。 #include <iostream> #include <fstream> #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; //繼承自CvSVM的類,因?yàn)樯蓅etSVMDetector()中用到的檢測(cè)子參數(shù)時(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() {//檢測(cè)窗口(64,128),塊尺寸(16,16),塊步長(zhǎng)(8,8),cell尺寸(8,8),直方圖bin個(gè)數(shù)9 HOGDescriptor hog(Size(64,128),Size(16,16),Size(8,8),Size(8,8),9);//HOG檢測(cè)器,用來計(jì)算HOG描述子的 int DescriptorDim;//HOG描述子的維數(shù),由圖片大小、檢測(cè)窗口大小、塊大小、細(xì)胞單元中直方圖bin個(gè)數(shù)決定 MySVM svm;//SVM分類器 svm.load("SVM_HOG_2400PosINRIA_12000Neg_HardExample.xml");//從XML文件讀取訓(xùn)練好的SVM模型 /************************************************************************************************* 線性SVM訓(xùn)練完成后得到的XML文件里面,有一個(gè)數(shù)組,叫做support vector,還有一個(gè)數(shù)組,叫做alpha,有一個(gè)浮點(diǎn)數(shù),叫做rho; 將alpha矩陣同support vector相乘,注意,alpha*supportVector,將得到一個(gè)列向量。之后,再該列向量的最后添加一個(gè)元素rho。 如此,變得到了一個(gè)分類器,利用該分類器,直接替換opencv中行人檢測(cè)默認(rèn)的那個(gè)分類器(cv::HOGDescriptor::setSVMDetector()), 就可以利用你的訓(xùn)練樣本訓(xùn)練出來的分類器進(jìn)行行人檢測(cè)了。 ***************************************************************************************************/ DescriptorDim = svm.get_var_count();//特征向量的維數(shù),即HOG描述子的維數(shù) int supportVectorNum = svm.get_support_vector_count();//支持向量的個(gè)數(shù) cout<<"支持向量個(gè)數(shù):"<<supportVectorNum<<endl; Mat alphaMat = Mat::zeros(1, supportVectorNum, CV_32FC1);//alpha向量,長(zhǎng)度等于支持向量個(gè)數(shù) Mat supportVectorMat = Mat::zeros(supportVectorNum, DescriptorDim, CV_32FC1);//支持向量矩陣 Mat resultMat = Mat::zeros(1, DescriptorDim, CV_32FC1);//alpha向量乘以支持向量矩陣的結(jié)果 //將支持向量的數(shù)據(jù)復(fù)制到supportVectorMat矩陣中 for(int i=0; i<supportVectorNum; i++) { const float * pSVData = svm.get_support_vector(i);//返回第i個(gè)支持向量的數(shù)據(jù)指針 for(int j=0; j<DescriptorDim; j++) { supportVectorMat.at<float>(i,j) = pSVData[j]; } } //將alpha向量的數(shù)據(jù)復(fù)制到alphaMat中 double * pAlphaData = svm.get_alpha_vector();//返回SVM的決策函數(shù)中的alpha向量 for(int i=0; i<supportVectorNum; i++) { alphaMat.at<float>(0,i) = pAlphaData[i]; } //計(jì)算-(alphaMat * supportVectorMat),結(jié)果放到resultMat中 //gemm(alphaMat, supportVectorMat, -1, 0, 1, resultMat);//不知道為什么加負(fù)號(hào)? resultMat = -1 * alphaMat * supportVectorMat; //得到最終的setSVMDetector(const vector<float>& detector)參數(shù)中可用的檢測(cè)子 vector<float> myDetector; //將resultMat中的數(shù)據(jù)復(fù)制到數(shù)組myDetector中 for(int i=0; i<DescriptorDim; i++) { myDetector.push_back(resultMat.at<float>(0,i)); } //最后添加偏移量rho,得到檢測(cè)子 myDetector.push_back(svm.get_rho()); cout<<"檢測(cè)子維數(shù):"<<myDetector.size()<<endl; //設(shè)置HOGDescriptor的檢測(cè)子 HOGDescriptor myHOG; myHOG.setSVMDetector(myDetector); //myHOG.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); //保存檢測(cè)子參數(shù)到文件 ofstream fout("HOGDetectorForOpenCV.txt"); for(int i=0; i<myDetector.size(); i++) { fout<<myDetector[i]<<endl; } /**************讀入圖片進(jìn)行HOG行人檢測(cè)******************/ Mat src = imread("Test.jpg"); vector<Rect> found, found_filtered;//矩形框數(shù)組 cout<<"進(jìn)行多尺度HOG人體檢測(cè)"<<endl; myHOG.detectMultiScale(src, found, 0, Size(8,8), Size(32,32), 1.05, 2);//對(duì)圖片進(jìn)行多尺度行人檢測(cè) cout<<"找到的矩形框個(gè)數(shù):"<<found.size()<<endl; //找出所有沒有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的話,則取外面最大的那個(gè)矩形框放入found_filtered中 for(int i=0; i < found.size(); i++) { Rect r = found[i]; int j=0; for(; j < found.size(); j++) if(j != i && (r & found[j]) == r) break; if( j == found.size()) found_filtered.push_back(r); } //畫矩形框,因?yàn)閔og檢測(cè)出的矩形框比實(shí)際人體框要稍微大些,所以這里需要做一些調(diào)整 for(int i=0; i<found_filtered.size(); i++) { Rect r = found_filtered[i]; r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.07); r.height = cvRound(r.height*0.8); rectangle(src, r.tl(), r.br(), Scalar(0,255,0), 3); } //imwrite("ImgProcessed.jpg",src); namedWindow("src",0); imshow("src",src); waitKey(0);//注意:imshow之后必須加waitKey,否則無法顯示圖像 ///******************讀入單個(gè)64*128的測(cè)試圖并對(duì)其HOG描述子進(jìn)行分類*********************/ 讀取測(cè)試圖片(64*128大小),并計(jì)算其HOG描述子 //Mat testImg = imread("noperson000026.jpg"); //vector<float> descriptor; //hog.compute(testImg,descriptor,Size(8,8));//計(jì)算HOG描述子,檢測(cè)窗口移動(dòng)步長(zhǎng)(8,8) //Mat testFeatureMat = Mat::zeros(1,3780,CV_32FC1);//測(cè)試樣本的特征向量矩陣 將計(jì)算好的HOG描述子復(fù)制到testFeatureMat矩陣中 //for(int i=0; i<descriptor.size(); i++) // testFeatureMat.at<float>(0,i) = descriptor[i]; 用訓(xùn)練好的SVM分類器對(duì)測(cè)試圖片的特征向量進(jìn)行分類 //int result = svm.predict(testFeatureMat);//返回類標(biāo) //cout<<"分類結(jié)果:"<<result<<endl; system("pause"); }運(yùn)行結(jié)果圖:

總結(jié)

以上是生活随笔為你收集整理的SVM+HOG:利用训练好的XML进行行人检测(检测效果)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。