SemanticSegmentation.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @file SemanticSegmentation.cpp
  3. * @brief abstract interface for semantic segmentation algorithms
  4. * @author Erik Rodner
  5. * @date 03/19/2009
  6. */
  7. #include <iostream>
  8. #include "SemanticSegmentation.h"
  9. #include "vislearning/baselib/Preprocess.h"
  10. #include "vislearning/baselib/Globals.h"
  11. using namespace OBJREC;
  12. using namespace std;
  13. using namespace NICE;
  14. void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, LabeledSetVector &lvec )
  15. {
  16. #ifdef DEBUG_PRINTS
  17. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  18. #endif
  19. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  20. {
  21. for ( int j = 0; j < ( int ) iter->second.size(); j++ )
  22. {
  23. Vector &tmp = * ( iter->second[j] );
  24. int dim = tmp.size();
  25. SparseVector *vec = new SparseVector ( dim );
  26. for ( int j = 0; j < dim; j++ )
  27. {
  28. if ( tmp[j] != 0.0 )
  29. {
  30. ( *vec ) [j] = tmp[j];
  31. }
  32. }
  33. Example ex;
  34. ex.svec = vec;
  35. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  36. }
  37. }
  38. lvec.clear();
  39. #ifdef DEBUG_PRINTS
  40. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  41. #endif
  42. }
  43. void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec, const bool & removeOldDataPointer )
  44. {
  45. #ifdef DEBUG_PRINTS
  46. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  47. #endif
  48. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  49. {
  50. for ( int j = 0; j < (int)iter->second.size(); j++ )
  51. {
  52. NICE::Vector *vec = new NICE::Vector ( * ( iter->second[j] ) );
  53. Example ex ( vec );
  54. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  55. }
  56. }
  57. if (!removeOldDataPointer)
  58. {
  59. //NOTE this is only useful, if our classifier does NOT need the data explicitely
  60. lvec.clear();
  61. }
  62. else
  63. {
  64. lvec.removePointersToDataWithoutDeletion();
  65. //after setting all the pointers to NULL, we can savely clear the LSet without deleting the previously
  66. //stored features, which might be needed somewhere else, e.g., in the VCNearestNeighbour
  67. lvec.clear();
  68. }
  69. #ifdef DEBUG_PRINTS
  70. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  71. #endif
  72. }
  73. void SemanticSegmentation::convertExamplesToLSet ( Examples &examples, LabeledSetVector &lvec )
  74. {
  75. #ifdef DEBUG_PRINTS
  76. cout << "SemSegRegionBased::convertExamplesToLSet starts" << endl;
  77. #endif
  78. lvec.clear();
  79. for ( int i = 0; i < ( int ) examples.size(); i++ )
  80. {
  81. if ( examples[i].second.vec != NULL )
  82. {
  83. lvec.add ( examples[i].first, *examples[i].second.vec );
  84. delete examples[i].second.vec;
  85. examples[i].second.vec = NULL;
  86. }
  87. else
  88. {
  89. if ( examples[i].second.svec != NULL )
  90. {
  91. NICE::Vector v;
  92. examples[i].second.svec->convertToVectorT(v);
  93. lvec.add ( examples[i].first, v );
  94. delete examples[i].second.svec;
  95. examples[i].second.svec = NULL;
  96. }
  97. else
  98. {
  99. throw ( "no features for LabeledSet" );
  100. }
  101. }
  102. }
  103. examples.clear();
  104. #ifdef DEBUG_PRINTS
  105. cout << "SemSegRegionBased::convertExamplesToLSet finished" << endl;
  106. #endif
  107. }
  108. void SemanticSegmentation::convertExamplesToVVector ( VVector &feats, Examples &examples, vector<int> &label )
  109. {
  110. #ifdef DEBUG_PRINTS
  111. cout << "SemSegRegionBased::convertExamplesToVVector starts" << endl;
  112. #endif
  113. feats.clear();
  114. label.clear();
  115. for ( int i = 0; i < ( int ) examples.size(); i++ )
  116. {
  117. label.push_back ( examples[i].first );
  118. feats.push_back ( *examples[i].second.vec );
  119. delete examples[i].second.vec;
  120. examples[i].second.vec = NULL;
  121. }
  122. examples.clear();
  123. #ifdef DEBUG_PRINTS
  124. cout << "SemSegRegionBased::convertExamplesToVVector finished" << endl;
  125. #endif
  126. }
  127. void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &examples, vector<int> &label )
  128. {
  129. #ifdef DEBUG_PRINTS
  130. cout << "SemSegRegionBased::convertVVectorToExamples starts" << endl;
  131. #endif
  132. for ( int i = 0; i < ( int ) feats.size(); i++ )
  133. {
  134. NICE::Vector *v = new NICE::Vector ( feats[i] );
  135. Example ex ( v );
  136. ex.position = 0; //TODO: hier mal was besseres überlegen, damit Klassifikator wieder Bildspezifisch lernt
  137. examples.push_back ( pair<int, Example> ( label[i], ex ) );
  138. feats[i].clear();
  139. }
  140. feats.clear();
  141. label.clear();
  142. #ifdef DEBUG_PRINTS
  143. cout << "SemSegRegionBased::convertVVectorToExamples finished" << endl;
  144. #endif
  145. }
  146. SemanticSegmentation::SemanticSegmentation ( const Config *conf,
  147. const ClassNames *classNames )
  148. {
  149. this->classNames = classNames;
  150. Preprocess::Init ( conf );
  151. std::string imagetype_s = conf->gS ( "main", "imagetype", "rgb" );
  152. if ( imagetype_s == "rgb" )
  153. imagetype = IMAGETYPE_RGB;
  154. else if ( imagetype_s == "gray" )
  155. imagetype = IMAGETYPE_GRAY;
  156. else {
  157. fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
  158. exit ( -1 );
  159. }
  160. }
  161. SemanticSegmentation::~SemanticSegmentation()
  162. {
  163. }
  164. void SemanticSegmentation::semanticseg ( const std::string & filename,
  165. NICE::Image & segresult,
  166. NICE::MultiChannelImageT<double> & probabilities )
  167. {
  168. Globals::setCurrentImgFN ( filename );
  169. CachedExample *ce;
  170. if ( imagetype == IMAGETYPE_RGB )
  171. {
  172. NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( filename );
  173. ce = new CachedExample ( img );
  174. } else {
  175. NICE::Image img = Preprocess::ReadImgAdv ( filename );
  176. ce = new CachedExample ( img );
  177. }
  178. fprintf ( stderr, "Starting Semantic Segmentation !\n" );
  179. semanticseg ( ce, segresult, probabilities );
  180. delete ce;
  181. }