CachedExample.cpp 12 KB

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