CachedExample.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @file CachedExample.h
  3. * @brief data caching of several feature images and many more
  4. * @author Erik Rodner
  5. * @date 04/21/2008
  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/basics/FileMgt.h"
  14. namespace OBJREC
  15. {
  16. /** data caching of several feature images and many more,
  17. can be used in conjunction with Example
  18. to share image data between different sliding windows within
  19. an image
  20. @see Example */
  21. class CachedExample
  22. {
  23. protected:
  24. /** resize image to this fixed width */
  25. int newWidth;
  26. /** resize image to this fixed height */
  27. int newHeight;
  28. /** original image width */
  29. int oxsize;
  30. /** original image height */
  31. int oysize;
  32. /** filename of image */
  33. std::string imgfn;
  34. /** array of double images */
  35. NICE::MultiChannelImageT<double> *dchannels;
  36. /** array of integer images */
  37. NICE::MultiChannelImageT<int> *ichannels;
  38. /** array of histogram images */
  39. NICE::MultiChannelImageT<long> *lchannels;
  40. /** maps for temporary files */
  41. std::map<int, std::string> dtemps;
  42. std::map<int, std::string> itemps;
  43. std::map<int, std::string> ltemps;
  44. /** read standard image data from file */
  45. void readImageData ();
  46. /** read rgb image data from file */
  47. void readImageDataRGB ();
  48. /** calc grayvalue integral image */
  49. void calcIntegralImage ();
  50. /** array of histogram images */
  51. NICE::SparseVector **svmap;
  52. /** sizes of histogram images */
  53. int *svmap_xsize;
  54. int *svmap_ysize;
  55. bool hasColorInformation;
  56. public:
  57. /** whether one can obtain color information */
  58. bool colorInformationAvailable() const;
  59. enum
  60. {
  61. L_INTEGRALIMAGE = 0,
  62. L_NUMCHANNELS
  63. };
  64. /** integer channel types */
  65. enum
  66. {
  67. I_GRAYVALUES = 0,
  68. I_COLOR,
  69. I_EDGES,
  70. I_NUMCHANNELS
  71. };
  72. /** double value channels */
  73. enum
  74. {
  75. D_EOH = 0,
  76. D_INTEGRALPRIOR,
  77. D_INTEGRALEOH,
  78. D_INTEGRALCOLOR,
  79. D_NUMCHANNELS
  80. };
  81. /** sparse histogram channel types */
  82. enum
  83. {
  84. SVTEXTON = 0,
  85. SVNUMCHANNELS
  86. };
  87. /** default init method */
  88. void init ();
  89. /** simple constructor
  90. @param imgfn image filename
  91. @param newWidth resize raw image to this width
  92. @param newHeight resize raw image to this height
  93. */
  94. CachedExample ( const std::string & imgfn, int newWidth = -1,
  95. int newHeight = -1 );
  96. /** constructor (disabled buffering)
  97. @param img gray-value image
  98. */
  99. CachedExample ( const NICE::Image & img );
  100. /** constructor (disabled buffering)
  101. @param img rgb image
  102. @param disableGrayConversion whether to provide gray values or not
  103. */
  104. CachedExample ( const NICE::ColorImage & img, bool disableGrayConversion = false );
  105. /** simple destructor */
  106. virtual ~CachedExample();
  107. /**
  108. * get the NICE::Image Filename
  109. * @return NICE::Image Filename
  110. */
  111. inline std::string getFilename();
  112. /** get double image channel
  113. @param channel channel type (choose from enum type)
  114. @param[out] xsize width of image
  115. @param[out] ysize height of image
  116. @return buffer to image data
  117. */
  118. inline NICE::MultiChannelImageT<double> & getDChannel ( int channel );
  119. /** get integer image channel
  120. @param channel channel type (choose from enum type)
  121. @param[out] xsize width of image
  122. @param[out] ysize height of image
  123. @return buffer to image data
  124. */
  125. inline NICE::MultiChannelImageT<int> & getIChannel ( int channel );
  126. /** get long image channel
  127. @param channel channel type (choose from enum type)
  128. @param[out] xsize width of image
  129. @param[out] ysize height of image
  130. @return buffer to image data
  131. */
  132. inline NICE::MultiChannelImageT<long> & getLChannel ( int channel );
  133. /** get histogram image
  134. @param svchannel channel type (choose from histogram channel enum)
  135. @param[out] xsize width of raw image
  136. @param[out] ysize height of raw image
  137. @param[out] tm_xsize width of histogram channel buffer
  138. @param[out] tm_ysize height of histogram channel buffer
  139. @remark buffer will be not copied !!
  140. @return pointer to histogram channel buffer
  141. */
  142. NICE::SparseVector *getSVMap ( int svchannel, int & xsize, int & ysize, int & tm_xsize, int & tm_ysize ) const;
  143. /** assign histogram channel buffer and compute integral image
  144. @param svchannel
  145. @param _map pointer to histogram channel buffer
  146. @param xsize_s width of histogram channel buffer
  147. @param ysize_s height of histogram channel buffer
  148. @remark buffer will be not copied !!
  149. */
  150. void buildIntegralSV ( int svchannel, NICE::SparseVector *_map, int xsize_s, int ysize_s );
  151. /** assign histogram channel buffer
  152. @param svchannel
  153. @param _map pointer to histogram channel buffer
  154. @param xsize_s width of histogram channel buffer
  155. @param ysize_s height of histogram channel buffer
  156. @remark buffer will be not copied !!
  157. */
  158. void setSVMap ( int svchannel, NICE::SparseVector *_map, int xsize_s, int ysize_s );
  159. /** get image sizes */
  160. void getImageSize ( int & xsize, int & ysize ) const
  161. {
  162. xsize = oxsize;
  163. ysize = oysize;
  164. };
  165. /** drop precached data:
  166. (1) this is only possible if an image filename is given
  167. (2) only data channels are deleted that can be reproduced by CachedExample itself
  168. */
  169. void dropPreCached();
  170. template<class ImgPixelValue>
  171. void dropImages ( NICE::MultiChannelImageT<ImgPixelValue> *images,
  172. std::map<int, std::string> & temps,
  173. int numImages );
  174. };
  175. /********************** INLINE FUNCTIONS *****************************/
  176. inline NICE::MultiChannelImageT<double> & CachedExample::getDChannel ( int channel )
  177. {
  178. assert ( ( channel >= 0 ) && ( channel < D_NUMCHANNELS ) );
  179. if ( dchannels[channel].channels() == 0 )
  180. {
  181. std::map<int, std::string>::const_iterator j = dtemps.find ( channel );
  182. if ( j == dtemps.end() )
  183. {
  184. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel %s (double %d)!\n",
  185. // imgfn.c_str(), channel);
  186. }
  187. else
  188. {
  189. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s ", j->second.c_str() );
  190. dchannels[channel].restore ( j->second );
  191. //fprintf (stderr, "(%d x %d)\n", dchannels[channel].xsize, dchannels[channel].ysize );
  192. }
  193. }
  194. return dchannels[channel];
  195. }
  196. inline std::string CachedExample::getFilename()
  197. {
  198. return imgfn;
  199. }
  200. inline NICE::MultiChannelImageT<int> & CachedExample::getIChannel ( int channel )
  201. {
  202. assert ( ( channel >= 0 ) && ( channel < I_NUMCHANNELS ) );
  203. if ( ( ichannels[channel].channels() == 0 ) )
  204. {
  205. if ( ( imgfn != "" ) && ( channel == I_GRAYVALUES ) )
  206. {
  207. readImageData();
  208. }
  209. else if ( ( imgfn != "" ) && ( channel == I_COLOR ) )
  210. {
  211. readImageDataRGB();
  212. assert ( hasColorInformation );
  213. }
  214. else
  215. {
  216. std::map<int, std::string>::const_iterator j = itemps.find ( channel );
  217. if ( j == itemps.end() )
  218. {
  219. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel (int %d)!\n", channel);
  220. //exit(-1);
  221. }
  222. else
  223. {
  224. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
  225. ichannels[channel].restore ( j->second );
  226. }
  227. }
  228. }
  229. return ichannels[channel];
  230. }
  231. inline NICE::MultiChannelImageT<long> & CachedExample::getLChannel ( int channel )
  232. {
  233. assert ( ( channel >= 0 ) && ( channel < L_NUMCHANNELS ) );
  234. if ( lchannels[channel].channels() == 0 )
  235. {
  236. std::map<int, std::string>::const_iterator j = ltemps.find ( channel );
  237. if ( j == ltemps.end() )
  238. {
  239. if ( channel == L_INTEGRALIMAGE )
  240. {
  241. calcIntegralImage();
  242. }
  243. else
  244. {
  245. //fprintf (stderr, "NICE::MultiChannelImageT: unable to recover data channel (long %d)!\n", channel);
  246. //exit(-1);
  247. }
  248. }
  249. else
  250. {
  251. //fprintf (stderr, "NICE::MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
  252. lchannels[channel].restore ( j->second );
  253. }
  254. }
  255. return lchannels[channel];
  256. }
  257. template<class ImgPixelValue>
  258. void CachedExample::dropImages ( NICE::MultiChannelImageT<ImgPixelValue> *images, std::map<int, std::string> & temps, int numImages )
  259. {
  260. for ( int i = 0 ; i < numImages; i++ )
  261. {
  262. std::map<int, std::string>::iterator j = temps.find ( i );
  263. if ( j == temps.end() )
  264. {
  265. std::string tempfilename = NICE::FileMgt::createTempFile ( "tmp/cachedexample_%s" );
  266. //fprintf (stderr, "CachedExample: dumping channel %d/%d to %s (%d x %d)\n", i, numImages, tempfilename.c_str(),
  267. // images[i].xsize, images[i].ysize );
  268. images[i].store ( tempfilename );
  269. temps[i] = tempfilename;
  270. }
  271. images[i].freeData();
  272. }
  273. }
  274. } // namespace
  275. #endif