Advertisement
Guest User

jompa234

a guest
Mar 12th, 2013
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. const int NUMBER_SUPPORT_VECTORS = 47;
  2. const int FEATURE_COUNT = 3780;
  3. const float RHO = -3.0012184359343319e-001;
  4. string trainedmodel_file_name = "\\trained_svm\\trained_svm_cup.dat";
  5.  
  6.     float alphas[NUMBER_SUPPORT_VECTORS] = {3.2053527868542001e-003, 1.5553905458136224e-003,
  7.              2.9452720547068517e-004, 3.2824372327225249e-003,
  8.              3.2425279604319417e-003, 1.3063714194970718e-003,
  9.              1.1695732894871692e-003, 9.6702676814445913e-004,
  10.              2.4027870634888526e-003, 2.3599372023786595e-004,
  11.              1.1337484230573383e-003, 4.3153771462965907e-003,
  12.              1.2042344053693243e-003, 1.0541910999459186e-003,
  13.              5.0809912373503859e-003, 4.7009057917870301e-003,
  14.              1.9739495612086169e-003, 1.1963889697291360e-003,
  15.              3.6251557531878976e-003, 3.7693095137171815e-004,
  16.              2.1398841909665467e-003, 3.1497181481758828e-004,
  17.              2.0503986996999310e-003, 1.2500314690926857e-003,
  18.              6.8125903822869422e-004, 5.4317464372580851e-003,
  19.              -4.1390085186689633e-003, -5.2683688057603879e-003,
  20.              -8.6586224845176536e-004, -3.4918318024996256e-004,
  21.              -2.5953444888890871e-003, -3.5922821776841812e-003,
  22.              -1.3102360136554253e-003, -3.6668285159909670e-003,
  23.              -1.1080503083321796e-003, -3.8175695946552997e-003,
  24.              -1.9677580869279982e-003, -4.9864690635346709e-003,
  25.              -2.6933825551601572e-003, -1.8505823303022439e-004,
  26.              -1.5228635958362317e-003, -5.4621914133806356e-004,
  27.              -2.3781817484084377e-003, -3.4432957270396991e-003,
  28.              -4.4566918814856159e-003, -2.6657078377878567e-003,
  29.              -2.6337912586287052e-003
  30.     };
  31.  
  32.         cout << endl << "Loading and converting svm model...";
  33.         svm.load((char*)trainedmodel_file_name.c_str()); // loading
  34.  
  35.         //int sv_count = svm.get_support_vector_count();
  36.         int sv_count = NUMBER_SUPPORT_VECTORS;
  37.         int feature_count = FEATURE_COUNT;
  38.        
  39.         //multiply each row with a constant.
  40.         Mat m(sv_count,feature_count,CV_32FC1);
  41.         for(int r=0; r< sv_count; r++){
  42.             float tmp=0;
  43.             const float* v = svm.get_support_vector(r);
  44.             for(int c=0; c<feature_count; c++){
  45.                 tmp = alphas[r] * (*v);
  46.                 m.row(r).col(c) = tmp;
  47.                 v++;
  48.             }
  49.         }
  50.        
  51.         //sum each column and flatten to [1 * 3780]
  52.         Mat newMat(1,feature_count,CV_32FC1);
  53.         for(int c=0; c<feature_count; c++){
  54.             float tmp1 =0;
  55.             for(int r=0; r<sv_count; r++){
  56.                 tmp1 = tmp1 + m.at<float>(r,c);
  57.             }
  58.             newMat.row(0).col(c) = tmp1;
  59.         }
  60.        
  61.         //copy from OpenCv:Mat to vector by using pointer.
  62.         const float* p = newMat.ptr<float>(0);
  63.         std::vector<float> finalVector(p, p + newMat.cols);
  64.        
  65.         //appending -rho
  66.         finalVector.push_back(-RHO);
  67.        
  68.        
  69.         cout << endl << "Done loading and converting model...";
  70.        
  71.         //set the custom detector-vector.
  72.         HOGDescriptor hog;
  73.         hog.setSVMDetector(finalVector);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement