CachedExample.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * @file CachedExample.cpp
  3. * @brief data caching
  4. * @author Erik Rodner
  5. * @date 04/21/2008
  6. */
  7. #include <iostream>
  8. #include "vislearning/cbaselib/CachedExample.h"
  9. #include "vislearning/baselib/Conversions.h"
  10. #include "vislearning/baselib/ProgressBar.h"
  11. #include "vislearning/image/GenericImageTools.h"
  12. #include "vislearning/baselib/Preprocess.h"
  13. #include "vislearning/baselib/ICETools.h"
  14. using namespace OBJREC;
  15. using namespace std;
  16. using namespace NICE;
  17. void CachedExample::init ()
  18. {
  19. dchannels = new MultiChannelImageT<double> [D_NUMCHANNELS];
  20. ichannels = new MultiChannelImageT<int> [I_NUMCHANNELS];
  21. lchannels = new MultiChannelImageT<long> [L_NUMCHANNELS];
  22. svmap = new SparseVector *[SVNUMCHANNELS];
  23. svmap_xsize = new int [SVNUMCHANNELS];
  24. svmap_ysize = new int [SVNUMCHANNELS];
  25. for ( uint k = 0 ; k < SVNUMCHANNELS ; k++ )
  26. {
  27. svmap[k] = NULL;
  28. svmap_xsize[k] = 0;
  29. svmap_ysize[k] = 0;
  30. }
  31. }
  32. CachedExample::CachedExample ( const std::string & _imgfn,
  33. int _newWidth,
  34. int _newHeight )
  35. {
  36. imgfn = _imgfn;
  37. newWidth = _newWidth;
  38. newHeight = _newHeight;
  39. Preprocess::getImageSize ( _imgfn, oxsize, oysize );
  40. init();
  41. hasColorInformation = true;
  42. }
  43. CachedExample::CachedExample ( const NICE::Image & _img )
  44. {
  45. imgfn = "";
  46. newWidth = -1;
  47. newHeight = -1;
  48. init();
  49. oxsize = _img.width();
  50. oysize = _img.height();
  51. ichannels[I_GRAYVALUES].freeData();
  52. ichannels[I_GRAYVALUES].addChannel(_img);
  53. hasColorInformation = false;
  54. }
  55. CachedExample::CachedExample ( const NICE::ColorImage & img, bool disableGrayConversion )
  56. {
  57. imgfn = "";
  58. oxsize = img.width();
  59. oysize = img.height();
  60. newWidth = -1;
  61. newHeight = -1;
  62. init();
  63. if ( ! disableGrayConversion )
  64. {
  65. // refactor-nice.pl: check this substitution
  66. // old: Image imggray;
  67. NICE::Image imggray;
  68. ICETools::calcGrayImage ( img, imggray );
  69. ichannels[I_GRAYVALUES].freeData();
  70. ichannels[I_GRAYVALUES].addChannel(imggray);
  71. }
  72. ichannels[I_COLOR].reInit ( oxsize, oysize, 3);
  73. for ( int y = 0 ; y < oysize ; y++ )
  74. for ( int x = 0 ; x < oxsize ; x++ )
  75. {
  76. ichannels[I_COLOR](x,y,0) = img.getPixel ( x, y, 0 );
  77. ichannels[I_COLOR](x,y,1) = img.getPixel ( x, y, 1 );
  78. ichannels[I_COLOR](x,y,2) = img.getPixel ( x, y, 2 );
  79. }
  80. hasColorInformation = true;
  81. }
  82. CachedExample::~CachedExample()
  83. {
  84. delete [] dchannels;
  85. delete [] ichannels;
  86. delete [] lchannels;
  87. for ( uint k = 0 ; k < SVNUMCHANNELS ; k++ )
  88. if ( svmap[k] != NULL )
  89. delete [] ( svmap[k] );
  90. delete [] svmap;
  91. delete [] svmap_xsize;
  92. delete [] svmap_ysize;
  93. // remove all temporary files
  94. for ( map<int, string>::const_iterator j = dtemps.begin();
  95. j != dtemps.end();
  96. j++ )
  97. {
  98. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  99. FileMgt::deleteTempFile ( j->second );
  100. }
  101. for ( map<int, string>::const_iterator j = itemps.begin();
  102. j != itemps.end();
  103. j++ )
  104. {
  105. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  106. FileMgt::deleteTempFile ( j->second );
  107. }
  108. for ( map<int, string>::const_iterator j = ltemps.begin();
  109. j != ltemps.end();
  110. j++ )
  111. {
  112. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  113. FileMgt::deleteTempFile ( j->second );
  114. }
  115. }
  116. void CachedExample::readImageData ()
  117. {
  118. if ( imgfn == "" ) return;
  119. NICE::Image orig = Preprocess::ReadImgAdv ( imgfn );
  120. NICE::Image imggray;
  121. if ( newWidth > 0 )
  122. Conversions::resizeImage ( orig, imggray, newWidth, newHeight );
  123. else
  124. imggray = orig;
  125. oxsize = imggray.width();
  126. oysize = imggray.height();
  127. ichannels[I_GRAYVALUES].freeData();
  128. ichannels[I_GRAYVALUES].addChannel(imggray);
  129. }
  130. void CachedExample::readImageDataRGB ()
  131. {
  132. if ( imgfn == "" ) return;
  133. NICE::ColorImage img;
  134. try {
  135. img = Preprocess::ReadImgAdvRGB ( imgfn );
  136. } catch ( NICE::ImageException & ) {
  137. fprintf ( stderr, "error reading rgb image %s\n", imgfn.c_str() );
  138. hasColorInformation = false;
  139. return;
  140. }
  141. oxsize = img.width();
  142. oysize = img.height();
  143. hasColorInformation = true;
  144. ichannels[I_COLOR].reInit ( oxsize, oysize, 3);
  145. for ( int y = 0 ; y < oysize ; y++ )
  146. for ( int x = 0 ; x < oxsize ; x++)
  147. {
  148. ichannels[I_COLOR](x,y,0) = img.getPixel ( x, y, 0 );
  149. ichannels[I_COLOR](x,y,1) = img.getPixel ( x, y, 1 );
  150. ichannels[I_COLOR](x,y,2) = img.getPixel ( x, y, 2 );
  151. }
  152. }
  153. void CachedExample::calcIntegralImage ()
  154. {
  155. if ( ichannels[I_GRAYVALUES].width() == 0 )
  156. {
  157. readImageData ();
  158. if ( ichannels[I_GRAYVALUES].width() == 0 )
  159. {
  160. fprintf ( stderr, "CachedExample::getChannel: unable to recover data channel\n" );
  161. exit ( -1 );
  162. }
  163. }
  164. lchannels[L_INTEGRALIMAGE].reInit ( ichannels[I_GRAYVALUES].width(),
  165. ichannels[I_GRAYVALUES].height(),
  166. 1);
  167. ImageT<long int> tmp = lchannels[L_INTEGRALIMAGE][0];
  168. GenericImageTools::calcIntegralImage (
  169. tmp,
  170. ichannels[I_GRAYVALUES][0],
  171. ichannels[I_GRAYVALUES].width(),
  172. ichannels[I_GRAYVALUES].height() );
  173. }
  174. void CachedExample::buildIntegralSV ( int svchannel,
  175. SparseVector *_map,
  176. int xsize_s, int ysize_s )
  177. {
  178. SparseVector *map = _map;
  179. svmap[svchannel] = _map;
  180. svmap_xsize[svchannel] = xsize_s;
  181. svmap_ysize[svchannel] = ysize_s;
  182. int k = xsize_s;
  183. for ( int y = 1 ; y < ysize_s; y++, k += xsize_s )
  184. map[k].add ( ( map[k-xsize_s] ) );
  185. k = 1;
  186. for ( int x = 1 ; x < xsize_s; x++, k++ )
  187. map[k].add ( ( map[k-1] ) );
  188. k = xsize_s + 1;
  189. for ( int y = 1 ; y < ysize_s ; y++, k++ )
  190. {
  191. for ( int x = 1 ; x < xsize_s ; x++, k++ )
  192. {
  193. map[k].add ( ( map[k-1] ) );
  194. map[k].add ( ( map[k-xsize_s] ) );
  195. map[k].add ( ( map[k-xsize_s-1] ), -1.0 );
  196. }
  197. }
  198. }
  199. void CachedExample::setSVMap ( int svchannel,
  200. SparseVector *_map,
  201. int xsize_s, int ysize_s )
  202. {
  203. svmap[svchannel] = _map;
  204. svmap_xsize[svchannel] = xsize_s;
  205. svmap_ysize[svchannel] = ysize_s;
  206. }
  207. SparseVector *CachedExample::getSVMap ( int svchannel,
  208. int & _xsize, int & _ysize,
  209. int & _tm_xsize, int & _tm_ysize ) const
  210. {
  211. _xsize = oxsize;
  212. _ysize = oysize;
  213. _tm_xsize = svmap_xsize[svchannel];
  214. _tm_ysize = svmap_ysize[svchannel];
  215. assert ( svmap[svchannel] != NULL );
  216. return svmap[svchannel];
  217. }
  218. bool CachedExample::colorInformationAvailable() const
  219. {
  220. if ( hasColorInformation ) return true;
  221. else {
  222. if ( imgfn.size() == 0 ) return false;
  223. // int tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr;
  224. // refactor: InfImgFile ( imgfn, tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr );
  225. ImageFile imgf ( imgfn );
  226. const ImageFile::Header & imgfheader = imgf.getHeader();
  227. // tmp_xsize = imgfheader.width;
  228. // tmp_ysize = imgfheader.height;
  229. // tmp_maxval = 255;
  230. int tmp_nr = imgfheader.channel;
  231. if ( tmp_nr > 1 ) return true;
  232. else return false;
  233. }
  234. }
  235. void CachedExample::dropPreCached()
  236. {
  237. dropImages<double> ( dchannels, dtemps, D_NUMCHANNELS );
  238. dropImages<int> ( ichannels, itemps, I_NUMCHANNELS );
  239. dropImages<long> ( lchannels, ltemps, L_NUMCHANNELS );
  240. }