SemanticSegmentation.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "objrec/baselib/Preprocess.h"
  10. #include "objrec/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)
  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 < 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. lvec.clear();
  58. #ifdef DEBUG_PRINTS
  59. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  60. #endif
  61. }
  62. void SemanticSegmentation::convertExamplesToLSet(Examples &examples, LabeledSetVector &lvec)
  63. {
  64. #ifdef DEBUG_PRINTS
  65. cout << "SemSegRegionBased::convertExamplesToLSet starts" << endl;
  66. #endif
  67. lvec.clear();
  68. for(int i = 0; i < (int)examples.size(); i++)
  69. {
  70. if(examples[i].second.vec != NULL)
  71. {
  72. lvec.add(examples[i].first, *examples[i].second.vec);
  73. delete examples[i].second.vec;
  74. examples[i].second.vec = NULL;
  75. }
  76. else
  77. {
  78. if(examples[i].second.svec != NULL)
  79. {
  80. throw("Transform SVEC to VEC not yet implemented");
  81. }
  82. else
  83. {
  84. throw("no features for LabeledSet");
  85. }
  86. }
  87. }
  88. examples.clear();
  89. #ifdef DEBUG_PRINTS
  90. cout << "SemSegRegionBased::convertExamplesToLSet finished" << endl;
  91. #endif
  92. }
  93. void SemanticSegmentation::convertExamplesToVVector(VVector &feats,Examples &examples, vector<int> &label)
  94. {
  95. #ifdef DEBUG_PRINTS
  96. cout << "SemSegRegionBased::convertExamplesToVVector starts" << endl;
  97. #endif
  98. feats.clear();
  99. label.clear();
  100. for(int i = 0; i < (int)examples.size(); i++)
  101. {
  102. label.push_back(examples[i].first);
  103. feats.push_back(*examples[i].second.vec);
  104. delete examples[i].second.vec;
  105. examples[i].second.vec = NULL;
  106. }
  107. examples.clear();
  108. #ifdef DEBUG_PRINTS
  109. cout << "SemSegRegionBased::convertExamplesToVVector finished" << endl;
  110. #endif
  111. }
  112. void SemanticSegmentation::convertVVectorToExamples(VVector &feats,Examples &examples, vector<int> &label)
  113. {
  114. #ifdef DEBUG_PRINTS
  115. cout << "SemSegRegionBased::convertVVectorToExamples starts" << endl;
  116. #endif
  117. for(int i = 0; i < (int)feats.size(); i++)
  118. {
  119. NICE::Vector *v = new NICE::Vector(feats[i]);
  120. Example ex(v);
  121. ex.position = 0; //TODO: hier mal was besseres überlegen, damit Klassifikator wieder Bildspezifisch lernt
  122. examples.push_back (pair<int, Example> ( label[i], ex));
  123. feats[i].clear();
  124. }
  125. feats.clear();
  126. label.clear();
  127. #ifdef DEBUG_PRINTS
  128. cout << "SemSegRegionBased::convertVVectorToExamples finished" << endl;
  129. #endif
  130. }
  131. SemanticSegmentation::SemanticSegmentation( const Config *conf,
  132. const ClassNames *classNames )
  133. {
  134. this->classNames = classNames;
  135. Preprocess::Init ( conf );
  136. std::string imagetype_s = conf->gS("main", "imagetype", "rgb");
  137. if (imagetype_s == "rgb")
  138. imagetype = IMAGETYPE_RGB;
  139. else if ( imagetype_s == "gray" )
  140. imagetype = IMAGETYPE_GRAY;
  141. else {
  142. fprintf (stderr, "SemanticSegmentation:: unknown image type option\n");
  143. exit(-1);
  144. }
  145. }
  146. SemanticSegmentation::~SemanticSegmentation()
  147. {
  148. }
  149. void SemanticSegmentation::semanticseg ( const std::string & filename,
  150. NICE::Image & segresult,
  151. GenericImage<double> & probabilities)
  152. {
  153. Globals::setCurrentImgFN(filename);
  154. CachedExample *ce;
  155. if ( imagetype == IMAGETYPE_RGB )
  156. {
  157. NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( filename );
  158. ce = new CachedExample ( img );
  159. } else {
  160. NICE::Image img = Preprocess::ReadImgAdv ( filename );
  161. ce = new CachedExample ( img );
  162. }
  163. fprintf (stderr, "Starting Semantic Segmentation !\n");
  164. semanticseg ( ce, segresult, probabilities);
  165. delete ce;
  166. }