Preprocess.cpp 3.8 KB

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