CachedExample.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 (
  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. ichannels3[I_GRAYVALUES].freeData();
  89. ichannels3[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) = img.getPixel ( x, y, 0 );
  118. ichannels[I_COLOR](x,y,1) = img.getPixel ( x, y, 1 );
  119. ichannels[I_COLOR](x,y,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. ichannels3[I_GRAYVALUES].freeData();
  140. ichannels3[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. ichannels3[I_GRAYVALUES](x,y,z,0) = imggray.getPixel(x,y);
  148. }
  149. }
  150. ichannels3[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. ichannels3[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. ichannels3[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. ichannels3[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. ichannels3[I_COLOR](x,y,z,0) = img.getPixel(x,y,0);
  247. ichannels3[I_COLOR](x,y,z,1) = img.getPixel(x,y,1);
  248. ichannels3[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. if ( ozsize == 1 )
  259. {
  260. if ( ichannels[I_GRAYVALUES].width() == 0 )
  261. {
  262. readImageData ();
  263. if ( ichannels[I_GRAYVALUES].width() == 0 )
  264. {
  265. fprintf ( stderr, "CachedExample::getChannel: unable to recover data channel\n" );
  266. exit ( -1 );
  267. }
  268. }
  269. lchannels[L_INTEGRALIMAGE].reInit (
  270. ichannels[I_GRAYVALUES].width(),
  271. ichannels[I_GRAYVALUES].height(),
  272. 1 );
  273. NICE::ImageT<long int> tmp = lchannels[L_INTEGRALIMAGE][0];
  274. GenericImageTools::calcIntegralImage (
  275. tmp,
  276. ichannels[I_GRAYVALUES][0],
  277. ichannels[I_GRAYVALUES].width(),
  278. ichannels[I_GRAYVALUES].height() );
  279. }
  280. // in case of a 3D images
  281. else
  282. {
  283. if ( ichannels[I_GRAYVALUES].width() == 0 )
  284. {
  285. readImageData ();
  286. if ( ichannels[I_GRAYVALUES].width() == 0 )
  287. {
  288. fprintf ( stderr, "CachedExample::getChannel: unable to recover data channel\n" );
  289. exit ( -1 );
  290. }
  291. }
  292. int nwidth = ichannels3[I_GRAYVALUES].width();
  293. int nheight = ichannels3[I_GRAYVALUES].height();
  294. int ndepth = ichannels3[I_GRAYVALUES].depth();
  295. int nchannels = ichannels3[I_GRAYVALUES].channels();
  296. lchannels3[L_INTEGRALIMAGE].reInit( nwidth, nheight, ndepth, nchannels);
  297. for ( int c = 0; c < nchannels; c++ )
  298. {
  299. for ( int z = 0; z < ndepth; z++ )
  300. for ( int y = 0; y < nheight; y++ )
  301. for ( int x = 0; x < nwidth; x++ )
  302. {
  303. lchannels3[L_INTEGRALIMAGE](x,y,z,c) =
  304. (long)ichannels3[L_INTEGRALIMAGE].get(x,y,z,c);
  305. }
  306. lchannels3[L_INTEGRALIMAGE].calcIntegral(c);
  307. }
  308. }
  309. }
  310. void CachedExample::buildIntegralSV (
  311. int svchannel,
  312. NICE::SparseVector *_map,
  313. int xsize_s, int ysize_s )
  314. {
  315. NICE::SparseVector *map = _map;
  316. svmap[svchannel] = _map;
  317. svmap_xsize[svchannel] = xsize_s;
  318. svmap_ysize[svchannel] = ysize_s;
  319. int k = xsize_s;
  320. for ( int y = 1 ; y < ysize_s; y++, k += xsize_s )
  321. map[k].add ( ( map[k-xsize_s] ) );
  322. k = 1;
  323. for ( int x = 1 ; x < xsize_s; x++, k++ )
  324. map[k].add ( ( map[k-1] ) );
  325. k = xsize_s + 1;
  326. for ( int y = 1 ; y < ysize_s ; y++, k++ )
  327. {
  328. for ( int x = 1 ; x < xsize_s ; x++, k++ )
  329. {
  330. map[k].add ( ( map[k-1] ) );
  331. map[k].add ( ( map[k-xsize_s] ) );
  332. map[k].add ( ( map[k-xsize_s-1] ), -1.0 );
  333. }
  334. }
  335. }
  336. void CachedExample::setSVMap (
  337. int svchannel,
  338. NICE::SparseVector *_map,
  339. int xsize_s, int ysize_s )
  340. {
  341. svmap[svchannel] = _map;
  342. svmap_xsize[svchannel] = xsize_s;
  343. svmap_ysize[svchannel] = ysize_s;
  344. }
  345. NICE::SparseVector *CachedExample::getSVMap ( int svchannel,
  346. int & _xsize, int & _ysize,
  347. int & _tm_xsize, int & _tm_ysize ) const
  348. {
  349. _xsize = oxsize;
  350. _ysize = oysize;
  351. _tm_xsize = svmap_xsize[svchannel];
  352. _tm_ysize = svmap_ysize[svchannel];
  353. assert ( svmap[svchannel] != NULL );
  354. return svmap[svchannel];
  355. }
  356. bool CachedExample::colorInformationAvailable() const
  357. {
  358. if ( hasColorInformation ) return true;
  359. else {
  360. if ( imgfn.size() == 0 ) return false;
  361. // int tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr;
  362. // refactor: InfImgFile ( imgfn, tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr );
  363. NICE::ImageFile imgf ( imgfn[0] );
  364. const NICE::ImageFile::Header & imgfheader = imgf.getHeader();
  365. // tmp_xsize = imgfheader.width;
  366. // tmp_ysize = imgfheader.height;
  367. // tmp_maxval = 255;
  368. int tmp_nr = imgfheader.channel;
  369. if ( tmp_nr > 1 ) return true;
  370. else return false;
  371. }
  372. }
  373. void CachedExample::dropPreCached()
  374. {
  375. dropImages<double> ( dchannels, dtemps, D_NUMCHANNELS );
  376. dropImages<int> ( ichannels, itemps, I_NUMCHANNELS );
  377. dropImages<long> ( lchannels, ltemps, L_NUMCHANNELS );
  378. }