CachedExample.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. int *gray = new int [ oxsize*oysize ];
  52. int k = 0;
  53. for ( int y = 0 ; y < oysize ; y++ )
  54. for ( int x = 0 ; x < oxsize ; x++,k++ )
  55. gray[k] = _img.getPixel(x,y);
  56. ichannels[I_GRAYVALUES].reInit ( oxsize, oysize, 1, false );
  57. ichannels[I_GRAYVALUES].setImage ( gray, oxsize, oysize, 0 );
  58. hasColorInformation = false;
  59. }
  60. CachedExample::CachedExample( const NICE::ColorImage & img, bool disableGrayConversion )
  61. {
  62. imgfn = "";
  63. oxsize = img.width();
  64. oysize = img.height();
  65. newWidth = -1;
  66. newHeight = -1;
  67. init();
  68. if ( ! disableGrayConversion )
  69. {
  70. // refactor-nice.pl: check this substitution
  71. // old: Image imggray;
  72. NICE::Image imggray;
  73. ICETools::calcGrayImage ( img, imggray );
  74. ichannels[I_GRAYVALUES].reInit ( oxsize, oysize, 1, true );
  75. int *gray = ichannels[I_GRAYVALUES].data[0];
  76. int k = 0;
  77. for ( int y = 0 ; y < oysize ; y++ )
  78. for ( int x = 0 ; x < oxsize ; x++,k++ )
  79. // refactor-nice.pl: check this substitution
  80. // old: gray[k] = GetVal(imggray,x,y);
  81. gray[k] = imggray.getPixel(x,y);
  82. }
  83. ichannels[I_COLOR].reInit ( oxsize, oysize, 3, true );
  84. long int k = 0;
  85. for ( int y = 0 ; y < oysize ; y++ )
  86. for ( int x = 0 ; x < oxsize ; x++,k++ )
  87. {
  88. // refactor-nice.pl: check this substitution
  89. // old: ichannels[I_COLOR].data[0][k] = GetVal(img.RedImage(), x, y);
  90. ichannels[I_COLOR].data[0][k] = img.getPixel(x,y,0);
  91. // refactor-nice.pl: check this substitution
  92. // old: ichannels[I_COLOR].data[1][k] = GetVal(img.GreenImage(), x, y);
  93. ichannels[I_COLOR].data[1][k] = img.getPixel(x,y,1);
  94. // refactor-nice.pl: check this substitution
  95. // old: ichannels[I_COLOR].data[2][k] = GetVal(img.BlueImage(), x, y);
  96. ichannels[I_COLOR].data[2][k] = img.getPixel(x,y,2);
  97. }
  98. hasColorInformation = true;
  99. }
  100. CachedExample::~CachedExample()
  101. {
  102. delete [] dchannels;
  103. delete [] ichannels;
  104. delete [] lchannels;
  105. for ( uint k = 0 ; k < SVNUMCHANNELS ; k++ )
  106. if ( svmap[k] != NULL )
  107. delete [] (svmap[k]);
  108. delete [] svmap;
  109. delete [] svmap_xsize;
  110. delete [] svmap_ysize;
  111. // remove all temporary files
  112. for ( map<int, string>::const_iterator j = dtemps.begin();
  113. j != dtemps.end();
  114. j++ )
  115. {
  116. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  117. FileMgt::deleteTempFile ( j->second );
  118. }
  119. for ( map<int, string>::const_iterator j = itemps.begin();
  120. j != itemps.end();
  121. j++ )
  122. {
  123. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  124. FileMgt::deleteTempFile ( j->second );
  125. }
  126. for ( map<int, string>::const_iterator j = ltemps.begin();
  127. j != ltemps.end();
  128. j++ )
  129. {
  130. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  131. FileMgt::deleteTempFile ( j->second );
  132. }
  133. }
  134. void CachedExample::readImageData ()
  135. {
  136. if ( imgfn == "" ) return;
  137. NICE::Image orig = Preprocess::ReadImgAdv(imgfn);
  138. NICE::Image imggray;
  139. if ( newWidth > 0 )
  140. Conversions::resizeImage ( orig, imggray, newWidth, newHeight );
  141. else
  142. imggray = orig;
  143. oxsize = imggray.width();
  144. oysize = imggray.height();
  145. ichannels[I_GRAYVALUES].reInit ( oxsize, oysize, 1, true );
  146. int *gray = ichannels[I_GRAYVALUES].data[0];
  147. int k = 0;
  148. for ( int y = 0 ; y < oysize ; y++ )
  149. for ( int x = 0 ; x < oxsize ; x++,k++ )
  150. gray[k] = imggray.getPixel(x,y);
  151. }
  152. void CachedExample::readImageDataRGB ()
  153. {
  154. if ( imgfn == "" ) return;
  155. NICE::ColorImage img;
  156. try {
  157. img = Preprocess::ReadImgAdvRGB(imgfn);
  158. } catch ( NICE::ImageException & ) {
  159. fprintf (stderr, "error reading rgb image %s\n", imgfn.c_str());
  160. hasColorInformation = false;
  161. return;
  162. }
  163. oxsize = img.width();
  164. oysize = img.height();
  165. hasColorInformation = true;
  166. ichannels[I_COLOR].reInit ( oxsize, oysize, 3, true );
  167. long k = 0;
  168. for ( int y = 0 ; y < oysize ; y++ )
  169. for ( int x = 0 ; x < oxsize ; x++,k++ )
  170. {
  171. ichannels[I_COLOR].data[0][k] = img.getPixel(x,y,0);
  172. ichannels[I_COLOR].data[1][k] = img.getPixel(x,y,1);
  173. ichannels[I_COLOR].data[2][k] = img.getPixel(x,y,2);
  174. }
  175. }
  176. void CachedExample::calcIntegralImage ()
  177. {
  178. if (ichannels[I_GRAYVALUES].xsize == 0)
  179. {
  180. readImageData ();
  181. if ( ichannels[I_GRAYVALUES].xsize == 0 )
  182. {
  183. fprintf (stderr, "CachedExample::getChannel: unable to recover data channel\n");
  184. exit(-1);
  185. }
  186. }
  187. lchannels[L_INTEGRALIMAGE].reInit ( ichannels[I_GRAYVALUES].xsize,
  188. ichannels[I_GRAYVALUES].ysize,
  189. 1, true );
  190. GenericImageTools::calcIntegralImage (
  191. lchannels[L_INTEGRALIMAGE].data[0],
  192. ichannels[I_GRAYVALUES].data[0],
  193. ichannels[I_GRAYVALUES].xsize,
  194. ichannels[I_GRAYVALUES].ysize );
  195. }
  196. void CachedExample::buildIntegralSV ( int svchannel,
  197. SparseVector *_map,
  198. int xsize_s, int ysize_s )
  199. {
  200. SparseVector *map = _map;
  201. svmap[svchannel] = _map;
  202. svmap_xsize[svchannel] = xsize_s;
  203. svmap_ysize[svchannel] = ysize_s;
  204. int k = xsize_s;
  205. for ( int y = 1 ; y < ysize_s; y++, k+=xsize_s )
  206. map[k].add ( (map[k-xsize_s]) );
  207. k = 1;
  208. for ( int x = 1 ; x < xsize_s; x++, k++ )
  209. map[k].add ( (map[k-1]) );
  210. k = xsize_s + 1;
  211. for ( int y = 1 ; y < ysize_s ; y++,k++ )
  212. {
  213. for ( int x = 1 ; x < xsize_s ; x++,k++ )
  214. {
  215. map[k].add ( (map[k-1]) );
  216. map[k].add ( (map[k-xsize_s]) );
  217. map[k].add ( (map[k-xsize_s-1]), -1.0 );
  218. }
  219. }
  220. }
  221. void CachedExample::setSVMap ( int svchannel,
  222. SparseVector *_map,
  223. int xsize_s, int ysize_s )
  224. {
  225. svmap[svchannel] = _map;
  226. svmap_xsize[svchannel] = xsize_s;
  227. svmap_ysize[svchannel] = ysize_s;
  228. }
  229. SparseVector *CachedExample::getSVMap ( int svchannel,
  230. int & _xsize, int & _ysize,
  231. int & _tm_xsize, int & _tm_ysize ) const
  232. {
  233. _xsize = oxsize;
  234. _ysize = oysize;
  235. _tm_xsize = svmap_xsize[svchannel];
  236. _tm_ysize = svmap_ysize[svchannel];
  237. assert ( svmap[svchannel] != NULL );
  238. return svmap[svchannel];
  239. }
  240. bool CachedExample::colorInformationAvailable() const
  241. {
  242. if ( hasColorInformation ) return true;
  243. else {
  244. if ( imgfn.size() == 0 ) return false;
  245. int tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr;
  246. // refactor: InfImgFile ( imgfn, tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr );
  247. ImageFile imgf ( imgfn );
  248. const ImageFile::Header & imgfheader = imgf.getHeader();
  249. tmp_xsize = imgfheader.width;
  250. tmp_ysize = imgfheader.height;
  251. tmp_maxval = 255;
  252. tmp_nr = imgfheader.channel;
  253. if ( tmp_nr > 1 ) return true;
  254. else return false;
  255. }
  256. }
  257. void CachedExample::dropPreCached()
  258. {
  259. dropImages<double> ( dchannels, dtemps, D_NUMCHANNELS );
  260. dropImages<int> ( ichannels, itemps, I_NUMCHANNELS );
  261. dropImages<long> ( lchannels, ltemps, L_NUMCHANNELS );
  262. }