123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- /**
- * @file CachedExample.cpp
- * @brief data caching
- * @author Erik Rodner, Sven Sickert
- * @date 04/21/2008 (modified 03/18/2016)
- */
- #include <iostream>
- #include "CachedExample.h"
- #include "vislearning/baselib/Conversions.h"
- #include "vislearning/baselib/ProgressBar.h"
- #include "vislearning/image/GenericImageTools.h"
- #include "vislearning/baselib/Preprocess.h"
- #include "vislearning/baselib/ICETools.h"
- using namespace OBJREC;
- void CachedExample::init ()
- {
- dchannels = new NICE::MultiChannelImageT<double> [D_NUMCHANNELS];
- ichannels = new NICE::MultiChannelImageT<int> [I_NUMCHANNELS];
- lchannels = new NICE::MultiChannelImageT<long> [L_NUMCHANNELS];
- dchannels3 = new NICE::MultiChannelImage3DT<double> [D_NUMCHANNELS];
- ichannels3 = new NICE::MultiChannelImage3DT<int> [I_NUMCHANNELS];
- lchannels3 = new NICE::MultiChannelImage3DT<long> [L_NUMCHANNELS];
- svmap = new NICE::SparseVector *[SVNUMCHANNELS];
- svmap_xsize = new int [SVNUMCHANNELS];
- svmap_ysize = new int [SVNUMCHANNELS];
- for ( uint k = 0 ; k < SVNUMCHANNELS ; k++ )
- {
- svmap[k] = NULL;
- svmap_xsize[k] = 0;
- svmap_ysize[k] = 0;
- }
- }
- CachedExample::CachedExample (
- const std::string & _imgfn,
- int _newWidth,
- int _newHeight )
- {
- imgfn.push_back(_imgfn);
- newWidth = _newWidth;
- newHeight = _newHeight;
- newDepth = 1;
- Preprocess::getImageSize ( _imgfn, oxsize, oysize );
- ozsize = 1;
- init();
- hasColorInformation = true;
- }
- CachedExample::CachedExample (
- const std::vector<std::string> & _imgfn,
- int _newWidth,
- int _newHeight,
- int _newDepth )
- {
- imgfn = _imgfn;
- newWidth = _newWidth;
- newHeight = _newHeight;
- newDepth = _newDepth;
- Preprocess::getImageSize ( _imgfn[0], oxsize, oysize );
- ozsize = _imgfn.size();
- init();
- hasColorInformation = true;
- }
- CachedExample::CachedExample ( const NICE::Image & _img )
- {
- imgfn.push_back("");
- newWidth = -1;
- newHeight = -1;
- newDepth = -1;
- init();
- oxsize = _img.width();
- oysize = _img.height();
- ozsize = 1;
- ichannels[I_GRAYVALUES].freeData();
- ichannels[I_GRAYVALUES].addChannel(_img);
- hasColorInformation = false;
- }
- CachedExample::CachedExample (
- const NICE::MultiChannelImageT<int> & _img )
- {
- newWidth = -1;
- newHeight = -1;
- newDepth = -1;
- init();
- oxsize = _img.width();
- oysize = _img.height();
- ozsize = _img.channels();
- for ( unsigned int i = 0; i < ozsize; i++ )
- imgfn.push_back("");
- ichannels3[I_GRAYVALUES].freeData();
- ichannels3[I_GRAYVALUES].addChannel(_img);
- hasColorInformation = false;
- }
- CachedExample::CachedExample (
- const NICE::ColorImage & img,
- bool disableGrayConversion )
- {
- imgfn.push_back("");
- oxsize = img.width();
- oysize = img.height();
- ozsize = 1;
- newWidth = -1;
- newHeight = -1;
- newDepth = -1;
- init();
- if ( ! disableGrayConversion )
- {
- // refactor-nice.pl: check this substitution
- // old: Image imggray;
- NICE::Image imggray;
- ICETools::calcGrayImage ( img, imggray );
- ichannels[I_GRAYVALUES].freeData();
- ichannels[I_GRAYVALUES].addChannel(imggray);
- }
- ichannels[I_COLOR].reInit ( oxsize, oysize, 3);
- for ( int y = 0 ; y < oysize ; y++ )
- for ( int x = 0 ; x < oxsize ; x++ )
- {
- ichannels[I_COLOR](x,y,0) = img.getPixel ( x, y, 0 );
- ichannels[I_COLOR](x,y,1) = img.getPixel ( x, y, 1 );
- ichannels[I_COLOR](x,y,2) = img.getPixel ( x, y, 2 );
- }
- hasColorInformation = true;
- }
- CachedExample::CachedExample (
- const NICE::MultiChannelImage3DT<int> & img,
- bool disableGrayConversion )
- {
- oxsize = img.width();
- oysize = img.height();
- ozsize = img.depth();
- newWidth = -1;
- newHeight = -1;
- newDepth = -1;
- for ( unsigned int i = 0; i < ozsize; i++ )
- imgfn.push_back("");
- init();
- int ochannels = img.channels();
- if ( ! disableGrayConversion && (ochannels > 2) )
- {
- ichannels3[I_GRAYVALUES].freeData();
- ichannels3[I_GRAYVALUES].addChannel(1);
- for ( int z = 0; z < ozsize; z++ )
- {
- NICE::Image imggray;
- ICETools::calcGrayImage ( img.getColor(z), imggray );
- for ( int y = 0; y < oysize; y++ )
- for ( int x = 0; x < oxsize; x++ )
- ichannels3[I_GRAYVALUES](x,y,z,0) = imggray.getPixel(x,y);
- }
- }
- ichannels3[I_COLOR].addChannel ( img );
- hasColorInformation = true;
- }
- CachedExample::~CachedExample()
- {
- delete [] dchannels;
- delete [] ichannels;
- delete [] lchannels;
- delete [] dchannels3;
- delete [] ichannels3;
- delete [] lchannels3;
- for ( uint k = 0 ; k < SVNUMCHANNELS ; k++ )
- if ( svmap[k] != NULL )
- delete [] ( svmap[k] );
- delete [] svmap;
- delete [] svmap_xsize;
- delete [] svmap_ysize;
- // remove all temporary files
- for ( std::map<int, std::string>::const_iterator j = dtemps.begin();
- j != dtemps.end();
- j++ )
- {
- //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
- NICE::FileMgt::deleteTempFile ( j->second );
- }
- for ( std::map<int, std::string>::const_iterator j = itemps.begin();
- j != itemps.end();
- j++ )
- {
- //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
- NICE::FileMgt::deleteTempFile ( j->second );
- }
- for ( std::map<int, std::string>::const_iterator j = ltemps.begin();
- j != ltemps.end();
- j++ )
- {
- //fprintf (stderr, "CachedExample: removing temp file %s\n", j->second.c_str() );
- NICE::FileMgt::deleteTempFile ( j->second );
- }
- }
- void CachedExample::readImageData ()
- {
- ozsize = imgfn.size();
- if ( ozsize == 0 || imgfn[0] == "" )
- return;
- for ( int z = 0; z < ozsize; z++ )
- {
- NICE::Image orig = Preprocess::ReadImgAdv ( imgfn[z] );
- NICE::Image imggray;
- if ( newWidth > 0 )
- Conversions::resizeImage ( orig, imggray, newWidth, newHeight );
- else
- imggray = orig;
- // FIXME: gray values are saved twice (ichannels and ichannels3)
- // for compatibility with all 2D programs that refer to ichannels
- // which is of type MultiChannelImageT instead of MultiChannelImage3DT
- if ( z == 0 )
- {
- oxsize = imggray.width();
- oysize = imggray.height();
- ichannels3[I_GRAYVALUES].reInit( oxsize, oysize, ozsize, 1);
- ichannels[I_GRAYVALUES].freeData();
- ichannels[I_GRAYVALUES].addChannel(imggray);
- }
- for ( int y = 0; y < oysize; y++ )
- for ( int x = 0; x < oxsize; x++ )
- ichannels3[I_GRAYVALUES](x,y,z,0) = imggray.getPixel(x,y);
- }
- }
- void CachedExample::readImageDataRGB ()
- {
- ozsize = imgfn.size();
- if ( ozsize == 0 || imgfn[0] == "" )
- return;
- for ( int z = 0; z < ozsize; z++ )
- {
- NICE::ColorImage img;
- try {
- img = Preprocess::ReadImgAdvRGB ( imgfn[z] );
- } catch ( NICE::ImageException & ) {
- fprintf ( stderr, "error reading rgb image %s\n", imgfn[z].c_str() );
- hasColorInformation = false;
- return;
- }
- if ( z == 0 )
- {
- oxsize = img.width();
- oysize = img.height();
- hasColorInformation = true;
- // FIXME: see readImageData issue
- ichannels3[I_COLOR].reInit ( oxsize, oysize, ozsize, 3);
- ichannels[I_COLOR].reInit ( oxsize, oysize, 3);
- }
- for ( int y = 0; y < oysize; y++ )
- for ( int x = 0; x < oxsize; x++ )
- {
- ichannels3[I_COLOR](x,y,z,0) = img.getPixel(x,y,0);
- ichannels3[I_COLOR](x,y,z,1) = img.getPixel(x,y,1);
- ichannels3[I_COLOR](x,y,z,2) = img.getPixel(x,y,2);
- ichannels[I_COLOR](x,y,0) = img.getPixel ( x, y, 0 );
- ichannels[I_COLOR](x,y,1) = img.getPixel ( x, y, 1 );
- ichannels[I_COLOR](x,y,2) = img.getPixel ( x, y, 2 );
- }
- }
- }
- void CachedExample::calcIntegralImage ()
- {
- // in case of standard 2D images
- if ( ozsize == 1 )
- {
- if ( ichannels[I_GRAYVALUES].width() == 0 )
- {
- readImageData ();
- if ( ichannels[I_GRAYVALUES].width() == 0 )
- {
- fprintf ( stderr, "CachedExample::getChannel: unable to recover data channel\n" );
- exit ( -1 );
- }
- }
- lchannels[L_INTEGRALIMAGE].reInit (
- ichannels[I_GRAYVALUES].width(),
- ichannels[I_GRAYVALUES].height(),
- 1 );
- NICE::ImageT<long int> tmp = lchannels[L_INTEGRALIMAGE][0];
- GenericImageTools::calcIntegralImage (
- tmp,
- ichannels[I_GRAYVALUES][0],
- ichannels[I_GRAYVALUES].width(),
- ichannels[I_GRAYVALUES].height() );
- }
- // in case of a 3D images
- else
- {
- if ( ichannels[I_GRAYVALUES].width() == 0 )
- {
- readImageData ();
- if ( ichannels[I_GRAYVALUES].width() == 0 )
- {
- fprintf ( stderr, "CachedExample::getChannel: unable to recover data channel\n" );
- exit ( -1 );
- }
- }
- int nwidth = ichannels3[I_GRAYVALUES].width();
- int nheight = ichannels3[I_GRAYVALUES].height();
- int ndepth = ichannels3[I_GRAYVALUES].depth();
- int nchannels = ichannels3[I_GRAYVALUES].channels();
- lchannels3[L_INTEGRALIMAGE].reInit( nwidth, nheight, ndepth, nchannels);
- for ( int c = 0; c < nchannels; c++ )
- {
- for ( int z = 0; z < ndepth; z++ )
- for ( int y = 0; y < nheight; y++ )
- for ( int x = 0; x < nwidth; x++ )
- {
- lchannels3[L_INTEGRALIMAGE](x,y,z,c) =
- (long)ichannels3[L_INTEGRALIMAGE].get(x,y,z,c);
- }
- lchannels3[L_INTEGRALIMAGE].calcIntegral(c);
- }
- }
- }
- void CachedExample::buildIntegralSV (
- int svchannel,
- NICE::SparseVector *_map,
- int xsize_s, int ysize_s )
- {
- NICE::SparseVector *map = _map;
- svmap[svchannel] = _map;
- svmap_xsize[svchannel] = xsize_s;
- svmap_ysize[svchannel] = ysize_s;
- int k = xsize_s;
- for ( int y = 1 ; y < ysize_s; y++, k += xsize_s )
- map[k].add ( ( map[k-xsize_s] ) );
- k = 1;
- for ( int x = 1 ; x < xsize_s; x++, k++ )
- map[k].add ( ( map[k-1] ) );
- k = xsize_s + 1;
- for ( int y = 1 ; y < ysize_s ; y++, k++ )
- {
- for ( int x = 1 ; x < xsize_s ; x++, k++ )
- {
- map[k].add ( ( map[k-1] ) );
- map[k].add ( ( map[k-xsize_s] ) );
- map[k].add ( ( map[k-xsize_s-1] ), -1.0 );
- }
- }
- }
- void CachedExample::setSVMap (
- int svchannel,
- NICE::SparseVector *_map,
- int xsize_s, int ysize_s )
- {
- svmap[svchannel] = _map;
- svmap_xsize[svchannel] = xsize_s;
- svmap_ysize[svchannel] = ysize_s;
- }
- NICE::SparseVector *CachedExample::getSVMap ( int svchannel,
- int & _xsize, int & _ysize,
- int & _tm_xsize, int & _tm_ysize ) const
- {
- _xsize = oxsize;
- _ysize = oysize;
- _tm_xsize = svmap_xsize[svchannel];
- _tm_ysize = svmap_ysize[svchannel];
- assert ( svmap[svchannel] != NULL );
- return svmap[svchannel];
- }
- bool CachedExample::colorInformationAvailable() const
- {
- if ( hasColorInformation ) return true;
- else {
- if ( imgfn.size() == 0 ) return false;
- // int tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr;
- // refactor: InfImgFile ( imgfn, tmp_xsize, tmp_ysize, tmp_maxval, tmp_nr );
- NICE::ImageFile imgf ( imgfn[0] );
- const NICE::ImageFile::Header & imgfheader = imgf.getHeader();
- // tmp_xsize = imgfheader.width;
- // tmp_ysize = imgfheader.height;
- // tmp_maxval = 255;
- int tmp_nr = imgfheader.channel;
- if ( tmp_nr > 1 ) return true;
- else return false;
- }
- }
- void CachedExample::dropPreCached()
- {
- dropImages<double> ( dchannels, dtemps, D_NUMCHANNELS );
- dropImages<int> ( ichannels, itemps, I_NUMCHANNELS );
- dropImages<long> ( lchannels, ltemps, L_NUMCHANNELS );
- }
|