SemanticSegmentation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /**
  2. * @file SemanticSegmentation.cpp
  3. * @brief abstract interface for semantic segmentation algorithms
  4. * @author Erik Rodner, Alexander Freytag, 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. : iterationCountSuffix(1)
  18. {
  19. this->classNames = NULL;
  20. this->imagetype = IMAGETYPE_GRAY;
  21. }
  22. SemanticSegmentation::SemanticSegmentation ( const Config *conf,
  23. const ClassNames *classNames )
  24. : iterationCountSuffix(1)
  25. {
  26. this->classNames = classNames;
  27. this->initFromConfig( conf );
  28. }
  29. SemanticSegmentation::~SemanticSegmentation()
  30. {
  31. }
  32. void SemanticSegmentation::initFromConfig(const Config* conf, const string& s_confSection)
  33. {
  34. std::string imagetype_s = conf->gS ( "main", "imagetype", "rgb" );
  35. if ( imagetype_s == "rgb" )
  36. imagetype = IMAGETYPE_RGB;
  37. else if ( imagetype_s == "gray" )
  38. imagetype = IMAGETYPE_GRAY;
  39. else {
  40. fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
  41. exit ( -1 );
  42. }
  43. // dangerous!!!
  44. Preprocess::Init ( conf );
  45. }
  46. ///////////////////// ///////////////////// /////////////////////
  47. // DATA CONVERSION
  48. ///////////////////// ///////////////////// /////////////////////
  49. void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, LabeledSetVector &lvec )
  50. {
  51. #ifdef DEBUG_PRINTS
  52. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  53. #endif
  54. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  55. {
  56. for ( int j = 0; j < ( int ) iter->second.size(); j++ )
  57. {
  58. Vector &tmp = * ( iter->second[j] );
  59. int dim = tmp.size();
  60. SparseVector *vec = new SparseVector ( dim );
  61. for ( int j = 0; j < dim; j++ )
  62. {
  63. if ( tmp[j] != 0.0 )
  64. {
  65. ( *vec ) [j] = tmp[j];
  66. }
  67. }
  68. Example ex;
  69. ex.svec = vec;
  70. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  71. }
  72. }
  73. lvec.clear();
  74. #ifdef DEBUG_PRINTS
  75. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  76. #endif
  77. }
  78. void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec )
  79. {
  80. #ifdef DEBUG_PRINTS
  81. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  82. #endif
  83. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  84. {
  85. for ( int j = 0; j < ( int ) iter->second.size(); j++ )
  86. {
  87. NICE::Vector *vec = new NICE::Vector ( * ( iter->second[j] ) );
  88. Example ex ( vec );
  89. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  90. }
  91. }
  92. lvec.clear();
  93. #ifdef DEBUG_PRINTS
  94. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  95. #endif
  96. }
  97. void SemanticSegmentation::convertExamplesToLSet ( Examples &examples, LabeledSetVector &lvec )
  98. {
  99. #ifdef DEBUG_PRINTS
  100. cout << "SemSegRegionBased::convertExamplesToLSet starts" << endl;
  101. #endif
  102. lvec.clear();
  103. for ( int i = 0; i < ( int ) examples.size(); i++ )
  104. {
  105. if ( examples[i].second.vec != NULL )
  106. {
  107. lvec.add ( examples[i].first, *examples[i].second.vec );
  108. delete examples[i].second.vec;
  109. examples[i].second.vec = NULL;
  110. }
  111. else
  112. {
  113. if ( examples[i].second.svec != NULL )
  114. {
  115. throw ( "Transform SVEC to VEC not yet implemented" );
  116. }
  117. else
  118. {
  119. throw ( "no features for LabeledSet" );
  120. }
  121. }
  122. }
  123. examples.clear();
  124. #ifdef DEBUG_PRINTS
  125. cout << "SemSegRegionBased::convertExamplesToLSet finished" << endl;
  126. #endif
  127. }
  128. void SemanticSegmentation::convertExamplesToVVector ( VVector &feats, Examples &examples, vector<int> &label )
  129. {
  130. #ifdef DEBUG_PRINTS
  131. cout << "SemSegRegionBased::convertExamplesToVVector starts" << endl;
  132. #endif
  133. feats.clear();
  134. label.clear();
  135. for ( int i = 0; i < ( int ) examples.size(); i++ )
  136. {
  137. label.push_back ( examples[i].first );
  138. feats.push_back ( *examples[i].second.vec );
  139. delete examples[i].second.vec;
  140. examples[i].second.vec = NULL;
  141. }
  142. examples.clear();
  143. #ifdef DEBUG_PRINTS
  144. cout << "SemSegRegionBased::convertExamplesToVVector finished" << endl;
  145. #endif
  146. }
  147. void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &examples, vector<int> &label )
  148. {
  149. #ifdef DEBUG_PRINTS
  150. cout << "SemSegRegionBased::convertVVectorToExamples starts" << endl;
  151. #endif
  152. for ( int i = 0; i < ( int ) feats.size(); i++ )
  153. {
  154. NICE::Vector *v = new NICE::Vector ( feats[i] );
  155. Example ex ( v );
  156. ex.position = 0; //TODO: hier mal was besseres überlegen, damit Klassifikator wieder Bildspezifisch lernt
  157. examples.push_back ( pair<int, Example> ( label[i], ex ) );
  158. feats[i].clear();
  159. }
  160. feats.clear();
  161. label.clear();
  162. #ifdef DEBUG_PRINTS
  163. cout << "SemSegRegionBased::convertVVectorToExamples finished" << endl;
  164. #endif
  165. }
  166. void SemanticSegmentation::getDepthVector ( const LabeledSet *Files,
  167. vector<int> & depthVec,
  168. const bool run3Dseg )
  169. {
  170. std::string oldName;
  171. int zsize = 0;
  172. bool isInit = false;
  173. for (LabeledSet::const_iterator it = Files->begin(); it != Files->end(); it++)
  174. {
  175. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  176. jt != it->second.end(); jt++)
  177. {
  178. ImageInfo & info = *(*jt);
  179. std::string file = info.img();
  180. std::vector< std::string > list;
  181. StringTools::split ( file, '/', list );
  182. std::string filename = list.back();
  183. uint found = filename.find_last_of ( "_" );
  184. if (run3Dseg && found < filename.size() && found-3 > 0 )
  185. {
  186. std::string curName = filename.substr ( found-3,3 );
  187. if ( !isInit )
  188. {
  189. oldName = curName;
  190. isInit = true;
  191. }
  192. if ( curName.compare ( oldName ) == 0 ) // if strings match up
  193. {
  194. zsize++;
  195. }
  196. else
  197. {
  198. depthVec.push_back ( zsize );
  199. zsize = 1;
  200. oldName = curName;
  201. }
  202. }
  203. else
  204. {
  205. zsize = 1;
  206. depthVec.push_back ( zsize );
  207. }
  208. }
  209. }
  210. depthVec.push_back ( zsize );
  211. }
  212. void SemanticSegmentation::make3DImage ( const std::vector<std::string> & filelist,
  213. NICE::MultiChannelImage3DT<double> & imgData )
  214. {
  215. bool isInit = false;
  216. for ( int it = 0; it < ( int ) filelist.size(); it++ )
  217. {
  218. if ( imagetype == IMAGETYPE_RGB )
  219. {
  220. NICE::ColorImage img;
  221. try
  222. {
  223. img.read ( filelist[it] );
  224. }
  225. catch ( ImageException iE )
  226. {
  227. fprintf ( stderr, "Failed to open color image file: %s\n", filelist[it].c_str() );
  228. fprintf ( stderr, "%s\n", iE.what() );
  229. exit ( -1 );
  230. }
  231. if ( !isInit )
  232. {
  233. imgData.reInit ( img.width(),img.height(),filelist.size(),3 );
  234. isInit = true;
  235. }
  236. for ( int y = 0; y < img.height(); y++ )
  237. {
  238. for ( int x = 0; x < img.width(); x++ )
  239. {
  240. for ( int r = 0; r < 3; r++ )
  241. {
  242. imgData.set ( x, y, it, img.getPixel ( x,y,r ), r );
  243. }
  244. }
  245. }
  246. }
  247. else
  248. {
  249. NICE::Image img;
  250. try
  251. {
  252. img.read ( filelist[it] );
  253. }
  254. catch ( ImageException iE )
  255. {
  256. fprintf ( stderr, "Failed to open image file: %s\n", filelist[it].c_str() );
  257. fprintf ( stderr, "%s\n", iE.what() );
  258. exit ( -1 );
  259. }
  260. if ( !isInit )
  261. {
  262. imgData.reInit ( img.width(),img.height(),filelist.size(),1 );
  263. isInit = true;
  264. }
  265. for ( int y = 0; y < img.height(); y++ )
  266. {
  267. for ( int x = 0; x < img.width(); x++ )
  268. {
  269. imgData.set ( x, y, it, img.getPixel ( x,y ), 0 );
  270. }
  271. }
  272. }
  273. }
  274. if ( imagetype == IMAGETYPE_GRAY )
  275. {
  276. imgData.equalizeHistogram( 0 );
  277. #ifdef DEBUG_PRINTS
  278. for (int z = 0; z < imgData.depth(); z++)
  279. {
  280. NICE::Image im = imgData.getChannel( z, 0);
  281. im.write( filelist[z]+"_eq.pgm");
  282. }
  283. #endif
  284. }
  285. }
  286. void SemanticSegmentation::getProbabilityMap ( const NICE::MultiChannelImage3DT<double> & prob )
  287. {
  288. std::string s;
  289. for ( int cl = 0; cl < prob.channels(); cl++ )
  290. for ( int z = 0; z < prob.depth(); z++ )
  291. {
  292. NICE::ColorImage img( prob.width(),prob.height() );
  293. NICE::ImageT<double> m = prob.getChannelT(z, cl);
  294. imageToPseudoColor(m, img);
  295. std::stringstream out;
  296. out << "probmap_s" << z << "_c" << cl << ".ppm";
  297. s = out.str();
  298. img.write( s );
  299. //showImage(img, "Probability map");
  300. //getchar();
  301. }
  302. }
  303. ///////////////////// INTERFACE PERSISTENT /////////////////////
  304. // interface specific methods for store and restore
  305. ///////////////////// INTERFACE PERSISTENT /////////////////////
  306. void SemanticSegmentation::restore ( std::istream & is, int format )
  307. {
  308. //delete everything we knew so far...
  309. this->clear();
  310. bool b_restoreVerbose ( false );
  311. #ifdef B_RESTOREVERBOSE
  312. b_restoreVerbose = true;
  313. #endif
  314. if ( is.good() )
  315. {
  316. if ( b_restoreVerbose )
  317. std::cerr << " restore SemanticSegmentation" << std::endl;
  318. std::string tmp;
  319. is >> tmp; //class name
  320. if ( ! this->isStartTag( tmp, "SemanticSegmentation" ) )
  321. {
  322. std::cerr << " WARNING - attempt to restore SemanticSegmentation, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  323. throw;
  324. }
  325. is.precision (numeric_limits<double>::digits10 + 1);
  326. bool b_endOfBlock ( false ) ;
  327. while ( !b_endOfBlock )
  328. {
  329. is >> tmp; // start of block
  330. if ( this->isEndTag( tmp, "SemanticSegmentation" ) )
  331. {
  332. b_endOfBlock = true;
  333. continue;
  334. }
  335. tmp = this->removeStartTag ( tmp );
  336. if ( b_restoreVerbose )
  337. std::cerr << " currently restore section " << tmp << " in SemanticSegmentation" << std::endl;
  338. if ( tmp.compare("classNames") == 0 )
  339. {
  340. //dirty solution to circumvent the const-flag
  341. const_cast<ClassNames*>(this->classNames)->restore ( is, format );
  342. is >> tmp; // end of block
  343. tmp = this->removeEndTag ( tmp );
  344. }
  345. else if ( tmp.compare("imagetype") == 0 )
  346. {
  347. unsigned int ui_imagetyp;
  348. is >> ui_imagetyp;
  349. this->imagetype = static_cast<IMAGETYP> ( ui_imagetyp );
  350. is >> tmp; // end of block
  351. tmp = this->removeEndTag ( tmp );
  352. }
  353. else if ( tmp.compare("iterationCountSuffix") == 0 )
  354. {
  355. is >> this->iterationCountSuffix;
  356. is >> tmp; // end of block
  357. tmp = this->removeEndTag ( tmp );
  358. }
  359. else
  360. {
  361. std::cerr << "WARNING -- unexpected SemanticSegmentation object -- " << tmp << " -- for restoration... aborting" << std::endl;
  362. throw;
  363. }
  364. }
  365. }
  366. else
  367. {
  368. std::cerr << "SemanticSegmentation::restore -- InStream not initialized - restoring not possible!" << std::endl;
  369. throw;
  370. }
  371. //TODO check whether we also have to do something linke Preprocess::Init ( conf );
  372. }
  373. void SemanticSegmentation::store ( std::ostream & os, int format ) const
  374. {
  375. if (os.good())
  376. {
  377. // show starting point
  378. os << this->createStartTag( "SemanticSegmentation" ) << std::endl;
  379. os.precision (numeric_limits<double>::digits10 + 1);
  380. os << this->createStartTag( "classNames" ) << std::endl;
  381. this->classNames->store ( os, format );
  382. os << this->createEndTag( "classNames" ) << std::endl;
  383. //
  384. os << this->createStartTag( "imagetype" ) << std::endl;
  385. os << imagetype << std::endl;
  386. os << this->createEndTag( "imagetype" ) << std::endl;
  387. //
  388. os << this->createStartTag( "iterationCountSuffix" ) << std::endl;
  389. os << iterationCountSuffix << std::endl;
  390. os << this->createEndTag( "iterationCountSuffix" ) << std::endl;
  391. // done
  392. os << this->createEndTag( "SemanticSegmentation" ) << std::endl;
  393. }
  394. else
  395. {
  396. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  397. }
  398. }
  399. void SemanticSegmentation::clear ()
  400. {
  401. //TODO
  402. }