LocalFeatureCentrist.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * @file LocalFeatureCentrist.cpp
  3. * @brief Implementation of the LocalFeatureCentrist feature published in "CENTRIST: A Visual Descriptor for Scene Categorization" (PAMI 2011)
  4. * @author Alexander Freytag
  5. * @date 12/06/2011
  6. */
  7. // STL includes
  8. #include <iostream>
  9. // nice-vislearning includes
  10. #include "vislearning/features/localfeatures/LocalFeatureCentrist.h"
  11. using namespace OBJREC;
  12. using namespace std;
  13. using namespace NICE;
  14. /* protected methods*/
  15. /**
  16. * @brief: perform centrist transformation for a single pixel
  17. * @author Alexander Freytag
  18. * @date 12/06/2011
  19. */
  20. int LocalFeatureCentrist::CensusTransform(const NICE::Image & img, const int & x, const int & y) const
  21. {
  22. int index(0);
  23. if (! img.isWithinImage(x,y)) return index;
  24. if( (img.isWithinImage(x-1,y-1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x-1,y-1)) ) index |= 0x80;
  25. if( (img.isWithinImage(x-1,y)) && (img.getPixelInt(x,y)<=img.getPixelInt(x-1,y)) ) index |= 0x40;
  26. if( (img.isWithinImage(x-1,y+1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x-1,y+1)) ) index |= 0x20;
  27. if( (img.isWithinImage(x,y-1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x,y-1)) ) index |= 0x10;
  28. if( (img.isWithinImage(x,y+1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x,y+1)) ) index |= 0x08;
  29. if( (img.isWithinImage(x+1,y-1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x+1,y-1)) ) index |= 0x04;
  30. if( (img.isWithinImage(x+1,y)) && (img.getPixelInt(x,y)<=img.getPixelInt(x+1,y)) ) index |= 0x02;
  31. if( (img.isWithinImage(x+1,y+1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x+1,y+1)) ) index |= 0x01;
  32. return index;
  33. }
  34. /**
  35. * @brief: perform centrist transformation for a single pixel
  36. * @author Alexander Freytag
  37. * @date 12/06/2011
  38. */
  39. int LocalFeatureCentrist::CensusTransform(const NICE::ColorImage & img, const int & x, const int & y, const int & channel) const
  40. {
  41. int index(0);
  42. if (! img.isWithinImage(x,y)) return index;
  43. if( (img.isWithinImage(x-1,y-1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x-1,y-1, channel)) ) index |= 0x80;
  44. if( (img.isWithinImage(x-1,y)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x-1,y, channel)) ) index |= 0x40;
  45. if( (img.isWithinImage(x-1,y+1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x-1,y+1, channel)) ) index |= 0x20;
  46. if( (img.isWithinImage(x,y-1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x,y-1, channel)) ) index |= 0x10;
  47. if( (img.isWithinImage(x,y+1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x,y+1, channel)) ) index |= 0x08;
  48. if( (img.isWithinImage(x+1,y-1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x+1,y-1, channel)) ) index |= 0x04;
  49. if( (img.isWithinImage(x+1,y)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x+1,y, channel)) ) index |= 0x02;
  50. if( (img.isWithinImage(x+1,y+1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x+1,y+1, channel)) ) index |= 0x01;
  51. return index;
  52. }
  53. /**
  54. * @brief: generate CT histogram for a given rectangle: pixels in [xi yi]-(xa,ya) -- including (xi,yi) but excluding (xa,ya)
  55. * @author Alexander Freytag
  56. * @date 12/06/2011
  57. */
  58. void LocalFeatureCentrist::GenerateHistForOneRect(const NICE::Image & img, const int & xi, const int & xa, const int & yi, const int & ya, NICE::Vector & feature) const
  59. {
  60. // double pixelSum(0.0);
  61. // double pixelSquare(0.0);
  62. feature.resize(256);
  63. feature.set(0.0);
  64. for (int j = yi; j < ya; j++)
  65. {
  66. for (int i = xi; i < xa; i++)
  67. {
  68. // pixelSum += img.getPixelInt(i,j);
  69. // pixelSquare += img.getPixelInt(i,j)*img.getPixelInt(i,j);
  70. feature[CensusTransform(img,i,j)]++;
  71. }
  72. }
  73. // normalize histogram to have 0 mean, remove the first and last entry, and normalize to have unit norm
  74. double sum(feature.Sum()/256.0);//shift more efficient?
  75. // normalize histogram to have 0 mean
  76. //but why?
  77. feature -= sum;
  78. //remove the first and last entry
  79. feature[0] = 0.0;
  80. feature[255] = 0.0;
  81. //normalization - here done using unit L2-norm
  82. feature.normalizeL2();
  83. }
  84. /**
  85. * @brief: generate CT histogram for a given rectangle: pixels in [xi yi]-(xa,ya) -- including (xi,yi) but excluding (xa,ya)
  86. * @author Alexander Freytag
  87. * @date 12/06/2011
  88. */
  89. void LocalFeatureCentrist::GenerateHistForOneRect(const NICE::ColorImage & img, const int & xi, const int & xa, const int & yi, const int & ya, NICE::Vector & feature) const
  90. {
  91. // double pixelSum(0.0);
  92. // double pixelSquare(0.0);
  93. int nrChannel (img.channels());
  94. feature.resize(256*nrChannel);
  95. feature.set(0.0);
  96. for (int channel = 0; channel < nrChannel; channel++)
  97. {
  98. for (int j = yi; j < ya; j++)
  99. {
  100. for (int i = xi; i < xa; i++)
  101. {
  102. // pixelSum += img.getPixelInt(i,j, channel);
  103. // pixelSquare += img.getPixelInt(i,j, channel)*img.getPixelInt(i,j, channel);
  104. feature[256*channel+this->CensusTransform(img,i,j,channel)]++;
  105. }
  106. }
  107. }
  108. // normalize histogram to have 0 mean, remove the first and last entry, and normalize to have unit norm
  109. double sum(feature.Sum()/256.0);//shift more efficient?
  110. // normalize histogram to have 0 mean
  111. //but why?
  112. feature -= sum;
  113. //remove the first and last entry
  114. feature[0] = 0.0;
  115. feature[255] = 0.0;
  116. //normalization - here done using unit L2-norm
  117. feature.normalizeL2();
  118. }
  119. /**
  120. * @brief Computes several CENTRIST descriptors for each of the given positions om a local neighborhood
  121. * @author Alexander Freytag
  122. * @date 12/06/2011
  123. */
  124. void LocalFeatureCentrist::computeDesc( const NICE::Image & img, NICE::VVector & positions, NICE::VVector & descriptors ) const
  125. {
  126. descriptors.clear();
  127. for (NICE::VVector::const_iterator it = positions.begin(); it != positions.end(); it++)
  128. {
  129. NICE::Vector descriptor;
  130. this->GenerateHistForOneRect(img, (*it)[0]-i_sizeNeighborhood/2, (*it)[0]+i_sizeNeighborhood/2, (*it)[1]-i_sizeNeighborhood/2, (*it)[1]+i_sizeNeighborhood/2, descriptor);
  131. descriptors.push_back(descriptor);
  132. }
  133. }
  134. /**
  135. * @brief Computes several CENTRIST descriptors for each of the given positions om a local neighborhood
  136. * @author Alexander Freytag
  137. * @date 12/06/2011
  138. */
  139. void LocalFeatureCentrist::computeDesc( const NICE::ColorImage & img, NICE::VVector & positions, NICE::VVector & descriptors ) const
  140. {
  141. descriptors.clear();
  142. for (NICE::VVector::const_iterator it = positions.begin(); it != positions.end(); it++)
  143. {
  144. NICE::Vector descriptor;
  145. this->GenerateHistForOneRect(img, (*it)[0]-i_sizeNeighborhood/2, (*it)[0]+i_sizeNeighborhood/2, (*it)[1]-i_sizeNeighborhood/2, (*it)[1]+i_sizeNeighborhood/2, descriptor);
  146. descriptors.push_back(descriptor);
  147. }
  148. }
  149. /* public methods*/
  150. /**
  151. * @brief Default constructor
  152. * @author Alexander Freytag
  153. * @date 12/06/2011
  154. */
  155. LocalFeatureCentrist::LocalFeatureCentrist()
  156. {
  157. this->i_sizeNeighborhood = 16;
  158. }
  159. /**
  160. * @brief Recommended constructor
  161. * @author Alexander Freytag
  162. * @date 12/06/2011
  163. */
  164. LocalFeatureCentrist::LocalFeatureCentrist( const Config *conf, const std::string & section )
  165. {
  166. this->i_sizeNeighborhood = conf->gI(section, "i_sizeNeighborhood", 16);
  167. }
  168. /**
  169. * @brief Default destructor
  170. * @author Alexander Freytag
  171. * @date 12/06/2011
  172. */
  173. LocalFeatureCentrist::~LocalFeatureCentrist()
  174. {
  175. }
  176. int LocalFeatureCentrist::getDescSize() const
  177. {
  178. // we have 8 neighbors, so the size is always 256 with the given implementation (without spatial levels, ...)
  179. return 256;
  180. }
  181. /**
  182. * @brief Computes several CENTRIST descriptors for each of the given positions on a local neighborhood
  183. * @author Alexander Freytag
  184. * @date 12/06/2011
  185. */
  186. int LocalFeatureCentrist::getDescriptors ( const NICE::Image & img, VVector & positions, VVector & descriptors ) const
  187. {
  188. this->computeDesc( img, positions, descriptors );
  189. return 0;
  190. }
  191. /**
  192. * @brief Computes several CENTRIST descriptors for each of the given positions on a local neighborhood
  193. * @author Alexander Freytag
  194. * @date 12/06/2011
  195. */
  196. int LocalFeatureCentrist::getDescriptors ( const NICE::ColorImage & img, NICE::VVector & positions, NICE::VVector & descriptors) const
  197. {
  198. this->computeDesc( img, positions, descriptors );
  199. return 0;
  200. }
  201. /**
  202. * @brief Visualizes CENTRIST descriptors, not implemented yet!
  203. * @author Alexander Freytag
  204. * @date 12/06/2011
  205. */
  206. void LocalFeatureCentrist::visualizeFeatures ( NICE::Image & mark,
  207. const VVector & positions,
  208. size_t color ) const
  209. {
  210. throw NICE::Exception ( "LocalFeatureCentrist::visualizeFeatures -- currently not implemented." );
  211. }