PPSuperregion.cpp 6.0 KB

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