/** * @file Preprocess.cpp * @brief simple preprocessing operations * @author Erik Rodner * @date 02/20/2008 */ #include "core/image/ImageT.h" #include "core/vector/VectorT.h" #include "core/vector/MatrixT.h" #include #include "vislearning/baselib/Preprocess.h" #include "vislearning/baselib/Conversions.h" #include "core/basics/StringTools.h" using namespace OBJREC; using namespace std; using namespace NICE; int Preprocess::normalizeWidth = -1; int Preprocess::normalizeHeight = -1; bool Preprocess::init = false; std::string Preprocess::substituteFilenameRegex = ""; std::string Preprocess::substituteFilenameSubs = ""; bool Preprocess::disableReadImg = false; bool Preprocess::disableReadImgHeader = false; void Preprocess::Init ( const Config *conf ) { init = true; normalizeWidth = conf->gI("preprocess", "normalize_width", -1 ); normalizeHeight = conf->gI("preprocess", "normalize_height", -1 ); substituteFilenameRegex = conf->gS("preprocess", "substitute_filename_regex", ""); substituteFilenameSubs = conf->gS("preprocess", "substitute_filename_subs", ""); disableReadImg = conf->gB("preprocess", "disable_readimg", false); disableReadImgHeader = conf->gB("preprocess", "disable_readimg_header", false); } void Preprocess::getImageSize ( const std::string & filename, int & xsize, int & ysize ) { ImageFile file ( filename ); xsize = file.width(); ysize = file.height(); if ( normalizeWidth > 0 ) xsize = normalizeWidth; if ( normalizeHeight > 0 ) ysize = normalizeHeight; } NICE::Image Preprocess::ReadImgAdv ( const std::string & filename ) { std::string realfilename = filename; if ( substituteFilenameRegex.size() > 0 ) { fprintf (stderr, "%s -> ", realfilename.c_str() ); StringTools::regexSubstitute ( realfilename, substituteFilenameRegex, substituteFilenameSubs );+ fprintf (stderr, "%s\n", realfilename.c_str() ); } if ( disableReadImg ) { int xsize,ysize; // refactor: ,maxval; if ( ! disableReadImgHeader ) { try { ImageFile file ( filename ); xsize = file.width(); ysize = file.height(); } catch ( ImageException & ) { xsize = 10; ysize = 10; } } else { xsize = 10; ysize = 10; } fprintf (stderr, "Preprocess: read image disabled (xsize=%d,ysize=%d) !\n", xsize, ysize); return Image(xsize,ysize); } NICE::Image img; try { img.read ( realfilename ); } catch(ImageException &) { fprintf (stderr, "Failed to open image file: %s\n", realfilename.c_str() ); exit(-1); } if ( ( (normalizeWidth > 0) && (normalizeWidth != img.width()) ) || ( (normalizeHeight >0) && (normalizeHeight != img.height()) ) ) { NICE::Image img_s; Conversions::resizeImage ( img, img_s, normalizeWidth, normalizeHeight ); return img_s; } else { return img; } } NICE::ColorImage Preprocess::ReadImgAdvRGB ( const std::string & filename ) { std::string realfilename = filename; if ( substituteFilenameRegex.size() > 0 ) { fprintf (stderr, "%s -> ", realfilename.c_str() ); StringTools::regexSubstitute ( realfilename, substituteFilenameRegex, substituteFilenameSubs ); fprintf (stderr, "%s\n", realfilename.c_str() ); } NICE::ColorImage img; try { img.read (realfilename); } catch ( ImageException & ) { fprintf (stderr, "Failed to open image file: %s\n", realfilename.c_str() ); exit(-1); } if ( ( (normalizeWidth > 0) && (normalizeWidth != img.width()) ) || ( (normalizeHeight > 0) && (normalizeHeight != img.height()) ) ) { NICE::ColorImage img_s; cerr << "resizing image to: " << normalizeWidth << " " << normalizeHeight << endl; Conversions::resizeImage ( img, img_s, normalizeWidth, normalizeHeight ); return img_s; } else { return img; } }