CachedExample.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. * @file CachedExample.h
  3. * @brief data caching of several feature images and many more
  4. * @author Erik Rodner, Sven Sickert
  5. * @date 04/21/2008 (modified 03/18/2016)
  6. */
  7. #ifndef CACHEDEXAMPLEINCLUDE
  8. #define CACHEDEXAMPLEINCLUDE
  9. #include <assert.h>
  10. #include <map>
  11. #include "core/vector/SparseVectorT.h"
  12. #include "core/image/MultiChannelImageT.h"
  13. #include "core/image/MultiChannelImage3DT.h"
  14. #include "core/basics/FileMgt.h"
  15. namespace OBJREC
  16. {
  17. /** data caching of several feature images and many more,
  18. can be used in conjunction with Example
  19. to share image data between different sliding windows within
  20. an image
  21. @see Example */
  22. class CachedExample
  23. {
  24. protected:
  25. /** resize image to this fixed width */
  26. int newWidth;
  27. /** resize image to this fixed height */
  28. int newHeight;
  29. /** resize image to this fixed depth */
  30. int newDepth;
  31. /** original image width */
  32. int oxsize;
  33. /** original image height */
  34. int oysize;
  35. /** original image depth */
  36. int ozsize;
  37. /** list of filenames of images */
  38. std::vector<std::string> imgfn;
  39. /** array of double images */
  40. // NICE::MultiChannelImageT<double> *dchannels;
  41. NICE::MultiChannelImage3DT<double> *dchannels;
  42. /** array of integer images */
  43. // NICE::MultiChannelImageT<int> *ichannels;
  44. NICE::MultiChannelImage3DT<int> *ichannels;
  45. /** array of histogram images */
  46. // NICE::MultiChannelImageT<long> *lchannels;
  47. NICE::MultiChannelImage3DT<long> *lchannels;
  48. /** maps for temporary files */
  49. std::map<int, std::string> dtemps;
  50. std::map<int, std::string> itemps;
  51. std::map<int, std::string> ltemps;
  52. /** read standard image data from file */
  53. void readImageData ();
  54. /** read rgb image data from file */
  55. void readImageDataRGB ();
  56. /** calc grayvalue integral image */
  57. void calcIntegralImage ();
  58. /** array of histogram images */
  59. NICE::SparseVector **svmap;
  60. /** sizes of histogram images */
  61. int *svmap_xsize;
  62. int *svmap_ysize;
  63. bool hasColorInformation;
  64. public:
  65. /** whether one can obtain color information */
  66. bool colorInformationAvailable() const;
  67. enum
  68. {
  69. L_INTEGRALIMAGE = 0,
  70. L_NUMCHANNELS
  71. };
  72. /** integer channel types */
  73. enum
  74. {
  75. I_GRAYVALUES = 0,
  76. I_COLOR,
  77. I_EDGES,
  78. I_NUMCHANNELS
  79. };
  80. /** double value channels */
  81. enum
  82. {
  83. D_EOH = 0,
  84. D_INTEGRALPRIOR,
  85. D_INTEGRALEOH,
  86. D_INTEGRALCOLOR,
  87. D_NUMCHANNELS
  88. };
  89. /** sparse histogram channel types */
  90. enum
  91. {
  92. SVTEXTON = 0,
  93. SVNUMCHANNELS
  94. };
  95. /** default init method */
  96. void init ();
  97. /** simple constructor
  98. @param imgfn image filename
  99. @param newWidth resize raw image to this width
  100. @param newHeight resize raw image to this height
  101. */
  102. CachedExample ( const std::string & imgfn,
  103. int newWidth = -1,
  104. int newHeight = -1 );
  105. /** simple 3d constructor
  106. @param imgfn image filename
  107. @param newWidth resize raw image to this width
  108. @param newHeight resize raw image to this height
  109. @param newDepth resize raw image to this depth
  110. */
  111. CachedExample ( const std::vector<std::string> & imgfn,
  112. int newWidth = -1,
  113. int newHeight = -1,
  114. int newDepth = -1 );
  115. /** constructor (disabled buffering)
  116. @param img gray-value image
  117. */
  118. CachedExample ( const NICE::Image & img );
  119. /** constructor (disabled buffering)
  120. @param img gray-value multi channel image
  121. */
  122. CachedExample ( const NICE::MultiChannelImageT<int> & img );
  123. /** constructor (disabled buffering)
  124. @param img rgb image
  125. @param disableGrayConversion whether to provide gray values or not
  126. */
  127. CachedExample ( const NICE::ColorImage & img, bool disableGrayConversion = false );
  128. /** constructor (disabled buffering)
  129. @param img multi channel image 3D
  130. @param disableGrayConversion whether to provide gray values or not
  131. */
  132. CachedExample ( const NICE::MultiChannelImage3DT<int> & img,
  133. bool disableGrayConversion = false );
  134. /** simple destructor */
  135. virtual ~CachedExample();
  136. /**
  137. * get the NICE::Image Filename
  138. * @return NICE::Image Filename
  139. */
  140. inline std::string getFilename(const int z = 0);
  141. /**
  142. * @brief get amount of images
  143. * @return amount of images
  144. */
  145. inline int getNumImages ();
  146. /** get double image channel
  147. @param channel channel type (choose from enum type)
  148. @return buffer to image data
  149. */
  150. // inline NICE::MultiChannelImageT<double> & getDChannel ( int channel );
  151. /** get double image channel 3d
  152. @param channel channel type (choose from enum type)
  153. @return buffer to image data
  154. */
  155. inline NICE::MultiChannelImage3DT<double> & getDChannel ( int channel );
  156. /** get integer image channel
  157. @param channel channel type (choose from enum type)
  158. @param[out] xsize width of image
  159. @param[out] ysize height of image
  160. @return buffer to image data
  161. */
  162. // inline NICE::MultiChannelImageT<int> & getIChannel ( int channel );
  163. /** get integer image channel 3d
  164. @param channel channel type (choose from enum type)
  165. @return buffer to image data
  166. */
  167. inline NICE::MultiChannelImage3DT<int> & getIChannel ( int channel );
  168. /** get long image channel
  169. @param channel channel type (choose from enum type)
  170. @param[out] xsize width of image
  171. @param[out] ysize height of image
  172. @return buffer to image data
  173. */
  174. // inline NICE::MultiChannelImageT<long> & getLChannel ( int channel );
  175. /** get long image channel 3d
  176. @param channel channel type (choose from enum type)
  177. @return buffer to image data
  178. */
  179. inline NICE::MultiChannelImage3DT<long> & getLChannel ( int channel );
  180. /** get histogram image
  181. @param svchannel channel type (choose from histogram channel enum)
  182. @param[out] xsize width of raw image
  183. @param[out] ysize height of raw image
  184. @param[out] tm_xsize width of histogram channel buffer
  185. @param[out] tm_ysize height of histogram channel buffer
  186. @remark buffer will be not copied !!
  187. @return pointer to histogram channel buffer
  188. */
  189. NICE::SparseVector *getSVMap ( int svchannel, int & xsize, int & ysize, int & tm_xsize, int & tm_ysize ) const;
  190. /** assign histogram channel buffer and compute integral image
  191. @param svchannel
  192. @param _map pointer to histogram channel buffer
  193. @param xsize_s width of histogram channel buffer
  194. @param ysize_s height of histogram channel buffer
  195. @remark buffer will be not copied !!
  196. */
  197. void buildIntegralSV ( int svchannel, NICE::SparseVector *_map, int xsize_s, int ysize_s );
  198. /** assign histogram channel buffer
  199. @param svchannel
  200. @param _map pointer to histogram channel buffer
  201. @param xsize_s width of histogram channel buffer
  202. @param ysize_s height of histogram channel buffer
  203. @remark buffer will be not copied !!
  204. */
  205. void setSVMap ( int svchannel, NICE::SparseVector *_map, int xsize_s, int ysize_s );
  206. /** get image sizes */
  207. void getImageSize ( int & xsize, int & ysize ) const
  208. {
  209. xsize = oxsize;
  210. ysize = oysize;
  211. }
  212. /** get image sizes 3d */
  213. void getImageSize3 ( int & xsize, int & ysize, int & zsize ) const
  214. {
  215. xsize = oxsize;
  216. ysize = oysize;
  217. zsize = ozsize;
  218. }
  219. /** drop precached data:
  220. (1) this is only possible if an image filename is given
  221. (2) only data channels are deleted that can be reproduced by CachedExample itself
  222. */
  223. void dropPreCached();
  224. template<class ImgPixelValue>
  225. void dropImages ( NICE::MultiChannelImage3DT<ImgPixelValue> *images,
  226. std::map<int, std::string> & temps,
  227. int numImages );
  228. };
  229. /********************** INLINE FUNCTIONS *****************************/
  230. inline std::string CachedExample::getFilename( const int z )
  231. {
  232. return imgfn[z];
  233. }
  234. inline int CachedExample::getNumImages ()
  235. {
  236. return imgfn.size();
  237. }
  238. /*
  239. inline NICE::MultiChannelImageT<double> & CachedExample::getDChannel ( int channel )
  240. {
  241. assert ( ( channel >= 0 ) && ( channel < D_NUMCHANNELS ) );
  242. if ( dchannels[channel].channels() == 0 )
  243. {
  244. std::map<int, std::string>::const_iterator j = dtemps.find ( channel );
  245. if ( j == dtemps.end() )
  246. {
  247. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel %s (double %d)!\n",
  248. // imgfn.c_str(), channel);
  249. }
  250. else
  251. {
  252. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s ", j->second.c_str() );
  253. dchannels[channel].restore ( j->second );
  254. //fprintf (stderr, "(%d x %d)\n", dchannels[channel].xsize, dchannels[channel].ysize );
  255. }
  256. }
  257. return dchannels[channel];
  258. }
  259. */
  260. inline NICE::MultiChannelImage3DT<double> & CachedExample::getDChannel ( int channel )
  261. {
  262. assert ( ( channel >= 0 ) && ( channel < D_NUMCHANNELS ) );
  263. if ( dchannels[channel].channels() == 0 )
  264. {
  265. std::map<int, std::string>::const_iterator j = dtemps.find ( channel );
  266. if ( j == dtemps.end() )
  267. {
  268. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel %s (double %d)!\n",
  269. //imgfn[0].c_str(), channel);
  270. }
  271. else
  272. {
  273. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s ", j->second.c_str() );
  274. dchannels[channel].restore ( j->second );
  275. //fprintf (stderr, "(%d x %d)\n", dchannels[channel].xsize, dchannels[channel].ysize );
  276. }
  277. }
  278. return dchannels[channel];
  279. }
  280. /*
  281. inline NICE::MultiChannelImageT<int> & CachedExample::getIChannel ( int channel )
  282. {
  283. assert ( ( channel >= 0 ) && ( channel < I_NUMCHANNELS ) );
  284. if ( ( ichannels[channel].channels() == 0 ) )
  285. {
  286. if ( ( imgfn[0] != "" ) && ( channel == I_GRAYVALUES ) )
  287. {
  288. readImageData();
  289. }
  290. else if ( ( imgfn[0] != "" ) && ( channel == I_COLOR ) )
  291. {
  292. readImageDataRGB();
  293. assert ( hasColorInformation );
  294. }
  295. else
  296. {
  297. std::map<int, std::string>::const_iterator j = itemps.find ( channel );
  298. if ( j == itemps.end() )
  299. {
  300. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel (int %d)!\n", channel);
  301. //exit(-1);
  302. }
  303. else
  304. {
  305. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
  306. ichannels[channel].restore ( j->second );
  307. }
  308. }
  309. }
  310. return ichannels[channel];
  311. }
  312. */
  313. inline NICE::MultiChannelImage3DT<int> & CachedExample::getIChannel ( int channel )
  314. {
  315. assert ( ( channel >= 0 ) && ( channel < I_NUMCHANNELS ) );
  316. if ( ( ichannels[channel].channels() == 0 ) )
  317. {
  318. if ( ( imgfn[0] != "" ) && ( channel == I_GRAYVALUES ) )
  319. {
  320. readImageData();
  321. }
  322. else if ( ( imgfn[0] != "" ) && ( channel == I_COLOR ) )
  323. {
  324. readImageDataRGB();
  325. assert ( hasColorInformation );
  326. }
  327. else
  328. {
  329. std::map<int, std::string>::const_iterator j = itemps.find ( channel );
  330. if ( j == itemps.end() )
  331. {
  332. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel (int %d)!\n", channel);
  333. //exit(-1);
  334. }
  335. else
  336. {
  337. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
  338. ichannels[channel].restore ( j->second );
  339. }
  340. }
  341. }
  342. return ichannels[channel];
  343. }
  344. /*
  345. inline NICE::MultiChannelImageT<long> & CachedExample::getLChannel ( int channel )
  346. {
  347. assert ( ( channel >= 0 ) && ( channel < L_NUMCHANNELS ) );
  348. if ( lchannels[channel].channels() == 0 )
  349. {
  350. std::map<int, std::string>::const_iterator j = ltemps.find ( channel );
  351. if ( j == ltemps.end() )
  352. {
  353. if ( channel == L_INTEGRALIMAGE )
  354. {
  355. calcIntegralImage();
  356. }
  357. else
  358. {
  359. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel (long %d)!\n", channel);
  360. //exit(-1);
  361. }
  362. }
  363. else
  364. {
  365. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
  366. lchannels[channel].restore ( j->second );
  367. }
  368. }
  369. return lchannels[channel];
  370. }
  371. */
  372. inline NICE::MultiChannelImage3DT<long> & CachedExample::getLChannel ( int channel )
  373. {
  374. assert ( ( channel >= 0 ) && ( channel < L_NUMCHANNELS ) );
  375. if ( lchannels[channel].channels() == 0 )
  376. {
  377. std::map<int, std::string>::const_iterator j = ltemps.find ( channel );
  378. if ( j == ltemps.end() )
  379. {
  380. if ( channel == L_INTEGRALIMAGE )
  381. {
  382. calcIntegralImage();
  383. }
  384. else
  385. {
  386. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel (long %d)!\n", channel);
  387. //exit(-1);
  388. }
  389. }
  390. else
  391. {
  392. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
  393. lchannels[channel].restore ( j->second );
  394. }
  395. }
  396. return lchannels[channel];
  397. }
  398. template<class ImgPixelValue>
  399. void CachedExample::dropImages ( NICE::MultiChannelImage3DT<ImgPixelValue> *images, std::map<int, std::string> & temps, int numImages )
  400. {
  401. for ( int i = 0 ; i < numImages; i++ )
  402. {
  403. std::map<int, std::string>::iterator j = temps.find ( i );
  404. if ( j == temps.end() )
  405. {
  406. std::string tempfilename = NICE::FileMgt::createTempFile ( "tmp/cachedexample_%s" );
  407. //fprintf (stderr, "CachedExample: dumping channel %d/%d to %s (%d x %d)\n", i, numImages, tempfilename.c_str(),
  408. // images[i].xsize, images[i].ysize );
  409. images[i].store ( tempfilename );
  410. temps[i] = tempfilename;
  411. }
  412. images[i].freeData();
  413. }
  414. }
  415. } // namespace
  416. #endif