CachedExample.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/SparseVector.h"
  12. #include "core/image/MultiChannelImageT.h"
  13. #include "core/basics/FileMgt.h"
  14. using namespace NICE;
  15. namespace OBJREC {
  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. MultiChannelImageT<double> *dchannels;
  36. /** array of integer images */
  37. MultiChannelImageT<int> *ichannels;
  38. /** array of histogram images */
  39. 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. 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. L_INTEGRALIMAGE = 0,
  61. L_NUMCHANNELS
  62. };
  63. /** integer channel types */
  64. enum {
  65. I_GRAYVALUES = 0,
  66. I_COLOR,
  67. I_EDGES,
  68. I_NUMCHANNELS
  69. };
  70. /** double value channels */
  71. enum {
  72. D_EOH = 0,
  73. D_INTEGRALPRIOR,
  74. D_INTEGRALEOH,
  75. D_INTEGRALCOLOR,
  76. D_NUMCHANNELS
  77. };
  78. /** sparse histogram channel types */
  79. enum {
  80. SVTEXTON = 0,
  81. SVNUMCHANNELS
  82. };
  83. /** default init method */
  84. void init ();
  85. /** simple constructor
  86. @param imgfn image filename
  87. @param newWidth resize raw image to this width
  88. @param newHeight resize raw image to this height
  89. */
  90. CachedExample( const std::string & imgfn, int newWidth = -1,
  91. int newHeight = -1 );
  92. /** constructor (disabled buffering)
  93. @param img gray-value image
  94. */
  95. CachedExample( const NICE::Image & img );
  96. /** constructor (disabled buffering)
  97. @param img rgb image
  98. @param disableGrayConversion whether to provide gray values or not
  99. */
  100. CachedExample( const NICE::ColorImage & img, bool disableGrayConversion = false );
  101. /** simple destructor */
  102. virtual ~CachedExample();
  103. /**
  104. * get the NICE::Image Filename
  105. * @return NICE::Image Filename
  106. */
  107. inline std::string getFilename();
  108. /** get double image channel
  109. @param channel channel type (choose from enum type)
  110. @param[out] xsize width of image
  111. @param[out] ysize height of image
  112. @return buffer to image data
  113. */
  114. inline MultiChannelImageT<double> & getDChannel ( int channel );
  115. /** get integer image channel
  116. @param channel channel type (choose from enum type)
  117. @param[out] xsize width of image
  118. @param[out] ysize height of image
  119. @return buffer to image data
  120. */
  121. inline MultiChannelImageT<int> & getIChannel ( int channel );
  122. /** get long image channel
  123. @param channel channel type (choose from enum type)
  124. @param[out] xsize width of image
  125. @param[out] ysize height of image
  126. @return buffer to image data
  127. */
  128. inline MultiChannelImageT<long> & getLChannel ( int channel );
  129. /** get histogram image
  130. @param svchannel channel type (choose from histogram channel enum)
  131. @param[out] xsize width of raw image
  132. @param[out] ysize height of raw image
  133. @param[out] tm_xsize width of histogram channel buffer
  134. @param[out] tm_ysize height of histogram channel buffer
  135. @remark buffer will be not copied !!
  136. @return pointer to histogram channel buffer
  137. */
  138. SparseVector *getSVMap ( int svchannel, int & xsize, int & ysize, int & tm_xsize, int & tm_ysize ) const;
  139. /** assign histogram channel buffer and compute integral image
  140. @param svchannel
  141. @param _map pointer to histogram channel buffer
  142. @param xsize_s width of histogram channel buffer
  143. @param ysize_s height of histogram channel buffer
  144. @remark buffer will be not copied !!
  145. */
  146. void buildIntegralSV ( int svchannel, SparseVector *_map, int xsize_s, int ysize_s );
  147. /** assign histogram channel buffer
  148. @param svchannel
  149. @param _map pointer to histogram channel buffer
  150. @param xsize_s width of histogram channel buffer
  151. @param ysize_s height of histogram channel buffer
  152. @remark buffer will be not copied !!
  153. */
  154. void setSVMap ( int svchannel, SparseVector *_map, int xsize_s, int ysize_s );
  155. /** get image sizes */
  156. void getImageSize ( int & xsize, int & ysize ) const { xsize = oxsize; ysize = oysize; };
  157. /** drop precached data:
  158. (1) this is only possible if an image filename is given
  159. (2) only data channels are deleted that can be reproduced by CachedExample itself
  160. */
  161. void dropPreCached();
  162. template<class ImgPixelValue>
  163. void dropImages ( MultiChannelImageT<ImgPixelValue> *images,
  164. std::map<int, std::string> & temps,
  165. int numImages );
  166. };
  167. /********************** INLINE FUNCTIONS *****************************/
  168. inline MultiChannelImageT<double> & CachedExample::getDChannel ( int channel )
  169. {
  170. assert ( (channel >= 0) && (channel < D_NUMCHANNELS) );
  171. if ( dchannels[channel].data == NULL ) {
  172. std::map<int, std::string>::const_iterator j = dtemps.find(channel);
  173. if ( j == dtemps.end() ) {
  174. //fprintf (stderr, "MultiChannelImageT: unable to recover data channel %s (double %d)!\n",
  175. // imgfn.c_str(), channel);
  176. } else {
  177. //fprintf (stderr, "MultiChannelImageT: restoring data from %s ", j->second.c_str() );
  178. dchannels[channel].restore ( j->second );
  179. //fprintf (stderr, "(%d x %d)\n", dchannels[channel].xsize, dchannels[channel].ysize );
  180. }
  181. }
  182. return dchannels[channel];
  183. }
  184. inline std::string CachedExample::getFilename()
  185. {
  186. return imgfn;
  187. }
  188. inline MultiChannelImageT<int> & CachedExample::getIChannel ( int channel )
  189. {
  190. assert ( (channel >= 0) && (channel < I_NUMCHANNELS) );
  191. if ( (ichannels[channel].data == NULL) )
  192. {
  193. if ( (imgfn != "") && (channel == I_GRAYVALUES) )
  194. {
  195. readImageData();
  196. } else if ( (imgfn != "") && (channel == I_COLOR) ) {
  197. readImageDataRGB();
  198. assert ( hasColorInformation );
  199. } else {
  200. std::map<int, std::string>::const_iterator j = itemps.find(channel);
  201. if ( j == itemps.end() ) {
  202. //fprintf (stderr, "MultiChannelImageT: unable to recover data channel (int %d)!\n", channel);
  203. //exit(-1);
  204. } else {
  205. //fprintf (stderr, "MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
  206. ichannels[channel].restore ( j->second );
  207. }
  208. }
  209. }
  210. return ichannels[channel];
  211. }
  212. inline MultiChannelImageT<long> & CachedExample::getLChannel ( int channel )
  213. {
  214. assert ( (channel >= 0) && (channel < L_NUMCHANNELS) );
  215. if ( lchannels[channel].data == NULL ) {
  216. std::map<int, std::string>::const_iterator j = ltemps.find(channel);
  217. if ( j == ltemps.end() ) {
  218. if ( channel == L_INTEGRALIMAGE ) {
  219. calcIntegralImage();
  220. } else {
  221. //fprintf (stderr, "MultiChannelImageT: unable to recover data channel (long %d)!\n", channel);
  222. //exit(-1);
  223. }
  224. } else {
  225. //fprintf (stderr, "MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
  226. lchannels[channel].restore ( j->second );
  227. }
  228. }
  229. return lchannels[channel];
  230. }
  231. template<class ImgPixelValue>
  232. void CachedExample::dropImages ( MultiChannelImageT<ImgPixelValue> *images, std::map<int, std::string> & temps, int numImages )
  233. {
  234. for ( int i = 0 ; i < numImages; i++ )
  235. {
  236. std::map<int, std::string>::iterator j = temps.find(i);
  237. if ( j == temps.end() )
  238. {
  239. std::string tempfilename = FileMgt::createTempFile("tmp/cachedexample_%s");
  240. //fprintf (stderr, "CachedExample: dumping channel %d/%d to %s (%d x %d)\n", i, numImages, tempfilename.c_str(),
  241. // images[i].xsize, images[i].ysize );
  242. images[i].store ( tempfilename );
  243. temps[i] = tempfilename;
  244. }
  245. images[i].freeData();
  246. }
  247. }
  248. } // namespace
  249. #endif