OpenGM  2.3.x
Discrete Graphical Model Library
minstcutibfs.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_MINSTCUTIBFS_HXX
3 #define OPENGM_MINSTCUTIBFS_HXX
4 
5 #include <queue>
6 #include <cassert>
7 #include "ibfs.h"
8 
9 namespace opengm {
10  namespace external {
11 
13  template<class NType, class VType>
14  class MinSTCutIBFS{
15  public:
16  // Type-Definitions
17  typedef NType node_type;
18  typedef VType ValueType;
19  //typedef IBFSGraph<ValueType,ValueType,ValueType> graph_type;
20  typedef IBFSGraph graph_type;
21 
22  // Methods
23  MinSTCutIBFS();
24  MinSTCutIBFS(size_t numberOfNodes, size_t numberOfEdges);
25  void addEdge(node_type,node_type,ValueType);
26  void calculateCut(std::vector<bool>&);
27 
28  private:
29  // Members
30  graph_type* graph_;
31  size_t numberOfNodes_;
32  size_t numberOfEdges_;
33  static const NType S = 0;
34  static const NType T = 1;
35  };
36 
37  //*********************
38  //** Implementation **
39  //*********************
40 
41  template<class NType, class VType>
43  numberOfNodes_ = 2;
44  numberOfEdges_ = 0;
45  graph_ = NULL;
46  };
47 
48  template<class NType, class VType>
49  MinSTCutIBFS<NType,VType>::MinSTCutIBFS(size_t numberOfNodes, size_t numberOfEdges) {
50  numberOfNodes_ = numberOfNodes;
51  numberOfEdges_ = numberOfEdges;
52  //graph_ = new graph_type(numberOfNodes_-2,numberOfEdges_);
53  graph_ = new graph_type();
54  graph_->initSize((int)(numberOfNodes_-2),(int)(numberOfEdges_ - 2*(numberOfNodes_-2)));
55  //for(size_t i=0; i<numberOfNodes_-2;++i)
56  // graph_->add_node();
57  };
58 
59 
60  template<class NType, class VType>
62  assert(n1 < numberOfNodes_);
63  assert(n2 < numberOfNodes_);
64  assert(cost >= 0);
65  if(n1==S) {
66  //graph_->add_tweights( n2-2, /* capacities */ cost, 0);
67  graph_->addNode( (int)(n2-2), /* capacities */ (int)(cost*10000), 0);
68  }else if(n2==T) {
69  //graph_->add_tweights( n1-2, /* capacities */ 0, cost);
70  graph_->addNode( (int)(n1-2), /* capacities */ 0, (int)(cost*10000));
71  }else{
72  //graph_->add_edge( n1-2, n2-2, /* capacities */ cost, 0 );
73  graph_->addEdge( (int)(n1-2), (int)(n2-2), /* capacities */ (int)(cost*10000), 0 );
74 
75  }
76  };
77 
78  template<class NType, class VType>
79  void MinSTCutIBFS<NType,VType>::calculateCut(std::vector<bool>& segmentation) {
80  //int flow = graph_->maxflow();
81  graph_->initGraph();
82  graph_->computeMaxFlow();
83  segmentation.resize(numberOfNodes_);
84  for(size_t i=2; i<numberOfNodes_; ++i) {
85  //if (graph_->what_segment(i-2) == graph_type::SOURCE)
86  if(graph_->isNodeOnSrcSide(i-2) )
87  segmentation[i]=false;
88  else
89  segmentation[i]=true;
90  }
91  return;
92  };
93 
94  } //namespace external
95 } // namespace opengm
96 
97 #endif // #ifndef OPENGM_MINSTCUTIBFS_HXX
98 
The OpenGM namespace.
Definition: config.hxx:43
IBFS solver for the min st-cut framework GraphCut.
void addEdge(node_type, node_type, ValueType)
void calculateCut(std::vector< bool > &)