ClassNames.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /**
  2. * @file ClassNames.cpp
  3. * @brief simple interface for class name confusion
  4. * @author Erik Rodner
  5. * @date 02/08/2008
  6. */
  7. #include "core/image/ImageT.h"
  8. #include "core/vector/VectorT.h"
  9. #include "core/vector/MatrixT.h"
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <string>
  13. #include <functional>
  14. #include <assert.h>
  15. #include "vislearning/cbaselib/ClassNames.h"
  16. #include "core/basics/StringTools.h"
  17. #include "core/image/ImageTools.h"
  18. #include "vislearning/baselib/ICETools.h"
  19. using namespace OBJREC;
  20. using namespace std;
  21. using namespace NICE;
  22. ClassNames::ClassNames()
  23. {
  24. maxClassNo = 0;
  25. }
  26. ClassNames::ClassNames ( const ClassNames & cn,
  27. const std::string & classselection )
  28. {
  29. std::set<int> selection;
  30. cn.getSelection ( classselection, selection );
  31. maxClassNo = 0;
  32. store ( cerr );
  33. for ( map<string, string>::const_iterator i = cn.tbl_code_text.begin();
  34. i != cn.tbl_code_text.end();
  35. i++ )
  36. {
  37. const std::string & classname = i->second;
  38. std::string code = i->first;
  39. if ( cn.tbl_code_classno.find ( code ) == cn.tbl_code_classno.end() )
  40. {
  41. fprintf ( stderr, "class %s excluded in base classnames\n", code.c_str() );
  42. continue;
  43. }
  44. int classno = cn.classno ( code );
  45. if ( selection.find ( classno ) != selection.end() )
  46. {
  47. addClass ( classno, code, classname );
  48. if ( classno > maxClassNo ) maxClassNo = classno;
  49. #ifdef DEBUG_ClassNames
  50. fprintf ( stderr, "class %s (%d) inherited\n", code.c_str(), classno );
  51. #endif
  52. } else {
  53. #ifdef DEBUG_ClassNames
  54. fprintf ( stderr, "class %s (%d) excluded in selection\n", code.c_str(), classno );
  55. #endif
  56. }
  57. }
  58. }
  59. ClassNames::ClassNames ( const ClassNames & cn )
  60. : tbl_code_text ( cn.tbl_code_text ), tbl_text_code ( cn.tbl_text_code ),
  61. tbl_classno_code ( cn.tbl_classno_code ), tbl_code_classno ( cn.tbl_code_classno ),
  62. tbl_color_classno ( cn.tbl_color_classno ), tbl_classno_color ( cn.tbl_classno_color ), maxClassNo ( cn.maxClassNo )
  63. {
  64. }
  65. ClassNames::~ClassNames()
  66. {
  67. }
  68. int ClassNames::classnoFromText ( std::string text ) const
  69. {
  70. map<string, string>::const_iterator j = tbl_text_code.find ( text );
  71. if ( j == tbl_text_code.end() ) return -1;
  72. map<string, int>::const_iterator jj = tbl_code_classno.find ( j->second );
  73. if ( jj == tbl_code_classno.end() ) return -1;
  74. return jj->second;
  75. }
  76. void ClassNames::getSelection ( const std::string & classselection,
  77. std::set<int> & classnos ) const
  78. {
  79. if ( classselection.size() <= 0 ) return;
  80. std::vector<string> classlist;
  81. StringTools::split ( classselection, ',', classlist );
  82. if ( classlist.size() <= 0 )
  83. {
  84. fprintf ( stderr, "FATAL ERROR: wrong format for classselection\n" );
  85. exit ( -1 );
  86. } else if ( classlist[0] == "*" )
  87. {
  88. map<string, bool> forbidden_classes;
  89. for ( size_t k = 1 ; k < classlist.size() ; k++ )
  90. if ( classlist[k].substr ( 0, 1 ) == "-" )
  91. {
  92. std::string f_class = classlist[k].substr ( 1 );
  93. #if defined DEBUG_ClassNames
  94. fprintf ( stderr, "ClassNames: class %s excluded !\n", f_class.c_str() );
  95. #endif
  96. forbidden_classes[ f_class ] = true;
  97. } else {
  98. fprintf ( stderr, "FATAL ERROR: wrong format for classselection: *,-class0,class1,...\n" );
  99. exit ( -1 );
  100. }
  101. for ( map<int, string>::const_iterator i = tbl_classno_code.begin();
  102. i != tbl_classno_code.end();
  103. i++ )
  104. {
  105. int myclassno = i->first;
  106. const std::string & classname = text ( myclassno );
  107. if ( forbidden_classes.find ( classname ) != forbidden_classes.end() )
  108. continue;
  109. classnos.insert ( myclassno );
  110. }
  111. } else {
  112. for ( vector<string>::const_iterator i = classlist.begin();
  113. i != classlist.end();
  114. i++ )
  115. {
  116. const std::string & classname = *i;
  117. map<string, string>::const_iterator j = tbl_text_code.find ( classname );
  118. if ( j == tbl_text_code.end() )
  119. {
  120. fprintf ( stderr, "ClassNames: FATAL ERROR This is not a selection of a subset: %s [%s]\n",
  121. classname.c_str(), classselection.c_str() );
  122. exit ( -1 );
  123. }
  124. const std::string & code = j->second;
  125. int myclassno = classno ( code );
  126. if ( myclassno < 0 ) {
  127. fprintf ( stderr, "ClassNames: FATAL ERROR This is not a selection of a subset\n" );
  128. exit ( -1 );
  129. }
  130. classnos.insert ( myclassno );
  131. }
  132. }
  133. }
  134. std::string ClassNames::text ( int classno ) const
  135. {
  136. map<string, string>::const_iterator i =
  137. tbl_code_text.find ( code ( classno ) );
  138. if ( i == tbl_code_text.end() )
  139. {
  140. fprintf ( stderr, "ClassNames: no name found for classno %d\n", classno );
  141. return "unknown";
  142. } else {
  143. return i->second;
  144. }
  145. }
  146. std::string ClassNames::code ( int classno ) const
  147. {
  148. map<int, string>::const_iterator i =
  149. tbl_classno_code.find ( classno );
  150. if ( i == tbl_classno_code.end() )
  151. {
  152. fprintf ( stderr, "ClassNames: no code found for classno %d\n", classno );
  153. return "unknown";
  154. } else {
  155. return i->second;
  156. }
  157. }
  158. int ClassNames::classno ( std::string code ) const
  159. {
  160. map<string, int>::const_iterator i =
  161. tbl_code_classno.find ( code );
  162. if ( i == tbl_code_classno.end() )
  163. {
  164. fthrow ( Exception, "no classno found for code <" << code << ">" );
  165. } else {
  166. return i->second;
  167. }
  168. }
  169. int ClassNames::numClasses () const
  170. {
  171. return tbl_classno_code.size();
  172. }
  173. void ClassNames::addClass ( int classno, const std::string & code,
  174. const std::string & text )
  175. {
  176. tbl_classno_code[classno] = code;
  177. tbl_text_code[text] = code;
  178. tbl_code_text[code] = text;
  179. tbl_code_classno[code] = classno;
  180. if ( classno > maxClassNo ) maxClassNo = classno;
  181. }
  182. bool ClassNames::existsClassno ( int classno ) const
  183. {
  184. return ( tbl_classno_code.find ( classno ) != tbl_classno_code.end() );
  185. }
  186. // refactor-nice.pl: check this substitution
  187. // old: bool ClassNames::existsClassCode ( const string & classcode ) const
  188. bool ClassNames::existsClassCode ( const std::string & classcode ) const
  189. {
  190. return ( tbl_code_classno.find ( classcode ) != tbl_code_classno.end() );
  191. }
  192. bool ClassNames::readFromConfig ( const Config & datasetconf,
  193. // refactor-nice.pl: check this substitution
  194. // old: const string & classselection )
  195. const std::string & classselection )
  196. {
  197. datasetconf.getAllS ( "classnames", tbl_code_text );
  198. if ( tbl_code_text.size() <= 0 ) {
  199. fprintf ( stderr, "ClassNames: no classnames specified\n" );
  200. return false;
  201. }
  202. // reverse map and lower case
  203. for ( map<string, string>::const_iterator i = tbl_code_text.begin();
  204. i != tbl_code_text.end(); i++ )
  205. tbl_text_code [ i->second ] = i->first;
  206. #if defined DEBUG_ClassNames
  207. cerr << "ClassNames::read: selection = " << classselection << endl;
  208. #endif
  209. std::vector<string> classlist;
  210. StringTools::split ( classselection, ',', classlist );
  211. if ( classlist.size() <= 0 )
  212. {
  213. fprintf ( stderr, "FATAL ERROR: wrong format for classselection\n" );
  214. exit ( -1 );
  215. } else if ( classlist[0] == "*" )
  216. {
  217. map<string, bool> forbidden_classes;
  218. for ( size_t k = 1 ; k < classlist.size() ; k++ )
  219. if ( classlist[k].substr ( 0, 1 ) == "-" )
  220. {
  221. // refactor-nice.pl: check this substitution
  222. // old: string f_class = classlist[k].substr(1);
  223. std::string f_class = classlist[k].substr ( 1 );
  224. #if defined DEBUG_ClassNames
  225. fprintf ( stderr, "ClassNames: class %s excluded !\n", f_class.c_str() );
  226. #endif
  227. forbidden_classes[ f_class ] = true;
  228. } else {
  229. fprintf ( stderr, "FATAL ERROR: wrong format for classselection: *,-class0,class1,...\n" );
  230. exit ( -1 );
  231. }
  232. int classno_seq = 0;
  233. for ( map<string, string>::const_iterator i = tbl_code_text.begin();
  234. i != tbl_code_text.end();
  235. i++, classno_seq++ )
  236. {
  237. const std::string & classname = i->second;
  238. if ( forbidden_classes.find ( classname ) != forbidden_classes.end() )
  239. continue;
  240. // refactor-nice.pl: check this substitution
  241. // old: string code = tbl_text_code [ i->second ];
  242. std::string code = tbl_text_code [ i->second ];
  243. int classno;
  244. classno = classno_seq;
  245. tbl_classno_code[classno] = code;
  246. tbl_code_classno[code] = classno;
  247. if ( classno > maxClassNo ) maxClassNo = classno;
  248. #if defined DEBUG_ClassNames
  249. fprintf ( stderr, "classno %d class code %s class text %s\n", classno, code.c_str(), classname.c_str() );
  250. #endif
  251. }
  252. } else {
  253. #if defined DEBUG_ClassNames
  254. cerr << "list : " << classlist.size() << endl;
  255. #endif
  256. for ( size_t classno_seq = 0 ; classno_seq < classlist.size() ; classno_seq++ )
  257. {
  258. std::string classname = classlist[classno_seq];
  259. if ( tbl_text_code.find ( classname ) != tbl_text_code.end() )
  260. {
  261. std::string code = tbl_text_code [ classname ];
  262. int classno;
  263. classno = classno_seq;
  264. tbl_classno_code[classno] = code;
  265. tbl_code_classno[code] = classno;
  266. if ( classno > maxClassNo ) maxClassNo = classno;
  267. #if defined DEBUG_ClassNames
  268. fprintf ( stderr, "classno %d class code %s class text %s\n", ( int ) classno, code.c_str(), classname.c_str() );
  269. #endif
  270. } else {
  271. fprintf ( stderr, "ClassNames::ClassNames: FATAL ERROR class >%s< not found in data set\n", classname.c_str() );
  272. exit ( -1 );
  273. }
  274. }
  275. }
  276. /****** after all, try to read color coding *******/
  277. map<string, string> list;
  278. datasetconf.getAllS ( "colors", list );
  279. if ( list.size() > 0 ) {
  280. for ( map<string, string>::const_iterator i = list.begin();
  281. i != list.end();
  282. i++ )
  283. {
  284. std::string value = i->second;
  285. std::string classname = i->first;
  286. int _classno = classno ( classname );
  287. vector<string> submatches;
  288. if ( StringTools::regexMatch ( value, "^ *([[:digit:]]+) *: *([[:digit:]]+) *: *([[:digit:]]+) *$", submatches )
  289. && ( submatches.size() == 4 ) )
  290. {
  291. int r = StringTools::convert<int> ( submatches[1] );
  292. int g = StringTools::convert<int> ( submatches[2] );
  293. int b = StringTools::convert<int> ( submatches[3] );
  294. long index = 256 * ( 256 * r + g ) + b;
  295. tbl_color_classno[index] = _classno;
  296. tbl_classno_color[_classno] = index;
  297. } else {
  298. fprintf ( stderr, "LabeledFileList: parse error colors >%s<\n", value.c_str() );
  299. exit ( -1 );
  300. }
  301. }
  302. }
  303. return true;
  304. }
  305. int ClassNames::getMaxClassno () const
  306. {
  307. return maxClassNo;
  308. }
  309. void ClassNames::getRGBColor ( int classno, int & r, int & g, int & b ) const
  310. {
  311. map<int, long>::const_iterator i = tbl_classno_color.find ( classno );
  312. if ( i == tbl_classno_color.end() )
  313. {
  314. fprintf ( stderr, "ClassNames: no color setting found for class %d\n", classno );
  315. getchar();
  316. double x = classno / ( double ) numClasses();
  317. double rd, gd, bd;
  318. convertToPseudoColor ( x, rd, gd, bd );
  319. r = ( int ) ( 255 * rd );
  320. g = ( int ) ( 255 * gd );
  321. b = ( int ) ( 255 * bd );
  322. } else {
  323. long color = i->second;
  324. b = color % 256;
  325. color /= 256;
  326. g = color % 256;
  327. color /= 256;
  328. r = color % 256;
  329. }
  330. }
  331. void ClassNames::getClassnoFromColor ( int & classno, int r, int g, int b ) const
  332. {
  333. long color = 256 * ( 256 * r + g ) + b;
  334. #if defined WIN32 && defined NICE_USELIB_BOOST
  335. boost::unordered_map<long, int>::const_iterator i = tbl_color_classno.find ( color );
  336. #else
  337. // __gnu_cxx::hash_map<long, int>::const_iterator i = tbl_color_classno.find ( color );
  338. std::tr1::unordered_map<long, int>::const_iterator i = tbl_color_classno.find ( color );
  339. #endif
  340. if ( i == tbl_color_classno.end() )
  341. {
  342. classno = -1;
  343. } else {
  344. classno = i->second;
  345. }
  346. }
  347. void ClassNames::labelToRGB ( const NICE::Image & img, NICE::ColorImage & rgb ) const
  348. {
  349. int red, green, blue;
  350. rgb.resize ( img.width(), img.height() );
  351. for ( int y = 0 ; y < img.height(); y++ )
  352. for ( int x = 0 ; x < img.width(); x++ )
  353. {
  354. int label = img.getPixel ( x, y );
  355. getRGBColor ( label, red, green, blue );
  356. rgb.setPixel ( x, y, 0, red );
  357. rgb.setPixel ( x, y, 1, green );
  358. rgb.setPixel ( x, y, 2, blue );
  359. }
  360. }
  361. int ClassNames::getBackgroundClass () const
  362. {
  363. if ( existsClassCode ( "various" ) )
  364. return classno ( "various" );
  365. else if ( existsClassCode ( "background" ) )
  366. return classno ( "background" );
  367. else if ( existsClassCode ( "clutter" ) )
  368. return classno ( "clutter" );
  369. else
  370. return 0;
  371. }
  372. void ClassNames::restore ( istream & is, int format )
  373. {
  374. maxClassNo = -1;
  375. while ( ! is.eof() )
  376. {
  377. std::string mytext;
  378. std::string mycode;
  379. int myclassno;
  380. if ( ! ( is >> mytext ) ) break;
  381. if ( mytext == "end" ) break;
  382. if ( ! ( is >> mycode ) ) break;
  383. if ( ! ( is >> myclassno ) ) break;
  384. tbl_code_text.insert ( pair<string, string> ( mycode, mytext ) );
  385. tbl_text_code.insert ( pair<string, string> ( mytext, mycode ) );
  386. tbl_classno_code.insert ( pair<int, string> ( myclassno, mycode ) );
  387. tbl_code_classno.insert ( pair<string, int> ( mycode, myclassno ) );
  388. if ( myclassno > maxClassNo ) maxClassNo = myclassno;
  389. }
  390. }
  391. void ClassNames::store ( ostream & os, int format ) const
  392. {
  393. assert ( tbl_classno_code.size() == tbl_code_classno.size() );
  394. for ( map<int, string>::const_iterator i = tbl_classno_code.begin() ;
  395. i != tbl_classno_code.end();
  396. i++ )
  397. {
  398. int myclassno = i->first;
  399. std::string mycode = i->second;
  400. std::string mytext = text ( myclassno );
  401. os << mytext << "\t" << mycode << "\t" << myclassno << endl;
  402. }
  403. os << "end" << endl;
  404. }
  405. void ClassNames::clear ()
  406. {
  407. tbl_code_text.clear();
  408. tbl_text_code.clear();
  409. tbl_classno_code.clear();
  410. tbl_code_classno.clear();
  411. tbl_color_classno.clear();
  412. tbl_classno_color.clear();
  413. }