FIHistograms.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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::MultiChannelImage3DT<int> & colorimg = ce->getIChannel ( CachedExample::I_COLOR );
  30. assert ( colorimg.channels() == 3 );
  31. NICE::MultiChannelImageT<double> hsvimg ( xsize, ysize, colorimg.channels() );
  32. ColorSpace::convert ( hsvimg,
  33. colorimg.getColorMCI(0),
  34. ColorSpace::COLORSPACE_HSL,
  35. ColorSpace::COLORSPACE_RGB,
  36. 1.0, 255.0 );
  37. int *discretecolor = new int [ xsize * ysize ];
  38. long k = 0;
  39. for ( int y = 0 ; y < hsvimg.height() ; y++ )
  40. for ( int x = 0 ; x < hsvimg.width() ; x++, k++ )
  41. {
  42. double h = hsvimg.get(x,y,0);
  43. double s = hsvimg.get(x,y,1);
  44. double v = hsvimg.get(x,y,2);
  45. int hbin = ( int ) ( numBinsH * h );
  46. if ( hbin >= numBinsH ) hbin = numBinsH - 1;
  47. int sbin = ( int ) ( numBinsS * s );
  48. if ( sbin >= numBinsS ) sbin = numBinsS - 1;
  49. int vbin = ( int ) ( numBinsV * v );
  50. if ( vbin >= numBinsV ) vbin = numBinsV - 1;
  51. int bin = ( hbin * numBinsS + sbin ) * numBinsV + vbin;
  52. discretecolor[k] = bin;
  53. }
  54. hsvimg.freeData();
  55. NICE::MultiChannelImage3DT<double> & colorhist = ce->getDChannel ( CachedExample::D_INTEGRALCOLOR );
  56. colorhist.reInit ( xsize_s, ysize_s, 1, numBins);
  57. colorhist.setAll ( 0 );
  58. long korig = 0;
  59. for ( int y = 0 ; y < ysize ; y++ )
  60. for ( int x = 0 ; x < xsize ; x++, korig++ )
  61. {
  62. int xs = x / subsamplex;
  63. int ys = y / subsampley;
  64. if ( xs >= xsize_s ) xs = xsize_s - 1;
  65. if ( xs < 0 ) xs = 0;
  66. if ( ys >= ysize_s ) ys = ysize_s - 1;
  67. if ( ys < 0 ) ys = 0;
  68. int k = xs + ys * xsize_s;
  69. int val = discretecolor[korig];
  70. if ( val >= colorhist.channels() )
  71. {
  72. fprintf ( stderr, "v %d nc %d\n", val, colorhist.channels() );
  73. }
  74. colorhist[val](x,y) += 1;
  75. if ( !NICE::isFinite( colorhist[val](x,y) ) ) {
  76. fprintf ( stderr, "EOH Image failed: %f\n", colorhist[val](x,y) );
  77. exit ( -1 );
  78. }
  79. }
  80. delete [] discretecolor;
  81. fprintf ( stderr, "Calculating Integral Images\n" );
  82. for ( uint i = 0 ; i < colorhist.channels() ; i++ )
  83. colorhist.calcIntegral ( i );
  84. fprintf ( stderr, "FIGradients: finished\n" );
  85. }