Preprocess.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @file Preprocess.cpp
  3. * @brief simple preprocessing operations
  4. * @author Erik Rodner
  5. * @date 02/20/2008
  6. */
  7. #include "core/image/ImageT.h"
  8. #include "core/vector/VectorT.h"
  9. #include "core/vector/MatrixT.h"
  10. #include <iostream>
  11. #include "vislearning/baselib/Preprocess.h"
  12. #include "vislearning/baselib/Conversions.h"
  13. #include "core/basics/StringTools.h"
  14. using namespace OBJREC;
  15. using namespace std;
  16. using namespace NICE;
  17. int Preprocess::normalizeWidth = -1;
  18. int Preprocess::normalizeHeight = -1;
  19. bool Preprocess::init = false;
  20. std::string Preprocess::substituteFilenameRegex = "";
  21. std::string Preprocess::substituteFilenameSubs = "";
  22. bool Preprocess::disableReadImg = false;
  23. bool Preprocess::disableReadImgHeader = false;
  24. void Preprocess::Init ( const Config *conf )
  25. {
  26. init = true;
  27. normalizeWidth = conf->gI("preprocess", "normalize_width", -1 );
  28. normalizeHeight = conf->gI("preprocess", "normalize_height", -1 );
  29. substituteFilenameRegex = conf->gS("preprocess", "substitute_filename_regex", "");
  30. substituteFilenameSubs = conf->gS("preprocess", "substitute_filename_subs", "");
  31. disableReadImg = conf->gB("preprocess", "disable_readimg", false);
  32. disableReadImgHeader = conf->gB("preprocess", "disable_readimg_header", false);
  33. }
  34. void Preprocess::getImageSize ( const std::string & filename, int & xsize, int & ysize )
  35. {
  36. ImageFile file ( filename );
  37. xsize = file.width();
  38. ysize = file.height();
  39. if ( normalizeWidth > 0 ) xsize = normalizeWidth;
  40. if ( normalizeHeight > 0 ) ysize = normalizeHeight;
  41. }
  42. NICE::Image Preprocess::ReadImgAdv ( const std::string & filename )
  43. {
  44. std::string realfilename = filename;
  45. if ( substituteFilenameRegex.size() > 0 ) {
  46. fprintf (stderr, "%s -> ", realfilename.c_str() );
  47. StringTools::regexSubstitute ( realfilename, substituteFilenameRegex, substituteFilenameSubs );+
  48. fprintf (stderr, "%s\n", realfilename.c_str() );
  49. }
  50. if ( disableReadImg ) {
  51. int xsize,ysize; // refactor: ,maxval;
  52. if ( ! disableReadImgHeader ) {
  53. try {
  54. ImageFile file ( filename );
  55. xsize = file.width();
  56. ysize = file.height();
  57. } catch ( ImageException & ) {
  58. xsize = 10;
  59. ysize = 10;
  60. }
  61. } else {
  62. xsize = 10;
  63. ysize = 10;
  64. }
  65. fprintf (stderr, "Preprocess: read image disabled (xsize=%d,ysize=%d) !\n", xsize, ysize);
  66. return Image(xsize,ysize);
  67. }
  68. NICE::Image img;
  69. try {
  70. img.read ( realfilename );
  71. }
  72. catch(ImageException &)
  73. {
  74. fprintf (stderr, "Failed to open image file: %s\n", realfilename.c_str() );
  75. exit(-1);
  76. }
  77. if ( ( (normalizeWidth > 0) && (normalizeWidth != img.width()) ) ||
  78. ( (normalizeHeight >0) && (normalizeHeight != img.height()) ) )
  79. {
  80. NICE::Image img_s;
  81. Conversions::resizeImage ( img, img_s, normalizeWidth, normalizeHeight );
  82. return img_s;
  83. } else {
  84. return img;
  85. }
  86. }
  87. NICE::ColorImage Preprocess::ReadImgAdvRGB ( const std::string & filename )
  88. {
  89. std::string realfilename = filename;
  90. if ( substituteFilenameRegex.size() > 0 ) {
  91. fprintf (stderr, "%s -> ", realfilename.c_str() );
  92. StringTools::regexSubstitute ( realfilename, substituteFilenameRegex, substituteFilenameSubs );
  93. fprintf (stderr, "%s\n", realfilename.c_str() );
  94. }
  95. NICE::ColorImage img;
  96. try {
  97. img.read (realfilename);
  98. } catch ( ImageException & ) {
  99. fprintf (stderr, "Failed to open image file: %s\n", realfilename.c_str() );
  100. exit(-1);
  101. }
  102. if ( ( (normalizeWidth > 0) && (normalizeWidth != img.width()) ) ||
  103. ( (normalizeHeight > 0) && (normalizeHeight != img.height()) ) )
  104. {
  105. NICE::ColorImage img_s;
  106. cerr << "resizing image to: " << normalizeWidth << " " << normalizeHeight << endl;
  107. Conversions::resizeImage ( img, img_s, normalizeWidth, normalizeHeight );
  108. return img_s;
  109. } else {
  110. return img;
  111. }
  112. }