123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847 |
- /**
- * @file PHOGFeature.cpp
- * @brief Implementation of the PHOG-Features and corresponding Kernel as done by Anna Bosch et al.
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- #include "PHOGFeature.h"
- using namespace std;
- using namespace OBJREC;
- using namespace NICE;
- /** protected things*/
- /**
- * @brief Calculates the PHOG-Pyramide for given gradient images (idea of Anna Bosch and Andrew Zisserman)
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- void PHOGFeature::calculate_PHOG_Pyramide(const NICE::Image & gradient_orientations, const NICE::ImageT<float> & gradient_magnitudes, std::vector< std::vector<float> > & PHOG_descriptor)
- {
- float sum_of_all = 0.0;
- float sum_of_level = 0.0;
- std::vector<float> one_collecting_vector;
- one_collecting_vector.clear();
- std::vector<float> HoG (number_Of_Bins);
- for (int j = 0; j < gradient_orientations.height(); j++)
- for (int i = 0; i < gradient_orientations.width(); i++)
- {
- int orientation (gradient_orientations(i,j));
- if (( (orientation<0) || ((uint)orientation>=HoG.size())) && verbose)
- {
- cerr << "orientation is " << orientation << " and does not fit to HoG.size(): " << HoG.size() << endl;
- cerr << "i: " << i << " j: " << j << " gradient_orientations.width() : " << gradient_orientations.width() << " gradient_orientations.height() : " << gradient_orientations.height() << endl;
- }
- HoG[gradient_orientations(i,j)] += gradient_magnitudes(i,j);
- sum_of_all += gradient_magnitudes(i,j);
- sum_of_level += gradient_magnitudes(i,j);
- }
-
- switch(histrogram_concatenation)
- {
- case 0:
- {
- if (sum_of_level != 0.0) //normalize the descriptor-entries
- {
- for (std::vector<float>::iterator HoG_it = HoG.begin(); HoG_it != HoG.end(); HoG_it++)
- {
- *HoG_it /= sum_of_level;
- }
- }
- PHOG_descriptor.push_back(HoG);
- break;
- }
- case 1:
- {
- if (sum_of_level != 0.0) //normalize the descriptor-entries
- {
- for (std::vector<float>::iterator HoG_it = HoG.begin(); HoG_it != HoG.end(); HoG_it++)
- {
- *HoG_it /= sum_of_level;
- }
- }
- PHOG_descriptor.push_back(HoG);
- break;
- }
- case 2:
- {
- one_collecting_vector.insert(one_collecting_vector.begin()+one_collecting_vector.size(), HoG.begin(), HoG.end());
- break;
- }
- }
- sum_of_all += sum_of_level;
- if (verbose) cerr << "PHOGFeature::calculate_PHOG_Pyramide -- Level 0 calculated, working on finer levels" << endl;
- if (verbose) cerr << "gradient_orientations.width(): " << gradient_orientations.width() << " gradient_orientations.height(): " << gradient_orientations.height() << endl;
- //further levels
- for (int level = 1; level < number_of_Levels; level++)
- {
- if (verbose) cerr << "PHOGFeature::calculate_PHOG_Pyramide -- working on level "<< level << endl;
- if (like_AnnaBosch)
- {
- int step_x = (int) floor(gradient_orientations.width() / pow(2.0f,level) );
- int step_y = (int) floor(gradient_orientations.height() / pow(2.0f,level) );
- vector<float> PHoG_level;
- int run_y = 0;
- for (int y_counter = 0; y_counter < pow(2.0,level) ; y_counter++)
- {
- int run_x = 0;
- for (int x_counter = 0; x_counter < pow(2.0,level) ; x_counter++)
- {
- vector<float> HoG_local (number_Of_Bins);
- float sum_of_hog(0.0);
-
- //check, wether rectangle lies on the boundary, if so, then correct the max-step
- int y_max = (run_y + step_y);
- if (gradient_orientations.height() < y_max)
- y_max = gradient_orientations.height();
-
- int x_max = (run_x + step_x);
- if (gradient_orientations.width() < x_max)
- x_max = gradient_orientations.width();
-
- for (int j = run_y; j < y_max; j++)
- for (int i = run_x; i < x_max; i++)
- {
- int orientation = 0;
- try{ orientation = gradient_orientations(i,j);
- }
- catch( ... )
- {
- cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide gradient_orientations(i,j) not possible. (i,j): " << i << " " << j << endl;
- }
- float magnitude = 0.0;
- try{ magnitude = gradient_magnitudes(i,j);
- }
- catch( ... )
- {
- cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide radient_magnitudes(i,j) not possible. (i,j): " << i << " " << j << endl;
- }
- HoG_local[orientation] += magnitude;
- sum_of_hog += magnitude;
- }
- sum_of_level += sum_of_hog;
-
- switch(histrogram_concatenation)
- {
- case 0:
- {
- if (sum_of_hog != 0.0) //normalize the descriptor-entries
- {
- for (std::vector<float>::iterator hog_it = HoG_local.begin(); hog_it != HoG_local.end(); hog_it++)
- {
- *hog_it /= sum_of_hog;
- }
- }
- PHOG_descriptor.push_back(HoG_local);
- break;
- }
- case 1:
- {
- for (std::vector<float>::const_iterator foo = HoG_local.begin(); foo != HoG_local.end(); foo++)
- {
- PHoG_level.insert(PHoG_level.begin()+PHoG_level.size(), HoG_local.begin(), HoG_local.end());
- }
- break;
- }
- case 2:
- {
- PHoG_level.insert(PHoG_level.begin()+PHoG_level.size(), HoG_local.begin(), HoG_local.end());
- break;
- }
- }
-
- run_x = run_x + step_x;
- }
- run_y = run_y + step_y;
- }
-
- sum_of_all += sum_of_level;
-
- switch(histrogram_concatenation)
- {
- case 0:
- {
- break;
- }
- case 1:
- {
- if (sum_of_level != 0.0) //normalize the descriptor-entries
- {
- for (std::vector<float>::iterator Level_it = PHoG_level.begin(); Level_it != PHoG_level.end(); Level_it++)
- {
- *Level_it /= sum_of_level;
- }
- }
- PHOG_descriptor.push_back(PHoG_level);
- break;
- }
- case 2:
- {
- one_collecting_vector.insert(one_collecting_vector.begin()+one_collecting_vector.size(), PHoG_level.begin(), PHoG_level.end());
- break;
- }
- }
- }
- else //better than anna bosch
- {
- int step_x = (int) ceil(gradient_orientations.width() / pow(2.0f,level) );
- int step_y = (int) ceil(gradient_orientations.height() / pow(2.0f,level) );
- if (verbose) cerr << "step_x: " << step_x << " step_y: " << step_y << endl;
- std::vector<float> PHoG_level;
-
- int run_y = 0;
- for (int y_counter = 0; y_counter < pow(2.0f,level) ; y_counter++)
- {
- int run_x = 0;
- if (verbose) cerr << "run_y: " << run_y << endl;
- for (int x_counter = 0; x_counter < pow(2.0f,level) ; x_counter++)
- {
- if (verbose) cerr << "run_x: " << run_x << endl;
- float sum_of_hog(0.0);
- vector<float> HoG_local (number_Of_Bins);
-
- //check, wether rectangle lies on the boundary, if so, then correct the max-step
- int y_max = (run_y + step_y);
- if (gradient_orientations.height() < y_max)
- {
- if (verbose) cerr << "y_max old:: " << y_max << " y_max_new: "<<gradient_orientations.height() << endl;
- y_max = gradient_orientations.height();
-
- }
-
- int x_max = (run_x + step_x);
- if (gradient_orientations.width() < x_max)
- {
- if (verbose) cerr << "x_max old:: " << x_max << " x_max_new: "<<gradient_orientations.width() << endl;
- x_max = gradient_orientations.width();
-
- }
-
- for (int j = run_y; j < y_max; j++)
- for (int i = run_x; i < x_max; i++)
- {
- int orientation = 0;
- try{ orientation = gradient_orientations.getPixel(i,j);
- }
- catch( ... )
- {
- cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide gradient_orientations(i,j) not possible. (i,j): " << i << " " << j << endl;
- }
- float magnitude = 0.0;
- try{ magnitude = gradient_magnitudes.getPixel(i,j);
- }
- catch( ... )
- {
- cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide gradient_magnitudes(i,j) not possible. (i,j): " << i << " " << j << endl;
- }
- if ( (orientation<0) || ((uint)orientation>=HoG_local.size()))
- {
- cerr << "orientation is " << orientation << " and does not fit to HoG_local.size(): " << HoG_local.size() << endl;
- cerr << "i: " << i << " j: " << j << " gradient_orientations.width() : " << gradient_orientations.width() << " gradient_orientations.height() : " << gradient_orientations.height() << endl;
- }
- try{
- HoG_local[orientation] += magnitude;
- }
- catch( ... )
- {
- cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide HoG_local[orientation] += magnitude not possible. orientation: " << orientation << " magnitude " << magnitude << endl;
- }
- sum_of_hog += magnitude;
- }
- sum_of_level += sum_of_hog;
-
- switch(histrogram_concatenation)
- {
- case 0:
- {
- if (sum_of_hog != 0.0) //normalize the descriptor-entries
- {
- for (std::vector<float>::iterator hog_it = HoG_local.begin(); hog_it != HoG_local.end(); hog_it++)
- {
- *hog_it /= sum_of_hog;
- }
- }
- PHOG_descriptor.push_back(HoG_local);
- break;
- }
- case 1:
- {
- for (std::vector<float>::const_iterator foo = HoG_local.begin(); foo != HoG_local.end(); foo++)
- {
- PHoG_level.insert(PHoG_level.begin()+PHoG_level.size(), HoG_local.begin(), HoG_local.end());
- }
- break;
- }
- case 2:
- {
- PHoG_level.insert(PHoG_level.begin()+PHoG_level.size(), HoG_local.begin(), HoG_local.end());
- break;
- }
- }
-
- run_x = run_x + step_x;
- }
- run_y = run_y + step_y;
- }
- sum_of_all += sum_of_level;
-
- switch(histrogram_concatenation)
- {
- case 0:
- {
- break;
- }
- case 1:
- {
- if (sum_of_level != 0.0) //normalize the descriptor-entries
- {
- for (std::vector<float>::iterator Level_it = PHoG_level.begin(); Level_it != PHoG_level.end(); Level_it++)
- {
- *Level_it /= sum_of_level;
- }
- }
- PHOG_descriptor.push_back(PHoG_level);
- break;
- }
- case 2:
- {
- one_collecting_vector.insert(one_collecting_vector.begin()+one_collecting_vector.size(), PHoG_level.begin(), PHoG_level.end());
- break;
- }
- }
- }
- }
-
- if (histrogram_concatenation == ALL)
- {
- if (sum_of_all != 0.0) //normalize the descriptor-entries
- {
- for (std::vector<float>::iterator it = one_collecting_vector.begin(); it != one_collecting_vector.end(); it++)
- {
- *it /= sum_of_all;
- }
- }
- PHOG_descriptor.push_back(one_collecting_vector);
- }
- }
- /**
- * @brief Calculates resulting PHOG-Features for the specified ROI in the given image
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- std::vector< std::vector<float> > PHOGFeature::calculate_PHOG_Features(const NICE::Image & orig_Image, const NICE::Rect & roi)
- {
- if (like_AnnaBosch) //not supported in that version, 'cause !
- {
- std::cerr << "PHOGFeature::calculate_PHOG_Features is not supported right now" << std::endl;
- // //Canny Edge Detector
- // Image canny_Image;
- // canny_Image = (*canny(orig_Image, 10,30) );
- //
- // //gradient-calculation
- // GrayImage16s* grad_x_Image= sobelX(canny_Image);
- // GrayImage16s* grad_y_Image = sobelY(canny_Image);
- // if (verbose) cerr << "gradient-calculation done" << endl;
- //
- // //gradient-orientation-calculation
- // NICE::Image gradient_orientations;
- // image_tool.calculateGradientOrientations( *grad_x_Image, *grad_y_Image, number_Of_Bins, gradient_orientations, unsignedBins);
- // if (verbose) cerr << "gradient-orientation-calculation done" << endl;
- //
- // //gradient-magnitude-calculation
- // //maybe this is not good - implicit cast from int to float in image structure
- // NICE::Image* gradient_magnitudes_int = gradientStrength( (*grad_x_Image), (*grad_y_Image) );
- // NICE::ImageT<float> gradient_magnitudes(orig_Image.width(), orig_Image.height());
- // for (int y = 0; y < orig_Image.height(); y++)
- // for (int x = 0; x < orig_Image.width(); x++)
- // {
- // gradient_magnitudes.setPixel(x,y,(*gradient_magnitudes_int).getPixel(x,y));
- // }
- // if (verbose) cerr << "gradient-magnitude-calculation done" << endl;
- //
- // NICE::Image go_roi (gradient_orientations.subImage(roi));
- // NICE::ImageT<float> gm_roi (gradient_magnitudes.subImage(roi));
- //
- // //pyramide-calculation
- //
- std::vector< std::vector<float> > PHOG_pyramide;
- // calculate_PHOG_Pyramide(go_roi, gm_roi, PHOG_pyramide);
- //
- // if (verbose) cerr << "Pyramide-calculation done" << endl;
- return PHOG_pyramide;
- }
- else
- {
- //gradient-calculation
- NICE::ImageT<float> grad_x_Image;
- NICE::ImageT<float> grad_y_Image;
- image_tool.calculateGradients(orig_Image, grad_x_Image, grad_y_Image );
- if (verbose) cerr << "gradient-calculation done" << endl;
- //gradient-orientation-calculation
- NICE::Image gradient_orientations;
- image_tool.calculateGradientOrientations(grad_x_Image, grad_y_Image, number_Of_Bins, gradient_orientations, unsignedBins);
- if (verbose) cerr << "gradient-orientation-calculation done" << endl;
- //gradient-magnitude-calculation
- NICE::ImageT<float> gradient_magnitudes;
- image_tool.calculateGradientMagnitudes(grad_x_Image, grad_y_Image, gradient_magnitudes);
- if (verbose) cerr << "gradient-magnitude-calculation done" << endl;
- NICE::Image go_roi (gradient_orientations.subImage(roi));
- NICE::ImageT<float> gm_roi (gradient_magnitudes.subImage(roi));
- //pyramide-calculation
- std::vector< std::vector<float> > PHOG_pyramide;
- calculate_PHOG_Pyramide(go_roi, gm_roi, PHOG_pyramide);
- if (verbose) cerr << "Pyramide-calculation done" << endl;
- return PHOG_pyramide;
- }
- }
- //TODO ebenso für Farbbilder, falls das erwünscht ist. Frau Bosch macht einfach nur eine Grauwertkonvertierunt, also auch nicht so schön, wie Dalal und Triggs.!
- /** public things*/
- /**
- * @brief Simple Default Constructor
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- PHOGFeature::PHOGFeature()
- {
- image_tool = OBJREC::Image_tools();
- number_Of_Bins = 9;
- unsignedBins = true;
- number_of_Levels = 3;
- like_AnnaBosch = false;
- distances_only_between_levels = true;
- histrogram_concatenation = NONE;
- verbose = false;
- }
- /**
- * @brief Recommended Constructor
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- PHOGFeature::PHOGFeature( const Config *conf, std::string section)
- {
- image_tool = OBJREC::Image_tools();
- number_Of_Bins = conf->gI("PHOGFeature", "number_Of_Bins", 9);
- unsignedBins = conf->gB("PHOGFeature", "unsignedBins", true);
- number_of_Levels = conf->gI("PHOGFeature", "number_of_Levels", 3);
- like_AnnaBosch = conf->gB("PHOGFeature", "like_AnnaBosch", false);
- distances_only_between_levels = conf->gB("PHOGFeature", "distances_only_between_levels", true);
- switch(conf->gI("PHOGFeature", "histrogram_concatenation", 0) )
- {
- case 0:
- {histrogram_concatenation = NONE; break;}
- case 1:
- {histrogram_concatenation = LEVELWISE; break;}
- case 2:
- {histrogram_concatenation = ALL; break;}
- }
- verbose = conf->gB("PHOGFeature", "verbose", false);
- }
- /**
- * @brief Simple Destructor
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- PHOGFeature::~PHOGFeature()
- {
- }
- /**
- * @brief Creating the PHOG-featureVectores of all trainingexamples
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- // std::vector<std::vector< std::vector<float> > > PHOGFeature::createAllFeatureVectors(const OBJREC::LabeledSet::Permutation & order)
- // {
- //
- // std::vector<std::vector< std::vector<float> > > PHOG_Features_all_images;
- //
- // ProgressBar pb_createFV ("PHOGFeature::createAllFeatureVectors");
- // pb_createFV.show();
- //
- // uint k = 0;
- // for ( LabeledSet::Permutation::const_iterator i = order.begin(); i != order.end(); i++,k++ )
- // {
- // string imgfilename = (*i).second->img();
- //
- // NICE::Image img(imgfilename);
- //
- // NICE::Rect roi = NICE::Rect(0,0,img.width(),img.height());
- // vector<vector<float> > PHOG = calculate_PHOG_Features(img, roi);
- //
- // PHOG_Features_all_images.push_back ( PHOG );
- //
- // pb_createFV.update ( order.size() );
- // }
- //
- // // hide the last progress bar
- // pb_createFV.hide();
- //
- // return PHOG_Features_all_images;
- // }
- /**
- * @brief Creating the PHOG-featureVectores of all trainingexamples
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- // std::vector<std::vector< std::vector<float> > > PHOGFeature::createAllFeatureVectors(const OBJREC::LabeledSet::Permutation & order, std::vector< NICE::Image > & gradient_orientations, std::vector<NICE::ImageT<float> > & gradient_magnitudes)
- // {
- //
- // std::vector<std::vector< std::vector<float> > > PHOG_Features_all_images;
- //
- // cerr << "Calculating gradient orientations." << endl;
- // gradient_orientations = calculate_gradient_orientations(order);
- // cerr << "Calculating gradient magnitudes." << endl;
- // gradient_magnitudes = calculate_gradient_magnitudes(order);
- //
- // ProgressBar pb_quantization ("PHOGFeature::createAllFeatureVectors");
- // pb_quantization.show();
- //
- // uint k = 0;
- // for ( LabeledSet::Permutation::const_iterator i = order.begin(); i != order.end(); i++,k++ )
- // {
- // std::string imgfilename = (*i).second->img();
- //
- // NICE::Image img(imgfilename);
- //
- // std::vector<std::vector<float> > PHOG;
- // calculate_PHOG_Pyramide(gradient_orientations[k], gradient_magnitudes[k], PHOG);
- //
- // PHOG_Features_all_images.push_back ( PHOG );
- //
- // pb_quantization.update ( order.size() );
- // }
- //
- // // hide the last progress bar
- // pb_quantization.hide();
- //
- // return PHOG_Features_all_images;
- // }
- /**
- * @brief Creating the PHOG-featureVectores of all trainingexamples
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- // // // std::vector<std::vector< std::vector<float> > > PHOGFeature::createAllFeatureVectors(const OBJREC::LabeledSet::Permutation & order, const std::vector<int> & trainSelection)
- // // // {
- // // //
- // // // std::vector<std::vector< std::vector<float> > > PHOG_Features_all_images;
- // // //
- // // // ProgressBar pb_quantization ("PHOGFeature::createAllFeatureVectors");
- // // // pb_quantization.show();
- // // //
- // // // uint k = 0;
- // // // for ( std::vector<int>::const_iterator i = trainSelection.begin(); i != trainSelection.end(); i++,k++ )
- // // // {
- // // // if (verbose) cerr << " Image number " << *i << " will be treated now" << endl;
- // // // string imgfilename = order[*i].second->img();
- // // //
- // // // NICE::Image img(imgfilename);
- // // //
- // // // NICE::Rect roi = NICE::Rect(0,0,img.width(),img.height());
- // // // vector<vector<float> > PHOG = calculate_PHOG_Features(img, roi);
- // // //
- // // // PHOG_Features_all_images.push_back ( PHOG );
- // // //
- // // // pb_quantization.update ( trainSelection.size() );
- // // // }
- // // //
- // // // // hide the last progress bar
- // // // pb_quantization.hide();
- // // //
- // // // return PHOG_Features_all_images;
- // // // }
- // //
- // //
- // //
- /**
- * @brief Creating the PHOG-featureVectore for one trainingexample
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- std::vector<std::vector<float> > PHOGFeature::createOneFeatureVector(const std::string & imgfilename, const NICE::Rect & ROI)
- {
- NICE::Image img(imgfilename);
- std::vector<std::vector<float> > PHOG (calculate_PHOG_Features(img, ROI));
- return PHOG;
- }
- /**
- * @brief Creating the PHOG-featureVectore for one trainingexample
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- std::vector<std::vector<float> > PHOGFeature::createOneFeatureVector(const std::string & imgfilename)
- {
- NICE::Image img(imgfilename);
- NICE::Rect ROI(0,0,img.width(), img.height());
- std::vector<std::vector<float> > PHOG (calculate_PHOG_Features(img, ROI));
- return PHOG;
- }
- /**
- * @brief Creating the PHOG-featureVectore for one trainingexample
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- std::vector<std::vector<float> > PHOGFeature::createOneFeatureVector(const NICE::Image img, const NICE::Rect & ROI)
- {
- std::vector<std::vector<float> > PHOG (calculate_PHOG_Features(img, ROI));
- return PHOG;
- }
- /**
- * @brief Creating the PHOG-featureVectore for one trainingexample
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- std::vector<std::vector<float> > PHOGFeature::createOneFeatureVector(const NICE::Image img)
- {
- NICE::Rect ROI(0,0,img.width(), img.height());
- std::vector<std::vector<float> > PHOG (calculate_PHOG_Features(img, ROI));
- return PHOG;
- }
- /**
- * @brief computes the featurevector for the given image, which is already converted to a gradient-image, just considering features in the specified ROI
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- std::vector< std::vector<float> > PHOGFeature::createOneFeatureVectorDirect(NICE::Image & gradient_orientations, NICE::ImageT<float> & gradient_magnitudes, const NICE::Rect & ROI)
- {
- NICE::Image ROI_go (gradient_orientations.subImage(ROI));
- NICE::ImageT<float> ROI_gm (gradient_magnitudes.subImage(ROI));
- std::vector< std::vector<float> > PHOG_pyramide;
- calculate_PHOG_Pyramide(ROI_go, ROI_gm, PHOG_pyramide);
- return PHOG_pyramide;
- }
- /**
- * @brief computes the featurevector for the given image, which is already converted to a gradient-image, considering the whole image
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- std::vector< std::vector<float> > PHOGFeature::createOneFeatureVectorDirect(NICE::Image & gradient_orientations, NICE::ImageT<float> & gradient_magnitudes)
- {
- NICE::Image ROI_go (gradient_orientations);
- NICE::ImageT<float> ROI_gm (gradient_magnitudes);
- std::vector< std::vector<float> > PHOG_pyramide;
- calculate_PHOG_Pyramide(ROI_go, ROI_gm, PHOG_pyramide );
- return PHOG_pyramide;
- }
- // //
- // // /**
- // // * @brief computes the gradient-orientation-images of all training-images
- // // * @author Alexander Lütz
- // // * @date 15/11/2011
- // // */
- // // std::vector< NICE::Image> PHOGFeature::calculate_gradient_orientations(const OBJREC::LabeledSet::Permutation & order)
- // // {
- // // std::vector< NICE::Image> gradient_orientations;
- // //
- // // for(OBJREC::LabeledSet::Permutation::const_iterator i = order.begin(); i != order.end(); i++)
- // // {
- // // string filename = (*i).second->img();
- // // NICE::Image img (filename);
- // // //gradient-calculation
- // // NICE::ImageT<float> grad_x_Image;
- // // NICE::ImageT<float> grad_y_Image;
- // // image_tool.calculateGradients(img, grad_x_Image, grad_y_Image );
- // //
- // // //gradient-orientation-calculation
- // // NICE::Image gradient_orientation_image;
- // // image_tool.calculateGradientOrientations(grad_x_Image, grad_y_Image, number_Of_Bins, gradient_orientation_image, unsignedBins);
- // //
- // // gradient_orientations.push_back(gradient_orientation_image);
- // // }
- // //
- // // return gradient_orientations;
- // // }
- //
- // /**
- // * @brief computes the gradient-magnitudes-images of all training-images
- // * @author Alexander Lütz
- // * @date 15/11/2011
- // */
- // std::vector< NICE::ImageT<float> > PHOGFeature::calculate_gradient_magnitudes(const OBJREC::LabeledSet::Permutation & order)
- // {
- // std::vector< NICE::ImageT<float> > gradient_magnitudes;
- //
- // for(OBJREC::LabeledSet::Permutation::const_iterator i = order.begin(); i != order.end(); i++)
- // {
- // string filename = (*i).second->img();
- // Image img (filename);
- // //gradient-calculation
- // NICE::ImageT<float> grad_x_Image;
- // NICE::ImageT<float> grad_y_Image;
- // image_tool.calculateGradients(img, grad_x_Image, grad_y_Image );
- //
- // //gradient-orientation-calculation
- // NICE::ImageT<float> gradient_magnitude_image;
- // image_tool.calculateGradientMagnitudes(grad_x_Image, grad_y_Image, gradient_magnitude_image);
- //
- // gradient_magnitudes.push_back(gradient_magnitude_image);
- // }
- //
- // return gradient_magnitudes;
- // }
- /**
- * @brief computes the gradient-orientation-images of all training-images
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- NICE::Image PHOGFeature::calculate_gradient_orientations(const std::string & imgfilenam)
- {
- NICE::Image gradient_orientations;
- NICE::Image img (imgfilenam);
- //gradient-calculation
- NICE::ImageT<float> grad_x_Image;
- NICE::ImageT<float> grad_y_Image;
- image_tool.calculateGradients(img, grad_x_Image, grad_y_Image );
- //gradient-orientation-calculation
- NICE::Image gradient_orientation_image;
- image_tool.calculateGradientOrientations(grad_x_Image, grad_y_Image, number_Of_Bins, gradient_orientation_image, unsignedBins);
- return gradient_orientations;
- }
- /**
- * @brief computes the gradient-magnitudes-images of all training-images
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- NICE::ImageT<float> PHOGFeature::calculate_gradient_magnitudes(const std::string & imgfilename)
- {
- NICE::ImageT<float> gradient_magnitudes;
- Image img (imgfilename);
- //gradient-calculation
- NICE::ImageT<float> grad_x_Image;
- NICE::ImageT<float> grad_y_Image;
- image_tool.calculateGradients(img, grad_x_Image, grad_y_Image );
- //gradient-orientation-calculation
- NICE::ImageT<float> gradient_magnitude_image;
- image_tool.calculateGradientMagnitudes(grad_x_Image, grad_y_Image, gradient_magnitude_image);
- return gradient_magnitudes;
- }
- /**
- * @brief Compute the resulting kernel measuring the distance between the given feature-vectores
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- void PHOGFeature::calculate_Kernel_from_Features(const std::vector<std::vector< std::vector<float> > > & PHoG_Features, NICE::Matrix & PHoG_Kernel){
- // ProgressBar pb_kernelcalc ("PHOGFeature::Kernel Calculation");
- // pb_kernelcalc.show();
- PHoG_Kernel = NICE::Matrix(PHoG_Features.size(), PHoG_Features.size(), 0.0);
- uint ik = 0;
- for ( std::vector< std::vector < std::vector<float> > >::const_iterator i = PHoG_Features.begin();i != PHoG_Features.end(); i++,ik++ )
- {
- uint jk = ik;
- for ( std::vector< std::vector< std::vector<float> > >::const_iterator j = i;
- j != PHoG_Features.end(); j++,jk++ )
- {
- double kernelValue (measureDistance(*i,*j));
-
- PHoG_Kernel(ik,jk) = kernelValue;
- PHoG_Kernel(jk,ik) = kernelValue;
- }
- // pb_kernelcalc.update ( PHoG_Features.size() );
- }
- }
- /**
- * @brief function which computes the exp^(-Chi^2)-kernel of two PHOG-feature-vectores a and b
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- double PHOGFeature::measureDistance ( const std::vector<std::vector<float> > & a, const std::vector<std::vector<float> > & b )
- {
- double expChi_value = 0.0;
- if (a.size() != b.size() ) {
- cerr << "a.size(): " << a.size() << " and b.size(): " << b.size() << endl;
- fthrow(IOException, "Sizes of PHOG-Feature-vectores do not fit!");
- }
- for (uint i = 0; i < a.size(); i++) //run over every HoG-Level
- {
- std::vector<float> a_intern = a[i];
- std::vector<float> b_intern = b[i];
- double chi_value = 0.0;
- if (a_intern.size() != b_intern.size() ) {
- cerr << "a_intern.size(): " << a_intern.size() << " and b_intern.size(): " << b_intern.size() << endl;
- fthrow(IOException, "Sizes of PHOG-Feature-vectores do not fit!");
- }
- for (uint j = 0; j < a_intern.size(); j++) //run over every HoG-entry of this level
- {
- float u = a_intern[j];
- float v = b_intern[j];
- float s = ( u + v );
- if ( fabs(s) < 10e-6 ) continue;
- float d = u-v;
- chi_value += d*d / s;
- }
- chi_value *= 0.5;
- expChi_value += exp(-chi_value);
- }
- expChi_value /= a.size(); //normalize to be in between zero and one
- return expChi_value;
- }
- /**
- * @brief Simply set the verbose-flag
- * @author Alexander Lütz
- * @date 15/11/2011
- */
- void PHOGFeature::set_verbose(const bool & new_verbose){
- verbose = new_verbose;
- }
|