FCGreyValues.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file FCGreyValues.cpp
  3. * @brief extract simple greyvalue features
  4. * @author Erik Rodner
  5. * @date 11/15/2007
  6. */
  7. #ifdef NOVISUAL
  8. #include <vislearning/nice_nonvis.h>
  9. #else
  10. #include <vislearning/nice.h>
  11. #endif
  12. #include <iostream>
  13. #include "vislearning/features/simplefeatures/FCGreyValues.h"
  14. using namespace OBJREC;
  15. using namespace std;
  16. // refactor-nice.pl: check this substitution
  17. // old: using namespace ice;
  18. using namespace NICE;
  19. FCGreyValues::FCGreyValues( const Config * conf, int _xsize, int _ysize ) :
  20. FeatureFactory ( conf ), xsize(_xsize), ysize(_ysize)
  21. {
  22. xsize = conf->gI("FCGreyValues", "xsize", _xsize <= 0 ? 20 : _xsize );
  23. ysize = conf->gI("FCGreyValues", "ysize", _ysize <= 0 ? 50 : _ysize );
  24. // refactor-nice.pl: check this substitution
  25. // old: string normalization_s = conf->gS("FCGreyValues", "normalization", "n01");
  26. std::string normalization_s = conf->gS("FCGreyValues", "normalization", "n01");
  27. if ( normalization_s == "n01" )
  28. normalization = NORMALIZE_N01;
  29. else if ( normalization_s == "stddev" )
  30. normalization = NORMALIZE_STDDEV;
  31. else if ( normalization_s == "mean" )
  32. normalization = NORMALIZE_MEAN;
  33. else if ( normalization_s == "none" )
  34. normalization = NORMALIZE_NONE;
  35. else {
  36. fprintf (stderr, "FCGreyValues::FCGreyValues: unknown normalization method\n");
  37. exit(-1);
  38. }
  39. // refactor-nice.pl: check this substitution
  40. // old: tmp = NewImg(xsize, ysize, 255);
  41. tmp.resize(xsize, ysize);
  42. }
  43. FCGreyValues::~FCGreyValues()
  44. {
  45. }
  46. // refactor-nice.pl: check this substitution
  47. // old: int FCGreyValues::convert ( const Image & img, Vector & vec )
  48. int FCGreyValues::convert ( const NICE::Image & img, NICE::Vector & vec )
  49. {
  50. tmp.set(0);
  51. NICE::scale ( img, &tmp );
  52. vec.resize(xsize*ysize);
  53. double mean = 0.0;
  54. double stddev = 0.0;
  55. if ( normalization != NORMALIZE_NONE )
  56. {
  57. // compute mean
  58. for ( int yi = 0 ; yi < ysize ; yi++ )
  59. for ( int xi = 0 ; xi < xsize ; xi++ )
  60. // refactor-nice.pl: check this substitution
  61. // old: mean += GetVal(tmp, xi, yi);
  62. mean += tmp.getPixel(xi,yi);
  63. mean /= (xsize*ysize);
  64. if ( normalization != NORMALIZE_MEAN )
  65. {
  66. // compute stddev
  67. stddev = 0.0;
  68. for ( int yi = 0 ; yi < ysize ; yi++ )
  69. for ( int xi = 0 ; xi < xsize ; xi++ )
  70. {
  71. // refactor-nice.pl: check this substitution
  72. // old: double d = GetVal(tmp,xi,yi) - mean;
  73. double d = tmp.getPixel(xi,yi) - mean;
  74. stddev += d*d;
  75. }
  76. if ( stddev < 10e-5 ) return -1;
  77. stddev /= xsize*ysize;
  78. stddev = sqrt(stddev);
  79. }
  80. }
  81. if ( normalization == NORMALIZE_STDDEV )
  82. {
  83. // normalize pixel values
  84. int k = 0;
  85. for ( int yi = 0; yi < ysize ; yi++ )
  86. for ( int xi = 0; xi < xsize ; xi++,k++ )
  87. // refactor-nice.pl: check this substitution
  88. // old: vec[k] = (GetVal(tmp,xi,yi) - mean) / stddev + mean;
  89. vec[k] = (tmp.getPixel(xi,yi) - mean) / stddev + mean;
  90. } else if ( normalization == NORMALIZE_N01 ) {
  91. // normalize pixel values
  92. int k = 0;
  93. for ( int yi = 0 ; yi < ysize ; yi++ )
  94. for ( int xi = 0 ; xi < xsize ; xi++,k++ )
  95. // refactor-nice.pl: check this substitution
  96. // old: vec[k] = (GetVal(tmp,xi,yi) - mean) / stddev;
  97. vec[k] = (tmp.getPixel(xi,yi) - mean) / stddev;
  98. } else if ( normalization == NORMALIZE_MEAN ) {
  99. // normalize pixel values
  100. int k = 0;
  101. for ( int yi = 0 ; yi < ysize ; yi++ )
  102. for ( int xi = 0 ; xi < xsize ; xi++,k++ )
  103. // refactor-nice.pl: check this substitution
  104. // old: vec[k] = (GetVal(tmp,xi,yi) - mean);
  105. vec[k] = (tmp.getPixel(xi,yi) - mean);
  106. } else {
  107. int k = 0;
  108. for ( int yi = 0 ; yi < ysize ; yi++ )
  109. for ( int xi = 0 ; xi < xsize ; xi++,k++ )
  110. // refactor-nice.pl: check this substitution
  111. // old: vec[k] = GetVal(tmp,xi,yi);
  112. vec[k] = tmp.getPixel(xi,yi);
  113. }
  114. return 0;
  115. }