想使用CvSVM进行训练和检测
自己写了个程序,训练的结果出来了,但检测达不到自己希望的程度
我希望说可以检测处一张图片的那个位置有人体
但是CvSVM似乎紧紧是返回1和0
使用hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2)
但这个函数需要一个检测算子,而不是XML文件
需要一个转换的过程,但没有弄懂怎么转换!还在研究中
这里贴出把图片读入并训练成xml文件的程序
如果谁知道怎么得到检测算子,请告诉我,万分感谢!
#include <fstream> #include <string> #include <cv.h> #include <highgui.h> #include <ml.h> #include <iostream> #include <fstream> #include <string> #include <vector> #include "cvaux.h" #include <iostream> using namespace std; using namespace cv; int main() { CvSVM svm; //CvMat *feature_vec_mat,*res_mat; int size = 2416+1218; CvMat *feature_vec_mat,*res_mat; feature_vec_mat = cvCreateMat(size,3780,CV_32FC1); cvSetZero(feature_vec_mat); res_mat = cvCreateMat(size,1,CV_32FC1); cvSetZero(res_mat); int i; ////////////////////////////////////////正样本///////////////////////////////////////////////// /////////////////////////////////read this picture filename/////////////////////////////////// ifstream readfile("D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/pos.lst",ios::in| ios::binary); ifstream readfile1("D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/neg.lst",ios::in| ios::binary); for(i=0;i<2416;i++) { //ifstream readfile("D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/pos.lst",ios::in| ios::binary); char filename[100]; readfile.getline(filename,100); cout<<filename<<endl; char filename1[1024]; //sprintf(filename1,"D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/".filename); strcpy(filename1,"D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/"); strcat(filename1,filename); ////////////////////////////////read the data of the picture/////////////////////////////////// Mat img; img = imread(filename1); //imshow("example",img); //waitKey(1); /////////////////////////////////chenge zhe picture to the 64*128//////////////////////////////// Mat imgtest(128,64,CV_8UC1); for(int j=0;j<128;j++) { for(int n=0;n<64;n++) { *(imgtest.data+j*imgtest.step+n)=*(img.data+(j+16)*img.step+(n+16)*3); } } //imshow("example",imgtest); //waitKey(0); /////////////////////////////////computer hog feature///////////////////////////////////////// HOGDescriptor hog; vector<float> descriptors; hog.compute(imgtest,descriptors,Size(8,8)); int feature_vec_length = descriptors.size(); //int feature_vec_length = 3780; for (int j=0;j<feature_vec_length;j++) { //feature_vec_mat->data.fl[j][i]=descriptors[i]; CV_MAT_ELEM(*feature_vec_mat, float, i, j ) = descriptors[j]; //feature_vec_mat->data.fl[j+i*feature_vec_mat->step]=descriptors[i]; } //CV_MAT_ELEM(*res_mat, float, i, 1 ) = 1; res_mat->data.fl[i] = 1; } //fclose(readfile); //////////////////////////////////////////负样本/////////////////////////////////////////// for(int e=0;e<1218;e++) { //ifstream readfile("D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/pos.lst",ios::in| ios::binary); char filename[100]; readfile1.getline(filename,100); cout<<filename<<endl; char filename1[1024]; //sprintf(filename1,"D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/".filename); strcpy(filename1,"D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/"); strcat(filename1,filename); ////////////////////////////////read the data of the picture/////////////////////////////////// Mat img; img = imread(filename1); //imshow("example",img); //waitKey(1); /////////////////////////////////chenge zhe picture to the 64*128//////////////////////////////// Mat imgtest(128,64,CV_8UC1); for(int j=0;j<128;j++) { for(int n=0;n<64;n++) { *(imgtest.data+j*imgtest.step+n)=*(img.data+(j+16)*img.step+(n+16)*3); } } //imshow("example",imgtest); //waitKey(0); /////////////////////////////////computer hog feature///////////////////////////////////////// HOGDescriptor hog; vector<float> descriptors; hog.compute(imgtest,descriptors,Size(8,8)); int feature_vec_length = descriptors.size(); //int feature_vec_length = 3780; for (int j=0;j<feature_vec_length;j++) { //feature_vec_mat->data.fl[j][i]=descriptors[i]; CV_MAT_ELEM(*feature_vec_mat, float, e+2416, j ) = descriptors[j]; //feature_vec_mat->data.fl[j+i*feature_vec_mat->step]=descriptors[i]; } //CV_MAT_ELEM(*res_mat, float, i+100, 1 ) = 0; res_mat->data.fl[e+2416] = 0; } ///////////////////////////////////////train SVM//////////////////////////////////////////////////// CvTermCriteria criteria; criteria = cvTermCriteria(CV_TERMCRIT_EPS,1000,FLT_EPSILON); svm.train(feature_vec_mat,res_mat,NULL,NULL,CvSVMParams(CvSVM::C_SVC,CvSVM::RBF,10.0,0.09,1.0,10.0,0.5,1.0,NULL,criteria)); svm.save("D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/result.xml"); //svm.load("D:/My Documents/Visual Studio 2008/Projects/hogjiance/Debug/result.xml"); //svm.get_support_vector return 0; }