minstcutkolmogorov.hxx

Go to the documentation of this file.
00001 #pragma once
00002 #ifndef OPENGM_MINSTCUTKOLMOGOROV_HXX
00003 #define OPENGM_MINSTCUTKOLMOGOROV_HXX
00004 
00005 #include <queue> 
00006 #include <cassert>
00007 
00008 #include <maxflowlib.h>
00009 
00010 namespace opengm {
00011 
00013 namespace external {
00014 
00016    
00018 template<class NType, class VType>
00019 class MinSTCutKolmogorov {
00020 public:
00021    typedef NType node_type;
00022    typedef VType ValueType; 
00023    typedef maxflowLib::Graph<ValueType,ValueType,ValueType> graph_type;
00024      
00025    MinSTCutKolmogorov();
00026    ~MinSTCutKolmogorov();
00027    MinSTCutKolmogorov(size_t numberOfNodes, size_t numberOfEdges);
00028    void addEdge(node_type,node_type,ValueType);
00029    void calculateCut(std::vector<bool>&);
00030       
00031 private: 
00032    graph_type*  graph_;
00033    size_t      numberOfNodes_;
00034    size_t      numberOfEdges_;
00035    static const NType S = 0;
00036    static const NType T = 1;
00037 };
00038 
00039     
00040 template<class NType, class VType>
00041 MinSTCutKolmogorov<NType,VType>::MinSTCutKolmogorov() {
00042    numberOfNodes_ = 2;
00043    numberOfEdges_ = 0;
00044    graph_         = NULL;
00045 }
00046 
00047 template<class NType, class VType>
00048 MinSTCutKolmogorov<NType,VType>::MinSTCutKolmogorov(size_t numberOfNodes, size_t numberOfEdges) {
00049    numberOfNodes_ = numberOfNodes;
00050    numberOfEdges_ = numberOfEdges;
00051    graph_         = new graph_type(numberOfNodes_-2,numberOfEdges_);
00052    graph_->add_node(numberOfNodes_-2);  
00053    //for(size_t i=0; i<numberOfNodes_-2;++i) {
00054    //   graph_->add_node(); 
00055    //}
00056 }
00057     
00058 template<class NType, class VType>
00059 MinSTCutKolmogorov<NType,VType>::~MinSTCutKolmogorov() 
00060 {
00061    if(graph_!=NULL){
00062       delete graph_;
00063    }
00064 }  
00065     
00066 template<class NType, class VType>
00067 void MinSTCutKolmogorov<NType,VType>::addEdge(node_type n1, node_type n2, ValueType cost) {
00068    assert(n1 < numberOfNodes_);
00069    assert(n2 < numberOfNodes_);
00070    assert(cost >= 0);
00071    if(n1==S) {
00072       graph_->add_tweights( n2-2,   /* capacities */  cost, 0);
00073    } else if(n2==T) {
00074       graph_->add_tweights( n1-2,   /* capacities */  0, cost);
00075    } else {
00076       graph_->add_edge( n1-2, n2-2,    /* capacities */  cost, 0 );
00077    }
00078 }
00079    
00080 template<class NType, class VType>
00081 void MinSTCutKolmogorov<NType,VType>::calculateCut(std::vector<bool>& segmentation) {  
00082    /*int flow =*/ graph_->maxflow(); 
00083    segmentation.resize(numberOfNodes_);
00084    for(size_t i=2; i<numberOfNodes_; ++i) {
00085       if (graph_->what_segment(i-2) == graph_type::SOURCE) {
00086          segmentation[i]=false;
00087       }
00088       else {
00089          segmentation[i]=true;
00090       }
00091    }  
00092    return;
00093 }
00094 
00096 
00097 } // namespace external
00098 } // namespace opengm
00099 
00100 #endif // #ifndef OPENGM_MINSTCUTBOOST_HXX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:04 2013 for OpenGM by  doxygen 1.6.3