RFStruct.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifdef NICE_USELIB_OPENMP
  2. #include <omp.h>
  3. #endif
  4. #include "vislearning/features/regionfeatures/RFStruct.h"
  5. #include <iostream>
  6. #include "vislearning/baselib/FastFilter.h"
  7. using namespace OBJREC;
  8. using namespace std;
  9. using namespace NICE;
  10. RFStruct::RFStruct( const Config *_conf):RegionFeatures(_conf)
  11. {
  12. numBins = conf->gI( "RFStruct", "numbins", 8 );
  13. usesigned = conf->gB( "RFStruct", "usesigned", false );
  14. }
  15. void RFStruct::extract ( const NICE::Image & img, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
  16. {
  17. const unsigned char *imgdraw = img.getPixelPointerXY(0,0);
  18. float *gradient = new float[ img.width() * img.height() ];
  19. unsigned char *graddir = new unsigned char[ img.width() * img.height() ];
  20. FastFilter::calcGradient<const unsigned char,float,unsigned char> ( imgdraw, img.width(), img.height(), gradient, graddir, numBins, usesigned );
  21. NICE::FloatImage imgd_gradient(gradient, img.width(), img.height(), GrayColorImageCommonImplementation::noAlignment);
  22. NICE::Image imgd_graddir(graddir, img.width(), img.height(), GrayColorImageCommonImplementation::noAlignment);
  23. /*
  24. Image tmp;
  25. floatToGray(imgd_gradient, &tmp);
  26. showImage(tmp, "Grad");*/
  27. extract ( imgd_gradient, imgd_graddir, rg, mask, feats );
  28. delete [] gradient;
  29. delete [] graddir;
  30. }
  31. void RFStruct::extractRGB ( const NICE::ColorImage & cimg, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
  32. {
  33. unsigned char *imgdraw[3];
  34. for(int i = 0; i < 3; i++)
  35. {
  36. imgdraw[i] = new unsigned char[ cimg.width() * cimg.height() ];
  37. long int k = 0;
  38. for(int y = 0; y < cimg.height(); y++)
  39. {
  40. for(int x = 0; x < cimg.width(); x++, k++)
  41. {
  42. imgdraw[i][k] = cimg.getPixel(x,y,i);
  43. }
  44. }
  45. }
  46. float *gradient = new float[ cimg.width() * cimg.height() ];
  47. unsigned char *graddir = new unsigned char[ cimg.width() * cimg.height() ];
  48. FastFilter::calcColorGradient<unsigned char,float,unsigned char> ( imgdraw[0],imgdraw[1],imgdraw[2], cimg.width(), cimg.height(), gradient, graddir, numBins, usesigned );
  49. NICE::FloatImage imgd_gradient(gradient, cimg.width(), cimg.height(), GrayColorImageCommonImplementation::noAlignment);
  50. NICE::Image imgd_graddir(graddir, cimg.width(), cimg.height(), GrayColorImageCommonImplementation::noAlignment);
  51. extract ( imgd_gradient, imgd_graddir, rg, mask, feats );
  52. for(int i = 0; i < 3; i++)
  53. {
  54. delete [] imgdraw[i];
  55. }
  56. delete [] gradient;
  57. delete [] graddir;
  58. }
  59. void RFStruct::extract ( const NICE::FloatImage &imgd_gradient, const NICE::Image &imgd_graddir, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
  60. {
  61. int rgsize = rg.size();
  62. feats.clear();
  63. for(int r = 0; r < rgsize; r++)
  64. {
  65. Vector f(numBins);
  66. f.set(0.0);
  67. feats.push_back(f);
  68. }
  69. #pragma omp parallel for
  70. for(int r = 0; r < rgsize; r++)
  71. {
  72. // ***** 6.3 Spatial/Orientation binning *****
  73. int x0,y0,x1,y1;
  74. rg[r]->getRect(x0,y0,x1,y1);
  75. for(int y = y0; y <= y1; y++)
  76. {
  77. for(int x = x0; x <= x1; x++)
  78. {
  79. int reg = mask(x, y);
  80. if(reg != r)
  81. continue;
  82. double gradient = imgd_gradient.getPixel(x, y);
  83. int graddir = imgd_graddir.getPixel(x, y);
  84. feats[r][graddir] += gradient;
  85. }
  86. }
  87. double sum = 0.0;
  88. for(int j = 0; j < (int) feats[r].size(); j++)
  89. {
  90. sum += feats[r][j];
  91. }
  92. for(int j = 0; j < (int) feats[r].size(); j++)
  93. {
  94. feats[r][j] /= sum;
  95. }
  96. }
  97. }
  98. RFStruct::~RFStruct()
  99. {
  100. }