PPGraphCut.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "PPGraphCut.h"
  2. #include "objrec/segmentation/RegionGraph.h"
  3. using namespace std;
  4. using namespace NICE;
  5. using namespace OBJREC;
  6. void PPGraphCut::setClassNo(int _classno)
  7. {
  8. classno = _classno;
  9. coocurence = new double[classno*classno];
  10. for(int i = 0; i < classno*classno; i++)
  11. {
  12. coocurence[i] = 0.0;
  13. }
  14. }
  15. PPGraphCut::PPGraphCut()
  16. {
  17. conf = new Config();
  18. Init();
  19. }
  20. PPGraphCut::PPGraphCut(const Config *_conf):conf(_conf)
  21. {
  22. Init();
  23. }
  24. void PPGraphCut::Init()
  25. {
  26. std::string section = "PostProcess";
  27. }
  28. PPGraphCut::~PPGraphCut()
  29. {
  30. }
  31. void PPGraphCut::optimizeImage(RegionGraph &regions, vector<vector<double> > & probabilities)
  32. {
  33. vector<Node*> nodes;
  34. regions.get(nodes);
  35. GCoptimizationGeneralGraph graphcut(nodes.size(), classno);
  36. graphcut.setSmoothCost(coocurence);
  37. map<pair<int,int>, int> pairs;
  38. for(int i = 0; i < (int) nodes.size(); i++)
  39. {
  40. vector<Node*> nbs;
  41. nodes[i]->getNeighbors(nbs);
  42. int pos1 = nodes[i]->getNumber();
  43. for(int j = 0; j < (int)nbs.size(); j++)
  44. {
  45. int pos2 = nbs[j]->getNumber();
  46. pair<int,int> p(std::min(pos1,pos2),std::max(pos1,pos2));
  47. map<pair<int,int>, int>::iterator iter = pairs.find(p);
  48. if(iter == pairs.end())
  49. {
  50. pairs.insert(make_pair(p,1));
  51. graphcut.setNeighbors(pos1, pos2,1.0);
  52. }
  53. }
  54. for(int l = 0; l < classno; l++)
  55. {
  56. double val = probabilities[i][l];
  57. if(val <= 0.0)
  58. val = 1e-10;
  59. val = -log(val);
  60. graphcut.setDataCost(pos1, l, val);
  61. }
  62. graphcut.setLabel(pos1, nodes[i]->getLabel());
  63. }
  64. graphcut.swap(20);
  65. //MRF::EnergyVal E_smooth = graphcut->smoothnessEnergy();
  66. //MRF::EnergyVal E_data = graphcut->dataEnergy();
  67. for (int i = 0; i < (int)nodes.size(); i++ )
  68. {
  69. regions[i]->setLabel(graphcut.whatLabel(i));
  70. }
  71. }
  72. void PPGraphCut::optimizeImage(Examples &regions, NICE::Matrix &mask, NICE::MultiChannelImageT<double> & probabilities)
  73. {
  74. RegionGraph g;
  75. g.computeGraph(regions, mask);
  76. vector<vector<double> > probs;
  77. for(int p = 0; p < (int)regions.size(); p++)
  78. {
  79. vector<double> pr;
  80. for(int l = 0; l < classno; l++)
  81. {
  82. pr.push_back(probabilities.get(regions[p].second.x, regions[p].second.y, l));
  83. }
  84. probs.push_back(pr);
  85. }
  86. optimizeImage(g, probs);
  87. }
  88. void PPGraphCut::trainImage(RegionGraph &g)
  89. {
  90. vector<Node*> nodes;
  91. g.get(nodes);
  92. for(int i = 0; i < (int) nodes.size(); i++)
  93. {
  94. vector<Node*> nbs;
  95. nodes[i]->getNeighbors(nbs);
  96. for(int j = 0; j < (int)nbs.size(); j++)
  97. {
  98. //if(nodes[i]->getLabel() != nbs[j]->getLabel())
  99. coocurence[nodes[i]->getLabel()*classno+nbs[j]->getLabel()]+=1.0;
  100. }
  101. }
  102. }
  103. void PPGraphCut::trainImage(Examples &regions, NICE::Matrix &mask)
  104. {
  105. // coocurence Matrix bestimmen
  106. RegionGraph g;
  107. g.computeGraph(regions, mask);
  108. trainImage(g);
  109. }
  110. void PPGraphCut::finishPP(ClassNames &cn)
  111. {
  112. for(int i = 0; i < classno; i++)
  113. {
  114. for(int j = 0; j < classno; j++)
  115. {
  116. cout << coocurence[classno*i+j] << " ";
  117. }
  118. cout << endl;
  119. }
  120. cout << endl;
  121. double weight = conf->gD( "PPGC", "weight", 0.01 );
  122. double maxv = -numeric_limits<double>::max();
  123. for(int i = 0; i < classno; i++)
  124. {
  125. for(int j = 0; j < classno; j++)
  126. {
  127. if(j == i)
  128. coocurence[classno*i+j] = 0.0;
  129. else
  130. maxv = std::max(maxv, coocurence[classno*i+j]);
  131. }
  132. }
  133. maxv+=1+1e-10;
  134. for(int i = 0; i < classno; i++)
  135. {
  136. for(int j = 0; j < classno; j++)
  137. {
  138. if(j == i)
  139. coocurence[classno*i+j] = 0.0;
  140. else
  141. coocurence[classno*i+j] = -weight*(log(( coocurence[classno*i+j]+1.0)/maxv));
  142. }
  143. }
  144. for(int i = 0; i < classno; i++)
  145. {
  146. for(int j = 0; j < classno; j++)
  147. {
  148. cout << coocurence[classno*i+j] << " ";
  149. }
  150. cout << endl;
  151. }
  152. //GetChar();
  153. }
  154. void PPGraphCut::restore (istream & is, int format)
  155. {
  156. }
  157. void PPGraphCut::store (ostream & os, int format) const
  158. {
  159. }
  160. void PPGraphCut::clear()
  161. {
  162. }