SemSegTools.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @file SemSegTools.cpp
  3. * @brief tools for semantic segmentation
  4. * @author Erik Rodner
  5. * @date 03/19/2009
  6. */
  7. #include <iostream>
  8. #include "SemSegTools.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. using namespace NICE;
  12. #undef DEBUG_LOCALIZATION
  13. void SemSegTools::collectTrainingExamples (
  14. const Config * conf,
  15. const std::string & section,
  16. const LabeledSet & train,
  17. const ClassNames & cn,
  18. Examples & examples,
  19. vector<CachedExample *> & imgexamples )
  20. {
  21. assert ( train.count() > 0 );
  22. examples.clear();
  23. imgexamples.clear();
  24. int grid_size_x = conf->gI(section, "grid_size_x", 5 );
  25. int grid_size_y = conf->gI(section, "grid_size_y", 5 );
  26. int grid_border_x = conf->gI(section, "grid_border_x", 20 );
  27. int grid_border_y = conf->gI(section, "grid_border_y", 20 );
  28. std::string selection = conf->gS(section, "train_selection" );
  29. set<int> classnoSelection;
  30. cn.getSelection ( selection, classnoSelection );
  31. bool useExcludedAsBG = conf->gB(section, "use_excluded_as_background", false );
  32. int backgroundClassNo = 0;
  33. if ( useExcludedAsBG )
  34. {
  35. backgroundClassNo = cn.classno("various");
  36. assert ( backgroundClassNo >= 0 );
  37. }
  38. LOOP_ALL_S (train)
  39. {
  40. EACH_INFO(image_classno,imgInfo);
  41. std::string imgfn = imgInfo.img();
  42. if ( ! imgInfo.hasLocalizationInfo() ) {
  43. fprintf (stderr, "WARNING: NO localization info found for %s !\n",
  44. imgfn.c_str() );
  45. continue;
  46. }
  47. int xsize, ysize;
  48. CachedExample *ce = new CachedExample ( imgfn );
  49. ce->getImageSize ( xsize, ysize );
  50. imgexamples.push_back ( ce );
  51. const LocalizationResult *locResult = imgInfo.localization();
  52. if ( locResult->size() <= 0 ) {
  53. fprintf (stderr, "WARNING: NO ground truth polygons found for %s !\n",
  54. imgfn.c_str());
  55. continue;
  56. }
  57. fprintf (stderr, "SemSegTools: Collecting pixel examples from localization info: %s\n",
  58. imgfn.c_str() );
  59. NICE::Image pixelLabels (xsize, ysize);
  60. pixelLabels.set(0);
  61. locResult->calcLabeledImage ( pixelLabels, cn.getBackgroundClass() );
  62. #ifdef DEBUG_LOCALIZATION
  63. NICE::Image img (imgfn);
  64. showImage(img);
  65. showImage(pixelLabels);
  66. #endif
  67. Example pce ( ce, 0, 0 );
  68. for ( int x = 0 ; x < xsize ; x += grid_size_x )
  69. for ( int y = 0 ; y < ysize ; y += grid_size_y )
  70. {
  71. if ( (x >= grid_border_x) &&
  72. ( y >= grid_border_y ) && ( x < xsize - grid_border_x ) &&
  73. ( y < ysize - grid_border_x ) )
  74. {
  75. pce.x = x; pce.y = y;
  76. int classno = pixelLabels.getPixel(x,y);
  77. if ( classnoSelection.find(classno) != classnoSelection.end() ) {
  78. examples.push_back ( pair<int, Example> (
  79. classno,
  80. pce // FIXME: offset handling
  81. ) );
  82. } else if ( useExcludedAsBG ) {
  83. examples.push_back ( pair<int, Example> (
  84. backgroundClassNo,
  85. pce // FIXME: offset handling
  86. ) );
  87. }
  88. }
  89. }
  90. }
  91. fprintf (stderr, "total number of examples: %d\n", (int)examples.size() );
  92. }