LocalFeatureCentrist.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // CONSTRUCTORS / DESTRUCTORS
  152. ///////////////////// ///////////////////// /////////////////////
  153. LocalFeatureCentrist::LocalFeatureCentrist() : LocalFeature ()
  154. {
  155. this->i_sizeNeighborhood = 16;
  156. }
  157. LocalFeatureCentrist::LocalFeatureCentrist( const NICE::Config * _conf )
  158. {
  159. this->initFromConfig( _conf );
  160. }
  161. LocalFeatureCentrist::~LocalFeatureCentrist()
  162. {
  163. }
  164. void OBJREC::LocalFeatureCentrist::initFromConfig( const NICE::Config* _conf, const std::string& _confSection )
  165. {
  166. this->i_sizeNeighborhood = _conf->gI(_confSection, "i_sizeNeighborhood", 16);
  167. }
  168. ///////////////////// ///////////////////// /////////////////////
  169. // FEATURE STUFF
  170. ///////////////////// ///////////////////// //////////////////
  171. int LocalFeatureCentrist::getDescSize() const
  172. {
  173. // we have 8 neighbors, so the size is always 256 with the given implementation (without spatial levels, ...)
  174. return 256;
  175. }
  176. /**
  177. * @brief Computes several CENTRIST descriptors for each of the given positions on a local neighborhood
  178. * @author Alexander Freytag
  179. * @date 12/06/2011
  180. */
  181. int LocalFeatureCentrist::getDescriptors ( const NICE::Image & img, VVector & positions, VVector & descriptors ) const
  182. {
  183. this->computeDesc( img, positions, descriptors );
  184. return 0;
  185. }
  186. /**
  187. * @brief Computes several CENTRIST descriptors for each of the given positions on a local neighborhood
  188. * @author Alexander Freytag
  189. * @date 12/06/2011
  190. */
  191. int LocalFeatureCentrist::getDescriptors ( const NICE::ColorImage & img, NICE::VVector & positions, NICE::VVector & descriptors) const
  192. {
  193. this->computeDesc( img, positions, descriptors );
  194. return 0;
  195. }
  196. /**
  197. * @brief Visualizes CENTRIST descriptors, not implemented yet!
  198. * @author Alexander Freytag
  199. * @date 12/06/2011
  200. */
  201. void LocalFeatureCentrist::visualizeFeatures ( NICE::Image & mark,
  202. const VVector & positions,
  203. size_t color ) const
  204. {
  205. throw NICE::Exception ( "LocalFeatureCentrist::visualizeFeatures -- currently not implemented." );
  206. }
  207. ///////////////////// INTERFACE PERSISTENT /////////////////////
  208. // interface specific methods for store and restore
  209. ///////////////////// INTERFACE PERSISTENT /////////////////////
  210. void LocalFeatureCentrist::restore ( std::istream & is, int format )
  211. {
  212. //delete everything we knew so far...
  213. this->clear();
  214. if ( is.good() )
  215. {
  216. std::string tmp;
  217. is >> tmp; //class name
  218. if ( ! this->isStartTag( tmp, "LocalFeatureCentrist" ) )
  219. {
  220. std::cerr << " WARNING - attempt to restore LocalFeatureCentrist, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  221. throw;
  222. }
  223. bool b_endOfBlock ( false ) ;
  224. while ( !b_endOfBlock )
  225. {
  226. is >> tmp; // start of block
  227. if ( this->isEndTag( tmp, "LocalFeatureCentrist" ) )
  228. {
  229. b_endOfBlock = true;
  230. continue;
  231. }
  232. tmp = this->removeStartTag ( tmp );
  233. if ( tmp.compare("i_sizeNeighborhood") == 0 )
  234. {
  235. is >> this->i_sizeNeighborhood;
  236. is >> tmp; // end of block
  237. tmp = this->removeEndTag ( tmp );
  238. }
  239. else
  240. {
  241. std::cerr << "WARNING -- unexpected LocalFeatureCentrist object -- " << tmp << " -- for restoration... aborting" << std::endl;
  242. throw;
  243. }
  244. }
  245. }
  246. else
  247. {
  248. std::cerr << "LocalFeatureCentrist::restore -- InStream not initialized - restoring not possible!" << std::endl;
  249. throw;
  250. }
  251. }
  252. void LocalFeatureCentrist::store ( std::ostream & os, int format ) const
  253. {
  254. if (os.good())
  255. {
  256. // show starting point
  257. os << this->createStartTag( "LocalFeatureCentrist" ) << std::endl;
  258. os << this->createStartTag( "i_sizeNeighborhood" ) << std::endl;
  259. os << this->i_sizeNeighborhood << std::endl;
  260. os << this->createEndTag( "i_sizeNeighborhood" ) << std::endl;
  261. // done
  262. os << this->createEndTag( "LocalFeatureCentrist" ) << std::endl;
  263. }
  264. else
  265. {
  266. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  267. }
  268. }
  269. void LocalFeatureCentrist::clear ()
  270. {
  271. }