ConvolutionFeature.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * @file ConvolutionFeature.cpp
  3. * @brief convolutional feature
  4. * @author Sven Sickert
  5. * @date 10/13/2008
  6. */
  7. #include <iostream>
  8. #include "ConvolutionFeature.h"
  9. #include "vislearning/cbaselib/FeaturePool.h"
  10. #include "vislearning/baselib/cc.h"
  11. using namespace OBJREC;
  12. using namespace NICE;
  13. /* Convolutional feature consists of shift parameter beta[0] and the
  14. convolutional mask, which is stored in the rest of the parameter vector
  15. beta */
  16. /** simple constructor */
  17. ConvolutionFeature::ConvolutionFeature ( )
  18. {
  19. window_size_x = 15;
  20. window_size_y = 15;
  21. isColor = false;
  22. initializeParameterVector();
  23. }
  24. /** alternative constructor */
  25. ConvolutionFeature::ConvolutionFeature (
  26. const int wsize_x,
  27. const int wsize_y,
  28. const bool color )
  29. {
  30. window_size_x = wsize_x;
  31. window_size_y = wsize_y;
  32. isColor = color;
  33. initializeParameterVector();
  34. }
  35. /** default constructor */
  36. ConvolutionFeature::ConvolutionFeature ( const Config *conf )
  37. {
  38. std::string section = "ConvolutionFeature";
  39. window_size_x = conf->gI ( section, "window_size_x", 15 );
  40. window_size_y = conf->gI ( section, "window_size_y", 15 );
  41. isColor = conf->gB ( section, "is_color", false );
  42. initializeParameterVector();
  43. }
  44. /** copy constructor */
  45. ConvolutionFeature::ConvolutionFeature ( const ConvolutionFeature *confFeat )
  46. {
  47. window_size_x = confFeat->window_size_x;
  48. window_size_y = confFeat->window_size_y;
  49. betaLength = confFeat->betaLength;
  50. isColor = confFeat->isColor;
  51. beta = new NICE::Vector( betaLength, 0.0 );
  52. int i = 0;
  53. for ( NICE::Vector::iterator it = confFeat->beta->begin();
  54. it != confFeat->beta->end(); ++it, i++ )
  55. {
  56. beta[i] = *it;
  57. }
  58. }
  59. /** simple destructor */
  60. ConvolutionFeature::~ConvolutionFeature ( )
  61. {
  62. }
  63. /** (re)initialize parameter vector */
  64. void ConvolutionFeature::initializeParameterVector()
  65. {
  66. if (window_size_x > 0 && window_size_y > 0)
  67. {
  68. if (isColor)
  69. betaLength = 3*window_size_x*window_size_y + 1;
  70. else
  71. betaLength = window_size_x*window_size_y + 1;
  72. beta = new NICE::Vector( betaLength, (1.0/(double)(betaLength-1) ) );
  73. beta[0] = 1;
  74. }
  75. else
  76. std::cerr << "ConvolutionFeature::initializeVector: Size of window is Zero! Could not initialize..."
  77. << std::endl;
  78. }
  79. /** return parameter vector */
  80. NICE::Vector ConvolutionFeature::getParameterVector() const
  81. {
  82. NICE::Vector res = (*this->beta);
  83. return res;
  84. }
  85. /** return feature vector */
  86. NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) const
  87. {
  88. NICE::Vector vec(betaLength, 1.0);
  89. NICE::MultiChannelImageT<int> * img = NULL;
  90. if (isColor)
  91. img = & example->ce->getIChannel( CachedExample::I_COLOR );
  92. else
  93. img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  94. int xsize, ysize, x, y;
  95. example->ce->getImageSize( xsize, ysize );
  96. x = example->x;
  97. y = example->y;
  98. int halfwsx = std::floor ( window_size_x / 2 );
  99. int halfwsy = std::floor ( window_size_y / 2 );
  100. int step = window_size_x*window_size_y;
  101. int k = 1;
  102. for ( int v = -halfwsy; v <= halfwsy; v++ )
  103. for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
  104. {
  105. int uu = u;
  106. int vv = v;
  107. if (x+u < 0 || x+u >= xsize) uu=-u;
  108. if (y+v < 0 || y+v >= ysize) vv=-v;
  109. if ( x+uu > 0
  110. && x+uu < xsize
  111. && y+vv > 0
  112. && y+vv < ysize
  113. && k < vec.size() )
  114. {
  115. if (isColor)
  116. {
  117. // using HSV color space
  118. double h,s,v;
  119. double r = img->get(x+uu,y+vv,0);
  120. double g = img->get(x+uu,y+vv,1);
  121. double b = img->get(x+uu,y+vv,2);
  122. ColorConversion::ccRGBtoHSV(r, g, b, &h, &s, &v);
  123. vec[k] = h;
  124. vec[k+step] = s;
  125. vec[k+step+step] = v;
  126. }
  127. else
  128. {
  129. vec[k] = img->get(x+uu,y+vv);
  130. }
  131. }
  132. }
  133. return vec;
  134. }
  135. /** return length of parameter vector */
  136. int ConvolutionFeature::getParameterLength() const
  137. {
  138. return betaLength;
  139. }
  140. /** set parameter vector */
  141. void ConvolutionFeature::setParameterVector( const Vector & vec )
  142. {
  143. if ( beta->size() == vec.size() )
  144. {
  145. int i = 0;
  146. for ( NICE::Vector::iterator it = beta->begin();
  147. it != beta->end(); ++it, i++ )
  148. {
  149. *it = vec[i];
  150. }
  151. beta->normalizeL2();
  152. }
  153. else
  154. std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match!"
  155. << " expected: " << beta->size() << ", got: " << vec.size()
  156. << std::endl;
  157. }
  158. /** return feature value */
  159. double ConvolutionFeature::val ( const Example *example ) const
  160. {
  161. double val1 = 0.0;
  162. // is parameter vector and image data available?
  163. if (beta == NULL)
  164. {
  165. std::cerr << "ConvolutionalFeature::val: Missing parameter vector!"
  166. << std::endl;
  167. return val1;
  168. }
  169. NICE::MultiChannelImageT<int> * img = NULL;
  170. if (isColor)
  171. img = & example->ce->getIChannel( CachedExample::I_COLOR );
  172. else
  173. img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  174. int xsize, ysize, x, y;
  175. example->ce->getImageSize( xsize, ysize );
  176. x = example->x;
  177. y = example->y;
  178. int halfwsx = std::floor ( window_size_x / 2 );
  179. int halfwsy = std::floor ( window_size_y / 2 );
  180. int step = window_size_x*window_size_y;
  181. int k = 1;
  182. for ( int v = -halfwsy; v <= halfwsy; v++ )
  183. for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
  184. {
  185. int uu = u;
  186. int vv = v;
  187. if (x+u < 0 || x+u >= xsize) uu=-u;
  188. if (y+v < 0 || y+v >= ysize) vv=-v;
  189. if ( x+uu > 0
  190. && x+uu < xsize
  191. && y+vv > 0
  192. && y+vv < ysize
  193. && k < beta->size() )
  194. {
  195. if (isColor)
  196. {
  197. // using HSV color space
  198. double h,s,v;
  199. double r = img->get(x+uu,y+vv,0);
  200. double g = img->get(x+uu,y+vv,1);
  201. double b = img->get(x+uu,y+vv,2);
  202. ColorConversion::ccRGBtoHSV(r, g, b, &h, &s, &v);
  203. val1 += h * beta->operator [](k);
  204. val1 += s * beta->operator [](k+step);
  205. val1 += v * beta->operator [](k+step+step);
  206. }
  207. else
  208. {
  209. val1 += (double)img->get(x+uu,y+vv) * beta->operator [](k);
  210. }
  211. }
  212. }
  213. return val1;
  214. }
  215. /** creature feature pool */
  216. void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
  217. {
  218. ConvolutionFeature *f =
  219. new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
  220. featurePool.addFeature(f);
  221. }
  222. /** clone current feature */
  223. Feature *ConvolutionFeature::clone ( ) const
  224. {
  225. ConvolutionFeature *f =
  226. new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
  227. f->setParameterVector( *beta );
  228. return f;
  229. }
  230. Feature *ConvolutionFeature::generateFirstParameter () const
  231. {
  232. return clone();
  233. }
  234. void ConvolutionFeature::restore ( std::istream & is, int format )
  235. {
  236. is >> window_size_x;
  237. is >> window_size_y;
  238. is >> betaLength;
  239. isColor = false;
  240. if ( betaLength > (window_size_x*window_size_y+1) )
  241. isColor = true;
  242. beta = new NICE::Vector( betaLength, 1.0 );
  243. for ( NICE::Vector::iterator it = beta->begin();
  244. it != beta->end(); ++it )
  245. is >> *it;
  246. }
  247. void ConvolutionFeature::store ( std::ostream & os, int format ) const
  248. {
  249. os << "ConvolutionFeature "
  250. << window_size_x << " "
  251. << window_size_y << " "
  252. << betaLength;
  253. for ( NICE::Vector::const_iterator it = beta->begin();
  254. it != beta->end(); ++it )
  255. os << ' ' << *it;
  256. }
  257. void ConvolutionFeature::clear ()
  258. {
  259. beta->clear();
  260. }