SemSegCsurka.cpp 52 KB

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