PPSuperregion.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "PPSuperregion.h"
  2. #ifdef NICE_USELIB_ICE
  3. #include <core/iceconversion/convertice.h>
  4. #endif
  5. #include "objrec/segmentation/RegionGraph.h"
  6. using namespace std;
  7. using namespace NICE;
  8. using namespace OBJREC;
  9. PPSuperregion::PPSuperregion()
  10. {
  11. conf = new Config();
  12. Init();
  13. }
  14. PPSuperregion::PPSuperregion(const Config *_conf):conf(_conf)
  15. {
  16. Init();
  17. }
  18. void PPSuperregion::Init()
  19. {
  20. std::string section = "PostProcessSG";
  21. rf = new FPCRandomForests( conf, "ShapeRF" );
  22. }
  23. PPSuperregion::~PPSuperregion()
  24. {
  25. }
  26. void PPSuperregion::optimizeShape(Examples &regions, NICE::Matrix &mask, NICE::MultiChannelImageT<double> & probabilities)
  27. {
  28. #ifdef NICE_USELIB_ICE
  29. vector<ice::Region> superregions;
  30. vector<double> probs;
  31. vector<int> classes;
  32. NICE::Matrix smask;
  33. getSuperregions(regions, mask, superregions, classes, smask);
  34. for(int i = 0; i < (int)superregions.size(); i++)
  35. {
  36. ice::Moments m;
  37. superregions[i].CalcMoments(m);
  38. NICE::Vector tmp = makeEVector(m.AffineHuInvariants());
  39. NICE::Vector *tmp2 = new NICE::Vector(tmp);
  40. Example tex(tmp2);
  41. ClassificationResult r = rf->classify ( tex );
  42. probs.push_back(r.scores[classes[i]]);
  43. }
  44. vector<ice::Region> orgregions;
  45. for(int i = 0; i < (int)regions.size(); i++)
  46. {
  47. orgregions.push_back(ice::Region());
  48. }
  49. for(int y = 0; y < (int)mask.cols(); y++)
  50. {
  51. for(int x = 0; x < (int)mask.rows(); x++)
  52. {
  53. int pos = mask(x,y);
  54. orgregions[pos].Add(x,y);
  55. }
  56. }
  57. // maps the regions to their superregions
  58. vector<int> regmapsreg(regions.size(), 0);
  59. for(int y = 0; y < (int)smask.cols(); y++)
  60. {
  61. for(int x = 0; x < (int)smask.rows(); x++)
  62. {
  63. int r = mask(x,y);
  64. int sr = smask(x,y);
  65. regmapsreg[r] = sr;
  66. }
  67. }
  68. RegionGraph g;
  69. g.computeGraph(regions, mask);
  70. vector<Node*> nodes;
  71. g.get(nodes);
  72. bool change = true;
  73. int k = 0;
  74. while(change && k < 100)
  75. {
  76. k++;
  77. change = false;
  78. int anders = 0;
  79. for(int i = 0; i < (int) nodes.size(); i++)
  80. {
  81. set<int> sr;
  82. int regnb = nodes[i]->getRegion();
  83. int orgreg = regmapsreg[regnb];
  84. if(nodes[i]->isAtBorder())
  85. {
  86. vector<Node*> nbs;
  87. nodes[i]->getNeighbors(nbs);
  88. for(int j = 0; j < (int)nbs.size(); j++)
  89. sr.insert(regmapsreg[nbs[j]->getRegion()]);
  90. }
  91. vector<double> otherprobs;
  92. ice::Region re = superregions[orgreg];
  93. re.Del(orgregions[regnb]);
  94. ice::Moments m;
  95. if(re.Area() > 0)
  96. {
  97. re.CalcMoments(m);
  98. NICE::Vector tmp = makeEVector( m.AffineHuInvariants());
  99. NICE::Vector *tmp2 = new NICE::Vector(tmp);
  100. Example tex(tmp2);
  101. ClassificationResult r = rf->classify ( tex );
  102. tex.vec = NULL;
  103. delete tmp2;
  104. double val = probabilities.get(regions[regnb].second.x, regions[regnb].second.y, classes[orgreg]) * r.scores[classes[orgreg]];
  105. otherprobs.push_back(val);
  106. if(otherprobs[0] < probs[orgreg])
  107. continue;
  108. }
  109. for( set<int>::const_iterator iter = sr.begin();iter != sr.end();++iter )
  110. {
  111. ice::Moments m2;
  112. ice::Region re2 = superregions[regmapsreg[*iter]];
  113. re2.Add(orgregions[regnb]);
  114. re2.CalcMoments(m2);
  115. NICE::Vector tmp = makeEVector(m2.AffineHuInvariants());
  116. NICE::Vector *tmp2 = new NICE::Vector(tmp);
  117. Example tex(tmp2);
  118. ClassificationResult r2 = rf->classify ( tex );
  119. tex.vec = NULL;
  120. delete tmp2;
  121. double val = probabilities.get(regions[regnb].second.x, regions[regnb].second.y, classes[*iter]) * r2.scores[classes[*iter]];
  122. otherprobs.push_back(val);
  123. }
  124. int k = 1;
  125. int best = -1;
  126. double bestval = -1.0;
  127. for( set<int>::const_iterator iter = sr.begin();iter != sr.end();++iter, k++ )
  128. {
  129. if(otherprobs[k] > probs[*iter])
  130. {
  131. if(bestval < otherprobs[k])
  132. {
  133. bestval = otherprobs[k];
  134. best = *iter;
  135. }
  136. }
  137. }
  138. if(best < 0 || bestval <= 0.0)
  139. continue;
  140. change = true;
  141. probs[best] = bestval;
  142. superregions[best].Add(orgregions[regnb]);
  143. probs[orgreg] = otherprobs[0];
  144. superregions[orgreg].Del(orgregions[regnb]);
  145. regmapsreg[regnb] = best;
  146. nodes[i]->setLabel(classes[best]);
  147. anders++;
  148. }
  149. }
  150. for(int i = 0; i < (int)regions.size(); i++)
  151. {
  152. regions[i].first = classes[regmapsreg[i]];
  153. }
  154. #else
  155. throw("PPSuperRegion.cpp: please use ice library for this function");
  156. #endif
  157. }
  158. #ifdef NICE_USELIB_ICE
  159. void PPSuperregion::getSuperregions(const Examples &regions, const NICE::Matrix &mask, vector<ice::Region> &superregions, vector<int> &classes, NICE::Matrix &smask)
  160. {
  161. NICE::Image tmp (mask.rows(), mask.cols());
  162. tmp.set(0);
  163. NICE::ColorImage m2 (tmp, tmp, tmp);
  164. for(int y = 0; y < (int)mask.cols(); y++)
  165. {
  166. for(int x = 0; x < (int)mask.rows(); x++)
  167. {
  168. int pos = mask(x,y);
  169. m2.setPixel(x,y,0,regions[pos].first);
  170. m2.setPixel(x,y,1,regions[pos].first);
  171. m2.setPixel(x,y,2,regions[pos].first);
  172. }
  173. }
  174. RSMeanShift rs(conf);
  175. int count = rs.transformSegmentedImg( m2, smask);
  176. classes.resize(count);
  177. for(int i = 0; i < count; i++)
  178. {
  179. superregions.push_back(ice::Region());
  180. }
  181. for(int y = 0; y < (int)smask.cols(); y++)
  182. {
  183. for(int x = 0; x < (int)smask.rows(); x++)
  184. {
  185. int pos = smask(x,y);
  186. superregions[pos].Add(x,y);
  187. classes[pos] = regions[mask(x,y)].first;
  188. }
  189. }
  190. }
  191. #endif
  192. void PPSuperregion::trainShape(Examples &regions, NICE::Matrix &mask)
  193. {
  194. #ifdef NICE_USELIB_ICE
  195. // bestimme Superregionen
  196. vector<ice::Region> superregions;
  197. vector<int> classes;
  198. // refactor-nice.pl: check this substitution
  199. // old: Image smask;
  200. NICE::Matrix smask;
  201. getSuperregions(regions, mask, superregions, classes, smask);
  202. // berechne die Momente der Superregionen und speichere diese als Merkmale ab
  203. for(int i = 0; i < (int)superregions.size(); i++)
  204. {
  205. ice::Moments m;
  206. superregions[i].CalcMoments(m);
  207. NICE::Vector tmp = makeEVector(m.AffineHuInvariants());
  208. NICE::Vector *tmp2 = new NICE::Vector(tmp);
  209. shapefeats.push_back(pair<int, Example>(classes[i], Example(tmp2)));
  210. }
  211. #else
  212. throw("PPSuperRegion.cpp: please use ice library for this function");
  213. #endif
  214. }
  215. void PPSuperregion::finishShape(ClassNames &cn)
  216. {
  217. //Lerne Klassifikator mit dem den Formmerkmalen an
  218. FeaturePool fp;
  219. Feature *f = new VectorFeature ( 7 );
  220. f->explode ( fp );
  221. delete f;
  222. rf->train ( fp, shapefeats);
  223. }
  224. void PPSuperregion::restore (istream & is, int format)
  225. {
  226. }
  227. void PPSuperregion::store (ostream & os, int format) const
  228. {
  229. }
  230. void PPSuperregion::clear()
  231. {
  232. }