- #include "cv.h"
- #include "highgui.h"
- #include "stdafx.h"
- #include <ml.h>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace cv;
- using namespace std;
- int main(int argc, char** argv)
- {
- vector<string> img_path;
- vector<int> img_catg;
- int nLine = 0;
- string buf;
- ifstream svm_data( "E:/SVM_DATA.txt" );
- unsigned long n;
- while( svm_data )
- {
- if( getline( svm_data, buf ) )
- {
- nLine ++;
- if( nLine % 2 == 0 )
- {
- img_catg.push_back( atoi( buf.c_str() ) );//atoi將字符串轉換成整型,標誌(0,1)
- }
- else
- {
- img_path.push_back( buf );//圖像路徑
- }
- }
- }
- svm_data.close();//關閉文件
- CvMat *data_mat, *res_mat;
- int nImgNum = nLine / 2; //讀入樣本數量
- ////樣本矩陣,nImgNum:橫座標是樣本數量, WIDTH * HEIGHT:樣本特徵向量,即圖像大小
- data_mat = cvCreateMat( nImgNum, 1764, CV_32FC1 );
- cvSetZero( data_mat );
- //類型矩陣,存儲每個樣本的類型標誌
- res_mat = cvCreateMat( nImgNum, 1, CV_32FC1 );
- cvSetZero( res_mat );
- IplImage* src;
- IplImage* trainImg=cvCreateImage(cvSize(64,64),8,3);//需要分析的圖片
- for( string::size_type i = 0; i != img_path.size(); i++ )
- {
- src=cvLoadImage(img_path[i].c_str(),1);
- if( src == NULL )
- {
- cout<<" can not load the image: "<<img_path[i].c_str()<<endl;
- continue;
- }
- cout<<" processing "<<img_path[i].c_str()<<endl;
- cvResize(src,trainImg); //讀取圖片
- HOGDescriptor *hog=new HOGDescriptor(cvSize(64,64),cvSize(16,16),cvSize(8,8),cvSize(8,8),9); //具體意思見參考文章1,2
- vector<float>descriptors;//結果數組
- hog->compute(trainImg, descriptors,Size(1,1), Size(0,0)); //調用計算函數開始計算
- cout<<"HOG dims: "<<descriptors.size()<<endl;
- //CvMat* SVMtrainMat=cvCreateMat(descriptors.size(),1,CV_32FC1);
- n=0;
- for(vector<float>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++)
- {
- cvmSet(data_mat,i,n,*iter);
- n++;
- }
- //cout<<SVMtrainMat->rows<<endl;
- cvmSet( res_mat, i, 0, img_catg[i] );
- cout<<" end processing "<<img_path[i].c_str()<<" "<<img_catg[i]<<endl;
- }
- CvSVM svm = CvSVM();
- CvSVMParams param;
- CvTermCriteria criteria;
- criteria = cvTermCriteria( CV_TERMCRIT_EPS, 1000, FLT_EPSILON );
- param = CvSVMParams( CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria );
- /*
- SVM種類:CvSVM::C_SVC
- Kernel的種類:CvSVM::RBF
- degree:10.0(此次不使用)
- gamma:8.0
- coef0:1.0(此次不使用)
- C:10.0
- nu:0.5(此次不使用)
- p:0.1(此次不使用)
- 然後對訓練數據正規化處理,並放在CvMat型的數組裏。
- */
- //☆☆☆☆☆☆☆☆☆(5)SVM學習☆☆☆☆☆☆☆☆☆☆☆☆
- svm.train( data_mat, res_mat, NULL, NULL, param );
- //☆☆利用訓練數據和確定的學習參數,進行SVM學習☆☆☆☆
- svm.save( "SVM_DATA.xml" );
- //檢測樣本
- IplImage *test;
- vector<string> img_tst_path;
- ifstream img_tst( "E:/SVM_TEST.txt" );
- while( img_tst )
- {
- if( getline( img_tst, buf ) )
- {
- img_tst_path.push_back( buf );
- }
- }
- img_tst.close();
- CvMat *test_hog = cvCreateMat( 1, 1764, CV_32FC1 );
- char line[512];
- ofstream predict_txt( "SVM_PREDICT.txt" );
- for( string::size_type j = 0; j != img_tst_path.size(); j++ )
- {
- test = cvLoadImage( img_tst_path[j].c_str(), 1);
- if( test == NULL )
- {
- cout<<" can not load the image: "<<img_tst_path[j].c_str()<<endl;
- continue;
- }
- cvZero(trainImg);
- cvResize(test,trainImg); //讀取圖片
- HOGDescriptor *hog=new HOGDescriptor(cvSize(64,64),cvSize(16,16),cvSize(8,8),cvSize(8,8),9); //具體意思見參考文章1,2
- vector<float>descriptors;//結果數組
- hog->compute(trainImg, descriptors,Size(1,1), Size(0,0)); //調用計算函數開始計算
- cout<<"HOG dims: "<<descriptors.size()<<endl;
- CvMat* SVMtrainMat=cvCreateMat(1,descriptors.size(),CV_32FC1);
- n=0;
- for(vector<float>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++)
- {
- cvmSet(SVMtrainMat,0,n,*iter);
- n++;
- }
- int ret = svm.predict(SVMtrainMat);
- sprintf( line, "%s %d\r\n", img_tst_path[j].c_str(), ret );
- predict_txt<<line;
- }
- predict_txt.close();
- //cvReleaseImage( &src);
- //cvReleaseImage( &sampleImg );
- //cvReleaseImage( &tst );
- //cvReleaseImage( &tst_tmp );
- cvReleaseMat( &data_mat );
- cvReleaseMat( &res_mat );
- return 0;
- }
學習OpenCV——HOG+SVM
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.