Centrist.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @file Centrist.cpp
  3. * @brief Implementation of the Centrist feature published in "CENTRIST: A Visual Descriptor for Scene Categorization" (PAMI 2011)
  4. * @author Alexander Lütz
  5. * @date 12/06/2011
  6. */
  7. #include <iostream>
  8. #include "vislearning/features/localfeatures/Centrist.h"
  9. #include "core/image/ImageT.h"
  10. #include "core/vector/VVector.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 Lütz
  18. * @date 12/06/2011
  19. */
  20. //TODO maybe inline?
  21. int CensusTransform(const NICE::Image & img, const int & x, const int & y)
  22. {
  23. int index(0);
  24. if (! img.isWithinImage(x,y)) return index;
  25. if( (img.isWithinImage(x-1,y-1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x-1,y-1)) ) index |= 0x80;
  26. if( (img.isWithinImage(x-1,y)) && (img.getPixelInt(x,y)<=img.getPixelInt(x-1,y)) ) index |= 0x40;
  27. if( (img.isWithinImage(x-1,y+1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x-1,y+1)) ) index |= 0x20;
  28. if( (img.isWithinImage(x,y-1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x,y-1)) ) index |= 0x10;
  29. if( (img.isWithinImage(x,y+1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x,y+1)) ) index |= 0x08;
  30. if( (img.isWithinImage(x+1,y-1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x+1,y-1)) ) index |= 0x04;
  31. if( (img.isWithinImage(x+1,y)) && (img.getPixelInt(x,y)<=img.getPixelInt(x+1,y)) ) index |= 0x02;
  32. if( (img.isWithinImage(x+1,y+1)) && (img.getPixelInt(x,y)<=img.getPixelInt(x+1,y+1)) ) index |= 0x01;
  33. return index;
  34. }
  35. /**
  36. * @brief: perform centrist transformation for a single pixel
  37. * @author Alexander Lütz
  38. * @date 12/06/2011
  39. */
  40. //TODO maybe inline?
  41. int CensusTransform(const NICE::ColorImage & img, const int & x, const int & y, const int & channel)
  42. {
  43. int index(0);
  44. if (! img.isWithinImage(x,y)) return index;
  45. if( (img.isWithinImage(x-1,y-1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x-1,y-1, channel)) ) index |= 0x80;
  46. if( (img.isWithinImage(x-1,y)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x-1,y, channel)) ) index |= 0x40;
  47. if( (img.isWithinImage(x-1,y+1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x-1,y+1, channel)) ) index |= 0x20;
  48. if( (img.isWithinImage(x,y-1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x,y-1, channel)) ) index |= 0x10;
  49. if( (img.isWithinImage(x,y+1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x,y+1, channel)) ) index |= 0x08;
  50. if( (img.isWithinImage(x+1,y-1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x+1,y-1, channel)) ) index |= 0x04;
  51. if( (img.isWithinImage(x+1,y)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x+1,y, channel)) ) index |= 0x02;
  52. if( (img.isWithinImage(x+1,y+1)) && (img.getPixelInt(x,y, channel)<=img.getPixelInt(x+1,y+1, channel)) ) index |= 0x01;
  53. return index;
  54. }
  55. /**
  56. * @brief: generate CT histogram for a given rectangle: pixels in [xi yi]-(xa,ya) -- including (xi,yi) but excluding (xa,ya)
  57. * @author Alexander Lütz
  58. * @date 12/06/2011
  59. */
  60. void Centrist::GenerateHistForOneRect(const NICE::Image & img, const int & xi, const int & xa, const int & yi, const int & ya, NICE::Vector & feature)
  61. {
  62. // double pixelSum(0.0);
  63. // double pixelSquare(0.0);
  64. feature.resize(256);
  65. feature.set(0.0);
  66. for (int j = yi; j < ya; j++)
  67. {
  68. for (int i = xi; i < xa; i++)
  69. {
  70. // pixelSum += img.getPixelInt(i,j);
  71. // pixelSquare += img.getPixelInt(i,j)*img.getPixelInt(i,j);
  72. feature[CensusTransform(img,i,j)]++;
  73. }
  74. }
  75. // const double size ( (xa-xi)*(ya-yi));
  76. // normalize histogram to have 0 mean, remove the first and last entry, and normalize to have unit norm
  77. double sum(feature.Sum()/256.0);//shift more efficient?
  78. // normalize histogram to have 0 mean
  79. //but why?
  80. feature -= sum;
  81. //remove the first and last entry
  82. feature[0] = 0.0;
  83. feature[255] = 0.0;
  84. //normalization - here done using unit L2-norm
  85. feature.normalizeL2();
  86. }
  87. /**
  88. * @brief: generate CT histogram for a given rectangle: pixels in [xi yi]-(xa,ya) -- including (xi,yi) but excluding (xa,ya)
  89. * @author Alexander Lütz
  90. * @date 12/06/2011
  91. */
  92. void Centrist::GenerateHistForOneRect(const NICE::ColorImage & img, const int & xi, const int & xa, const int & yi, const int & ya, NICE::Vector & feature)
  93. {
  94. // double pixelSum(0.0);
  95. // double pixelSquare(0.0);
  96. int nrChannel (img.channels());
  97. feature.resize(256*nrChannel);
  98. feature.set(0.0);
  99. for (int channel = 0; channel < nrChannel; channel++)
  100. {
  101. for (int j = yi; j < ya; j++)
  102. {
  103. for (int i = xi; i < xa; i++)
  104. {
  105. // pixelSum += img.getPixelInt(i,j, channel);
  106. // pixelSquare += img.getPixelInt(i,j, channel)*img.getPixelInt(i,j, channel);
  107. feature[256*channel+CensusTransform(img,i,j,channel)]++;
  108. }
  109. }
  110. }
  111. // const double size ( (xa-xi)*(ya-yi))
  112. // normalize histogram to have 0 mean, remove the first and last entry, and normalize to have unit norm
  113. double sum(feature.Sum()/256.0);//shift more efficient?
  114. // normalize histogram to have 0 mean
  115. //but why?
  116. feature -= sum;
  117. //remove the first and last entry
  118. feature[0] = 0.0;
  119. feature[255] = 0.0;
  120. //normalization - here done using unit L2-norm
  121. feature.normalizeL2();
  122. }
  123. /**
  124. * @brief Computes several CENTRIST descriptors for each of the given positions om a local neighborhood
  125. * @author Alexander Lütz
  126. * @date 12/06/2011
  127. */
  128. void Centrist::computeDesc( const NICE::Image & img, NICE::VVector & positions, NICE::VVector & descriptors )
  129. {
  130. descriptors.clear();
  131. for (NICE::VVector::const_iterator it = positions.begin(); it != positions.end(); it++)
  132. {
  133. NICE::Vector descriptor;
  134. GenerateHistForOneRect(img, (*it)[0]-sizeNeighborhood/2, (*it)[0]+sizeNeighborhood/2, (*it)[1]-sizeNeighborhood/2, (*it)[1]+sizeNeighborhood/2, descriptor);
  135. descriptors.push_back(descriptor);
  136. }
  137. }
  138. /**
  139. * @brief Computes several CENTRIST descriptors for each of the given positions om a local neighborhood
  140. * @author Alexander Lütz
  141. * @date 12/06/2011
  142. */
  143. void Centrist::computeDesc( const NICE::ColorImage & img, NICE::VVector & positions, NICE::VVector & descriptors )
  144. {
  145. descriptors.clear();
  146. for (NICE::VVector::const_iterator it = positions.begin(); it != positions.end(); it++)
  147. {
  148. NICE::Vector descriptor;
  149. GenerateHistForOneRect(img, (*it)[0]-sizeNeighborhood/2, (*it)[0]+sizeNeighborhood/2, (*it)[1]-sizeNeighborhood/2, (*it)[1]+sizeNeighborhood/2, descriptor);
  150. descriptors.push_back(descriptor);
  151. }
  152. }
  153. /* public methods*/
  154. /**
  155. * @brief Default constructor
  156. * @author Alexander Lütz
  157. * @date 12/06/2011
  158. */
  159. Centrist::Centrist()
  160. {
  161. sizeNeighborhood = 16;
  162. }
  163. /**
  164. * @brief Recommended constructor
  165. * @author Alexander Lütz
  166. * @date 12/06/2011
  167. */
  168. Centrist::Centrist( const Config *conf, const std::string & section )
  169. {
  170. sizeNeighborhood = conf->gI(section, "sizeNeighborhood", 16);
  171. }
  172. /**
  173. * @brief Default destructor
  174. * @author Alexander Lütz
  175. * @date 12/06/2011
  176. */
  177. Centrist::~Centrist()
  178. {
  179. }
  180. /**
  181. * @brief Computes several CENTRIST descriptors for each of the given positions on a local neighborhood
  182. * @author Alexander Lütz
  183. * @date 12/06/2011
  184. */
  185. int Centrist::getDescriptors ( const NICE::Image & img, VVector & positions, VVector & descriptors )
  186. {
  187. computeDesc(img, positions, descriptors);
  188. return 0;
  189. }
  190. /**
  191. * @brief Computes several CENTRIST descriptors for each of the given positions on a local neighborhood
  192. * @author Alexander Lütz
  193. * @date 12/06/2011
  194. */
  195. int Centrist::getDescriptors ( const NICE::ColorImage & img, NICE::VVector & positions, NICE::VVector & descriptors)
  196. {
  197. computeDesc(img, positions, descriptors);
  198. return 0;
  199. }
  200. /**
  201. * @brief Visualizes CENTRIST descriptors, not implemented yet!
  202. * @author Alexander Lütz
  203. * @date 12/06/2011
  204. */
  205. void Centrist::visualizeFeatures ( NICE::Image & mark,
  206. const VVector & positions,
  207. size_t color ) const
  208. {
  209. // ice::Image mark_ice = ice::NewImg ( mark.width(),
  210. // mark.height(), 255 );
  211. // for ( size_t k = 0 ; k < positions.size() ; k++ )
  212. // {
  213. // const NICE::Vector & pos = positions[k];
  214. // ice::Matrix points ( 0, 2 );
  215. // const int size = 6;
  216. // points.Append ( ice::Vector(-size, -size) );
  217. // points.Append ( ice::Vector(-size, size) );
  218. // points.Append ( ice::Vector(size, size) );
  219. // points.Append ( ice::Vector(size, -size) );
  220. //
  221. // ice::Trafo tr;
  222. //
  223. // tr.Scale ( 0, 0, pos[2] );
  224. // tr.Rotate ( 0, 0, pos[3] );
  225. // tr.Shift ( pos[0], pos[1] );
  226. //
  227. // ice::TransformList(tr, points);
  228. //
  229. // for ( int j = 0 ; j < points.rows(); j++ )
  230. // {
  231. // if (points[j][0] < 0 )
  232. // points[j][0] = 0;
  233. // if (points[j][0] >= mark_ice->xsize)
  234. // points[j][0] = mark_ice->xsize - 1;
  235. // if (points[j][1] < 0 )
  236. // points[j][1] = 0;
  237. // if (points[j][1] >= mark_ice->ysize)
  238. // points[j][1] = mark_ice->ysize - 1;
  239. // }
  240. // ice::DrawPolygon ( points, color, mark_ice );
  241. // }
  242. //
  243. // for ( unsigned int y = 0 ; y < mark.height(); y++ )
  244. // for ( unsigned int x = 0 ; x < mark.width(); x++ )
  245. // mark.setPixel(x,y, GetVal(mark_ice,x,y));
  246. }