123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- /**
- * @file CachedExample.h
- * @brief data caching of several feature images and many more
- * @author Erik Rodner
- * @date 04/21/2008
- */
- #ifndef CACHEDEXAMPLEINCLUDE
- #define CACHEDEXAMPLEINCLUDE
- #include <assert.h>
- #include <map>
- #include "core/vector/SparseVector.h"
- #include "core/image/MultiChannelImageT.h"
- #include "core/basics/FileMgt.h"
- using namespace NICE;
- namespace OBJREC {
- /** data caching of several feature images and many more,
- can be used in conjunction with Example
- to share image data between different sliding windows within
- an image
- @see Example */
- class CachedExample
- {
- protected:
- /** resize image to this fixed width */
- int newWidth;
- /** resize image to this fixed height */
- int newHeight;
- /** original image width */
- int oxsize;
- /** original image height */
- int oysize;
- /** filename of image */
- std::string imgfn;
- /** array of double images */
- MultiChannelImageT<double> *dchannels;
- /** array of integer images */
- MultiChannelImageT<int> *ichannels;
- /** array of histogram images */
- MultiChannelImageT<long> *lchannels;
- /** maps for temporary files */
- std::map<int, std::string> dtemps;
- std::map<int, std::string> itemps;
- std::map<int, std::string> ltemps;
- /** read standard image data from file */
- void readImageData ();
-
- /** read rgb image data from file */
- void readImageDataRGB ();
- /** calc grayvalue integral image */
- void calcIntegralImage ();
- /** array of histogram images */
- SparseVector **svmap;
-
- /** sizes of histogram images */
- int *svmap_xsize;
- int *svmap_ysize;
- bool hasColorInformation;
- public:
- /** whether one can obtain color information */
- bool colorInformationAvailable() const;
- enum {
- L_INTEGRALIMAGE = 0,
- L_NUMCHANNELS
- };
- /** integer channel types */
- enum {
- I_GRAYVALUES = 0,
- I_COLOR,
- I_EDGES,
- I_NUMCHANNELS
- };
- /** double value channels */
- enum {
- D_EOH = 0,
- D_INTEGRALPRIOR,
- D_INTEGRALEOH,
- D_INTEGRALCOLOR,
- D_NUMCHANNELS
- };
-
- /** sparse histogram channel types */
- enum {
- SVTEXTON = 0,
- SVNUMCHANNELS
- };
- /** default init method */
- void init ();
- /** simple constructor
- @param imgfn image filename
- @param newWidth resize raw image to this width
- @param newHeight resize raw image to this height
- */
- CachedExample( const std::string & imgfn, int newWidth = -1,
- int newHeight = -1 );
-
- /** constructor (disabled buffering)
- @param img gray-value image
- */
- CachedExample( const NICE::Image & img );
-
- /** constructor (disabled buffering)
- @param img rgb image
- @param disableGrayConversion whether to provide gray values or not
- */
- CachedExample( const NICE::ColorImage & img, bool disableGrayConversion = false );
- /** simple destructor */
- virtual ~CachedExample();
- /**
- * get the NICE::Image Filename
- * @return NICE::Image Filename
- */
- inline std::string getFilename();
-
-
- /** get double image channel
- @param channel channel type (choose from enum type)
- @param[out] xsize width of image
- @param[out] ysize height of image
- @return buffer to image data
- */
- inline MultiChannelImageT<double> & getDChannel ( int channel );
- /** get integer image channel
- @param channel channel type (choose from enum type)
- @param[out] xsize width of image
- @param[out] ysize height of image
- @return buffer to image data
- */
- inline MultiChannelImageT<int> & getIChannel ( int channel );
- /** get long image channel
- @param channel channel type (choose from enum type)
- @param[out] xsize width of image
- @param[out] ysize height of image
- @return buffer to image data
- */
- inline MultiChannelImageT<long> & getLChannel ( int channel );
- /** get histogram image
- @param svchannel channel type (choose from histogram channel enum)
- @param[out] xsize width of raw image
- @param[out] ysize height of raw image
- @param[out] tm_xsize width of histogram channel buffer
- @param[out] tm_ysize height of histogram channel buffer
- @remark buffer will be not copied !!
- @return pointer to histogram channel buffer
- */
- SparseVector *getSVMap ( int svchannel, int & xsize, int & ysize, int & tm_xsize, int & tm_ysize ) const;
- /** assign histogram channel buffer and compute integral image
- @param svchannel
- @param _map pointer to histogram channel buffer
- @param xsize_s width of histogram channel buffer
- @param ysize_s height of histogram channel buffer
- @remark buffer will be not copied !!
- */
- void buildIntegralSV ( int svchannel, SparseVector *_map, int xsize_s, int ysize_s );
-
- /** assign histogram channel buffer
- @param svchannel
- @param _map pointer to histogram channel buffer
- @param xsize_s width of histogram channel buffer
- @param ysize_s height of histogram channel buffer
- @remark buffer will be not copied !!
- */
- void setSVMap ( int svchannel, SparseVector *_map, int xsize_s, int ysize_s );
- /** get image sizes */
- void getImageSize ( int & xsize, int & ysize ) const { xsize = oxsize; ysize = oysize; };
- /** drop precached data:
- (1) this is only possible if an image filename is given
- (2) only data channels are deleted that can be reproduced by CachedExample itself
- */
- void dropPreCached();
- template<class ImgPixelValue>
- void dropImages ( MultiChannelImageT<ImgPixelValue> *images,
- std::map<int, std::string> & temps,
- int numImages );
- };
- /********************** INLINE FUNCTIONS *****************************/
- inline MultiChannelImageT<double> & CachedExample::getDChannel ( int channel )
- {
- assert ( (channel >= 0) && (channel < D_NUMCHANNELS) );
- if ( dchannels[channel].data == NULL ) {
- std::map<int, std::string>::const_iterator j = dtemps.find(channel);
- if ( j == dtemps.end() ) {
- //fprintf (stderr, "MultiChannelImageT: unable to recover data channel %s (double %d)!\n",
- // imgfn.c_str(), channel);
- } else {
- //fprintf (stderr, "MultiChannelImageT: restoring data from %s ", j->second.c_str() );
- dchannels[channel].restore ( j->second );
- //fprintf (stderr, "(%d x %d)\n", dchannels[channel].xsize, dchannels[channel].ysize );
- }
- }
- return dchannels[channel];
- }
- inline std::string CachedExample::getFilename()
- {
- return imgfn;
- }
- inline MultiChannelImageT<int> & CachedExample::getIChannel ( int channel )
- {
- assert ( (channel >= 0) && (channel < I_NUMCHANNELS) );
- if ( (ichannels[channel].data == NULL) )
- {
- if ( (imgfn != "") && (channel == I_GRAYVALUES) )
- {
- readImageData();
- } else if ( (imgfn != "") && (channel == I_COLOR) ) {
- readImageDataRGB();
- assert ( hasColorInformation );
- } else {
- std::map<int, std::string>::const_iterator j = itemps.find(channel);
- if ( j == itemps.end() ) {
- //fprintf (stderr, "MultiChannelImageT: unable to recover data channel (int %d)!\n", channel);
- //exit(-1);
- } else {
- //fprintf (stderr, "MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
- ichannels[channel].restore ( j->second );
- }
- }
- }
- return ichannels[channel];
- }
- inline MultiChannelImageT<long> & CachedExample::getLChannel ( int channel )
- {
- assert ( (channel >= 0) && (channel < L_NUMCHANNELS) );
- if ( lchannels[channel].data == NULL ) {
- std::map<int, std::string>::const_iterator j = ltemps.find(channel);
- if ( j == ltemps.end() ) {
- if ( channel == L_INTEGRALIMAGE ) {
- calcIntegralImage();
- } else {
- //fprintf (stderr, "MultiChannelImageT: unable to recover data channel (long %d)!\n", channel);
- //exit(-1);
- }
- } else {
- //fprintf (stderr, "MultiChannelImageT: restoring data from %s\n", j->second.c_str() );
- lchannels[channel].restore ( j->second );
- }
- }
- return lchannels[channel];
- }
- template<class ImgPixelValue>
- void CachedExample::dropImages ( MultiChannelImageT<ImgPixelValue> *images, std::map<int, std::string> & temps, int numImages )
- {
- for ( int i = 0 ; i < numImages; i++ )
- {
- std::map<int, std::string>::iterator j = temps.find(i);
- if ( j == temps.end() )
- {
- std::string tempfilename = FileMgt::createTempFile("tmp/cachedexample_%s");
- //fprintf (stderr, "CachedExample: dumping channel %d/%d to %s (%d x %d)\n", i, numImages, tempfilename.c_str(),
- // images[i].xsize, images[i].ysize );
- images[i].store ( tempfilename );
- temps[i] = tempfilename;
- }
- images[i].freeData();
- }
- }
- } // namespace
- #endif
|