Adding an export for HOGDetector compatibility.
authorMathieu Barnachon <mathieu.barnachon@gmail.com>
Thu, 22 Aug 2013 03:00:32 +0000 (15:00 +1200)
committerMathieu Barnachon <mathieu.barnachon@gmail.com>
Thu, 22 Aug 2013 03:00:32 +0000 (15:00 +1200)
An SVM train with HOG could export a single vector for HOGDetector.

modules/ml/include/opencv2/ml.hpp
modules/ml/src/svm.cpp

index 7a334f2..dfa605d 100644 (file)
@@ -518,6 +518,9 @@ public:
     virtual CvSVMParams get_params() const { return params; };
     CV_WRAP virtual void clear();
 
+    // return a single vector for HOG detector.
+    virtual void get_svm_detector( std::vector< float > & detector ) const;
+
     static CvParamGrid get_default_grid( int param_id );
 
     virtual void write( CvFileStorage* storage, const char* name ) const;
index 581abb6..7ebecf8 100644 (file)
@@ -1245,6 +1245,37 @@ const float* CvSVM::get_support_vector(int i) const
     return sv && (unsigned)i < (unsigned)sv_total ? sv[i] : 0;
 }
 
+void CvSVM::get_svm_detector( std::vector< float > & detector ) const
+{
+    CV_Assert( var_all > 0 &&
+               sv_total > 0 &&
+               sv != 0 &&
+               decision_func != 0 &&
+               decision_func->alpha != 0 &&
+               decision_func->sv_count == sv_total );
+    float svi = 0.f;
+
+    detector.clear(); //clear stuff in vector.
+    detector.reserve( var_all + 1 ); //reserve place for memory efficiency.
+
+    /**
+     * detector^i = \sum_j support_vector_j^i * \alpha_j
+     * detector^dim = -\rho
+     */
+    for( int i = 0 ; i < var_all ; ++i )
+    {
+        svi = 0.f;
+        for( int j = 0 ; j < sv_total ; ++j )
+        {
+            if( decision_func->sv_index != NULL ) // sometime the sv_index isn't store on YML/XML.
+                svi += (float)( sv[decision_func->sv_index[j]][i] * decision_func->alpha[ j ] );
+            else
+                svi += (float)( sv[j][i] * decision_func->alpha[ j ] );
+        }
+        detector.push_back( svi );
+    }
+    detector.push_back( -decision_func->rho );
+}
 
 bool CvSVM::set_params( const CvSVMParams& _params )
 {