PHOGFeature.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /**
  2. * @file PHOGFeature.cpp
  3. * @brief Implementation of the PHOG-Features and corresponding Kernel as done by Anna Bosch et al.
  4. * @author Alexander Lütz
  5. * @date 15/11/2011
  6. */
  7. #include "PHOGFeature.h"
  8. using namespace std;
  9. using namespace OBJREC;
  10. using namespace NICE;
  11. /** protected things*/
  12. /**
  13. * @brief Calculates the PHOG-Pyramide for given gradient images (idea of Anna Bosch and Andrew Zisserman)
  14. * @author Alexander Lütz
  15. * @date 15/11/2011
  16. */
  17. void PHOGFeature::calculate_PHOG_Pyramide(const NICE::Image & gradient_orientations, const NICE::ImageT<float> & gradient_magnitudes, std::vector< std::vector<float> > & PHOG_descriptor)
  18. {
  19. float sum_of_all = 0.0;
  20. float sum_of_level = 0.0;
  21. std::vector<float> one_collecting_vector;
  22. one_collecting_vector.clear();
  23. std::vector<float> HoG (number_Of_Bins);
  24. for (int j = 0; j < gradient_orientations.height(); j++)
  25. for (int i = 0; i < gradient_orientations.width(); i++)
  26. {
  27. int orientation (gradient_orientations(i,j));
  28. if (( (orientation<0) || ((uint)orientation>=HoG.size())) && verbose)
  29. {
  30. cerr << "orientation is " << orientation << " and does not fit to HoG.size(): " << HoG.size() << endl;
  31. cerr << "i: " << i << " j: " << j << " gradient_orientations.width() : " << gradient_orientations.width() << " gradient_orientations.height() : " << gradient_orientations.height() << endl;
  32. }
  33. HoG[gradient_orientations(i,j)] += gradient_magnitudes(i,j);
  34. sum_of_all += gradient_magnitudes(i,j);
  35. sum_of_level += gradient_magnitudes(i,j);
  36. }
  37. switch(histrogram_concatenation)
  38. {
  39. case 0:
  40. {
  41. if (sum_of_level != 0.0) //normalize the descriptor-entries
  42. {
  43. for (std::vector<float>::iterator HoG_it = HoG.begin(); HoG_it != HoG.end(); HoG_it++)
  44. {
  45. *HoG_it /= sum_of_level;
  46. }
  47. }
  48. PHOG_descriptor.push_back(HoG);
  49. break;
  50. }
  51. case 1:
  52. {
  53. if (sum_of_level != 0.0) //normalize the descriptor-entries
  54. {
  55. for (std::vector<float>::iterator HoG_it = HoG.begin(); HoG_it != HoG.end(); HoG_it++)
  56. {
  57. *HoG_it /= sum_of_level;
  58. }
  59. }
  60. PHOG_descriptor.push_back(HoG);
  61. break;
  62. }
  63. case 2:
  64. {
  65. one_collecting_vector.insert(one_collecting_vector.begin()+one_collecting_vector.size(), HoG.begin(), HoG.end());
  66. break;
  67. }
  68. }
  69. sum_of_all += sum_of_level;
  70. if (verbose) cerr << "PHOGFeature::calculate_PHOG_Pyramide -- Level 0 calculated, working on finer levels" << endl;
  71. if (verbose) cerr << "gradient_orientations.width(): " << gradient_orientations.width() << " gradient_orientations.height(): " << gradient_orientations.height() << endl;
  72. //further levels
  73. for (int level = 1; level < number_of_Levels; level++)
  74. {
  75. if (verbose) cerr << "PHOGFeature::calculate_PHOG_Pyramide -- working on level "<< level << endl;
  76. if (like_AnnaBosch)
  77. {
  78. int step_x = (int) floor(gradient_orientations.width() / pow(2.0f,level) );
  79. int step_y = (int) floor(gradient_orientations.height() / pow(2.0f,level) );
  80. vector<float> PHoG_level;
  81. int run_y = 0;
  82. for (int y_counter = 0; y_counter < pow(2.0,level) ; y_counter++)
  83. {
  84. int run_x = 0;
  85. for (int x_counter = 0; x_counter < pow(2.0,level) ; x_counter++)
  86. {
  87. vector<float> HoG_local (number_Of_Bins);
  88. float sum_of_hog(0.0);
  89. //check, wether rectangle lies on the boundary, if so, then correct the max-step
  90. int y_max = (run_y + step_y);
  91. if (gradient_orientations.height() < y_max)
  92. y_max = gradient_orientations.height();
  93. int x_max = (run_x + step_x);
  94. if (gradient_orientations.width() < x_max)
  95. x_max = gradient_orientations.width();
  96. for (int j = run_y; j < y_max; j++)
  97. for (int i = run_x; i < x_max; i++)
  98. {
  99. int orientation = 0;
  100. try{ orientation = gradient_orientations(i,j);
  101. }
  102. catch( ... )
  103. {
  104. cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide gradient_orientations(i,j) not possible. (i,j): " << i << " " << j << endl;
  105. }
  106. float magnitude = 0.0;
  107. try{ magnitude = gradient_magnitudes(i,j);
  108. }
  109. catch( ... )
  110. {
  111. cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide radient_magnitudes(i,j) not possible. (i,j): " << i << " " << j << endl;
  112. }
  113. HoG_local[orientation] += magnitude;
  114. sum_of_hog += magnitude;
  115. }
  116. sum_of_level += sum_of_hog;
  117. switch(histrogram_concatenation)
  118. {
  119. case 0:
  120. {
  121. if (sum_of_hog != 0.0) //normalize the descriptor-entries
  122. {
  123. for (std::vector<float>::iterator hog_it = HoG_local.begin(); hog_it != HoG_local.end(); hog_it++)
  124. {
  125. *hog_it /= sum_of_hog;
  126. }
  127. }
  128. PHOG_descriptor.push_back(HoG_local);
  129. break;
  130. }
  131. case 1:
  132. {
  133. for (std::vector<float>::const_iterator foo = HoG_local.begin(); foo != HoG_local.end(); foo++)
  134. {
  135. PHoG_level.insert(PHoG_level.begin()+PHoG_level.size(), HoG_local.begin(), HoG_local.end());
  136. }
  137. break;
  138. }
  139. case 2:
  140. {
  141. PHoG_level.insert(PHoG_level.begin()+PHoG_level.size(), HoG_local.begin(), HoG_local.end());
  142. break;
  143. }
  144. }
  145. run_x = run_x + step_x;
  146. }
  147. run_y = run_y + step_y;
  148. }
  149. sum_of_all += sum_of_level;
  150. switch(histrogram_concatenation)
  151. {
  152. case 0:
  153. {
  154. break;
  155. }
  156. case 1:
  157. {
  158. if (sum_of_level != 0.0) //normalize the descriptor-entries
  159. {
  160. for (std::vector<float>::iterator Level_it = PHoG_level.begin(); Level_it != PHoG_level.end(); Level_it++)
  161. {
  162. *Level_it /= sum_of_level;
  163. }
  164. }
  165. PHOG_descriptor.push_back(PHoG_level);
  166. break;
  167. }
  168. case 2:
  169. {
  170. one_collecting_vector.insert(one_collecting_vector.begin()+one_collecting_vector.size(), PHoG_level.begin(), PHoG_level.end());
  171. break;
  172. }
  173. }
  174. }
  175. else //better than anna bosch
  176. {
  177. int step_x = (int) ceil(gradient_orientations.width() / pow(2.0f,level) );
  178. int step_y = (int) ceil(gradient_orientations.height() / pow(2.0f,level) );
  179. if (verbose) cerr << "step_x: " << step_x << " step_y: " << step_y << endl;
  180. std::vector<float> PHoG_level;
  181. int run_y = 0;
  182. for (int y_counter = 0; y_counter < pow(2.0f,level) ; y_counter++)
  183. {
  184. int run_x = 0;
  185. if (verbose) cerr << "run_y: " << run_y << endl;
  186. for (int x_counter = 0; x_counter < pow(2.0f,level) ; x_counter++)
  187. {
  188. if (verbose) cerr << "run_x: " << run_x << endl;
  189. float sum_of_hog(0.0);
  190. vector<float> HoG_local (number_Of_Bins);
  191. //check, wether rectangle lies on the boundary, if so, then correct the max-step
  192. int y_max = (run_y + step_y);
  193. if (gradient_orientations.height() < y_max)
  194. {
  195. if (verbose) cerr << "y_max old:: " << y_max << " y_max_new: "<<gradient_orientations.height() << endl;
  196. y_max = gradient_orientations.height();
  197. }
  198. int x_max = (run_x + step_x);
  199. if (gradient_orientations.width() < x_max)
  200. {
  201. if (verbose) cerr << "x_max old:: " << x_max << " x_max_new: "<<gradient_orientations.width() << endl;
  202. x_max = gradient_orientations.width();
  203. }
  204. for (int j = run_y; j < y_max; j++)
  205. for (int i = run_x; i < x_max; i++)
  206. {
  207. int orientation = 0;
  208. try{ orientation = gradient_orientations.getPixel(i,j);
  209. }
  210. catch( ... )
  211. {
  212. cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide gradient_orientations(i,j) not possible. (i,j): " << i << " " << j << endl;
  213. }
  214. float magnitude = 0.0;
  215. try{ magnitude = gradient_magnitudes.getPixel(i,j);
  216. }
  217. catch( ... )
  218. {
  219. cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide gradient_magnitudes(i,j) not possible. (i,j): " << i << " " << j << endl;
  220. }
  221. if ( (orientation<0) || ((uint)orientation>=HoG_local.size()))
  222. {
  223. cerr << "orientation is " << orientation << " and does not fit to HoG_local.size(): " << HoG_local.size() << endl;
  224. cerr << "i: " << i << " j: " << j << " gradient_orientations.width() : " << gradient_orientations.width() << " gradient_orientations.height() : " << gradient_orientations.height() << endl;
  225. }
  226. try{
  227. HoG_local[orientation] += magnitude;
  228. }
  229. catch( ... )
  230. {
  231. cerr << "WARNING: PHOGFeature::calculate_PHOG_Pyramide HoG_local[orientation] += magnitude not possible. orientation: " << orientation << " magnitude " << magnitude << endl;
  232. }
  233. sum_of_hog += magnitude;
  234. }
  235. sum_of_level += sum_of_hog;
  236. switch(histrogram_concatenation)
  237. {
  238. case 0:
  239. {
  240. if (sum_of_hog != 0.0) //normalize the descriptor-entries
  241. {
  242. for (std::vector<float>::iterator hog_it = HoG_local.begin(); hog_it != HoG_local.end(); hog_it++)
  243. {
  244. *hog_it /= sum_of_hog;
  245. }
  246. }
  247. PHOG_descriptor.push_back(HoG_local);
  248. break;
  249. }
  250. case 1:
  251. {
  252. for (std::vector<float>::const_iterator foo = HoG_local.begin(); foo != HoG_local.end(); foo++)
  253. {
  254. PHoG_level.insert(PHoG_level.begin()+PHoG_level.size(), HoG_local.begin(), HoG_local.end());
  255. }
  256. break;
  257. }
  258. case 2:
  259. {
  260. PHoG_level.insert(PHoG_level.begin()+PHoG_level.size(), HoG_local.begin(), HoG_local.end());
  261. break;
  262. }
  263. }
  264. run_x = run_x + step_x;
  265. }
  266. run_y = run_y + step_y;
  267. }
  268. sum_of_all += sum_of_level;
  269. switch(histrogram_concatenation)
  270. {
  271. case 0:
  272. {
  273. break;
  274. }
  275. case 1:
  276. {
  277. if (sum_of_level != 0.0) //normalize the descriptor-entries
  278. {
  279. for (std::vector<float>::iterator Level_it = PHoG_level.begin(); Level_it != PHoG_level.end(); Level_it++)
  280. {
  281. *Level_it /= sum_of_level;
  282. }
  283. }
  284. PHOG_descriptor.push_back(PHoG_level);
  285. break;
  286. }
  287. case 2:
  288. {
  289. one_collecting_vector.insert(one_collecting_vector.begin()+one_collecting_vector.size(), PHoG_level.begin(), PHoG_level.end());
  290. break;
  291. }
  292. }
  293. }
  294. }
  295. if (histrogram_concatenation == ALL)
  296. {
  297. if (sum_of_all != 0.0) //normalize the descriptor-entries
  298. {
  299. for (std::vector<float>::iterator it = one_collecting_vector.begin(); it != one_collecting_vector.end(); it++)
  300. {
  301. *it /= sum_of_all;
  302. }
  303. }
  304. PHOG_descriptor.push_back(one_collecting_vector);
  305. }
  306. }
  307. /**
  308. * @brief Calculates resulting PHOG-Features for the specified ROI in the given image
  309. * @author Alexander Lütz
  310. * @date 15/11/2011
  311. */
  312. std::vector< std::vector<float> > PHOGFeature::calculate_PHOG_Features(const NICE::Image & orig_Image, const NICE::Rect & roi)
  313. {
  314. if (like_AnnaBosch) //not supported in that version, 'cause !
  315. {
  316. std::cerr << "PHOGFeature::calculate_PHOG_Features is not supported right now" << std::endl;
  317. // //Canny Edge Detector
  318. // Image canny_Image;
  319. // canny_Image = (*canny(orig_Image, 10,30) );
  320. //
  321. // //gradient-calculation
  322. // GrayImage16s* grad_x_Image= sobelX(canny_Image);
  323. // GrayImage16s* grad_y_Image = sobelY(canny_Image);
  324. // if (verbose) cerr << "gradient-calculation done" << endl;
  325. //
  326. // //gradient-orientation-calculation
  327. // NICE::Image gradient_orientations;
  328. // image_tool.calculateGradientOrientations( *grad_x_Image, *grad_y_Image, number_Of_Bins, gradient_orientations, unsignedBins);
  329. // if (verbose) cerr << "gradient-orientation-calculation done" << endl;
  330. //
  331. // //gradient-magnitude-calculation
  332. // //maybe this is not good - implicit cast from int to float in image structure
  333. // NICE::Image* gradient_magnitudes_int = gradientStrength( (*grad_x_Image), (*grad_y_Image) );
  334. // NICE::ImageT<float> gradient_magnitudes(orig_Image.width(), orig_Image.height());
  335. // for (int y = 0; y < orig_Image.height(); y++)
  336. // for (int x = 0; x < orig_Image.width(); x++)
  337. // {
  338. // gradient_magnitudes.setPixel(x,y,(*gradient_magnitudes_int).getPixel(x,y));
  339. // }
  340. // if (verbose) cerr << "gradient-magnitude-calculation done" << endl;
  341. //
  342. // NICE::Image go_roi (gradient_orientations.subImage(roi));
  343. // NICE::ImageT<float> gm_roi (gradient_magnitudes.subImage(roi));
  344. //
  345. // //pyramide-calculation
  346. //
  347. std::vector< std::vector<float> > PHOG_pyramide;
  348. // calculate_PHOG_Pyramide(go_roi, gm_roi, PHOG_pyramide);
  349. //
  350. // if (verbose) cerr << "Pyramide-calculation done" << endl;
  351. return PHOG_pyramide;
  352. }
  353. else
  354. {
  355. //gradient-calculation
  356. NICE::ImageT<float> grad_x_Image;
  357. NICE::ImageT<float> grad_y_Image;
  358. image_tool.calculateGradients(orig_Image, grad_x_Image, grad_y_Image );
  359. if (verbose) cerr << "gradient-calculation done" << endl;
  360. //gradient-orientation-calculation
  361. NICE::Image gradient_orientations;
  362. image_tool.calculateGradientOrientations(grad_x_Image, grad_y_Image, number_Of_Bins, gradient_orientations, unsignedBins);
  363. if (verbose) cerr << "gradient-orientation-calculation done" << endl;
  364. //gradient-magnitude-calculation
  365. NICE::ImageT<float> gradient_magnitudes;
  366. image_tool.calculateGradientMagnitudes(grad_x_Image, grad_y_Image, gradient_magnitudes);
  367. if (verbose) cerr << "gradient-magnitude-calculation done" << endl;
  368. NICE::Image go_roi (gradient_orientations.subImage(roi));
  369. NICE::ImageT<float> gm_roi (gradient_magnitudes.subImage(roi));
  370. //pyramide-calculation
  371. std::vector< std::vector<float> > PHOG_pyramide;
  372. calculate_PHOG_Pyramide(go_roi, gm_roi, PHOG_pyramide);
  373. if (verbose) cerr << "Pyramide-calculation done" << endl;
  374. return PHOG_pyramide;
  375. }
  376. }
  377. //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.!
  378. /** public things*/
  379. /**
  380. * @brief Simple Default Constructor
  381. * @author Alexander Lütz
  382. * @date 15/11/2011
  383. */
  384. PHOGFeature::PHOGFeature()
  385. {
  386. image_tool = OBJREC::Image_tools();
  387. number_Of_Bins = 9;
  388. unsignedBins = true;
  389. number_of_Levels = 3;
  390. like_AnnaBosch = false;
  391. distances_only_between_levels = true;
  392. histrogram_concatenation = NONE;
  393. verbose = false;
  394. }
  395. /**
  396. * @brief Recommended Constructor
  397. * @author Alexander Lütz
  398. * @date 15/11/2011
  399. */
  400. PHOGFeature::PHOGFeature( const Config *conf, std::string section)
  401. {
  402. image_tool = OBJREC::Image_tools();
  403. number_Of_Bins = conf->gI("PHOGFeature", "number_Of_Bins", 9);
  404. unsignedBins = conf->gB("PHOGFeature", "unsignedBins", true);
  405. number_of_Levels = conf->gI("PHOGFeature", "number_of_Levels", 3);
  406. like_AnnaBosch = conf->gB("PHOGFeature", "like_AnnaBosch", false);
  407. distances_only_between_levels = conf->gB("PHOGFeature", "distances_only_between_levels", true);
  408. switch(conf->gI("PHOGFeature", "histrogram_concatenation", 0) )
  409. {
  410. case 0:
  411. {histrogram_concatenation = NONE; break;}
  412. case 1:
  413. {histrogram_concatenation = LEVELWISE; break;}
  414. case 2:
  415. {histrogram_concatenation = ALL; break;}
  416. }
  417. verbose = conf->gB("PHOGFeature", "verbose", false);
  418. }
  419. /**
  420. * @brief Simple Destructor
  421. * @author Alexander Lütz
  422. * @date 15/11/2011
  423. */
  424. PHOGFeature::~PHOGFeature()
  425. {
  426. }
  427. /**
  428. * @brief Creating the PHOG-featureVectores of all trainingexamples
  429. * @author Alexander Lütz
  430. * @date 15/11/2011
  431. */
  432. // std::vector<std::vector< std::vector<float> > > PHOGFeature::createAllFeatureVectors(const OBJREC::LabeledSet::Permutation & order)
  433. // {
  434. //
  435. // std::vector<std::vector< std::vector<float> > > PHOG_Features_all_images;
  436. //
  437. // ProgressBar pb_createFV ("PHOGFeature::createAllFeatureVectors");
  438. // pb_createFV.show();
  439. //
  440. // uint k = 0;
  441. // for ( LabeledSet::Permutation::const_iterator i = order.begin(); i != order.end(); i++,k++ )
  442. // {
  443. // string imgfilename = (*i).second->img();
  444. //
  445. // NICE::Image img(imgfilename);
  446. //
  447. // NICE::Rect roi = NICE::Rect(0,0,img.width(),img.height());
  448. // vector<vector<float> > PHOG = calculate_PHOG_Features(img, roi);
  449. //
  450. // PHOG_Features_all_images.push_back ( PHOG );
  451. //
  452. // pb_createFV.update ( order.size() );
  453. // }
  454. //
  455. // // hide the last progress bar
  456. // pb_createFV.hide();
  457. //
  458. // return PHOG_Features_all_images;
  459. // }
  460. /**
  461. * @brief Creating the PHOG-featureVectores of all trainingexamples
  462. * @author Alexander Lütz
  463. * @date 15/11/2011
  464. */
  465. // 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)
  466. // {
  467. //
  468. // std::vector<std::vector< std::vector<float> > > PHOG_Features_all_images;
  469. //
  470. // cerr << "Calculating gradient orientations." << endl;
  471. // gradient_orientations = calculate_gradient_orientations(order);
  472. // cerr << "Calculating gradient magnitudes." << endl;
  473. // gradient_magnitudes = calculate_gradient_magnitudes(order);
  474. //
  475. // ProgressBar pb_quantization ("PHOGFeature::createAllFeatureVectors");
  476. // pb_quantization.show();
  477. //
  478. // uint k = 0;
  479. // for ( LabeledSet::Permutation::const_iterator i = order.begin(); i != order.end(); i++,k++ )
  480. // {
  481. // std::string imgfilename = (*i).second->img();
  482. //
  483. // NICE::Image img(imgfilename);
  484. //
  485. // std::vector<std::vector<float> > PHOG;
  486. // calculate_PHOG_Pyramide(gradient_orientations[k], gradient_magnitudes[k], PHOG);
  487. //
  488. // PHOG_Features_all_images.push_back ( PHOG );
  489. //
  490. // pb_quantization.update ( order.size() );
  491. // }
  492. //
  493. // // hide the last progress bar
  494. // pb_quantization.hide();
  495. //
  496. // return PHOG_Features_all_images;
  497. // }
  498. /**
  499. * @brief Creating the PHOG-featureVectores of all trainingexamples
  500. * @author Alexander Lütz
  501. * @date 15/11/2011
  502. */
  503. // // // std::vector<std::vector< std::vector<float> > > PHOGFeature::createAllFeatureVectors(const OBJREC::LabeledSet::Permutation & order, const std::vector<int> & trainSelection)
  504. // // // {
  505. // // //
  506. // // // std::vector<std::vector< std::vector<float> > > PHOG_Features_all_images;
  507. // // //
  508. // // // ProgressBar pb_quantization ("PHOGFeature::createAllFeatureVectors");
  509. // // // pb_quantization.show();
  510. // // //
  511. // // // uint k = 0;
  512. // // // for ( std::vector<int>::const_iterator i = trainSelection.begin(); i != trainSelection.end(); i++,k++ )
  513. // // // {
  514. // // // if (verbose) cerr << " Image number " << *i << " will be treated now" << endl;
  515. // // // string imgfilename = order[*i].second->img();
  516. // // //
  517. // // // NICE::Image img(imgfilename);
  518. // // //
  519. // // // NICE::Rect roi = NICE::Rect(0,0,img.width(),img.height());
  520. // // // vector<vector<float> > PHOG = calculate_PHOG_Features(img, roi);
  521. // // //
  522. // // // PHOG_Features_all_images.push_back ( PHOG );
  523. // // //
  524. // // // pb_quantization.update ( trainSelection.size() );
  525. // // // }
  526. // // //
  527. // // // // hide the last progress bar
  528. // // // pb_quantization.hide();
  529. // // //
  530. // // // return PHOG_Features_all_images;
  531. // // // }
  532. // //
  533. // //
  534. // //
  535. /**
  536. * @brief Creating the PHOG-featureVectore for one trainingexample
  537. * @author Alexander Lütz
  538. * @date 15/11/2011
  539. */
  540. std::vector<std::vector<float> > PHOGFeature::createOneFeatureVector(const std::string & imgfilename, const NICE::Rect & ROI)
  541. {
  542. NICE::Image img(imgfilename);
  543. std::vector<std::vector<float> > PHOG (calculate_PHOG_Features(img, ROI));
  544. return PHOG;
  545. }
  546. /**
  547. * @brief Creating the PHOG-featureVectore for one trainingexample
  548. * @author Alexander Lütz
  549. * @date 15/11/2011
  550. */
  551. std::vector<std::vector<float> > PHOGFeature::createOneFeatureVector(const std::string & imgfilename)
  552. {
  553. NICE::Image img(imgfilename);
  554. NICE::Rect ROI(0,0,img.width(), img.height());
  555. std::vector<std::vector<float> > PHOG (calculate_PHOG_Features(img, ROI));
  556. return PHOG;
  557. }
  558. /**
  559. * @brief Creating the PHOG-featureVectore for one trainingexample
  560. * @author Alexander Lütz
  561. * @date 15/11/2011
  562. */
  563. std::vector<std::vector<float> > PHOGFeature::createOneFeatureVector(const NICE::Image img, const NICE::Rect & ROI)
  564. {
  565. std::vector<std::vector<float> > PHOG (calculate_PHOG_Features(img, ROI));
  566. return PHOG;
  567. }
  568. /**
  569. * @brief Creating the PHOG-featureVectore for one trainingexample
  570. * @author Alexander Lütz
  571. * @date 15/11/2011
  572. */
  573. std::vector<std::vector<float> > PHOGFeature::createOneFeatureVector(const NICE::Image img)
  574. {
  575. NICE::Rect ROI(0,0,img.width(), img.height());
  576. std::vector<std::vector<float> > PHOG (calculate_PHOG_Features(img, ROI));
  577. return PHOG;
  578. }
  579. /**
  580. * @brief computes the featurevector for the given image, which is already converted to a gradient-image, just considering features in the specified ROI
  581. * @author Alexander Lütz
  582. * @date 15/11/2011
  583. */
  584. std::vector< std::vector<float> > PHOGFeature::createOneFeatureVectorDirect(NICE::Image & gradient_orientations, NICE::ImageT<float> & gradient_magnitudes, const NICE::Rect & ROI)
  585. {
  586. NICE::Image ROI_go (gradient_orientations.subImage(ROI));
  587. NICE::ImageT<float> ROI_gm (gradient_magnitudes.subImage(ROI));
  588. std::vector< std::vector<float> > PHOG_pyramide;
  589. calculate_PHOG_Pyramide(ROI_go, ROI_gm, PHOG_pyramide);
  590. return PHOG_pyramide;
  591. }
  592. /**
  593. * @brief computes the featurevector for the given image, which is already converted to a gradient-image, considering the whole image
  594. * @author Alexander Lütz
  595. * @date 15/11/2011
  596. */
  597. std::vector< std::vector<float> > PHOGFeature::createOneFeatureVectorDirect(NICE::Image & gradient_orientations, NICE::ImageT<float> & gradient_magnitudes)
  598. {
  599. NICE::Image ROI_go (gradient_orientations);
  600. NICE::ImageT<float> ROI_gm (gradient_magnitudes);
  601. std::vector< std::vector<float> > PHOG_pyramide;
  602. calculate_PHOG_Pyramide(ROI_go, ROI_gm, PHOG_pyramide );
  603. return PHOG_pyramide;
  604. }
  605. // //
  606. // // /**
  607. // // * @brief computes the gradient-orientation-images of all training-images
  608. // // * @author Alexander Lütz
  609. // // * @date 15/11/2011
  610. // // */
  611. // // std::vector< NICE::Image> PHOGFeature::calculate_gradient_orientations(const OBJREC::LabeledSet::Permutation & order)
  612. // // {
  613. // // std::vector< NICE::Image> gradient_orientations;
  614. // //
  615. // // for(OBJREC::LabeledSet::Permutation::const_iterator i = order.begin(); i != order.end(); i++)
  616. // // {
  617. // // string filename = (*i).second->img();
  618. // // NICE::Image img (filename);
  619. // // //gradient-calculation
  620. // // NICE::ImageT<float> grad_x_Image;
  621. // // NICE::ImageT<float> grad_y_Image;
  622. // // image_tool.calculateGradients(img, grad_x_Image, grad_y_Image );
  623. // //
  624. // // //gradient-orientation-calculation
  625. // // NICE::Image gradient_orientation_image;
  626. // // image_tool.calculateGradientOrientations(grad_x_Image, grad_y_Image, number_Of_Bins, gradient_orientation_image, unsignedBins);
  627. // //
  628. // // gradient_orientations.push_back(gradient_orientation_image);
  629. // // }
  630. // //
  631. // // return gradient_orientations;
  632. // // }
  633. //
  634. // /**
  635. // * @brief computes the gradient-magnitudes-images of all training-images
  636. // * @author Alexander Lütz
  637. // * @date 15/11/2011
  638. // */
  639. // std::vector< NICE::ImageT<float> > PHOGFeature::calculate_gradient_magnitudes(const OBJREC::LabeledSet::Permutation & order)
  640. // {
  641. // std::vector< NICE::ImageT<float> > gradient_magnitudes;
  642. //
  643. // for(OBJREC::LabeledSet::Permutation::const_iterator i = order.begin(); i != order.end(); i++)
  644. // {
  645. // string filename = (*i).second->img();
  646. // Image img (filename);
  647. // //gradient-calculation
  648. // NICE::ImageT<float> grad_x_Image;
  649. // NICE::ImageT<float> grad_y_Image;
  650. // image_tool.calculateGradients(img, grad_x_Image, grad_y_Image );
  651. //
  652. // //gradient-orientation-calculation
  653. // NICE::ImageT<float> gradient_magnitude_image;
  654. // image_tool.calculateGradientMagnitudes(grad_x_Image, grad_y_Image, gradient_magnitude_image);
  655. //
  656. // gradient_magnitudes.push_back(gradient_magnitude_image);
  657. // }
  658. //
  659. // return gradient_magnitudes;
  660. // }
  661. /**
  662. * @brief computes the gradient-orientation-images of all training-images
  663. * @author Alexander Lütz
  664. * @date 15/11/2011
  665. */
  666. NICE::Image PHOGFeature::calculate_gradient_orientations(const std::string & imgfilenam)
  667. {
  668. NICE::Image gradient_orientations;
  669. NICE::Image img (imgfilenam);
  670. //gradient-calculation
  671. NICE::ImageT<float> grad_x_Image;
  672. NICE::ImageT<float> grad_y_Image;
  673. image_tool.calculateGradients(img, grad_x_Image, grad_y_Image );
  674. //gradient-orientation-calculation
  675. NICE::Image gradient_orientation_image;
  676. image_tool.calculateGradientOrientations(grad_x_Image, grad_y_Image, number_Of_Bins, gradient_orientation_image, unsignedBins);
  677. return gradient_orientations;
  678. }
  679. /**
  680. * @brief computes the gradient-magnitudes-images of all training-images
  681. * @author Alexander Lütz
  682. * @date 15/11/2011
  683. */
  684. NICE::ImageT<float> PHOGFeature::calculate_gradient_magnitudes(const std::string & imgfilename)
  685. {
  686. NICE::ImageT<float> gradient_magnitudes;
  687. Image img (imgfilename);
  688. //gradient-calculation
  689. NICE::ImageT<float> grad_x_Image;
  690. NICE::ImageT<float> grad_y_Image;
  691. image_tool.calculateGradients(img, grad_x_Image, grad_y_Image );
  692. //gradient-orientation-calculation
  693. NICE::ImageT<float> gradient_magnitude_image;
  694. image_tool.calculateGradientMagnitudes(grad_x_Image, grad_y_Image, gradient_magnitude_image);
  695. return gradient_magnitudes;
  696. }
  697. /**
  698. * @brief Compute the resulting kernel measuring the distance between the given feature-vectores
  699. * @author Alexander Lütz
  700. * @date 15/11/2011
  701. */
  702. void PHOGFeature::calculate_Kernel_from_Features(const std::vector<std::vector< std::vector<float> > > & PHoG_Features, NICE::Matrix & PHoG_Kernel){
  703. // ProgressBar pb_kernelcalc ("PHOGFeature::Kernel Calculation");
  704. // pb_kernelcalc.show();
  705. PHoG_Kernel = NICE::Matrix(PHoG_Features.size(), PHoG_Features.size(), 0.0);
  706. uint ik = 0;
  707. for ( std::vector< std::vector < std::vector<float> > >::const_iterator i = PHoG_Features.begin();i != PHoG_Features.end(); i++,ik++ )
  708. {
  709. uint jk = ik;
  710. for ( std::vector< std::vector< std::vector<float> > >::const_iterator j = i;
  711. j != PHoG_Features.end(); j++,jk++ )
  712. {
  713. double kernelValue (measureDistance(*i,*j));
  714. PHoG_Kernel(ik,jk) = kernelValue;
  715. PHoG_Kernel(jk,ik) = kernelValue;
  716. }
  717. // pb_kernelcalc.update ( PHoG_Features.size() );
  718. }
  719. }
  720. /**
  721. * @brief function which computes the exp^(-Chi^2)-kernel of two PHOG-feature-vectores a and b
  722. * @author Alexander Lütz
  723. * @date 15/11/2011
  724. */
  725. double PHOGFeature::measureDistance ( const std::vector<std::vector<float> > & a, const std::vector<std::vector<float> > & b )
  726. {
  727. double expChi_value = 0.0;
  728. if (a.size() != b.size() ) {
  729. cerr << "a.size(): " << a.size() << " and b.size(): " << b.size() << endl;
  730. fthrow(IOException, "Sizes of PHOG-Feature-vectores do not fit!");
  731. }
  732. for (uint i = 0; i < a.size(); i++) //run over every HoG-Level
  733. {
  734. std::vector<float> a_intern = a[i];
  735. std::vector<float> b_intern = b[i];
  736. double chi_value = 0.0;
  737. if (a_intern.size() != b_intern.size() ) {
  738. cerr << "a_intern.size(): " << a_intern.size() << " and b_intern.size(): " << b_intern.size() << endl;
  739. fthrow(IOException, "Sizes of PHOG-Feature-vectores do not fit!");
  740. }
  741. for (uint j = 0; j < a_intern.size(); j++) //run over every HoG-entry of this level
  742. {
  743. float u = a_intern[j];
  744. float v = b_intern[j];
  745. float s = ( u + v );
  746. if ( fabs(s) < 10e-6 ) continue;
  747. float d = u-v;
  748. chi_value += d*d / s;
  749. }
  750. chi_value *= 0.5;
  751. expChi_value += exp(-chi_value);
  752. }
  753. expChi_value /= a.size(); //normalize to be in between zero and one
  754. return expChi_value;
  755. }
  756. /**
  757. * @brief Simply set the verbose-flag
  758. * @author Alexander Lütz
  759. * @date 15/11/2011
  760. */
  761. void PHOGFeature::set_verbose(const bool & new_verbose){
  762. verbose = new_verbose;
  763. }