CachedExample.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. * @file CachedExample.cpp
  3. * @brief data caching
  4. * @author Erik Rodner, Sven Sickert
  5. * @date 04/21/2008 (modified 03/18/2016)
  6. */
  7. #include <iostream>
  8. #include "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. void CachedExample::init ()
  16. {
  17. dchannels = new NICE::MultiChannelImageT<double> [D_NUMCHANNELS];
  18. ichannels = new NICE::MultiChannelImageT<int> [I_NUMCHANNELS];
  19. lchannels = new NICE::MultiChannelImageT<long> [L_NUMCHANNELS];
  20. dchannels3 = new NICE::MultiChannelImage3DT<double> [D_NUMCHANNELS];
  21. ichannels3 = new NICE::MultiChannelImage3DT<int> [I_NUMCHANNELS];
  22. lchannels3 = new NICE::MultiChannelImage3DT<long> [L_NUMCHANNELS];
  23. svmap = new NICE::SparseVector *[SVNUMCHANNELS];
  24. svmap_xsize = new int [SVNUMCHANNELS];
  25. svmap_ysize = new int [SVNUMCHANNELS];
  26. for ( uint k = 0 ; k < SVNUMCHANNELS ; k++ )
  27. {
  28. svmap[k] = NULL;
  29. svmap_xsize[k] = 0;
  30. svmap_ysize[k] = 0;
  31. }
  32. }
  33. CachedExample::CachedExample ( const std::string & _imgfn,
  34. int _newWidth,
  35. int _newHeight )
  36. {
  37. imgfn.push_back(_imgfn);
  38. newWidth = _newWidth;
  39. newHeight = _newHeight;
  40. newDepth = 1;
  41. Preprocess::getImageSize ( _imgfn, oxsize, oysize );
  42. ozsize = 1;
  43. init();
  44. hasColorInformation = true;
  45. }
  46. CachedExample::CachedExample (
  47. const std::vector<std::string> & _imgfn,
  48. int _newWidth,
  49. int _newHeight,
  50. int _newDepth )
  51. {
  52. imgfn = _imgfn;
  53. newWidth = _newWidth;
  54. newHeight = _newHeight;
  55. newDepth = _newDepth;
  56. Preprocess::getImageSize ( _imgfn[0], oxsize, oysize );
  57. ozsize = _imgfn.size();
  58. init();
  59. hasColorInformation = true;
  60. }
  61. CachedExample::CachedExample ( const NICE::Image & _img )
  62. {
  63. imgfn.push_back("");
  64. newWidth = -1;
  65. newHeight = -1;
  66. newDepth = -1;
  67. init();
  68. oxsize = _img.width();
  69. oysize = _img.height();
  70. ozsize = 1;
  71. ichannels[I_GRAYVALUES].freeData();
  72. ichannels[I_GRAYVALUES].addChannel(_img);
  73. hasColorInformation = false;
  74. }
  75. CachedExample::CachedExample (
  76. const NICE::MultiChannelImageT<int> & _img )
  77. {
  78. newWidth = -1;
  79. newHeight = -1;
  80. newDepth = -1;
  81. init();
  82. oxsize = _img.width();
  83. oysize = _img.height();
  84. ozsize = _img.channels();
  85. for ( unsigned int i = 0; i < ozsize; i++ )
  86. imgfn.push_back("");
  87. ichannels3[I_GRAYVALUES].freeData();
  88. ichannels3[I_GRAYVALUES].addChannel(_img);
  89. hasColorInformation = false;
  90. }
  91. CachedExample::CachedExample ( const NICE::ColorImage & img, bool disableGrayConversion )
  92. {
  93. imgfn.push_back("");
  94. oxsize = img.width();
  95. oysize = img.height();
  96. ozsize = 1;
  97. newWidth = -1;
  98. newHeight = -1;
  99. newDepth = -1;
  100. init();
  101. if ( ! disableGrayConversion )
  102. {
  103. // refactor-nice.pl: check this substitution
  104. // old: Image imggray;
  105. NICE::Image imggray;
  106. ICETools::calcGrayImage ( img, imggray );
  107. ichannels[I_GRAYVALUES].freeData();
  108. ichannels[I_GRAYVALUES].addChannel(imggray);
  109. }
  110. ichannels[I_COLOR].reInit ( oxsize, oysize, 3);
  111. for ( int y = 0 ; y < oysize ; y++ )
  112. for ( int x = 0 ; x < oxsize ; x++ )
  113. {
  114. ichannels[I_COLOR](x,y,0) = img.getPixel ( x, y, 0 );
  115. ichannels[I_COLOR](x,y,1) = img.getPixel ( x, y, 1 );
  116. ichannels[I_COLOR](x,y,2) = img.getPixel ( x, y, 2 );
  117. }
  118. hasColorInformation = true;
  119. }
  120. CachedExample::CachedExample (
  121. const NICE::MultiChannelImage3DT<int> & img,
  122. bool disableGrayConversion )
  123. {
  124. oxsize = img.width();
  125. oysize = img.height();
  126. ozsize = img.depth();
  127. newWidth = -1;
  128. newHeight = -1;
  129. newDepth = -1;
  130. for ( unsigned int i = 0; i < ozsize; i++ )
  131. imgfn.push_back("");
  132. init();
  133. int ochannels = img.channels();
  134. if ( ! disableGrayConversion && (ochannels > 2) )
  135. {
  136. ichannels3[I_GRAYVALUES].freeData();
  137. ichannels3[I_GRAYVALUES].addChannel(1);
  138. for ( int z = 0; z < ozsize; z++ )
  139. {
  140. NICE::Image imggray;
  141. ICETools::calcGrayImage ( img.getColor(z), imggray );
  142. for ( int y = 0; y < oysize; y++ )
  143. for ( int x = 0; x < oxsize; x++ )
  144. ichannels3[I_GRAYVALUES](x,y,z,0) = imggray.getPixel(x,y);
  145. }
  146. }
  147. ichannels3[I_COLOR].addChannel ( img );
  148. hasColorInformation = true;
  149. }
  150. CachedExample::~CachedExample()
  151. {
  152. delete [] dchannels;
  153. delete [] ichannels;
  154. delete [] lchannels;
  155. delete [] dchannels3;
  156. delete [] ichannels3;
  157. delete [] lchannels3;
  158. for ( uint k = 0 ; k < SVNUMCHANNELS ; k++ )
  159. if ( svmap[k] != NULL )
  160. delete [] ( svmap[k] );
  161. delete [] svmap;
  162. delete [] svmap_xsize;
  163. delete [] svmap_ysize;
  164. // remove all temporary files
  165. for ( std::map<int, std::string>::const_iterator j = dtemps.begin();
  166. j != dtemps.end();
  167. j++ )
  168. {
  169. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  170. NICE::FileMgt::deleteTempFile ( j->second );
  171. }
  172. for ( std::map<int, std::string>::const_iterator j = itemps.begin();
  173. j != itemps.end();
  174. j++ )
  175. {
  176. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  177. NICE::FileMgt::deleteTempFile ( j->second );
  178. }
  179. for ( std::map<int, std::string>::const_iterator j = ltemps.begin();
  180. j != ltemps.end();
  181. j++ )
  182. {
  183. //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
  184. NICE::FileMgt::deleteTempFile ( j->second );
  185. }
  186. }
  187. void CachedExample::readImageData ()
  188. {
  189. ozsize = imgfn.size();
  190. if ( ozsize == 0 || imgfn[0] == "" )
  191. return;
  192. for ( int z = 0; z < ozsize; z++ )
  193. {
  194. NICE::Image orig = Preprocess::ReadImgAdv ( imgfn[z] );
  195. NICE::Image imggray;
  196. if ( newWidth > 0 )
  197. Conversions::resizeImage ( orig, imggray, newWidth, newHeight );
  198. else
  199. imggray = orig;
  200. if ( z == 0 )
  201. {
  202. oxsize = imggray.width();
  203. oysize = imggray.height();
  204. if ( ozsize > 1 )
  205. ichannels3[I_GRAYVALUES].reInit( oxsize, oysize, ozsize, 1);
  206. else
  207. {
  208. ichannels[I_GRAYVALUES].freeData();
  209. ichannels[I_GRAYVALUES].addChannel(imggray);
  210. }
  211. }
  212. if ( ozsize > 1 )
  213. for ( int y = 0; y < oysize; y++ )
  214. for ( int x = 0; x < oxsize; x++ )
  215. ichannels3[I_GRAYVALUES](x,y,z,0) = imggray.getPixel(x,y);
  216. }
  217. }
  218. void CachedExample::readImageDataRGB ()
  219. {
  220. ozsize = imgfn.size();
  221. if ( ozsize == 0 || imgfn[0] == "" )
  222. return;
  223. for ( int z = 0; z < ozsize; z++ )
  224. {
  225. NICE::ColorImage img;
  226. try {
  227. img = Preprocess::ReadImgAdvRGB ( imgfn[z] );
  228. } catch ( NICE::ImageException & ) {
  229. fprintf ( stderr, "error reading rgb image %s\n", imgfn[z].c_str() );
  230. hasColorInformation = false;
  231. return;
  232. }
  233. if ( z == 0 )
  234. {
  235. oxsize = img.width();
  236. oysize = img.height();
  237. hasColorInformation = true;
  238. if ( ozsize > 1 )
  239. ichannels3[I_COLOR].reInit ( oxsize, oysize, ozsize, 3);
  240. else
  241. ichannels[I_COLOR].reInit ( oxsize, oysize, 3);
  242. }
  243. for ( int y = 0; y < oysize; y++ )
  244. for ( int x = 0; x < oxsize; x++ )
  245. {
  246. if ( ozsize > 1 )
  247. {
  248. ichannels3[I_COLOR](x,y,z,0) = img.getPixel(x,y,0);
  249. ichannels3[I_COLOR](x,y,z,1) = img.getPixel(x,y,1);
  250. ichannels3[I_COLOR](x,y,z,2) = img.getPixel(x,y,2);
  251. }
  252. else
  253. {
  254. ichannels[I_COLOR](x,y,0) = img.getPixel ( x, y, 0 );
  255. ichannels[I_COLOR](x,y,1) = img.getPixel ( x, y, 1 );
  256. ichannels[I_COLOR](x,y,2) = img.getPixel ( x, y, 2 );
  257. }
  258. }
  259. }
  260. }
  261. void CachedExample::calcIntegralImage ()
  262. {
  263. // in case of standard 2D images
  264. if ( ozsize == 1 )
  265. {
  266. if ( ichannels[I_GRAYVALUES].width() == 0 )
  267. {
  268. readImageData ();
  269. if ( ichannels[I_GRAYVALUES].width() == 0 )
  270. {
  271. fprintf ( stderr, "CachedExample::getChannel: unable to recover data channel\n" );
  272. exit ( -1 );
  273. }
  274. }
  275. lchannels[L_INTEGRALIMAGE].reInit (
  276. ichannels[I_GRAYVALUES].width(),
  277. ichannels[I_GRAYVALUES].height(),
  278. 1 );
  279. NICE::ImageT<long int> tmp = lchannels[L_INTEGRALIMAGE][0];
  280. GenericImageTools::calcIntegralImage (
  281. tmp,
  282. ichannels[I_GRAYVALUES][0],
  283. ichannels[I_GRAYVALUES].width(),
  284. ichannels[I_GRAYVALUES].height() );
  285. }
  286. // in case of a 3D images
  287. else
  288. {
  289. if ( ichannels[I_GRAYVALUES].width() == 0 )
  290. {
  291. readImageData ();
  292. if ( ichannels[I_GRAYVALUES].width() == 0 )
  293. {
  294. fprintf ( stderr, "CachedExample::getChannel: unable to recover data channel\n" );
  295. exit ( -1 );
  296. }
  297. }
  298. int nwidth = ichannels3[I_GRAYVALUES].width();
  299. int nheight = ichannels3[I_GRAYVALUES].height();
  300. int ndepth = ichannels3[I_GRAYVALUES].depth();
  301. int nchannels = ichannels3[I_GRAYVALUES].channels();
  302. lchannels3[L_INTEGRALIMAGE].reInit( nwidth, nheight, ndepth, nchannels);
  303. for ( int c = 0; c < nchannels; c++ )
  304. {
  305. for ( int z = 0; z < ndepth; z++ )
  306. for ( int y = 0; y < nheight; y++ )
  307. for ( int x = 0; x < nwidth; x++ )
  308. {
  309. lchannels3[L_INTEGRALIMAGE](x,y,z,c) =
  310. (long)ichannels3[L_INTEGRALIMAGE].get(x,y,z,c);
  311. }
  312. lchannels3[L_INTEGRALIMAGE].calcIntegral(c);
  313. }
  314. }
  315. }
  316. void CachedExample::buildIntegralSV ( int svchannel,
  317. NICE::SparseVector *_map,
  318. int xsize_s, int ysize_s )
  319. {
  320. NICE::SparseVector *map = _map;
  321. svmap[svchannel] = _map;
  322. svmap_xsize[svchannel] = xsize_s;
  323. svmap_ysize[svchannel] = ysize_s;
  324. int k = xsize_s;
  325. for ( int y = 1 ; y < ysize_s; y++, k += xsize_s )
  326. map[k].add ( ( map[k-xsize_s] ) );
  327. k = 1;
  328. for ( int x = 1 ; x < xsize_s; x++, k++ )
  329. map[k].add ( ( map[k-1] ) );
  330. k = xsize_s + 1;
  331. for ( int y = 1 ; y < ysize_s ; y++, k++ )
  332. {
  333. for ( int x = 1 ; x < xsize_s ; x++, k++ )
  334. {
  335. map[k].add ( ( map[k-1] ) );
  336. map[k].add ( ( map[k-xsize_s] ) );
  337. map[k].add ( ( map[k-xsize_s-1] ), -1.0 );
  338. }
  339. }
  340. }
  341. void CachedExample::setSVMap ( int svchannel,
  342. NICE::SparseVector *_map,
  343. int xsize_s, int ysize_s )
  344. {
  345. svmap[svchannel] = _map;
  346. svmap_xsize[svchannel] = xsize_s;
  347. svmap_ysize[svchannel] = ysize_s;
  348. }
  349. NICE::SparseVector *CachedExample::getSVMap ( int svchannel,
  350. int & _xsize, int & _ysize,
  351. int & _tm_xsize, int & _tm_ysize ) const
  352. {
  353. _xsize = oxsize;
  354. _ysize = oysize;
  355. _tm_xsize = svmap_xsize[svchannel];
  356. _tm_ysize = svmap_ysize[svchannel];
  357. assert ( svmap[svchannel] != NULL );
  358. return svmap[svchannel];
  359. }
  360. bool CachedExample::colorInformationAvailable() const
  361. {
  362. if ( hasColorInformation ) return true;
  363. else {
  364. if ( imgfn.size() == 0 ) return false;
  365. // int tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr;
  366. // refactor: InfImgFile ( imgfn, tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr );
  367. NICE::ImageFile imgf ( imgfn[0] );
  368. const NICE::ImageFile::Header & imgfheader = imgf.getHeader();
  369. // tmp_xsize = imgfheader.width;
  370. // tmp_ysize = imgfheader.height;
  371. // tmp_maxval = 255;
  372. int tmp_nr = imgfheader.channel;
  373. if ( tmp_nr > 1 ) return true;
  374. else return false;
  375. }
  376. }
  377. void CachedExample::dropPreCached()
  378. {
  379. dropImages<double> ( dchannels, dtemps, D_NUMCHANNELS );
  380. dropImages<int> ( ichannels, itemps, I_NUMCHANNELS );
  381. dropImages<long> ( lchannels, ltemps, L_NUMCHANNELS );
  382. }