SemSegCsurka.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  1. #include <sstream>
  2. #include <iostream>
  3. #include "SemSegCsurka.h"
  4. #include "vislearning/baselib/ICETools.h"
  5. #include "core/image/Filter.h"
  6. #include "semseg/semseg/postsegmentation/PSSImageLevelPrior.h"
  7. using namespace std;
  8. using namespace NICE;
  9. using namespace OBJREC;
  10. #undef DEBUG_CSURK
  11. #undef UNCERTAINTY
  12. SemSegCsurka::SemSegCsurka ( const Config *conf,
  13. const MultiDataset *md )
  14. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  15. {
  16. this->conf = conf;
  17. opSiftImpl = conf->gS ( "Descriptor", "implementation", "VANDESANDE" );
  18. readfeat = conf->gB ( "Descriptor", "read", true );
  19. writefeat = conf->gB ( "Descriptor", "write", true );
  20. #ifdef DEBUG_CSURK
  21. clog << "[log] SemSegCsurka::SemSegCsurka: OppenentSift implemenation: " << opSiftImpl << endl;
  22. #endif
  23. save_cache = conf->gB ( "FPCPixel", "save_cache", true );
  24. read_cache = conf->gB ( "FPCPixel", "read_cache", false );
  25. cache = conf->gS ( "cache", "root", "" );
  26. sigmaweight = conf->gD ( "SemSegCsurka", "sigmaweight", 0.6 );
  27. dim = conf->gI ( "SemSegCsurka", "pcadim", 50 );
  28. usepca = conf->gB ( "SemSegCsurka", "usepca", true );
  29. calcpca = conf->gB ( "SemSegCsurka", "calcpca", false );
  30. usegmm = conf->gB ( "SemSegCsurka", "usegmm", false );
  31. norm = conf->gB ( "SemSegCsurka", "normalize", false );
  32. usefisher = conf->gB ( "SemSegCsurka", "usefisher", false );
  33. dogmm = conf->gB ( "SemSegCsurka", "dogmm", false );
  34. gaussians = conf->gI ( "SemSegCsurka", "gaussians", 50 );
  35. usekmeans = conf->gB ( "SemSegCsurka", "usekmeans", false );
  36. kmeansfeat = conf->gI ( "SemSegCsurka", "kmeansfeat", 50 );
  37. kmeanshard = conf->gB ( "SemSegCsurka", "kmeanshard", false );
  38. cname = conf->gS ( "SemSegCsurka", "classifier", "RandomForests" );
  39. anteil = conf->gD ( "SemSegCsurka", "anteil", 1.0 );
  40. userellocprior = conf->gB ( "SemSegCsurka", "rellocfeat", false );
  41. bool usesrg = conf->gB ( "SemSegCsurka", "usesrg", false );
  42. useregions = conf->gB ( "SemSegCsurka", "useregions", true );
  43. savesteps = conf->gB ( "SemSegCsurka", "savesteps", true );
  44. bool usegcopt = conf->gB ( "SemSegCsurka", "usegcopt", false );
  45. bestclasses = conf->gI ( "SemSegCsurka", "bestclasses", 0 );
  46. smoothhl = conf->gB ( "SemSegCsurka", "smoothhl", false );
  47. smoothfactor = conf->gD ( "SemSegCsurka", "smoothfactor", 1.0 );
  48. usecolorfeats = conf->gB ( "SemSegCsurka", "usecolorfeats", false );
  49. string rsMethod = conf->gS ( "SemSegCsurka", "segmentation", "meanshift" );
  50. g = NULL;
  51. k = NULL;
  52. relloc = NULL;
  53. srg = NULL;
  54. gcopt = NULL;
  55. if ( !useregions && ( userellocprior || usesrg ) )
  56. {
  57. cerr << "relative location priors and super region growing are just supported in combination with useregions" << endl;
  58. exit ( 1 );
  59. }
  60. if ( usepca )
  61. pca = PCA ( dim );
  62. RegionSegmentationMethod * tmpseg;
  63. if ( rsMethod == "meanshift" )
  64. tmpseg = new RSMeanShift ( conf );
  65. else
  66. tmpseg = new RSGraphBased ( conf );
  67. if ( save_cache )
  68. seg = new RSCache ( conf, tmpseg );
  69. else
  70. seg = tmpseg;
  71. if ( userellocprior )
  72. relloc = new RelativeLocationPrior ( conf );
  73. else
  74. relloc = NULL;
  75. #ifdef NICE_USELIB_ICE
  76. if ( usesrg )
  77. srg = new PPSuperregion ( conf );
  78. else
  79. srg = NULL;
  80. #else
  81. srg = NULL;
  82. #endif
  83. if ( usegcopt )
  84. gcopt = new PPGraphCut ( conf );
  85. else
  86. gcopt = NULL;
  87. classifier = NULL;
  88. vclassifier = NULL;
  89. if ( cname == "RandomForests" )
  90. classifier = new FPCRandomForests ( conf, "ClassifierForest" );
  91. else if ( cname == "SMLR" )
  92. classifier = new FPCSMLR ( conf, "ClassifierSMLR" );
  93. else if ( cname == "GPHIK" )
  94. classifier = new GPHIKClassifierNICE ( conf, "ClassiferGPHIK" );
  95. else
  96. vclassifier = CSGeneric::selectVecClassifier ( conf, "main" );
  97. //classifier = new FPCSparseMultinomialLogisticRegression(conf, "ClassifierSMLR");
  98. if ( classifier != NULL )
  99. classifier->setMaxClassNo ( classNames->getMaxClassno() );
  100. else
  101. vclassifier->setMaxClassNo ( classNames->getMaxClassno() );
  102. cn = md->getClassNames ( "train" );
  103. if ( read_cache )
  104. {
  105. fprintf ( stderr, "SemSegCsurka:: Reading classifier data from %s\n", ( cache + "/fpcrf.data" ).c_str() );
  106. if ( classifier != NULL )
  107. classifier->read ( cache + "/fpcrf.data" );
  108. else
  109. vclassifier->read ( cache + "/veccl.data" );
  110. if ( usepca )
  111. {
  112. std::string filename = cache + "/pca";
  113. pca.read ( filename );
  114. }
  115. if ( usegmm )
  116. {
  117. g = new GMM ( conf, gaussians );
  118. if ( !g->loadData ( cache + "/gmm" ) )
  119. {
  120. cerr << "SemSegCsurka:: no gmm file found" << endl;
  121. exit ( -1 );
  122. }
  123. }
  124. else {
  125. g = NULL;
  126. }
  127. if ( usekmeans )
  128. {
  129. k = new KMeansOnline ( gaussians );
  130. }
  131. fprintf ( stderr, "SemSegCsurka:: successfully read\n" );
  132. std::string filename = cache + "/rlp";
  133. FILE *value;
  134. value = fopen ( filename.c_str(), "r" );
  135. if ( value == NULL )
  136. {
  137. trainpostprocess ( md );
  138. }
  139. else
  140. {
  141. if ( userellocprior )
  142. {
  143. relloc->read ( filename );
  144. }
  145. }
  146. filename = cache + "/srg";
  147. value = fopen ( filename.c_str(), "r" );
  148. if ( value == NULL )
  149. {
  150. trainpostprocess ( md );
  151. }
  152. else
  153. {
  154. if ( srg != NULL )
  155. {
  156. srg->read ( filename );
  157. }
  158. }
  159. }
  160. else
  161. {
  162. train ( md );
  163. }
  164. }
  165. SemSegCsurka::~SemSegCsurka()
  166. {
  167. // clean-up
  168. if ( classifier != NULL )
  169. delete classifier;
  170. if ( vclassifier != NULL )
  171. delete vclassifier;
  172. if ( seg != NULL )
  173. delete seg;
  174. g = NULL;
  175. if ( g != NULL )
  176. delete g;
  177. }
  178. void SemSegCsurka::normalize ( Examples &ex )
  179. {
  180. assert ( ex.size() > 0 );
  181. if ( vecmin.size() == 0 )
  182. {
  183. for ( int j = 0; j < ( int ) ex[0].second.vec->size(); j++ )
  184. {
  185. double maxv = -numeric_limits<int>::max();
  186. double minv = numeric_limits<int>::max();
  187. for ( int i = 0; i < ( int ) ex.size(); i++ )
  188. {
  189. maxv = std::max ( maxv, ( *ex[i].second.vec ) [j] );
  190. minv = std::min ( minv, ( *ex[i].second.vec ) [j] );
  191. }
  192. vecmin.push_back ( minv );
  193. vecmax.push_back ( maxv );
  194. }
  195. }
  196. for ( int i = 0; i < ( int ) ex.size(); i++ )
  197. {
  198. for ( int j = 0; j < ( int ) ex[i].second.vec->size(); j++ )
  199. {
  200. ( *ex[i].second.vec ) [j] = ( ( *ex[i].second.vec ) [j] - vecmin[j] ) / ( vecmax[j] - vecmin[j] );
  201. }
  202. }
  203. return;
  204. }
  205. void SemSegCsurka::convertLowToHigh ( Examples &ex, double reduce )
  206. {
  207. cout << "converting low-level features to high-level features" << endl;
  208. if ( reduce >= 1.0 )
  209. {
  210. for ( int i = 0; i < ( int ) ex.size(); i++ )
  211. {
  212. SparseVector *f = new SparseVector();
  213. if ( usekmeans )
  214. {
  215. k->getDist ( *ex[i].second.vec, *f, kmeansfeat, kmeanshard );
  216. }
  217. else
  218. {
  219. if ( usefisher )
  220. g->getFisher ( *ex[i].second.vec, *f );
  221. else
  222. g->getProbs ( *ex[i].second.vec, *f );
  223. }
  224. delete ex[i].second.vec;
  225. ex[i].second.vec = NULL;
  226. ex[i].second.svec = f;
  227. }
  228. }
  229. else
  230. {
  231. srand ( time ( NULL ) );
  232. vector<bool> del ( ex.size(), false );
  233. cout << "Example size old " << ex.size() << endl;
  234. //#pragma omp parallel for
  235. for ( int i = 0; i < ( int ) ex.size(); i++ )
  236. {
  237. double rval = ( double ) rand() / ( double ) RAND_MAX;
  238. if ( rval < reduce )
  239. {
  240. SparseVector *f = new SparseVector();
  241. if ( usekmeans )
  242. k->getDist ( *ex[i].second.vec, *f, kmeansfeat, kmeanshard );
  243. else
  244. {
  245. if ( usefisher )
  246. g->getFisher ( *ex[i].second.vec, *f );
  247. else
  248. g->getProbs ( *ex[i].second.vec, *f );
  249. }
  250. delete ex[i].second.vec;
  251. ex[i].second.vec = NULL;
  252. ex[i].second.svec = f;
  253. }
  254. else
  255. {
  256. del[i] = true;
  257. }
  258. }
  259. for ( int i = ( int ) del.size() - 1; i >= 0; i-- )
  260. {
  261. if ( del[i] )
  262. {
  263. ex.erase ( ex.begin() + i );
  264. }
  265. }
  266. cerr << "Example size new " << ex.size() << endl;
  267. }
  268. cerr << "converting low-level features to high-level features finished" << endl;
  269. }
  270. void SemSegCsurka::smoothHL ( Examples ex )
  271. {
  272. if ( !smoothhl )
  273. return;
  274. assert ( ex.size() > 1 );
  275. long long int minx = numeric_limits<long long int>::max();
  276. long long int miny = numeric_limits<long long int>::max();
  277. long long int maxx = -numeric_limits<long long int>::max();
  278. long long int maxy = -numeric_limits<long long int>::max();
  279. long long int distx = numeric_limits<long long int>::max();
  280. long long int disty = numeric_limits<long long int>::max();
  281. set<double> scales;
  282. for ( int i = 0; i < ( int ) ex.size(); i++ )
  283. {
  284. scales.insert ( ex[i].second.scale );
  285. }
  286. map<double, int> scalepos;
  287. int it = 0;
  288. for ( set<double>::const_iterator iter = scales.begin(); iter != scales.end(); ++iter, ++it )
  289. {
  290. scalepos.insert ( make_pair ( *iter, it ) );
  291. }
  292. for ( int i = 0; i < ( int ) ex.size(); i++ )
  293. {
  294. if ( minx < numeric_limits<int>::max() && ex[i].second.x - minx > 0 )
  295. distx = std::min ( distx, ex[i].second.x - minx );
  296. if ( miny < numeric_limits<int>::max() && ex[i].second.y - miny > 0 )
  297. disty = std::min ( disty, ex[i].second.y - miny );
  298. minx = std::min ( ( long long int ) ex[i].second.x, minx );
  299. maxx = std::max ( ( long long int ) ex[i].second.x, maxx );
  300. miny = std::min ( ( long long int ) ex[i].second.y, miny );
  301. maxy = std::max ( ( long long int ) ex[i].second.y, maxy );
  302. }
  303. distx = abs ( distx );
  304. int xsize = ( maxx - minx ) / distx + 1;
  305. int ysize = ( maxy - miny ) / disty + 1;
  306. double valx = ( ( double ) xsize - 1 ) / ( double ) ( maxx - minx );
  307. double valy = ( ( double ) ysize - 1 ) / ( double ) ( maxy - miny );
  308. //double sigma = smoothfactor;
  309. double sigma = std::max ( xsize, ysize ) * smoothfactor;
  310. //double sigma = 0.2;
  311. cout << "sigma1: " << sigma << endl;
  312. vector<NICE::FloatImage> imgv;
  313. vector<NICE::FloatImage> gaussImgv;
  314. for ( int i = 0; i < ( int ) scalepos.size(); i++ )
  315. {
  316. NICE::FloatImage img ( xsize, ysize );
  317. NICE::FloatImage gaussImg ( xsize, ysize );
  318. imgv.push_back ( img );
  319. gaussImgv.push_back ( gaussImg );
  320. }
  321. for ( int d = 0; d < ex[0].second.svec->getDim(); d++ )
  322. {
  323. //TODO: max und min dynamisches bestimmen
  324. for ( int i = 0; i < ( int ) scalepos.size(); i++ )
  325. {
  326. imgv[i].set ( 0.0 );
  327. gaussImgv[i].set ( 0.0 );
  328. }
  329. for ( int i = 0; i < ( int ) ex.size(); i++ )
  330. {
  331. int xpos = ( ex[i].second.x - minx ) * valx;
  332. int ypos = ( ex[i].second.y - miny ) * valy;
  333. double val = ex[i].second.svec->get ( d );
  334. imgv[scalepos[ex[i].second.scale]].setPixel ( xpos, ypos, val );
  335. }
  336. /*
  337. for(int y = 0; y < ysize; y++)
  338. {
  339. for(int x = 0; x < xsize; x++)
  340. {
  341. // refactor-nice.pl: check this substitution
  342. // old: double val = GetValD(img,x,y);
  343. double val = img.getPixel(x,y);
  344. double c = 0.0;
  345. if(val == 0.0)
  346. {
  347. if(x > 0)
  348. {
  349. // refactor-nice.pl: check this substitution
  350. // old: val+=GetValD(img,x-1,y);
  351. val+=img.getPixel(x-1,y);
  352. c+=1.0;
  353. }
  354. if(y > 0)
  355. {
  356. // refactor-nice.pl: check this substitution
  357. // old: val+=GetValD(img,x,y-1);
  358. val+=img.getPixel(x,y-1);
  359. c+=1.0;
  360. }
  361. if(x < xsize-1)
  362. {
  363. // refactor-nice.pl: check this substitution
  364. // old: val+=GetValD(img,x+1,y);
  365. val+=img.getPixel(x+1,y);
  366. c+=1.0;
  367. }
  368. if(y < ysize-1)
  369. {
  370. // refactor-nice.pl: check this substitution
  371. // old: val+=GetValD(img,x,y+1);
  372. val+=img.getPixel(x,y+1);
  373. c+=1.0;
  374. }
  375. // refactor-nice.pl: check this substitution
  376. // old: PutValD(img,x,y,val/c);
  377. img.setPixel(x,y,val/c);
  378. }
  379. }
  380. }*/
  381. for ( int i = 0; i < ( int ) imgv.size(); i++ )
  382. filterGaussSigmaApproximate<float, float, float> ( imgv[i], sigma, &gaussImgv[i] );
  383. for ( int i = 0; i < ( int ) ex.size(); i++ )
  384. {
  385. int xpos = ( ex[i].second.x - minx ) * valx;
  386. int ypos = ( ex[i].second.y - miny ) * valy;
  387. // refactor-nice.pl: check this substitution
  388. // old: double val = GetValD ( gaussImgv[scalepos[ex[i].second.scale]], xpos, ypos );
  389. double val = gaussImgv[scalepos[ex[i].second.scale]].getPixel ( xpos, ypos );
  390. if ( fabs ( val ) < 1e-7 )
  391. {
  392. if ( ex[i].second.svec->get ( d ) != 0.0 )
  393. {
  394. ex[i].second.svec->erase ( d );
  395. }
  396. }
  397. else
  398. {
  399. ( *ex[i].second.svec ) [d] = val;
  400. }
  401. }
  402. }
  403. }
  404. void SemSegCsurka::initializePCA ( Examples &ex )
  405. {
  406. #ifdef DEBUG
  407. cerr << "start computing pca" << endl;
  408. #endif
  409. std::string filename = cache + "/pca";
  410. FILE *value;
  411. value = fopen ( filename.c_str(), "r" );
  412. if ( value == NULL || calcpca )
  413. {
  414. srand ( time ( NULL ) );
  415. int featsize = ( int ) ex.size();
  416. int maxfeatures = dim * 10;
  417. int olddim = ex[0].second.vec->size();
  418. maxfeatures = std::min ( maxfeatures, featsize );
  419. NICE::Matrix features ( maxfeatures, olddim );
  420. for ( int i = 0; i < maxfeatures; i++ )
  421. {
  422. int k = rand() % featsize;
  423. int vsize = ( int ) ex[k].second.vec->size();
  424. for ( int j = 0; j < vsize; j++ )
  425. {
  426. features ( i, j ) = ( * ( ex[k].second.vec ) ) [j];
  427. }
  428. }
  429. pca.calculateBasis ( features, dim );
  430. if ( save_cache )
  431. pca.save ( filename );
  432. }
  433. else
  434. {
  435. cout << "readpca: " << filename << endl;
  436. pca.read ( filename );
  437. cout << "end" << endl;
  438. }
  439. #ifdef DEBUG
  440. cerr << "finished computing pca" << endl;
  441. #endif
  442. }
  443. void SemSegCsurka::doPCA ( Examples &ex )
  444. {
  445. cout << "converting features using pca starts" << endl;
  446. std::string savedir = cname = conf->gS ( "cache", "root", "/dev/null/" );
  447. std::string shortf = ex.filename;
  448. if ( string::npos != ex.filename.rfind ( "/" ) )
  449. shortf = ex.filename.substr ( ex.filename.rfind ( "/" ) );
  450. std::string filename = savedir + "/pcasave/" + shortf;
  451. std::string syscall = "mkdir " + savedir + "/pcasave";
  452. system ( syscall.c_str() );
  453. cout << "filename: " << filename << endl;
  454. if ( !FileMgt::fileExists ( filename ) || calcpca )
  455. {
  456. ofstream ofStream;
  457. //Opens the file binary
  458. ofStream.open ( filename.c_str(), fstream::out | fstream::binary );
  459. for ( int k = 0; k < ( int ) ex.size(); k++ )
  460. {
  461. NICE::Vector tmp = pca.getFeatureVector ( * ( ex[k].second.vec ), true );
  462. delete ex[k].second.vec;
  463. for ( int d = 0; d < ( int ) tmp.size(); d++ )
  464. ofStream.write ( ( char* ) &tmp[d], sizeof ( double ) );
  465. ex[k].second.vec = new NICE::Vector ( tmp );
  466. }
  467. ofStream.close();
  468. cout << endl;
  469. }
  470. else
  471. {
  472. ifstream ifStream;
  473. ifStream.open ( filename.c_str(), std::fstream::in | std::fstream::binary );
  474. for ( int k = 0; k < ( int ) ex.size(); k++ )
  475. {
  476. NICE::Vector tmp = NICE::Vector ( dim );
  477. delete ex[k].second.vec;
  478. for ( int d = 0; d < dim; d++ )
  479. ifStream.read ( ( char* ) &tmp[d], sizeof ( double ) );
  480. ex[k].second.vec = new NICE::Vector ( tmp );
  481. }
  482. ifStream.close();
  483. }
  484. cout << "converting features using pca finished" << endl;
  485. }
  486. void SemSegCsurka::train ( const MultiDataset *md )
  487. {
  488. /*die einzelnen Trainingsschritte
  489. 1. auf allen Trainingsbilder SIFT Merkmale an den Gitterpunkten bei allen Auflösungen bestimmen
  490. 2. PCA anwenden
  491. 3. aus diesen ein GMM erstellen
  492. 4. für jedes SIFT-Merkmal einen Vektor erstellen, der an der Stelle i die Wahrscheinlichkeit enthällt zur Verteilung i des GMM, Zur Zeit mit BoV-Alternative durch Moosman06 erledigt
  493. 5. diese Vektoren in einem diskriminitativen Klassifikator ( z.B. SLR oder Randomized Forests) zusammen mit ihrer Klassenzugehörigkeit anlernen
  494. */
  495. #ifdef DEBUG
  496. cerr << "SemSegCsurka:: training starts" << endl;
  497. #endif
  498. Examples examples;
  499. examples.filename = "training";
  500. // Welche Opponentsift Implementierung soll genutzt werden ?
  501. LocalFeatureRepresentation *cSIFT = NULL;
  502. LocalFeatureRepresentation *writeFeats = NULL;
  503. LocalFeatureRepresentation *readFeats = NULL;
  504. LocalFeatureRepresentation *getFeats = NULL;
  505. if ( opSiftImpl == "NICE" )
  506. {
  507. cSIFT = new LFonHSG ( conf, "HSGtrain" );
  508. }
  509. else if ( opSiftImpl == "VANDESANDE" )
  510. {
  511. // the used features
  512. cSIFT = new LFColorSande ( conf, "LFColorSandeTrain" );
  513. }
  514. else
  515. {
  516. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  517. }
  518. getFeats = cSIFT;
  519. if ( writefeat )
  520. {
  521. // write the features to a file, if there isn't any to read
  522. writeFeats = new LFWriteCache ( conf, cSIFT );
  523. getFeats = writeFeats;
  524. }
  525. if ( readfeat )
  526. {
  527. // read the features from a file
  528. if ( writefeat )
  529. {
  530. readFeats = new LFReadCache ( conf, writeFeats, -1 );
  531. }
  532. else
  533. {
  534. readFeats = new LFReadCache ( conf, cSIFT, -1 );
  535. }
  536. getFeats = readFeats;
  537. }
  538. // additional Colorfeatures
  539. LFColorWeijer lcw ( conf );
  540. int lfdimension = -1;
  541. const LabeledSet train = * ( *md ) ["train"];
  542. const LabeledSet *trainp = &train;
  543. ////////////////////////
  544. // Merkmale berechnen //
  545. ////////////////////////
  546. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  547. if ( forbidden_classes_s == "" )
  548. {
  549. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  550. }
  551. cn.getSelection ( forbidden_classes_s, forbidden_classes );
  552. cerr << "forbidden: " << forbidden_classes_s << endl;
  553. ProgressBar pb ( "Local Feature Extraction" );
  554. pb.show();
  555. int imgnb = 0;
  556. LOOP_ALL_S ( *trainp )
  557. {
  558. //EACH_S(classno, currentFile);
  559. EACH_INFO ( classno, info );
  560. pb.update ( trainp->count() );
  561. NICE::ColorImage img;
  562. std::string currentFile = info.img();
  563. CachedExample *ce = new CachedExample ( currentFile );
  564. const LocalizationResult *locResult = info.localization();
  565. if ( locResult->size() <= 0 )
  566. {
  567. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  568. currentFile.c_str() );
  569. continue;
  570. }
  571. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  572. currentFile.c_str() );
  573. int xsize, ysize;
  574. ce->getImageSize ( xsize, ysize );
  575. NICE::Image pixelLabels ( xsize, ysize );
  576. pixelLabels.set ( 0 );
  577. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  578. try {
  579. img = ColorImage ( currentFile );
  580. } catch ( Exception ) {
  581. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  582. continue;
  583. }
  584. Globals::setCurrentImgFN ( currentFile );
  585. VVector features;
  586. VVector cfeatures;
  587. VVector positions;
  588. NICE::ColorImage cimg ( currentFile );
  589. getFeats->extractFeatures ( img, features, positions );
  590. #ifdef DEBUG_CSURK
  591. cout << "[log] SemSegCsruka::train -> " << currentFile << " an " << positions.size() << " Positionen wurden Features (Anz = " << features.size() << ") " << endl;
  592. cout << "mit einer Dimension von " << features[ 0].size() << " extrahiert." << endl;
  593. #endif
  594. if ( usecolorfeats )
  595. lcw.getDescriptors ( cimg, cfeatures, positions );
  596. int j = 0;
  597. for ( VVector::const_iterator i = features.begin();
  598. i != features.end();
  599. i++, j++ )
  600. {
  601. const NICE::Vector & x = *i;
  602. classno = pixelLabels.getPixel ( ( int ) positions[j][0], ( int ) positions[j][1] );
  603. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  604. continue;
  605. if ( lfdimension < 0 )
  606. lfdimension = ( int ) x.size();
  607. else
  608. assert ( lfdimension == ( int ) x.size() );
  609. NICE::Vector *v = new NICE::Vector ( x );
  610. if ( usecolorfeats && !usepca )
  611. v->append ( cfeatures[j] );
  612. Example example ( v );
  613. example.position = imgnb;
  614. examples.push_back (
  615. pair<int, Example> ( classno, example ) );
  616. }
  617. features.clear();
  618. positions.clear();
  619. delete ce;
  620. imgnb++;
  621. }
  622. pb.hide();
  623. //////////////////
  624. // PCA anwenden //
  625. //////////////////
  626. if ( usepca )
  627. {
  628. if ( !read_cache )
  629. {
  630. initializePCA ( examples );
  631. }
  632. doPCA ( examples );
  633. lfdimension = dim;
  634. }
  635. /////////////////////////////////////////////////////
  636. // Low-Level Features in High-Level transformieren //
  637. /////////////////////////////////////////////////////
  638. int hlfdimension = lfdimension;
  639. if ( norm )
  640. normalize ( examples );
  641. if ( usegmm )
  642. {
  643. if ( !usepca && !norm )
  644. normalize ( examples );
  645. g = new GMM ( conf, gaussians );
  646. if ( dogmm || !g->loadData ( cache + "/gmm" ) )
  647. {
  648. g->computeMixture ( examples );
  649. if ( save_cache )
  650. g->saveData ( cache + "/gmm" );
  651. }
  652. hlfdimension = gaussians;
  653. if ( usefisher )
  654. hlfdimension = gaussians * 2 * dim;
  655. }
  656. if ( usekmeans )
  657. {
  658. if ( !usepca || norm )
  659. normalize ( examples );
  660. k = new KMeansOnline ( gaussians );
  661. k->cluster ( examples );
  662. hlfdimension = gaussians;
  663. }
  664. if ( usekmeans || usegmm )
  665. {
  666. examples.clear();
  667. pb.reset ( "Local Feature Extraction" );
  668. lfdimension = -1;
  669. pb.update ( trainp->count() );
  670. LOOP_ALL_S ( *trainp )
  671. {
  672. EACH_INFO ( classno, info );
  673. pb.update ( trainp->count() );
  674. NICE::ColorImage img;
  675. std::string currentFile = info.img();
  676. CachedExample *ce = new CachedExample ( currentFile );
  677. const LocalizationResult *locResult = info.localization();
  678. if ( locResult->size() <= 0 )
  679. {
  680. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  681. currentFile.c_str() );
  682. continue;
  683. }
  684. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  685. currentFile.c_str() );
  686. int xsize, ysize;
  687. ce->getImageSize ( xsize, ysize );
  688. NICE::Image pixelLabels ( xsize, ysize );
  689. pixelLabels.set ( 0 );
  690. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  691. try {
  692. img = ColorImage ( currentFile );
  693. }
  694. catch ( Exception ) {
  695. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  696. continue;
  697. }
  698. Globals::setCurrentImgFN ( currentFile );
  699. VVector features;
  700. VVector cfeatures;
  701. VVector positions;
  702. NICE::ColorImage cimg ( currentFile );
  703. getFeats->extractFeatures ( img, features, positions );
  704. if ( usecolorfeats )
  705. lcw.getDescriptors ( cimg, cfeatures, positions );
  706. int j = 0;
  707. Examples tmpex;
  708. for ( VVector::const_iterator i = features.begin();
  709. i != features.end();
  710. i++, j++ )
  711. {
  712. const NICE::Vector & x = *i;
  713. classno = pixelLabels.getPixel ( ( int ) positions[j][0], ( int ) positions[j][1] );
  714. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  715. continue;
  716. if ( lfdimension < 0 )
  717. lfdimension = ( int ) x.size();
  718. else
  719. assert ( lfdimension == ( int ) x.size() );
  720. NICE::Vector *v = new NICE::Vector ( x );
  721. if ( usecolorfeats )
  722. v->append ( cfeatures[j] );
  723. Example example ( v );
  724. example.position = imgnb;
  725. example.x = ( int ) positions[j][0];
  726. example.y = ( int ) positions[j][1];
  727. example.scale = positions[j][2];
  728. tmpex.push_back ( pair<int, Example> ( classno, example ) );
  729. }
  730. tmpex.filename = currentFile;
  731. if ( usepca )
  732. {
  733. doPCA ( tmpex );
  734. }
  735. convertLowToHigh ( tmpex, anteil );
  736. smoothHL ( tmpex );
  737. for ( int i = 0; i < ( int ) tmpex.size(); i++ )
  738. {
  739. examples.push_back ( pair<int, Example> ( tmpex[i].first, tmpex[i].second ) );
  740. }
  741. tmpex.clear();
  742. features.clear();
  743. positions.clear();
  744. delete ce;
  745. imgnb++;
  746. }
  747. pb.hide();
  748. }
  749. ////////////////////////////
  750. // Klassifikator anlernen //
  751. ////////////////////////////
  752. FeaturePool fp;
  753. Feature *f;
  754. if ( usegmm || usekmeans )
  755. f = new SparseVectorFeature ( hlfdimension );
  756. else
  757. f = new VectorFeature ( hlfdimension );
  758. f->explode ( fp );
  759. delete f;
  760. if ( usecolorfeats && ! ( usekmeans || usegmm ) )
  761. {
  762. int dimension = hlfdimension + 11;
  763. for ( int i = hlfdimension ; i < dimension ; i++ )
  764. {
  765. VectorFeature *f = new VectorFeature ( dimension );
  766. f->feature_index = i;
  767. fp.addFeature ( f, 1.0 / dimension );
  768. }
  769. }
  770. /*
  771. cout << "train classifier" << endl;
  772. fp.store(cout);
  773. getchar();
  774. for(int z = 0; z < examples.size(); z++)
  775. {
  776. cout << "examples.size() " << examples.size() << endl;
  777. cout << "class: " << examples[z].first << endl;
  778. cout << *examples[z].second.vec << endl;
  779. getchar();
  780. }*/
  781. if ( classifier != NULL )
  782. classifier->train ( fp, examples );
  783. else
  784. {
  785. LabeledSetVector lvec;
  786. convertExamplesToLSet ( examples, lvec );
  787. vclassifier->teach ( lvec );
  788. if ( usegmm )
  789. convertLSetToSparseExamples ( examples, lvec );
  790. else
  791. convertLSetToExamples ( examples, lvec );
  792. vclassifier->finishTeaching();
  793. }
  794. fp.destroy();
  795. if ( save_cache )
  796. {
  797. if ( classifier != NULL )
  798. classifier->save ( cache + "/fpcrf.data" );
  799. else
  800. vclassifier->save ( cache + "/veccl.data" );
  801. }
  802. ////////////
  803. //clean up//
  804. ////////////
  805. for ( int i = 0; i < ( int ) examples.size(); i++ )
  806. {
  807. examples[i].second.clean();
  808. }
  809. examples.clear();
  810. if ( cSIFT != NULL )
  811. delete cSIFT;
  812. if ( writeFeats != NULL )
  813. delete writeFeats;
  814. if ( readFeats != NULL )
  815. delete readFeats;
  816. getFeats = NULL;
  817. trainpostprocess ( md );
  818. cerr << "SemSeg training finished" << endl;
  819. }
  820. void SemSegCsurka::trainpostprocess ( const MultiDataset *md )
  821. {
  822. cout << "start postprocess" << endl;
  823. ////////////////////////////
  824. // Postprocess trainieren //
  825. ////////////////////////////
  826. const LabeledSet train = * ( *md ) ["train"];
  827. const LabeledSet *trainp = &train;
  828. if ( userellocprior || srg != NULL || gcopt != NULL )
  829. {
  830. clog << "[log] SemSegCsurka::trainpostprocess: if ( userellocprior || srg != NULL || gcopt !=NULL )" << endl;
  831. if ( userellocprior )
  832. relloc->setClassNo ( cn.numClasses() );
  833. if ( gcopt != NULL )
  834. {
  835. gcopt->setClassNo ( cn.numClasses() );
  836. }
  837. ProgressBar pb ( "learn relative location prior maps" );
  838. pb.show();
  839. LOOP_ALL_S ( *trainp ) // für alle Bilder den ersten Klassifikationsschritt durchführen um den zweiten Klassifikator anzutrainieren
  840. {
  841. EACH_INFO ( classno, info );
  842. pb.update ( trainp->count() );
  843. NICE::ColorImage img;
  844. std::string currentFile = info.img();
  845. Globals::setCurrentImgFN ( currentFile );
  846. CachedExample *ce = new CachedExample ( currentFile );
  847. const LocalizationResult *locResult = info.localization();
  848. if ( locResult->size() <= 0 )
  849. {
  850. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  851. currentFile.c_str() );
  852. continue;
  853. }
  854. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  855. currentFile.c_str() );
  856. int xsize, ysize;
  857. ce->getImageSize ( xsize, ysize );
  858. NICE::Image pixelLabels ( xsize, ysize );
  859. pixelLabels.set ( 0 );
  860. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  861. try {
  862. img = ColorImage ( currentFile );
  863. }
  864. catch ( Exception )
  865. {
  866. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  867. continue;
  868. }
  869. //Regionen ermitteln
  870. NICE::Matrix mask;
  871. int regionsize = seg->segRegions ( img, mask );
  872. #ifdef DEBUG_CSURK
  873. Image overlay ( img.width(), img.height() );
  874. double maxval = -numeric_limits<double>::max();
  875. for ( int y = 0; y < img.height(); y++ )
  876. {
  877. for ( int x = 0; x < img.width(); x++ )
  878. {
  879. int val = ( ( int ) mask ( x, y ) + 1 ) % 256;
  880. overlay.setPixel ( x, y, val );
  881. maxval = std::max ( mask ( x, y ), maxval );
  882. }
  883. }
  884. cout << maxval << " different regions found" << endl;
  885. NICE::showImageOverlay ( img, overlay, "Segmentation Result" );
  886. #endif
  887. Examples regions;
  888. vector<vector<int> > hists;
  889. for ( int i = 0; i < regionsize; i++ )
  890. {
  891. Example tmp;
  892. regions.push_back ( pair<int, Example> ( 0, tmp ) );
  893. vector<int> hist ( cn.numClasses(), 0 );
  894. hists.push_back ( hist );
  895. }
  896. for ( int x = 0; x < xsize; x++ )
  897. {
  898. for ( int y = 0; y < ysize; y++ )
  899. {
  900. int numb = mask ( x, y );
  901. regions[numb].second.x += x;
  902. regions[numb].second.y += y;
  903. regions[numb].second.weight += 1.0;
  904. hists[numb][pixelLabels.getPixel ( x,y ) ]++;
  905. }
  906. }
  907. for ( int i = 0; i < regionsize; i++ )
  908. {
  909. regions[i].second.x /= ( int ) regions[i].second.weight;
  910. regions[i].second.y /= ( int ) regions[i].second.weight;
  911. int maxval = -numeric_limits<int>::max();
  912. int maxpos = -1;
  913. int secondpos = -1;
  914. for ( int k = 0; k < ( int ) hists[i].size(); k++ )
  915. {
  916. if ( maxval < hists[i][k] )
  917. {
  918. maxval = hists[i][k];
  919. secondpos = maxpos;
  920. maxpos = k;
  921. }
  922. }
  923. if ( cn.text ( maxpos ) == "various" )
  924. regions[i].first = secondpos;
  925. else
  926. regions[i].first = maxpos;
  927. }
  928. if ( userellocprior )
  929. relloc->trainPriorsMaps ( regions, xsize, ysize );
  930. if ( srg != NULL )
  931. srg->trainShape ( regions, mask );
  932. if ( gcopt != NULL )
  933. gcopt->trainImage ( regions, mask );
  934. delete ce;
  935. }
  936. pb.hide();
  937. if ( userellocprior )
  938. relloc->finishPriorsMaps ( cn );
  939. if ( srg != NULL )
  940. srg->finishShape ( cn );
  941. if ( gcopt != NULL )
  942. gcopt->finishPP ( cn );
  943. }
  944. if ( userellocprior )
  945. {
  946. clog << "[log] SemSegCsurka::trainpostprocess: if ( userellocprior )" << endl;
  947. ProgressBar pb ( "learn relative location classifier" );
  948. pb.show();
  949. int nummer = 0;
  950. LOOP_ALL_S ( *trainp ) // für alle Bilder den ersten Klassifikationsschritt durchführen um den zweiten Klassifikator anzutrainieren
  951. {
  952. //EACH_S(classno, currentFile);
  953. EACH_INFO ( classno, info );
  954. nummer++;
  955. pb.update ( trainp->count() );
  956. NICE::Image img;
  957. std::string currentFile = info.img();
  958. CachedExample *ce = new CachedExample ( currentFile );
  959. const LocalizationResult *locResult = info.localization();
  960. if ( locResult->size() <= 0 )
  961. {
  962. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  963. currentFile.c_str() );
  964. continue;
  965. }
  966. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  967. currentFile.c_str() );
  968. int xsize, ysize;
  969. ce->getImageSize ( xsize, ysize );
  970. NICE::Image pixelLabels ( xsize, ysize );
  971. pixelLabels.set ( 0 );
  972. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  973. try {
  974. img = Preprocess::ReadImgAdv ( currentFile.c_str() );
  975. }
  976. catch ( Exception )
  977. {
  978. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  979. continue;
  980. }
  981. Globals::setCurrentImgFN ( currentFile );
  982. NICE::Image segresult;
  983. NICE::MultiChannelImageT<double> probabilities ( xsize, ysize, classno );
  984. Examples regions;
  985. NICE::Matrix mask;
  986. if ( savesteps )
  987. {
  988. std::ostringstream s1;
  989. s1 << cache << "/rlpsave/" << nummer;
  990. std::string filename = s1.str();
  991. s1 << ".probs";
  992. std::string fn2 = s1.str();
  993. FILE *file;
  994. file = fopen ( filename.c_str(), "r" );
  995. if ( file == NULL )
  996. {
  997. //berechnen
  998. classifyregions ( ce, segresult, probabilities, regions, mask );
  999. //schreiben
  1000. ofstream fout ( filename.c_str(), ios::app );
  1001. fout << regions.size() << endl;
  1002. for ( int i = 0; i < ( int ) regions.size(); i++ )
  1003. {
  1004. regions[i].second.store ( fout );
  1005. fout << regions[i].first << endl;
  1006. }
  1007. fout.close();
  1008. probabilities.store ( fn2 );
  1009. }
  1010. else
  1011. {
  1012. //lesen
  1013. ifstream fin ( filename.c_str() );
  1014. int size;
  1015. fin >> size;
  1016. for ( int i = 0; i < size; i++ )
  1017. {
  1018. Example ex;
  1019. ex.restore ( fin );
  1020. int tmp;
  1021. fin >> tmp;
  1022. regions.push_back ( pair<int, Example> ( tmp, ex ) );
  1023. }
  1024. fin.close();
  1025. probabilities.restore ( fn2 );
  1026. }
  1027. }
  1028. else
  1029. {
  1030. classifyregions ( ce, segresult, probabilities, regions, mask );
  1031. }
  1032. relloc->trainClassifier ( regions, probabilities );
  1033. delete ce;
  1034. }
  1035. relloc->finishClassifier();
  1036. pb.hide();
  1037. relloc->save ( cache + "/rlp" );
  1038. }
  1039. cout << "finished postprocess" << endl;
  1040. }
  1041. void SemSegCsurka::classifyregions ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities, Examples &Regionen, NICE::Matrix & mask )
  1042. {
  1043. /* die einzelnen Testschritte:
  1044. 1.x auf dem Testbild alle SIFT Merkmale an den Gitterpunkten bei allen Auflösungen bestimmen
  1045. 2.x für jedes SIFT-Merkmal einen Vektor erstellen, der an der Stelle i die Wahrscheinlichkeit enthällt zur Verteilung i des GMM
  1046. 3.x diese Vektoren klassifizieren, so dass für jede Klasse die Wahrscheinlichkeit gespeichert wird
  1047. 4.x für jeden Pixel die Wahrscheinlichkeiten mitteln aus allen Patches, in denen der Pixel vorkommt
  1048. 5.x das Originalbild in homogene Bereiche segmentieren
  1049. 6.x die homogenen Bereiche bekommen die gemittelten Wahrscheinlichkeiten ihrer Pixel
  1050. 7. (einzelne Klassen mit einem globalen Klassifikator ausschließen)
  1051. 8.x jeder Pixel bekommt die Klasse seiner Region zugeordnet
  1052. */
  1053. clog << "[log] SemSegCsruka::classifyregions" << endl;
  1054. int xsize, ysize;
  1055. ce->getImageSize ( xsize, ysize );
  1056. probabilities.reInit ( xsize, ysize, classNames->getMaxClassno() + 1 );
  1057. clog << "[log] SemSegCsruka::classifyregions: probabilities.channels() = " << probabilities.channels() << endl;
  1058. segresult.resize ( xsize, ysize );
  1059. Examples pce;
  1060. // Welche Opponentsift Implementierung soll genutzt werden ?
  1061. LocalFeatureRepresentation *cSIFT = NULL;
  1062. LocalFeatureRepresentation *writeFeats = NULL;
  1063. LocalFeatureRepresentation *readFeats = NULL;
  1064. LocalFeatureRepresentation *getFeats = NULL;
  1065. if ( opSiftImpl == "NICE" )
  1066. {
  1067. cSIFT = new LFonHSG ( conf, "HSGtest" );
  1068. }
  1069. else if ( opSiftImpl == "VANDESANDE" )
  1070. {
  1071. // the used features
  1072. cSIFT = new LFColorSande ( conf, "LFColorSandeTrain" );
  1073. }
  1074. else
  1075. {
  1076. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  1077. }
  1078. getFeats = cSIFT;
  1079. if ( writefeat )
  1080. {
  1081. // write the features to a file, if there isn't any to read
  1082. writeFeats = new LFWriteCache ( conf, cSIFT );
  1083. getFeats = writeFeats;
  1084. }
  1085. if ( readfeat )
  1086. {
  1087. // read the features from a file
  1088. if ( writefeat )
  1089. {
  1090. readFeats = new LFReadCache ( conf, writeFeats, -1 );
  1091. }
  1092. else
  1093. {
  1094. readFeats = new LFReadCache ( conf, cSIFT, -1 );
  1095. }
  1096. getFeats = readFeats;
  1097. }
  1098. // additional Colorfeatures
  1099. LFColorWeijer lcw ( conf );
  1100. NICE::ColorImage img;
  1101. std::string currentFile = Globals::getCurrentImgFN();
  1102. try
  1103. {
  1104. img = ColorImage ( currentFile );
  1105. }
  1106. catch ( Exception )
  1107. {
  1108. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  1109. }
  1110. VVector features;
  1111. VVector cfeatures;
  1112. VVector positions;
  1113. getFeats->extractFeatures ( img, features, positions );
  1114. if ( usecolorfeats )
  1115. lcw.getDescriptors ( img, cfeatures, positions );
  1116. set<double> scales;
  1117. int j = 0;
  1118. int lfdimension = -1;
  1119. for ( VVector::const_iterator i = features.begin();
  1120. i != features.end();
  1121. i++, j++ )
  1122. {
  1123. const NICE::Vector & x = *i;
  1124. if ( lfdimension < 0 ) lfdimension = ( int ) x.size();
  1125. else assert ( lfdimension == ( int ) x.size() );
  1126. NICE::Vector *v = new NICE::Vector ( x );
  1127. if ( usecolorfeats )
  1128. v->append ( cfeatures[j] );
  1129. Example tmp = Example ( v );
  1130. tmp.x = ( int ) positions[j][0];
  1131. tmp.y = ( int ) positions[j][1];
  1132. tmp.width = ( int ) ( 16.0 * positions[j][2] );
  1133. tmp.height = tmp.width;
  1134. tmp.scale = positions[j][2];
  1135. scales.insert ( positions[j][2] );
  1136. pce.push_back ( pair<int, Example> ( 0, tmp ) );
  1137. }
  1138. //////////////////
  1139. // PCA anwenden //
  1140. //////////////////
  1141. pce.filename = currentFile;
  1142. if ( usepca )
  1143. {
  1144. doPCA ( pce );
  1145. lfdimension = dim;
  1146. }
  1147. //////////////////
  1148. // BoV anwenden //
  1149. //////////////////
  1150. if ( norm )
  1151. normalize ( pce );
  1152. if ( usegmm || usekmeans )
  1153. {
  1154. if ( !usepca && !norm )
  1155. normalize ( pce );
  1156. convertLowToHigh ( pce );
  1157. smoothHL ( pce );
  1158. lfdimension = gaussians;
  1159. }
  1160. /////////////////////////////////////////
  1161. // Wahrscheinlichkeitskarten erstellen //
  1162. /////////////////////////////////////////
  1163. int klassen = probabilities.channels();
  1164. NICE::MultiChannelImageT<double> preMap ( xsize, ysize, klassen*scales.size() );
  1165. // initialisieren
  1166. for ( int y = 0 ; y < ysize ; y++ )
  1167. for ( int x = 0 ; x < xsize ; x++ )
  1168. {
  1169. // alles zum Hintergrund machen
  1170. segresult.setPixel ( x, y, 0 );
  1171. // Die Wahrscheinlichkeitsmaps auf 0 initialisieren
  1172. for ( int i = 0 ; i < ( int ) probabilities.channels(); i++ )
  1173. {
  1174. probabilities[i](x,y) = 0.0;
  1175. }
  1176. for ( int j = 0; j < ( int ) preMap.channels(); j++ )
  1177. {
  1178. preMap[j](x,y) = 0.0;
  1179. }
  1180. }
  1181. // Die Wahrscheinlichkeitsmaps mit den einzelnen Wahrscheinlichkeiten je Skalierung füllen
  1182. int scalesize = scales.size();
  1183. // Globale Häufigkeiten akkumulieren
  1184. FullVector fV ( ( int ) probabilities.channels() );
  1185. for ( int i = 0; i < fV.size(); i++ )
  1186. fV[i] = 0.0;
  1187. // read allowed classes
  1188. string cndir = conf->gS ( "SemSegCsurka", "cndir", "" );
  1189. int classes = ( int ) probabilities.channels();
  1190. vector<int> useclass ( classes, 1 );
  1191. std::vector< std::string > list;
  1192. StringTools::split ( currentFile, '/', list );
  1193. string orgname = list.back();
  1194. if ( cndir != "" )
  1195. {
  1196. useclass = vector<int> ( classes, 0 );
  1197. ifstream infile ( ( cndir + "/" + orgname + ".dat" ).c_str() );
  1198. while ( !infile.eof() && infile.good() )
  1199. {
  1200. int tmp;
  1201. infile >> tmp;
  1202. if ( tmp >= 0 && tmp < classes )
  1203. {
  1204. useclass[tmp] = 1;
  1205. }
  1206. }
  1207. }
  1208. #ifdef UNCERTAINTY
  1209. vector<FloatImage> uncert;
  1210. for(int s = 0; s < scalesize; s++)
  1211. {
  1212. uncert.push_back(FloatImage(xsize, ysize));
  1213. uncert[s].set(0.0);
  1214. }
  1215. ColorImage imgrgb ( xsize, ysize );
  1216. std::string s;
  1217. std::stringstream out;
  1218. std::vector< std::string > list2;
  1219. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  1220. out << "uncertainty/" << list2.back();
  1221. double maxu = -numeric_limits<double>::max();
  1222. double minu = numeric_limits<double>::max();
  1223. #endif
  1224. if ( classifier != NULL )
  1225. {
  1226. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen: classifier != NULL" << endl;
  1227. #pragma omp parallel for
  1228. for ( int s = 0; s < scalesize; s++ )
  1229. {
  1230. #pragma omp parallel for
  1231. for ( int i = s; i < ( int ) pce.size(); i += scalesize )
  1232. {
  1233. ClassificationResult r = classifier->classify ( pce[i].second );
  1234. for ( int j = 0 ; j < fV.size(); j++ )
  1235. {
  1236. if ( useclass[j] == 0 )
  1237. continue;
  1238. fV[j] += r.scores[j];
  1239. preMap.set ( pce[i].second.x, pce[i].second.y, r.scores[j], j + s*klassen );
  1240. }
  1241. /*if(r.uncertainty < 0.0)
  1242. {
  1243. cerr << "uncertainty: " << r.uncertainty << endl;
  1244. pce[i].second.svec->store(cerr);
  1245. cerr << endl;
  1246. exit(-1);
  1247. }*/
  1248. #ifdef UNCERTAINTY
  1249. uncert[s] ( pce[i].second.x, pce[i].second.y ) = r.uncertainty;
  1250. maxu = std::max ( r.uncertainty, maxu );
  1251. minu = std::min ( r.uncertainty, minu );
  1252. #endif
  1253. }
  1254. }
  1255. }
  1256. else
  1257. {
  1258. //#pragma omp parallel for
  1259. for ( int s = 0; s < scalesize; s++ )
  1260. {
  1261. //#pragma omp parallel for
  1262. for ( int i = s; i < ( int ) pce.size(); i += scalesize )
  1263. {
  1264. ClassificationResult r = vclassifier->classify ( * ( pce[i].second.vec ) );
  1265. for ( int j = 0 ; j < ( int ) fV.size(); j++ )
  1266. {
  1267. if ( useclass[j] == 0 )
  1268. continue;
  1269. fV[j] += r.scores[j];
  1270. preMap.set ( pce[i].second.x, pce[i].second.y, r.scores[j], j + s*klassen );
  1271. }
  1272. #ifdef UNCERTAINTY
  1273. uncert[s] ( pce[i].second.x, pce[i].second.y ) = r.uncertainty;
  1274. maxu = std::max ( r.uncertainty, maxu );
  1275. minu = std::min ( r.uncertainty, minu );
  1276. #endif
  1277. }
  1278. }
  1279. }
  1280. #ifdef UNCERTAINTY
  1281. cout << "maxvdirect: " << maxu << " minvdirect: " << minu << endl;
  1282. FloatImage gaussUncert ( xsize, ysize );
  1283. ICETools::convertToRGB ( uncert[0], imgrgb );
  1284. imgrgb.write ( out.str() + "rough.png" );
  1285. #endif
  1286. vector<double> scalesVec;
  1287. for ( set<double>::const_iterator iter = scales.begin();
  1288. iter != scales.end();
  1289. ++iter )
  1290. {
  1291. scalesVec.push_back ( *iter );
  1292. }
  1293. #undef VISSEMSEG
  1294. #ifdef VISSEMSEG
  1295. for ( int j = 0 ; j < ( int ) preMap.channels(); j++ )
  1296. {
  1297. cout << "klasse: " << j << endl;//" " << cn.text ( j ) << endl;
  1298. NICE::Matrix tmp ( preMap.ysize, preMap.xsize );
  1299. double maxval = 0.0;
  1300. for ( int y = 0; y < preMap.ysize; y++ )
  1301. for ( int x = 0; x < preMap.xsize; x++ )
  1302. {
  1303. double val = preMap.get ( x, y, j );
  1304. tmp ( y, x ) = val;
  1305. maxval = std::max ( val, maxval );
  1306. }
  1307. NICE::ColorImage imgrgb ( preMap.xsize, preMap.ysize );
  1308. ICETools::convertToRGB ( tmp, imgrgb );
  1309. cout << "maxval = " << maxval << " for class " << j << endl; //cn.text ( j ) << endl;
  1310. //Show ( ON, imgrgb, cn.text ( j ) );
  1311. //showImage(imgrgb, "Ergebnis");
  1312. std::string s;
  1313. std::stringstream out;
  1314. out << "tmpprebmap" << j << ".ppm";
  1315. s = out.str();
  1316. imgrgb.writePPM ( s );
  1317. //getchar();
  1318. }
  1319. #endif
  1320. // Gaußfiltern
  1321. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen -> Gaussfiltern" << endl;
  1322. for ( int s = 0; s < scalesize; s++ )
  1323. {
  1324. double sigma = sigmaweight * 16.0 * scalesVec[s];
  1325. cerr << "sigma: " << sigma << endl;
  1326. #pragma omp parallel for
  1327. for ( int i = 0; i < klassen; i++ )
  1328. {
  1329. if ( forbidden_classes.find ( i ) != forbidden_classes.end() )
  1330. {
  1331. continue;
  1332. }
  1333. int pos = i + s * klassen;
  1334. double maxval = preMap[pos](0,0);
  1335. double minval = maxval;
  1336. for ( int y = 0; y < ysize; y++ )
  1337. {
  1338. for ( int x = 0; x < xsize; x++ )
  1339. {
  1340. maxval = std::max ( maxval, preMap[pos](x,y) );
  1341. minval = std::min ( minval, preMap[pos](x,y) );
  1342. }
  1343. }
  1344. NICE::FloatImage dblImg ( xsize, ysize );
  1345. NICE::FloatImage gaussImg ( xsize, ysize );
  1346. for ( int y = 0; y < ysize; y++ )
  1347. {
  1348. for ( int x = 0; x < xsize; x++ )
  1349. {
  1350. dblImg.setPixel ( x, y, preMap[pos](x,y) );
  1351. }
  1352. }
  1353. filterGaussSigmaApproximate<float, float, float> ( dblImg, sigma, &gaussImg );
  1354. for ( int y = 0; y < ysize; y++ )
  1355. {
  1356. for ( int x = 0; x < xsize; x++ )
  1357. {
  1358. preMap[pos](x,y) = gaussImg.getPixel ( x, y );
  1359. }
  1360. }
  1361. }
  1362. #ifdef UNCERTAINTY
  1363. filterGaussSigmaApproximate<float, float, float> ( uncert[s], sigma, &gaussUncert );
  1364. uncert[s] = gaussUncert;
  1365. #endif
  1366. }
  1367. // Zusammenfassen und auswerten
  1368. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen -> zusammenfassen und auswerten" << endl;
  1369. //#pragma omp parallel for
  1370. for ( int x = 0; x < xsize; x++ )
  1371. {
  1372. for ( int y = 0; y < ysize; y++ )
  1373. {
  1374. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1375. {
  1376. double prob = 0.0;
  1377. for ( int s = 0; s < ( int ) scalesize; s++ )
  1378. {
  1379. prob += preMap.get ( x, y, j + s * klassen );
  1380. }
  1381. double val = prob / ( double ) ( scalesize );
  1382. probabilities.set ( x, y, val, j );
  1383. }
  1384. }
  1385. }
  1386. #ifdef UNCERTAINTY
  1387. for ( int x = 0; x < xsize; x++ )
  1388. {
  1389. for ( int y = 0; y < ysize; y++ )
  1390. {
  1391. for ( int s = 0; s < ( int ) scalesize; s++ )
  1392. {
  1393. gaussUncert(x,y) += uncert[s](x,y);
  1394. }
  1395. gaussUncert(x,y)/=scalesize;
  1396. }
  1397. }
  1398. maxu = -numeric_limits<double>::max();
  1399. minu = numeric_limits<double>::max();
  1400. for ( int y = 0; y < ysize; y++ )
  1401. {
  1402. for ( int x = 0; x < xsize; x++ )
  1403. {
  1404. double val = uncert[0] ( x, y );
  1405. maxu = std::max ( val, maxu );
  1406. minu = std::min ( val, minu );
  1407. }
  1408. }
  1409. cout << "maxvo = " << maxu << " minvo = " << minu << endl;
  1410. maxu = -numeric_limits<float>::max();
  1411. minu = numeric_limits<float>::max();
  1412. for ( int y = 0; y < ysize; y++ )
  1413. {
  1414. for ( int x = 0; x < xsize; x++ )
  1415. {
  1416. double val = gaussUncert ( x, y );
  1417. maxu = std::max ( val, maxu );
  1418. minu = std::min ( val, minu );
  1419. }
  1420. }
  1421. cout << "maxvf = " << maxu << " minvf = " << minu << endl;
  1422. gaussUncert(0,0) = 0.0;
  1423. gaussUncert(0,1) = 0.04;
  1424. ICETools::convertToRGB ( gaussUncert, imgrgb );
  1425. imgrgb.write ( out.str() + "filtered.png" );
  1426. #endif
  1427. #undef VISSEMSEG
  1428. #ifdef VISSEMSEG
  1429. std::string s;
  1430. std::stringstream out;
  1431. std::vector< std::string > list2;
  1432. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  1433. out << "probmaps/" << list2.back() << ".probs";
  1434. s = out.str();
  1435. probabilities.store ( s );
  1436. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1437. {
  1438. cout << "klasse: " << j << endl;//" " << cn.text ( j ) << endl;
  1439. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  1440. double maxval = 0.0;
  1441. for ( int y = 0; y < probabilities.ysize; y++ )
  1442. for ( int x = 0; x < probabilities.xsize; x++ )
  1443. {
  1444. double val = probabilities.get ( x, y, j );
  1445. tmp ( y, x ) = val;
  1446. maxval = std::max ( val, maxval );
  1447. }
  1448. NICE::ColorImage imgrgb ( probabilities.xsize, probabilities.ysize );
  1449. ICETools::convertToRGB ( tmp, imgrgb );
  1450. cout << "maxval = " << maxval << " for class " << j << endl; //cn.text ( j ) << endl;
  1451. //Show ( ON, imgrgb, cn.text ( j ) );
  1452. //showImage(imgrgb, "Ergebnis");
  1453. std::string s;
  1454. std::stringstream out;
  1455. out << "tmp" << j << ".ppm";
  1456. s = out.str();
  1457. imgrgb.writePPM ( s );
  1458. //getchar();
  1459. }
  1460. #endif
  1461. if ( useregions )
  1462. {
  1463. if ( bestclasses > 0 )
  1464. {
  1465. PSSImageLevelPrior pss ( 0, bestclasses, 0.2 );
  1466. pss.setPrior ( fV );
  1467. pss.postprocess ( segresult, probabilities );
  1468. }
  1469. //Regionen ermitteln
  1470. int regionsize = seg->segRegions ( img, mask );
  1471. Regionen.clear();
  1472. vector<vector <double> > regionprob;
  1473. #ifdef UNCERTAINTY
  1474. vector<double> regionUncert;
  1475. #endif
  1476. // Wahrscheinlichkeiten für jede Region initialisieren
  1477. for ( int i = 0; i < regionsize; i++ )
  1478. {
  1479. vector<double> tmp;
  1480. for ( int j = 0; j < ( int ) probabilities.channels(); j++ )
  1481. {
  1482. tmp.push_back ( 0.0 );
  1483. }
  1484. regionprob.push_back ( tmp );
  1485. Regionen.push_back ( pair<int, Example> ( 0, Example() ) );
  1486. #ifdef UNCERTAINTY
  1487. regionUncert.push_back ( 0.0 );
  1488. #endif
  1489. }
  1490. // Wahrscheinlichkeiten für Regionen bestimmen
  1491. for ( int x = 0; x < xsize; x++ )
  1492. {
  1493. for ( int y = 0; y < ysize; y++ )
  1494. {
  1495. int pos = mask ( x, y );
  1496. Regionen[pos].second.weight += 1.0;
  1497. Regionen[pos].second.x += x;
  1498. Regionen[pos].second.y += y;
  1499. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1500. {
  1501. double val = probabilities.get ( x, y, j );
  1502. regionprob[pos][j] += val;
  1503. }
  1504. #ifdef UNCERTAINTY
  1505. regionUncert[pos] += gaussUncert ( x, y );
  1506. #endif
  1507. }
  1508. }
  1509. /*
  1510. cout << "regions: " << regionsize << endl;
  1511. cout << "outfeats: " << endl;
  1512. for(int j = 0; j < regionprob.size(); j++)
  1513. {
  1514. for(int i = 0; i < regionprob[j].size(); i++)
  1515. {
  1516. cout << regionprob[j][i] << " ";
  1517. }
  1518. cout << endl;
  1519. }
  1520. cout << endl;
  1521. getchar();*/
  1522. // beste Wahrscheinlichkeit je Region wählen
  1523. for ( int i = 0; i < regionsize; i++ )
  1524. {
  1525. if ( Regionen[i].second.weight > 0 )
  1526. {
  1527. Regionen[i].second.x /= ( int ) Regionen[i].second.weight;
  1528. Regionen[i].second.y /= ( int ) Regionen[i].second.weight;
  1529. }
  1530. double maxval = -numeric_limits<double>::max();
  1531. int maxpos = 0;
  1532. for ( int j = 0 ; j < ( int ) regionprob[i].size(); j++ )
  1533. {
  1534. if ( forbidden_classes.find ( j ) != forbidden_classes.end() )
  1535. continue;
  1536. regionprob[i][j] /= Regionen[i].second.weight;
  1537. if ( maxval < regionprob[i][j] )
  1538. {
  1539. maxval = regionprob[i][j];
  1540. maxpos = j;
  1541. }
  1542. probabilities.set ( Regionen[i].second.x, Regionen[i].second.y, regionprob[i][j], j );
  1543. }
  1544. Regionen[i].first = maxpos;
  1545. #ifdef UNCERTAINTY
  1546. regionUncert[i] /= Regionen[i].second.weight;
  1547. #endif
  1548. }
  1549. // Pixel jeder Region labeln
  1550. for ( int y = 0; y < ( int ) mask.cols(); y++ )
  1551. {
  1552. for ( int x = 0; x < ( int ) mask.rows(); x++ )
  1553. {
  1554. int pos = mask ( x, y );
  1555. segresult.setPixel ( x, y, Regionen[pos].first );
  1556. #ifdef UNCERTAINTY
  1557. gaussUncert ( x, y ) = regionUncert[pos];
  1558. #endif
  1559. }
  1560. }
  1561. #ifdef UNCERTAINTY
  1562. maxu = -numeric_limits<float>::max();
  1563. minu = numeric_limits<float>::max();
  1564. for ( int y = 0; y < ysize; y++ )
  1565. {
  1566. for ( int x = 0; x < xsize; x++ )
  1567. {
  1568. //float val = uncert(x,y);
  1569. double val = gaussUncert ( x, y );
  1570. maxu = std::max ( val, maxu );
  1571. minu = std::min ( val, minu );
  1572. }
  1573. }
  1574. cout << "maxvr = " << maxu << " minvr = " << minu << endl;
  1575. // uncert(0,0) = 1;
  1576. // uncert(0,1) = 0;
  1577. ICETools::convertToRGB ( gaussUncert, imgrgb );
  1578. imgrgb.write ( out.str() + "region.png" );
  1579. #endif
  1580. #undef WRITEREGIONS
  1581. #ifdef WRITEREGIONS
  1582. RegionGraph rg;
  1583. seg->getGraphRepresentation ( img, mask, rg );
  1584. for ( uint pos = 0; pos < regionprob.size(); pos++ )
  1585. {
  1586. rg[pos]->setProbs ( regionprob[pos] );
  1587. }
  1588. std::string s;
  1589. std::stringstream out;
  1590. std::vector< std::string > list;
  1591. StringTools::split ( Globals::getCurrentImgFN (), '/', list );
  1592. out << "rgout/" << list.back() << ".graph";
  1593. string writefile = out.str();
  1594. rg.write ( writefile );
  1595. #endif
  1596. }
  1597. else
  1598. {
  1599. PSSImageLevelPrior pss ( 1, 4, 0.2 );
  1600. pss.setPrior ( fV );
  1601. pss.postprocess ( segresult, probabilities );
  1602. }
  1603. // Saubermachen:
  1604. clog << "[log] SemSegCsurka::classifyregions: sauber machen" << endl;
  1605. for ( int i = 0; i < ( int ) pce.size(); i++ )
  1606. {
  1607. pce[i].second.clean();
  1608. }
  1609. pce.clear();
  1610. if ( cSIFT != NULL )
  1611. delete cSIFT;
  1612. if ( writeFeats != NULL )
  1613. delete writeFeats;
  1614. if ( readFeats != NULL )
  1615. delete readFeats;
  1616. getFeats = NULL;
  1617. }
  1618. void SemSegCsurka::semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  1619. {
  1620. Examples regions;
  1621. NICE::Matrix regionmask;
  1622. classifyregions ( ce, segresult, probabilities, regions, regionmask );
  1623. if ( userellocprior || srg != NULL || gcopt != NULL )
  1624. {
  1625. if ( userellocprior )
  1626. relloc->postprocess ( regions, probabilities );
  1627. if ( srg != NULL )
  1628. srg->optimizeShape ( regions, regionmask, probabilities );
  1629. if ( gcopt != NULL )
  1630. gcopt->optimizeImage ( regions, regionmask, probabilities );
  1631. // Pixel jeder Region labeln
  1632. for ( int y = 0; y < ( int ) regionmask.cols(); y++ )
  1633. {
  1634. for ( int x = 0; x < ( int ) regionmask.rows(); x++ )
  1635. {
  1636. int pos = regionmask ( x, y );
  1637. segresult.setPixel ( x, y, regions[pos].first );
  1638. }
  1639. }
  1640. }
  1641. #ifndef NOVISUAL
  1642. #undef VISSEMSEG
  1643. #ifdef VISSEMSEG
  1644. // showImage(img);
  1645. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1646. {
  1647. cout << "klasse: " << j << " " << cn.text ( j ) << endl;
  1648. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  1649. double maxval = -numeric_limits<double>::max();
  1650. for ( int y = 0; y < probabilities.ysize; y++ )
  1651. for ( int x = 0; x < probabilities.xsize; x++ )
  1652. {
  1653. double val = probabilities.get ( x, y, j );
  1654. tmp ( y, x ) = val;
  1655. maxval = std::max ( val, maxval );
  1656. }
  1657. NICE::ColorImage imgrgb ( probabilities.xsize, probabilities.ysize );
  1658. ICETools::convertToRGB ( tmp, imgrgb );
  1659. cout << "maxval = " << maxval << " for class " << cn.text ( j ) << endl;
  1660. Show ( ON, imgrgb, cn.text ( j ) );
  1661. imgrgb.Write ( "tmp.ppm" );
  1662. getchar();
  1663. }
  1664. #endif
  1665. #endif
  1666. }