PPSuperregion.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #ifdef NICE_USELIB_ICE
  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. #else
  153. throw("PPSuperRegion.cpp: please use ice library for this function");
  154. #endif
  155. }
  156. #ifdef NICE_USELIB_ICE
  157. void PPSuperregion::getSuperregions(const Examples &regions, const NICE::Matrix &mask, vector<ice::Region> &superregions, vector<int> &classes, NICE::Matrix &smask)
  158. {
  159. NICE::Image tmp (mask.rows(), mask.cols());
  160. tmp.set(0);
  161. NICE::ColorImage m2 (tmp, tmp, tmp);
  162. for(int y = 0; y < (int)mask.cols(); y++)
  163. {
  164. for(int x = 0; x < (int)mask.rows(); x++)
  165. {
  166. int pos = mask(x,y);
  167. m2.setPixel(x,y,0,regions[pos].first);
  168. m2.setPixel(x,y,1,regions[pos].first);
  169. m2.setPixel(x,y,2,regions[pos].first);
  170. }
  171. }
  172. RSMeanShift rs(conf);
  173. int count = rs.transformSegmentedImg( m2, smask);
  174. classes.resize(count);
  175. for(int i = 0; i < count; i++)
  176. {
  177. superregions.push_back(ice::Region());
  178. }
  179. for(int y = 0; y < (int)smask.cols(); y++)
  180. {
  181. for(int x = 0; x < (int)smask.rows(); x++)
  182. {
  183. int pos = smask(x,y);
  184. superregions[pos].Add(x,y);
  185. classes[pos] = regions[mask(x,y)].first;
  186. }
  187. }
  188. }
  189. #endif
  190. void PPSuperregion::trainShape(Examples &regions, NICE::Matrix &mask)
  191. {
  192. #ifdef NICE_USELIB_ICE
  193. // bestimme Superregionen
  194. vector<ice::Region> superregions;
  195. vector<int> classes;
  196. // refactor-nice.pl: check this substitution
  197. // old: Image smask;
  198. NICE::Matrix smask;
  199. getSuperregions(regions, mask, superregions, classes, smask);
  200. // berechne die Momente der Superregionen und speichere diese als Merkmale ab
  201. for(int i = 0; i < (int)superregions.size(); i++)
  202. {
  203. ice::Moments m;
  204. superregions[i].CalcMoments(m);
  205. NICE::Vector tmp = makeEVector(m.AffineHuInvariants());
  206. NICE::Vector *tmp2 = new NICE::Vector(tmp);
  207. shapefeats.push_back(pair<int, Example>(classes[i], Example(tmp2)));
  208. }
  209. #else
  210. throw("PPSuperRegion.cpp: please use ice library for this function");
  211. #endif
  212. }
  213. void PPSuperregion::finishShape(ClassNames &cn)
  214. {
  215. //Lerne Klassifikator mit dem den Formmerkmalen an
  216. FeaturePool fp;
  217. Feature *f = new VectorFeature ( 7 );
  218. f->explode ( fp );
  219. delete f;
  220. rf->train ( fp, shapefeats);
  221. }
  222. void PPSuperregion::restore (istream & is, int format)
  223. {
  224. }
  225. void PPSuperregion::store (ostream & os, int format) const
  226. {
  227. }
  228. void PPSuperregion::clear()
  229. {
  230. }
  231. #endif