RFHoG.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifdef NICE_USELIB_OPENMP
  2. #include <omp.h>
  3. #endif
  4. #include "vislearning/features/regionfeatures/RFHoG.h"
  5. #include <iostream>
  6. #include "vislearning/baselib/FastFilter.h"
  7. using namespace OBJREC;
  8. using namespace std;
  9. using namespace NICE;
  10. RFHoG::RFHoG( const Config *_conf):RFStruct(_conf)
  11. {
  12. numBins = conf->gI( "RFHoG", "numbins", 8 );
  13. usesigned = conf->gB( "RFHoG", "usesigned", false );
  14. amount_cell_x = conf->gI("RFHoG", "cell_x", 4 );
  15. amount_cell_y = conf->gI("RFHoG", "cell_y", 4 );
  16. block_x = conf->gI("RFHoG", "block_x", 2 );
  17. block_y = conf->gI("RFHoG", "block_y", 2 );
  18. }
  19. void RFHoG::extract ( const NICE::FloatImage &imgd_gradient, const NICE::Image &imgd_graddir, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
  20. {
  21. int rgsize = rg.size();
  22. int block_part_x = amount_cell_x - block_x + 1;
  23. int block_part_y = amount_cell_y - block_y + 1;
  24. for(int r = 0; r < rgsize; r++)
  25. {
  26. Vector v(block_part_x * block_part_y * block_x * block_y * numBins);
  27. feats.push_back(v);
  28. }
  29. #pragma omp parallel for
  30. for(int r = 0; r < rgsize; r++)
  31. {
  32. // ***** 6.3 Spatial/Orientation binning *****
  33. int x0,y0,x1,y1;
  34. rg[r]->getRect(x0,y0,x1,y1);
  35. double x_size = x1-x0;
  36. double y_size = y1-y0;
  37. double width = imgd_gradient.width();
  38. double height = imgd_gradient.height();
  39. int cell_x = int ( ceil( x_size / amount_cell_x ) );
  40. int cell_y = int ( ceil( y_size / amount_cell_y ) );
  41. vector<vector<double> > array_cell_hist_vec;
  42. int pos = 0;
  43. for (int i = 0; i < amount_cell_x; i++){
  44. for (int j = 0; j < amount_cell_y; j++, pos++){
  45. vector<double> cell_hist_vec(numBins,0);
  46. array_cell_hist_vec.push_back(cell_hist_vec);
  47. for (int k = 0; k < cell_x; k++){
  48. int x_coord, y_coord;
  49. for (int l = 0; l < cell_y; l++){
  50. x_coord = i*cell_x + k + x0;
  51. y_coord = j*cell_y + l + y0;
  52. if ((x_coord <= x1) && (y_coord <= y_size+y1) && x_coord < width && y_coord < height){
  53. int reg = mask(x_coord, y_coord);
  54. //!TODO: eventuell auch mal ausprobieren mit Pixeln in Bounding Box, die nicht zur Region gehören
  55. if(reg != r)
  56. continue;
  57. double gradient = imgd_gradient.getPixel(x_coord,y_coord);
  58. int graddir = imgd_graddir.getPixel(x_coord,y_coord);
  59. array_cell_hist_vec[pos][graddir] += gradient;
  60. }
  61. else break;
  62. }
  63. }
  64. }
  65. }
  66. // **************************************************
  67. // ***** 6.4 Normalization and Descriptor Block *****
  68. int co = 0;
  69. for (int i = 0; i < block_part_x; i++)
  70. {
  71. for (int j = 0; j < block_part_y; j++)
  72. {
  73. vector<double> fdi; // final descriptor intermediate
  74. for (int k = 0; k < block_x; k++)
  75. {
  76. for (int l = 0; l < block_y; l++)
  77. {
  78. int number = (i+k)*amount_cell_y + (j+l);
  79. fdi.insert(fdi.begin(),array_cell_hist_vec.at(number).begin(),array_cell_hist_vec.at(number).end());
  80. }
  81. }
  82. vector<double>::iterator it;
  83. it = fdi.begin();
  84. double norm_factor = 1e-10; //epsilon^2
  85. while(it!=fdi.end())
  86. {
  87. norm_factor += (*it)*(*it);
  88. it++;
  89. }
  90. double sqrt_norm_factor = sqrt(norm_factor);
  91. it = fdi.begin();
  92. while(it!=fdi.end())
  93. {
  94. feats[r][co] = (*it) / sqrt_norm_factor;
  95. it++;
  96. co++;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. RFHoG::~RFHoG()
  103. {
  104. }