FIHistograms.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "FIHistograms.h"
  2. #include "vislearning/baselib/FastFilter.h"
  3. #include "vislearning/image/GenericImageTools.h"
  4. #include "vislearning/baselib/ColorSpace.h"
  5. using namespace OBJREC;
  6. using namespace NICE;
  7. using namespace std;
  8. void FIHistograms::buildHSVMap (
  9. CachedExample *ce,
  10. int subsamplex,
  11. int subsampley,
  12. int numBinsH,
  13. int numBinsS,
  14. int numBinsV )
  15. {
  16. // build HSV image
  17. // discrete !!
  18. // build integral images
  19. int numBins = numBinsH*numBinsS*numBinsV;
  20. int xsize;
  21. int ysize;
  22. ce->getImageSize ( xsize, ysize );
  23. int xsize_s = xsize / subsamplex;
  24. int ysize_s = ysize / subsampley;
  25. if ( ! ce->colorInformationAvailable() ) {
  26. fprintf (stderr, "FIHistograms: No color information available !\n");
  27. exit(-1);
  28. }
  29. NICE::MultiChannelImageT<int> & colorimg = ce->getIChannel ( CachedExample::I_COLOR );
  30. assert ( colorimg.numChannels == 3 );
  31. NICE::MultiChannelImageT<double> hsvimg ( xsize, ysize, colorimg.numChannels, true );
  32. ColorSpace::convert ( hsvimg, colorimg,
  33. ColorSpace::COLORSPACE_HSL,
  34. ColorSpace::COLORSPACE_RGB,
  35. 1.0, 255.0 );
  36. int *discretecolor = new int [ xsize * ysize ];
  37. long k = 0;
  38. for ( int y = 0 ; y < hsvimg.ysize ; y++ )
  39. for ( int x = 0 ; x < hsvimg.xsize ; x++,k++ )
  40. {
  41. double h = hsvimg.data[0][k];
  42. double s = hsvimg.data[1][k];
  43. double v = hsvimg.data[2][k];
  44. int hbin = (int)(numBinsH * h);
  45. if ( hbin >= numBinsH ) hbin = numBinsH - 1;
  46. int sbin = (int)(numBinsS * s);
  47. if ( sbin >= numBinsS ) sbin = numBinsS - 1;
  48. int vbin = (int)(numBinsV * v);
  49. if ( vbin >= numBinsV ) vbin = numBinsV - 1;
  50. int bin = ( hbin*numBinsS + sbin )*numBinsV + vbin;
  51. discretecolor[k] = bin;
  52. }
  53. hsvimg.freeData();
  54. NICE::MultiChannelImageT<double> & colorhist = ce->getDChannel ( CachedExample::D_INTEGRALCOLOR );
  55. colorhist.reInit ( xsize_s, ysize_s, numBins, true );
  56. colorhist.setAll ( 0 );
  57. long korig = 0;
  58. for ( int y = 0 ; y < ysize ; y++ )
  59. for ( int x = 0 ; x < xsize ; x++,korig++ )
  60. {
  61. int xs = x / subsamplex;
  62. int ys = y / subsampley;
  63. if ( xs >= xsize_s ) xs = xsize_s-1;
  64. if ( xs < 0 ) xs = 0;
  65. if ( ys >= ysize_s ) ys = ysize_s-1;
  66. if ( ys < 0 ) ys = 0;
  67. int k = xs + ys*xsize_s;
  68. int val = discretecolor[korig];
  69. if ( val >= colorhist.numChannels )
  70. {
  71. fprintf (stderr, "v %d nc %d\n", val, colorhist.numChannels );
  72. }
  73. colorhist.data[val][k] += 1;
  74. if ( !finite(colorhist.data[val][k]) ) {
  75. fprintf (stderr, "EOH Image failed: %f\n", colorhist.data[val][k]);
  76. exit(-1);
  77. }
  78. }
  79. delete [] discretecolor;
  80. fprintf (stderr, "Calculating Integral Images\n");
  81. for ( uint i = 0 ; i < colorhist.numChannels ; i++ )
  82. colorhist.calcIntegral ( i );
  83. fprintf (stderr, "FIGradients: finished\n");
  84. }