SemanticSegmentation.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * @file SemanticSegmentation.cpp
  3. * @brief abstract interface for semantic segmentation algorithms
  4. * @author Erik Rodner and Sven Sickert
  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 "core/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. {
  145. fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
  146. exit ( -1 );
  147. }
  148. }
  149. SemanticSegmentation::~SemanticSegmentation()
  150. {
  151. }
  152. void SemanticSegmentation::getDepthVector ( const LabeledSet *Files, vector<int> & depthVec )
  153. {
  154. std::string oldName;
  155. int zsize = 0;
  156. bool isInit = false;
  157. for (LabeledSet::const_iterator it = Files->begin(); it != Files->end(); it++)
  158. {
  159. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  160. jt != it->second.end(); jt++)
  161. {
  162. ImageInfo & info = *(*jt);
  163. std::string file = info.img();
  164. std::vector< std::string > list;
  165. StringTools::split ( file, '/', list );
  166. std::string filename = list.back();
  167. uint found = filename.find_last_of ( "_" );
  168. if (found < filename.size() )
  169. {
  170. std::string curName = filename.substr ( found-3,3 );
  171. if ( !isInit )
  172. {
  173. oldName = curName;
  174. isInit = true;
  175. }
  176. if ( curName.compare ( oldName ) == 0 )
  177. {
  178. zsize++;
  179. }
  180. else
  181. {
  182. depthVec.push_back ( zsize );
  183. zsize = 1;
  184. oldName = curName;
  185. }
  186. }
  187. else
  188. {
  189. zsize = 1;
  190. depthVec.push_back ( zsize );
  191. }
  192. }
  193. }
  194. depthVec.push_back ( zsize );
  195. }
  196. void SemanticSegmentation::make3DImage ( const std::vector<std::string> & filelist,
  197. NICE::MultiChannelImage3DT<double> & imgData )
  198. {
  199. bool isInit = false;
  200. for ( int it = 0; it < ( int ) filelist.size(); it++ )
  201. {
  202. if ( imagetype == IMAGETYPE_RGB )
  203. {
  204. NICE::ColorImage img;
  205. try
  206. {
  207. img.read ( filelist[it] );
  208. }
  209. catch ( ImageException iE )
  210. {
  211. fprintf ( stderr, "Failed to open color image file: %s\n", filelist[it].c_str() );
  212. fprintf ( stderr, "%s\n", iE.what() );
  213. exit ( -1 );
  214. }
  215. if ( !isInit )
  216. {
  217. imgData.reInit ( img.width(),img.height(),filelist.size(),3 );
  218. isInit = true;
  219. }
  220. for ( int y = 0; y < img.height(); y++ )
  221. {
  222. for ( int x = 0; x < img.width(); x++ )
  223. {
  224. for ( int r = 0; r < 3; r++ )
  225. {
  226. imgData.set ( x, y, it, img.getPixel ( x,y,r ), r );
  227. }
  228. }
  229. }
  230. }
  231. else
  232. {
  233. NICE::Image img;
  234. try
  235. {
  236. img.read ( filelist[it] );
  237. }
  238. catch ( ImageException iE )
  239. {
  240. fprintf ( stderr, "Failed to open image file: %s\n", filelist[it].c_str() );
  241. fprintf ( stderr, "%s\n", iE.what() );
  242. exit ( -1 );
  243. }
  244. if ( !isInit )
  245. {
  246. imgData.reInit ( img.width(),img.height(),filelist.size(),1 );
  247. isInit = true;
  248. }
  249. for ( int y = 0; y < img.height(); y++ )
  250. {
  251. for ( int x = 0; x < img.width(); x++ )
  252. {
  253. imgData.set ( x, y, it, img.getPixel ( x,y ), 0 );
  254. }
  255. }
  256. }
  257. }
  258. if ( imagetype == IMAGETYPE_GRAY )
  259. {
  260. imgData.correctShading( 0 );
  261. }
  262. }