Centrist.cpp~ 8.8 KB

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