From 7f3273c77f72d3cb22500d2434709eb2b540dbdf Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Thu, 22 Aug 2013 15:00:32 +1200 Subject: [PATCH 1/8] Adding an export for HOGDetector compatibility. An SVM train with HOG could export a single vector for HOGDetector. --- modules/ml/include/opencv2/ml.hpp | 3 +++ modules/ml/src/svm.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/modules/ml/include/opencv2/ml.hpp b/modules/ml/include/opencv2/ml.hpp index 7a334f2649..dfa605dbb0 100644 --- a/modules/ml/include/opencv2/ml.hpp +++ b/modules/ml/include/opencv2/ml.hpp @@ -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; diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 581abb61be..7ebecf8b87 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -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 ) { From 4fbd5707f7dc2985481d941c768ca2eb14556127 Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Thu, 22 Aug 2013 17:34:31 +1200 Subject: [PATCH 2/8] Fix warning on precision lost due to double to float conversion. --- modules/ml/src/svm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 7ebecf8b87..f7cf28d5a5 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -1274,7 +1274,7 @@ void CvSVM::get_svm_detector( std::vector< float > & detector ) const } detector.push_back( svi ); } - detector.push_back( -decision_func->rho ); + detector.push_back( (float)-decision_func->rho ); } bool CvSVM::set_params( const CvSVMParams& _params ) From 2fe340bf8a8620852b5f2dd083e8bedc4fbff53e Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Thu, 5 Sep 2013 17:33:08 +1200 Subject: [PATCH 3/8] adding sample to train HOG and compare it with default people detector. --- samples/cpp/train_HOG.cpp | 402 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 402 insertions(+) create mode 100644 samples/cpp/train_HOG.cpp diff --git a/samples/cpp/train_HOG.cpp b/samples/cpp/train_HOG.cpp new file mode 100644 index 0000000000..eeaafa831b --- /dev/null +++ b/samples/cpp/train_HOG.cpp @@ -0,0 +1,402 @@ +#include + +#include +#include +#include +#include + +#include + +using namespace cv; +using namespace std; + + +/* + * Convert training/testing set to be used by OpenCV Machine Learning algorithms. + * TrainData is a matrix of size (#samples x max(#cols,#rows) per samples), in 32FC1. + * Transposition of samples are made if needed. + */ +void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData ) +{ + //--Convert data + const int rows = (int)train_samples.size(); + const int cols = (int)std::max( train_samples[0].cols, train_samples[0].rows ); + cv::Mat tmp(1, cols, CV_32FC1); //< used for transposition if needed + trainData = cv::Mat(rows, cols, CV_32FC1 ); + auto& itr = train_samples.begin(); + auto& end = train_samples.end(); + for( int i = 0 ; itr != end ; ++itr, ++i ) + { + CV_Assert( itr->cols == 1 || + itr->rows == 1 ); + if( itr->cols == 1 ) + { + transpose( *(itr), tmp ); + tmp.copyTo( trainData.row( i ) ); + } + else if( itr->rows == 1 ) + { + itr->copyTo( trainData.row( i ) ); + } + } +} + +void load_images( const string & prefix, const string & filename, vector< Mat > & img_lst ) +{ + string line; + ifstream file; + + file.open( (prefix+filename).c_str() ); + if( !file.is_open() ) + { + cerr << "Unable to open the list of images from " << filename << " filename." << endl; + exit( -1 ); + } + + while( 1 ) + { + getline( file, line ); + if( line == "" ) // no more file to read + break; + Mat img = imread( (prefix+line).c_str() ); // load the image + if( !img.data ) // invalid image, just skip it. + continue; +#ifdef _DEBUG + imshow( "image", img ); + waitKey( 10 ); +#endif + img_lst.push_back( img.clone() ); + } +} + +void sample_neg( const vector< Mat > & full_neg_lst, vector< Mat > & neg_lst, const Size & size ) +{ + Rect box; + box.width = size.width; + box.height = size.height; + + const int size_x = box.width; + const int size_y = box.height; + + srand( time( NULL ) ); + + auto& img = full_neg_lst.begin(); + auto& end = full_neg_lst.end(); + for( ; img != end ; ++img ) + { + box.x = rand() % (img->cols - size_x); + box.y = rand() % (img->rows - size_y); + Mat roi = (*img)(box); + neg_lst.push_back( roi.clone() ); +#ifdef _DEBUG + imshow( "img", roi.clone() ); + waitKey( 10 ); +#endif + } +} + +Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, const Size & size ) +{ + const int DIMX = size.width; + const int DIMY = size.height; + float zoomFac = 3; + Mat visu; + resize(color_origImg, visu, Size(color_origImg.cols*zoomFac, color_origImg.rows*zoomFac)); + + int blockSize = 16; + int cellSize = 8; + int gradientBinSize = 9; + float radRangeForOneBin = CV_PI/(float)gradientBinSize; // dividing 180° into 9 bins, how large (in rad) is one bin? + + // prepare data structure: 9 orientation / gradient strenghts for each cell + int cells_in_x_dir = DIMX / cellSize; + int cells_in_y_dir = DIMY / cellSize; + int totalnrofcells = cells_in_x_dir * cells_in_y_dir; + float*** gradientStrengths = new float**[cells_in_y_dir]; + int** cellUpdateCounter = new int*[cells_in_y_dir]; + for (int y=0; y & img_lst, vector< Mat > & gradient_lst, const Size & size ) +{ + HOGDescriptor hog; + hog.winSize = size; + Mat gray; + vector< Point > location; + vector< float > descriptors; + + auto& img = img_lst.begin(); + auto& end = img_lst.end(); + for( ; img != end ; ++img ) + { + cvtColor( *img, gray, COLOR_BGR2GRAY ); + hog.compute( gray, descriptors, Size( 8, 8 ), Size( 0, 0 ), location ); + gradient_lst.push_back( Mat( descriptors ).clone() ); +#ifdef _DEBUG + imshow( "gradient", get_hogdescriptor_visu( img->clone(), descriptors, size ) ); + waitKey( 10 ); +#endif + } +} + +void train_svm( const vector< Mat > & gradient_lst, const vector< int > & labels ) +{ + SVM svm; + + /* Default values to train SVM */ + SVMParams params; + params.coef0 = 0.0; + params.degree = 3; + params.term_crit.epsilon = 1e-3; + params.gamma = 0; + params.kernel_type = SVM::LINEAR; + params.nu = 0.5; + params.p = 0.1; // for EPSILON_SVR, epsilon in loss function? + params.C = 0.01; // From paper, soft classifier + params.svm_type = SVM::EPS_SVR; // C_SVC; // EPSILON_SVR; // may be also NU_SVR; // do regression task + + Mat train_data; + convert_to_ml( gradient_lst, train_data ); + + clog << "Start training..."; + svm.train( train_data, Mat( labels ), Mat(), Mat(), params ); + clog << "...[done]" << endl; + + svm.save( "my_people_detector.yml" ); +} + +void draw_locations( Mat & img, const vector< Rect > & locations, const Scalar & color ) +{ + if( !locations.empty() ) + { + auto& loc = locations.begin(); + auto& end = locations.end(); + for( ; loc != end ; ++loc ) + { + rectangle( img, *loc, color, 2 ); + } + } +} + +void test_it( const Size & size ) +{ + char key = 27; + Scalar reference( 0, 255, 0 ); + Scalar trained( 0, 0, 255 ); + Mat img, draw; + SVM svm; + HOGDescriptor hog; + HOGDescriptor my_hog; + my_hog.winSize = size; + VideoCapture video; + vector< Rect > locations; + + // Load the trained SVM. + svm.load( "my_people_detector.yml" ); + // Set the trained svm to my_hog + vector< float > hog_detector; + svm.get_svm_detector( hog_detector ); + my_hog.setSVMDetector( hog_detector ); + // Set the people detector. + hog.setSVMDetector( hog.getDefaultPeopleDetector() ); + // Open the camera. + video.open(0); + if( !video.isOpened() ) + { + cerr << "Unable to open the device 0" << endl; + exit( -1 ); + } + + while( true ) + { + video >> img; + if( !img.data ) + break; + + draw = img.clone(); + + locations.clear(); + hog.detectMultiScale( img, locations ); + draw_locations( draw, locations, reference ); + + locations.clear(); + my_hog.detectMultiScale( img, locations ); + draw_locations( draw, locations, trained ); + + imshow( "Video", draw ); + key = waitKey( 10 ); + if( 27 == key ) + break; + } +} + +int main( int argc, char** argv ) +{ + if( argc != 4 ) + { + cout << "Wrong number of parameters." << endl + << "Usage: " << argv[0] << " pos_dir pos.lst neg_dir neg.lst" << endl + << "example: " << argv[0] << " /INRIA_dataset/ Train/pos.lst /INRIA_dataset/ Train/neg.lst" << endl; + exit( -1 ); + } + vector< Mat > pos_lst; + vector< Mat > full_neg_lst; + vector< Mat > neg_lst; + vector< Mat > gradient_lst; + vector< int > labels; + + load_images( argv[1], argv[2], pos_lst ); + labels.assign( pos_lst.size(), +1 ); + const int old = labels.size(); + load_images( argv[3], argv[4], full_neg_lst ); + sample_neg( full_neg_lst, neg_lst, Size( 96,160 ) ); + labels.insert( labels.end(), neg_lst.size(), -1 ); + CV_Assert( old < labels.size() ); + + compute_hog( pos_lst, gradient_lst, Size( 96, 160 ) ); + compute_hog( neg_lst, gradient_lst, Size( 96, 160 ) ); + + train_svm( gradient_lst, labels ); + + test_it( Size( 96, 160 ) ); // change with your parameters + + return 0; +} From 0934344a3d5ed5d7e4c66fb1307fdaa0ec0d7bed Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Thu, 12 Sep 2013 18:38:49 +0200 Subject: [PATCH 4/8] Update sample and code with external computation of HOG detector. --- modules/ml/include/opencv2/ml.hpp | 3 +- modules/ml/src/svm.cpp | 32 ------ samples/cpp/train_HOG.cpp | 156 +++++++++++++++++++----------- 3 files changed, 100 insertions(+), 91 deletions(-) diff --git a/modules/ml/include/opencv2/ml.hpp b/modules/ml/include/opencv2/ml.hpp index dfa605dbb0..09b5abdd76 100644 --- a/modules/ml/include/opencv2/ml.hpp +++ b/modules/ml/include/opencv2/ml.hpp @@ -518,8 +518,7 @@ 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; + virtual const CvSVMDecisionFunc* get_decision_function() const { return decision_func; } static CvParamGrid get_default_grid( int param_id ); diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index f7cf28d5a5..ca538ad5cc 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -1245,38 +1245,6 @@ 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( (float)-decision_func->rho ); -} - bool CvSVM::set_params( const CvSVMParams& _params ) { bool ok = false; diff --git a/samples/cpp/train_HOG.cpp b/samples/cpp/train_HOG.cpp index eeaafa831b..ae2d1963bf 100644 --- a/samples/cpp/train_HOG.cpp +++ b/samples/cpp/train_HOG.cpp @@ -11,22 +11,64 @@ using namespace cv; using namespace std; +void get_svm_detector(const SVM& svm, vector< float > & hog_detector ) +{ + // get the number of variables + const int var_all = svm.get_var_count(); + // get the number of support vectors + const int sv_total = svm.get_support_vector_count(); + // get the decision function + const CvSVMDecisionFunc* decision_func = svm.get_decision_function(); + // get the support vectors + const float** sv = &(svm.get_support_vector(0)); + + CV_Assert( var_all > 0 && + sv_total > 0 && + decision_func != 0 && + decision_func->alpha != 0 && + decision_func->sv_count == sv_total ); + + float svi = 0.f; + + hog_detector.clear(); //clear stuff in vector. + hog_detector.reserve( var_all + 1 ); //reserve place for memory efficiency. + + /** + * hog_detector^i = \sum_j support_vector_j^i * \alpha_j + * hog_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 ] ); + } + hog_detector.push_back( svi ); + } + hog_detector.push_back( (float)-decision_func->rho ); +} + + /* - * Convert training/testing set to be used by OpenCV Machine Learning algorithms. - * TrainData is a matrix of size (#samples x max(#cols,#rows) per samples), in 32FC1. - * Transposition of samples are made if needed. - */ +* Convert training/testing set to be used by OpenCV Machine Learning algorithms. +* TrainData is a matrix of size (#samples x max(#cols,#rows) per samples), in 32FC1. +* Transposition of samples are made if needed. +*/ void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData ) { - //--Convert data + //--Convert data const int rows = (int)train_samples.size(); const int cols = (int)std::max( train_samples[0].cols, train_samples[0].rows ); cv::Mat tmp(1, cols, CV_32FC1); //< used for transposition if needed - trainData = cv::Mat(rows, cols, CV_32FC1 ); - auto& itr = train_samples.begin(); - auto& end = train_samples.end(); - for( int i = 0 ; itr != end ; ++itr, ++i ) - { + trainData = cv::Mat(rows, cols, CV_32FC1 ); + auto& itr = train_samples.begin(); + auto& end = train_samples.end(); + for( int i = 0 ; itr != end ; ++itr, ++i ) + { CV_Assert( itr->cols == 1 || itr->rows == 1 ); if( itr->cols == 1 ) @@ -38,7 +80,7 @@ void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainD { itr->copyTo( trainData.row( i ) ); } - } + } } void load_images( const string & prefix, const string & filename, vector< Mat > & img_lst ) @@ -52,7 +94,7 @@ void load_images( const string & prefix, const string & filename, vector< Mat > cerr << "Unable to open the list of images from " << filename << " filename." << endl; exit( -1 ); } - + while( 1 ) { getline( file, line ); @@ -102,12 +144,12 @@ Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, float zoomFac = 3; Mat visu; resize(color_origImg, visu, Size(color_origImg.cols*zoomFac, color_origImg.rows*zoomFac)); - + int blockSize = 16; int cellSize = 8; int gradientBinSize = 9; float radRangeForOneBin = CV_PI/(float)gradientBinSize; // dividing 180° into 9 bins, how large (in rad) is one bin? - + // prepare data structure: 9 orientation / gradient strenghts for each cell int cells_in_x_dir = DIMX / cellSize; int cells_in_y_dir = DIMY / cellSize; @@ -122,22 +164,22 @@ Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, { gradientStrengths[y][x] = new float[gradientBinSize]; cellUpdateCounter[y][x] = 0; - + for (int bin=0; bin& descriptorValues, cellx++; celly++; } - + for (int bin=0; bin& descriptorValues, } } } - + // draw cells for (int celly=0; celly& descriptorValues, { int drawX = cellx * cellSize; int drawY = celly * cellSize; - + int mx = drawX + cellSize/2; int my = drawY + cellSize/2; - + rectangle(visu, Point(drawX*zoomFac,drawY*zoomFac), Point((drawX+cellSize)*zoomFac,(drawY+cellSize)*zoomFac), CV_RGB(100,100,100), 1); - + // draw in each cell all 9 gradient strengths for (int bin=0; bin & img_lst, vector< Mat > & gradient_lst, const Size & size ) @@ -322,7 +364,7 @@ void test_it( const Size & size ) Scalar reference( 0, 255, 0 ); Scalar trained( 0, 0, 255 ); Mat img, draw; - SVM svm; + MySVM svm; HOGDescriptor hog; HOGDescriptor my_hog; my_hog.winSize = size; @@ -333,7 +375,7 @@ void test_it( const Size & size ) svm.load( "my_people_detector.yml" ); // Set the trained svm to my_hog vector< float > hog_detector; - svm.get_svm_detector( hog_detector ); + get_svm_detector( svm, hog_detector ); my_hog.setSVMDetector( hog_detector ); // Set the people detector. hog.setSVMDetector( hog.getDefaultPeopleDetector() ); @@ -344,7 +386,7 @@ void test_it( const Size & size ) cerr << "Unable to open the device 0" << endl; exit( -1 ); } - + while( true ) { video >> img; @@ -352,7 +394,7 @@ void test_it( const Size & size ) break; draw = img.clone(); - + locations.clear(); hog.detectMultiScale( img, locations ); draw_locations( draw, locations, reference ); @@ -373,8 +415,8 @@ int main( int argc, char** argv ) if( argc != 4 ) { cout << "Wrong number of parameters." << endl - << "Usage: " << argv[0] << " pos_dir pos.lst neg_dir neg.lst" << endl - << "example: " << argv[0] << " /INRIA_dataset/ Train/pos.lst /INRIA_dataset/ Train/neg.lst" << endl; + << "Usage: " << argv[0] << " pos_dir pos.lst neg_dir neg.lst" << endl + << "example: " << argv[0] << " /INRIA_dataset/ Train/pos.lst /INRIA_dataset/ Train/neg.lst" << endl; exit( -1 ); } vector< Mat > pos_lst; From a4ceb7b6ee9aa7c9507195de34ed3a67ff014ace Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Mon, 11 Nov 2013 19:29:39 +0100 Subject: [PATCH 5/8] Fix compilation issues. --- samples/cpp/train_HOG.cpp | 41 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/samples/cpp/train_HOG.cpp b/samples/cpp/train_HOG.cpp index ae2d1963bf..2cc15db115 100644 --- a/samples/cpp/train_HOG.cpp +++ b/samples/cpp/train_HOG.cpp @@ -10,6 +10,15 @@ using namespace cv; using namespace std; +void get_svm_detector(const SVM& svm, vector< float > & hog_detector ); +void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData ); +void load_images( const string & prefix, const string & filename, vector< Mat > & img_lst ); +void sample_neg( const vector< Mat > & full_neg_lst, vector< Mat > & neg_lst, const Size & size ); +Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, const Size & size ); +void compute_hog( const vector< Mat > & img_lst, vector< Mat > & gradient_lst, const Size & size ); +void train_svm( const vector< Mat > & gradient_lst, const vector< int > & labels ); +void draw_locations( Mat & img, const vector< Rect > & locations, const Scalar & color ); +void test_it( const Size & size ); void get_svm_detector(const SVM& svm, vector< float > & hog_detector ) { @@ -20,7 +29,9 @@ void get_svm_detector(const SVM& svm, vector< float > & hog_detector ) // get the decision function const CvSVMDecisionFunc* decision_func = svm.get_decision_function(); // get the support vectors - const float** sv = &(svm.get_support_vector(0)); + const float** sv = new const float*[ sv_total ]; + for( int i = 0 ; i < sv_total ; ++i ) + sv[ i ] = svm.get_support_vector(i); CV_Assert( var_all > 0 && sv_total > 0 && @@ -50,6 +61,8 @@ void get_svm_detector(const SVM& svm, vector< float > & hog_detector ) hog_detector.push_back( svi ); } hog_detector.push_back( (float)-decision_func->rho ); + + delete[] sv; } @@ -65,8 +78,8 @@ void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainD const int cols = (int)std::max( train_samples[0].cols, train_samples[0].rows ); cv::Mat tmp(1, cols, CV_32FC1); //< used for transposition if needed trainData = cv::Mat(rows, cols, CV_32FC1 ); - auto& itr = train_samples.begin(); - auto& end = train_samples.end(); + vector< Mat >::const_iterator itr = train_samples.begin(); + vector< Mat >::const_iterator end = train_samples.end(); for( int i = 0 ; itr != end ; ++itr, ++i ) { CV_Assert( itr->cols == 1 || @@ -122,8 +135,8 @@ void sample_neg( const vector< Mat > & full_neg_lst, vector< Mat > & neg_lst, co srand( time( NULL ) ); - auto& img = full_neg_lst.begin(); - auto& end = full_neg_lst.end(); + vector< Mat >::const_iterator img = full_neg_lst.begin(); + vector< Mat >::const_iterator end = full_neg_lst.end(); for( ; img != end ; ++img ) { box.x = rand() % (img->cols - size_x); @@ -221,9 +234,9 @@ Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, // compute average gradient strengths - for (int celly=0; celly& descriptorValues, } // draw cells - for (int celly=0; celly & img_lst, vector< Mat > & gradient_lst, c vector< Point > location; vector< float > descriptors; - auto& img = img_lst.begin(); - auto& end = img_lst.end(); + vector< Mat >::const_iterator img = img_lst.begin(); + vector< Mat >::const_iterator end = img_lst.end(); for( ; img != end ; ++img ) { cvtColor( *img, gray, COLOR_BGR2GRAY ); @@ -349,8 +362,8 @@ void draw_locations( Mat & img, const vector< Rect > & locations, const Scalar & { if( !locations.empty() ) { - auto& loc = locations.begin(); - auto& end = locations.end(); + vector< Rect >::const_iterator loc = locations.begin(); + vector< Rect >::const_iterator end = locations.end(); for( ; loc != end ; ++loc ) { rectangle( img, *loc, color, 2 ); @@ -364,7 +377,7 @@ void test_it( const Size & size ) Scalar reference( 0, 255, 0 ); Scalar trained( 0, 0, 255 ); Mat img, draw; - MySVM svm; + SVM svm; HOGDescriptor hog; HOGDescriptor my_hog; my_hog.winSize = size; From e1955759afaffb87bbb0d7a9abd03b7356be9414 Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Sun, 24 Nov 2013 13:22:10 +0100 Subject: [PATCH 6/8] Fix some warning. Adding missing credit for a function. --- samples/cpp/train_HOG.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/samples/cpp/train_HOG.cpp b/samples/cpp/train_HOG.cpp index 2cc15db115..5975bf88ce 100644 --- a/samples/cpp/train_HOG.cpp +++ b/samples/cpp/train_HOG.cpp @@ -150,6 +150,7 @@ void sample_neg( const vector< Mat > & full_neg_lst, vector< Mat > & neg_lst, co } } +// From http://www.juergenwiki.de/work/wiki/doku.php?id=public:hog_descriptor_computation_and_visualization Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, const Size & size ) { const int DIMX = size.width; @@ -158,7 +159,6 @@ Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, Mat visu; resize(color_origImg, visu, Size(color_origImg.cols*zoomFac, color_origImg.rows*zoomFac)); - int blockSize = 16; int cellSize = 8; int gradientBinSize = 9; float radRangeForOneBin = CV_PI/(float)gradientBinSize; // dividing 180° into 9 bins, how large (in rad) is one bin? @@ -166,7 +166,6 @@ Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, // prepare data structure: 9 orientation / gradient strenghts for each cell int cells_in_x_dir = DIMX / cellSize; int cells_in_y_dir = DIMY / cellSize; - int totalnrofcells = cells_in_x_dir * cells_in_y_dir; float*** gradientStrengths = new float**[cells_in_y_dir]; int** cellUpdateCounter = new int*[cells_in_y_dir]; for (int y=0; y& descriptorValues, for (int cellNr=0; cellNr<4; cellNr++) { // compute corresponding cell nr - int cellx = blockx; - int celly = blocky; + cellx = blockx; + celly = blocky; if (cellNr==1) celly++; if (cellNr==2) cellx++; if (cellNr==3) @@ -440,7 +439,7 @@ int main( int argc, char** argv ) load_images( argv[1], argv[2], pos_lst ); labels.assign( pos_lst.size(), +1 ); - const int old = labels.size(); + const unsigned int old = labels.size(); load_images( argv[3], argv[4], full_neg_lst ); sample_neg( full_neg_lst, neg_lst, Size( 96,160 ) ); labels.insert( labels.end(), neg_lst.size(), -1 ); From e015691b41984dfae796a3ff320825e847f73745 Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Sun, 24 Nov 2013 15:32:52 +0100 Subject: [PATCH 7/8] Fix warnings as errors. --- samples/cpp/train_HOG.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/samples/cpp/train_HOG.cpp b/samples/cpp/train_HOG.cpp index 5975bf88ce..32efa0ef20 100644 --- a/samples/cpp/train_HOG.cpp +++ b/samples/cpp/train_HOG.cpp @@ -38,7 +38,7 @@ void get_svm_detector(const SVM& svm, vector< float > & hog_detector ) decision_func != 0 && decision_func->alpha != 0 && decision_func->sv_count == sv_total ); - + float svi = 0.f; hog_detector.clear(); //clear stuff in vector. @@ -108,11 +108,15 @@ void load_images( const string & prefix, const string & filename, vector< Mat > exit( -1 ); } - while( 1 ) + bool end_of_parsing = false; + while( !end_of_parsing ) { getline( file, line ); if( line == "" ) // no more file to read + { + end_of_parsing = true; break; + } Mat img = imread( (prefix+line).c_str() ); // load the image if( !img.data ) // invalid image, just skip it. continue; @@ -133,7 +137,7 @@ void sample_neg( const vector< Mat > & full_neg_lst, vector< Mat > & neg_lst, co const int size_x = box.width; const int size_y = box.height; - srand( time( NULL ) ); + srand( (unsigned int)time( NULL ) ); vector< Mat >::const_iterator img = full_neg_lst.begin(); vector< Mat >::const_iterator end = full_neg_lst.end(); @@ -152,16 +156,16 @@ void sample_neg( const vector< Mat > & full_neg_lst, vector< Mat > & neg_lst, co // From http://www.juergenwiki.de/work/wiki/doku.php?id=public:hog_descriptor_computation_and_visualization Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, const Size & size ) -{ +{ const int DIMX = size.width; const int DIMY = size.height; float zoomFac = 3; Mat visu; - resize(color_origImg, visu, Size(color_origImg.cols*zoomFac, color_origImg.rows*zoomFac)); + resize(color_origImg, visu, Size( (int)(color_origImg.cols*zoomFac), (int)(color_origImg.rows*zoomFac) ) ); int cellSize = 8; int gradientBinSize = 9; - float radRangeForOneBin = CV_PI/(float)gradientBinSize; // dividing 180° into 9 bins, how large (in rad) is one bin? + float radRangeForOneBin = (float)(CV_PI/(float)gradientBinSize); // dividing 180° into 9 bins, how large (in rad) is one bin? // prepare data structure: 9 orientation / gradient strenghts for each cell int cells_in_x_dir = DIMX / cellSize; @@ -194,7 +198,7 @@ Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, for (int blockx=0; blockx& descriptorValues, int mx = drawX + cellSize/2; int my = drawY + cellSize/2; - rectangle(visu, Point(drawX*zoomFac,drawY*zoomFac), Point((drawX+cellSize)*zoomFac,(drawY+cellSize)*zoomFac), CV_RGB(100,100,100), 1); + rectangle(visu, Point((int)(drawX*zoomFac), (int)(drawY*zoomFac)), Point((int)((drawX+cellSize)*zoomFac), (int)((drawY+cellSize)*zoomFac)), CV_RGB(100,100,100), 1); // draw in each cell all 9 gradient strengths for (int bin=0; bin& descriptorValues, float dirVecX = cos( currRad ); float dirVecY = sin( currRad ); - float maxVecLen = cellSize/2; + float maxVecLen = (float)(cellSize/2.f); float scale = 2.5; // just a visualization scale, to see the lines better // compute line coordinates @@ -284,7 +288,7 @@ Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, float y2 = my + dirVecY * currentGradStrength * maxVecLen * scale; // draw gradient visualization - line(visu, Point(x1*zoomFac,y1*zoomFac), Point(x2*zoomFac,y2*zoomFac), CV_RGB(0,255,0), 1); + line(visu, Point((int)(x1*zoomFac),(int)(y1*zoomFac)), Point((int)(x2*zoomFac),(int)(y2*zoomFac)), CV_RGB(0,255,0), 1); } // for (all bins) @@ -297,7 +301,7 @@ Mat get_hogdescriptor_visu(Mat& color_origImg, vector& descriptorValues, { for (int x=0; x> img; if( !img.data ) @@ -416,9 +421,9 @@ void test_it( const Size & size ) draw_locations( draw, locations, trained ); imshow( "Video", draw ); - key = waitKey( 10 ); + key = (char)waitKey( 10 ); if( 27 == key ) - break; + end_of_process = true; } } @@ -439,7 +444,7 @@ int main( int argc, char** argv ) load_images( argv[1], argv[2], pos_lst ); labels.assign( pos_lst.size(), +1 ); - const unsigned int old = labels.size(); + const unsigned int old = (unsigned int)labels.size(); load_images( argv[3], argv[4], full_neg_lst ); sample_neg( full_neg_lst, neg_lst, Size( 96,160 ) ); labels.insert( labels.end(), neg_lst.size(), -1 ); From 089b8e2f4ad89934ed54636584f664a9737a6077 Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Sun, 24 Nov 2013 17:10:06 +0100 Subject: [PATCH 8/8] Suppress a space! --- samples/cpp/train_HOG.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/cpp/train_HOG.cpp b/samples/cpp/train_HOG.cpp index 32efa0ef20..3f21206a18 100644 --- a/samples/cpp/train_HOG.cpp +++ b/samples/cpp/train_HOG.cpp @@ -38,7 +38,7 @@ void get_svm_detector(const SVM& svm, vector< float > & hog_detector ) decision_func != 0 && decision_func->alpha != 0 && decision_func->sv_count == sv_total ); - + float svi = 0.f; hog_detector.clear(); //clear stuff in vector.