SemanticSegmentation.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. SemanticSegmentation::SemanticSegmentation ()
  17. {
  18. this->classNames = NULL;
  19. this->imagetype = IMAGETYPE_GRAY;
  20. }
  21. SemanticSegmentation::SemanticSegmentation ( const Config *conf,
  22. const ClassNames *classNames )
  23. {
  24. this->classNames = classNames;
  25. Preprocess::Init ( conf );
  26. std::string imagetype_s = conf->gS ( "main", "imagetype", "gray" );
  27. if ( imagetype_s == "rgb" )
  28. this->imagetype = IMAGETYPE_RGB;
  29. else if ( imagetype_s == "gray" )
  30. this->imagetype = IMAGETYPE_GRAY;
  31. else
  32. {
  33. fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
  34. exit ( -1 );
  35. }
  36. }
  37. SemanticSegmentation::~SemanticSegmentation()
  38. {
  39. }
  40. void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, LabeledSetVector &lvec )
  41. {
  42. #ifdef DEBUG_PRINTS
  43. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  44. #endif
  45. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  46. {
  47. for ( int j = 0; j < ( int ) iter->second.size(); j++ )
  48. {
  49. Vector &tmp = * ( iter->second[j] );
  50. int dim = tmp.size();
  51. SparseVector *vec = new SparseVector ( dim );
  52. for ( int j = 0; j < dim; j++ )
  53. {
  54. if ( tmp[j] != 0.0 )
  55. {
  56. ( *vec ) [j] = tmp[j];
  57. }
  58. }
  59. Example ex;
  60. ex.svec = vec;
  61. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  62. }
  63. }
  64. lvec.clear();
  65. #ifdef DEBUG_PRINTS
  66. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  67. #endif
  68. }
  69. void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec )
  70. {
  71. #ifdef DEBUG_PRINTS
  72. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  73. #endif
  74. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  75. {
  76. for ( int j = 0; j < ( int ) iter->second.size(); j++ )
  77. {
  78. NICE::Vector *vec = new NICE::Vector ( * ( iter->second[j] ) );
  79. Example ex ( vec );
  80. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  81. }
  82. }
  83. lvec.clear();
  84. #ifdef DEBUG_PRINTS
  85. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  86. #endif
  87. }
  88. void SemanticSegmentation::convertExamplesToLSet ( Examples &examples, LabeledSetVector &lvec )
  89. {
  90. #ifdef DEBUG_PRINTS
  91. cout << "SemSegRegionBased::convertExamplesToLSet starts" << endl;
  92. #endif
  93. lvec.clear();
  94. for ( int i = 0; i < ( int ) examples.size(); i++ )
  95. {
  96. if ( examples[i].second.vec != NULL )
  97. {
  98. lvec.add ( examples[i].first, *examples[i].second.vec );
  99. delete examples[i].second.vec;
  100. examples[i].second.vec = NULL;
  101. }
  102. else
  103. {
  104. if ( examples[i].second.svec != NULL )
  105. {
  106. throw ( "Transform SVEC to VEC not yet implemented" );
  107. }
  108. else
  109. {
  110. throw ( "no features for LabeledSet" );
  111. }
  112. }
  113. }
  114. examples.clear();
  115. #ifdef DEBUG_PRINTS
  116. cout << "SemSegRegionBased::convertExamplesToLSet finished" << endl;
  117. #endif
  118. }
  119. void SemanticSegmentation::convertExamplesToVVector ( VVector &feats, Examples &examples, vector<int> &label )
  120. {
  121. #ifdef DEBUG_PRINTS
  122. cout << "SemSegRegionBased::convertExamplesToVVector starts" << endl;
  123. #endif
  124. feats.clear();
  125. label.clear();
  126. for ( int i = 0; i < ( int ) examples.size(); i++ )
  127. {
  128. label.push_back ( examples[i].first );
  129. feats.push_back ( *examples[i].second.vec );
  130. delete examples[i].second.vec;
  131. examples[i].second.vec = NULL;
  132. }
  133. examples.clear();
  134. #ifdef DEBUG_PRINTS
  135. cout << "SemSegRegionBased::convertExamplesToVVector finished" << endl;
  136. #endif
  137. }
  138. void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &examples, vector<int> &label )
  139. {
  140. #ifdef DEBUG_PRINTS
  141. cout << "SemSegRegionBased::convertVVectorToExamples starts" << endl;
  142. #endif
  143. for ( int i = 0; i < ( int ) feats.size(); i++ )
  144. {
  145. NICE::Vector *v = new NICE::Vector ( feats[i] );
  146. Example ex ( v );
  147. ex.position = 0; //TODO: hier mal was besseres überlegen, damit Klassifikator wieder Bildspezifisch lernt
  148. examples.push_back ( pair<int, Example> ( label[i], ex ) );
  149. feats[i].clear();
  150. }
  151. feats.clear();
  152. label.clear();
  153. #ifdef DEBUG_PRINTS
  154. cout << "SemSegRegionBased::convertVVectorToExamples finished" << endl;
  155. #endif
  156. }
  157. void SemanticSegmentation::getDepthVector ( const LabeledSet *Files,
  158. vector<int> & depthVec,
  159. const bool run3Dseg )
  160. {
  161. std::string oldName;
  162. int zsize = 0;
  163. bool isInit = false;
  164. for (LabeledSet::const_iterator it = Files->begin(); it != Files->end(); it++)
  165. {
  166. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  167. jt != it->second.end(); jt++)
  168. {
  169. ImageInfo & info = *(*jt);
  170. std::string file = info.img();
  171. std::vector< std::string > list;
  172. StringTools::split ( file, '/', list );
  173. std::string filename = list.back();
  174. uint found = filename.find_last_of ( "_" );
  175. if (run3Dseg && found < filename.size() && found-3 > 0 )
  176. {
  177. std::string curName = filename.substr ( found-3,3 );
  178. if ( !isInit )
  179. {
  180. oldName = curName;
  181. isInit = true;
  182. }
  183. if ( curName.compare ( oldName ) == 0 ) // if strings match up
  184. {
  185. zsize++;
  186. }
  187. else
  188. {
  189. depthVec.push_back ( zsize );
  190. zsize = 1;
  191. oldName = curName;
  192. }
  193. }
  194. else
  195. {
  196. zsize = 1;
  197. depthVec.push_back ( zsize );
  198. }
  199. }
  200. }
  201. depthVec.push_back ( zsize );
  202. }
  203. void SemanticSegmentation::make3DImage ( const std::vector<std::string> & filelist,
  204. NICE::MultiChannelImage3DT<double> & imgData )
  205. {
  206. bool isInit = false;
  207. for ( int it = 0; it < ( int ) filelist.size(); it++ )
  208. {
  209. if ( imagetype == IMAGETYPE_RGB )
  210. {
  211. NICE::ColorImage img;
  212. try
  213. {
  214. img.read ( filelist[it] );
  215. }
  216. catch ( ImageException iE )
  217. {
  218. fprintf ( stderr, "Failed to open color image file: %s\n", filelist[it].c_str() );
  219. fprintf ( stderr, "%s\n", iE.what() );
  220. exit ( -1 );
  221. }
  222. if ( !isInit )
  223. {
  224. imgData.reInit ( img.width(),img.height(),filelist.size(),3 );
  225. isInit = true;
  226. }
  227. for ( int y = 0; y < img.height(); y++ )
  228. {
  229. for ( int x = 0; x < img.width(); x++ )
  230. {
  231. for ( int r = 0; r < 3; r++ )
  232. {
  233. imgData.set ( x, y, it, img.getPixel ( x,y,r ), r );
  234. }
  235. }
  236. }
  237. }
  238. else
  239. {
  240. NICE::Image img;
  241. try
  242. {
  243. img.read ( filelist[it] );
  244. }
  245. catch ( ImageException iE )
  246. {
  247. fprintf ( stderr, "Failed to open image file: %s\n", filelist[it].c_str() );
  248. fprintf ( stderr, "%s\n", iE.what() );
  249. exit ( -1 );
  250. }
  251. if ( !isInit )
  252. {
  253. imgData.reInit ( img.width(),img.height(),filelist.size(),1 );
  254. isInit = true;
  255. }
  256. for ( int y = 0; y < img.height(); y++ )
  257. {
  258. for ( int x = 0; x < img.width(); x++ )
  259. {
  260. imgData.set ( x, y, it, img.getPixel ( x,y ), 0 );
  261. }
  262. }
  263. }
  264. }
  265. if ( imagetype == IMAGETYPE_GRAY )
  266. {
  267. imgData.equalizeHistogram( 0 );
  268. #ifdef DEBUG_PRINTS
  269. for (int z = 0; z < imgData.depth(); z++)
  270. {
  271. NICE::Image im = imgData.getChannel( z, 0);
  272. im.write( filelist[z]+"_eq.pgm");
  273. }
  274. #endif
  275. }
  276. }
  277. void SemanticSegmentation::getProbabilityMap ( const NICE::MultiChannelImage3DT<double> & prob )
  278. {
  279. std::string s;
  280. for ( int cl = 0; cl < prob.channels(); cl++ )
  281. for ( int z = 0; z < prob.depth(); z++ )
  282. {
  283. NICE::ColorImage img( prob.width(),prob.height() );
  284. NICE::ImageT<double> m = prob.getChannelT(z, cl);
  285. imageToPseudoColor(m, img);
  286. std::stringstream out;
  287. out << "probmap_s" << z << "_c" << cl << ".ppm";
  288. s = out.str();
  289. img.write( s );
  290. //showImage(img, "Probability map");
  291. //getchar();
  292. }
  293. }