SemanticSegmentation.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "semseg3d/image/MultiChannelImage3DT.h"
  12. #include "core/basics/StringTools.h"
  13. using namespace OBJREC;
  14. using namespace std;
  15. using namespace NICE;
  16. void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, LabeledSetVector &lvec )
  17. {
  18. #ifdef DEBUG_PRINTS
  19. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  20. #endif
  21. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  22. {
  23. for ( int j = 0; j < ( int ) iter->second.size(); j++ )
  24. {
  25. Vector &tmp = * ( iter->second[j] );
  26. int dim = tmp.size();
  27. SparseVector *vec = new SparseVector ( dim );
  28. for ( int j = 0; j < dim; j++ )
  29. {
  30. if ( tmp[j] != 0.0 )
  31. {
  32. ( *vec ) [j] = tmp[j];
  33. }
  34. }
  35. Example ex;
  36. ex.svec = vec;
  37. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  38. }
  39. }
  40. lvec.clear();
  41. #ifdef DEBUG_PRINTS
  42. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  43. #endif
  44. }
  45. void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec )
  46. {
  47. #ifdef DEBUG_PRINTS
  48. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  49. #endif
  50. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  51. {
  52. for ( int j = 0; j < (int)iter->second.size(); j++ )
  53. {
  54. NICE::Vector *vec = new NICE::Vector ( * ( iter->second[j] ) );
  55. Example ex ( vec );
  56. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  57. }
  58. }
  59. lvec.clear();
  60. #ifdef DEBUG_PRINTS
  61. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  62. #endif
  63. }
  64. void SemanticSegmentation::convertExamplesToLSet ( Examples &examples, LabeledSetVector &lvec )
  65. {
  66. #ifdef DEBUG_PRINTS
  67. cout << "SemSegRegionBased::convertExamplesToLSet starts" << endl;
  68. #endif
  69. lvec.clear();
  70. for ( int i = 0; i < ( int ) examples.size(); i++ )
  71. {
  72. if ( examples[i].second.vec != NULL )
  73. {
  74. lvec.add ( examples[i].first, *examples[i].second.vec );
  75. delete examples[i].second.vec;
  76. examples[i].second.vec = NULL;
  77. }
  78. else
  79. {
  80. if ( examples[i].second.svec != NULL )
  81. {
  82. throw ( "Transform SVEC to VEC not yet implemented" );
  83. }
  84. else
  85. {
  86. throw ( "no features for LabeledSet" );
  87. }
  88. }
  89. }
  90. examples.clear();
  91. #ifdef DEBUG_PRINTS
  92. cout << "SemSegRegionBased::convertExamplesToLSet finished" << endl;
  93. #endif
  94. }
  95. void SemanticSegmentation::convertExamplesToVVector ( VVector &feats, Examples &examples, vector<int> &label )
  96. {
  97. #ifdef DEBUG_PRINTS
  98. cout << "SemSegRegionBased::convertExamplesToVVector starts" << endl;
  99. #endif
  100. feats.clear();
  101. label.clear();
  102. for ( int i = 0; i < ( int ) examples.size(); i++ )
  103. {
  104. label.push_back ( examples[i].first );
  105. feats.push_back ( *examples[i].second.vec );
  106. delete examples[i].second.vec;
  107. examples[i].second.vec = NULL;
  108. }
  109. examples.clear();
  110. #ifdef DEBUG_PRINTS
  111. cout << "SemSegRegionBased::convertExamplesToVVector finished" << endl;
  112. #endif
  113. }
  114. void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &examples, vector<int> &label )
  115. {
  116. #ifdef DEBUG_PRINTS
  117. cout << "SemSegRegionBased::convertVVectorToExamples starts" << endl;
  118. #endif
  119. for ( int i = 0; i < ( int ) feats.size(); i++ )
  120. {
  121. NICE::Vector *v = new NICE::Vector ( feats[i] );
  122. Example ex ( v );
  123. ex.position = 0; //TODO: hier mal was besseres überlegen, damit Klassifikator wieder Bildspezifisch lernt
  124. examples.push_back ( pair<int, Example> ( label[i], ex ) );
  125. feats[i].clear();
  126. }
  127. feats.clear();
  128. label.clear();
  129. #ifdef DEBUG_PRINTS
  130. cout << "SemSegRegionBased::convertVVectorToExamples finished" << endl;
  131. #endif
  132. }
  133. SemanticSegmentation::SemanticSegmentation ( const Config *conf,
  134. const ClassNames *classNames )
  135. {
  136. this->classNames = classNames;
  137. Preprocess::Init ( conf );
  138. std::string imagetype_s = conf->gS ( "main", "imagetype", "gray" );
  139. if ( imagetype_s == "rgb" )
  140. imagetype = IMAGETYPE_RGB;
  141. else if ( imagetype_s == "gray" )
  142. imagetype = IMAGETYPE_GRAY;
  143. else {
  144. fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
  145. exit ( -1 );
  146. }
  147. }
  148. SemanticSegmentation::~SemanticSegmentation()
  149. {
  150. }
  151. void SemanticSegmentation::getDepthVector( const LabeledSet *Files, vector<int> & depthVec )
  152. {
  153. std::string oldName = "0";
  154. int zsize = 0;
  155. LOOP_ALL_S( *Files )
  156. {
  157. EACH_INFO( classno, info );
  158. std::string file = info.img();
  159. std::vector< std::string > list;
  160. StringTools::split (file, '/', list);
  161. std::string filename = list.back();
  162. uint found = filename.find_last_of("_");
  163. std::string curName = filename.substr(found-1,1);
  164. if ( curName.compare( oldName ) == 0 )
  165. {
  166. zsize++;
  167. }
  168. else
  169. {
  170. depthVec.push_back( zsize );
  171. zsize = 1;
  172. oldName = curName;
  173. }
  174. }
  175. depthVec.push_back( zsize );
  176. }
  177. void SemanticSegmentation::make3DImage( const std::vector<std::string> & filelist,
  178. NICE::MultiChannelImage3DT<double> & imgData )
  179. {
  180. bool isInit = false;
  181. for( int it = 0; it < (int)filelist.size(); it++ )
  182. {
  183. if( imagetype == IMAGETYPE_RGB )
  184. {
  185. NICE::ColorImage img = Preprocess::ReadImgAdvRGB( filelist[it] );
  186. if (!isInit)
  187. {
  188. imgData.reInit(img.width(),img.height(),filelist.size(),3);
  189. isInit = true;
  190. }
  191. for( int y = 0; y < img.height(); y++ )
  192. {
  193. for( int x = 0; x < img.width(); x++ )
  194. {
  195. for( int r = 0; r < 3; r++)
  196. {
  197. imgData.set(x, y, it, img.getPixel(x,y,r), r);
  198. }
  199. }
  200. }
  201. } else {
  202. NICE::Image img = Preprocess::ReadImgAdv( filelist[it] );
  203. if (!isInit)
  204. {
  205. imgData.reInit(img.width(),img.height(),filelist.size(),1);
  206. isInit = true;
  207. }
  208. for( int y = 0; y < img.height(); y++ )
  209. {
  210. for( int x = 0; x < img.width(); x++ )
  211. {
  212. imgData.set(x, y, it, img.getPixel(x,y), 0);
  213. }
  214. }
  215. }
  216. }
  217. }