PPSuperregion.cpp 6.0 KB

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