42 #ifndef OPENCV_IMGPROC_DETAIL_GCGRAPH_HPP
43 #define OPENCV_IMGPROC_DETAIL_GCGRAPH_HPP
47 namespace cv {
namespace detail {
48 template <
class TWeight>
class GCGraph
52 GCGraph(
unsigned int vtxCount,
unsigned int edgeCount );
54 void create(
unsigned int vtxCount,
unsigned int edgeCount );
56 void addEdges(
int i,
int j, TWeight w, TWeight revw );
57 void addTermWeights(
int i, TWeight sourceW, TWeight sinkW );
59 bool inSourceSegment(
int i );
85 template <
class TWeight>
86 GCGraph<TWeight>::GCGraph()
90 template <
class TWeight>
91 GCGraph<TWeight>::GCGraph(
unsigned int vtxCount,
unsigned int edgeCount )
93 create( vtxCount, edgeCount );
95 template <
class TWeight>
96 GCGraph<TWeight>::~GCGraph()
99 template <
class TWeight>
100 void GCGraph<TWeight>::create(
unsigned int vtxCount,
unsigned int edgeCount )
102 vtcs.reserve( vtxCount );
103 edges.reserve( edgeCount + 2 );
107 template <
class TWeight>
108 int GCGraph<TWeight>::addVtx()
111 memset( &v, 0,
sizeof(Vtx));
113 return (
int)vtcs.size() - 1;
116 template <
class TWeight>
117 void GCGraph<TWeight>::addEdges(
int i,
int j, TWeight w, TWeight revw )
129 fromI.next = vtcs[i].first;
131 vtcs[i].first = (int)
edges.size();
132 edges.push_back( fromI );
135 toI.next = vtcs[j].first;
137 vtcs[j].first = (int)
edges.size();
138 edges.push_back( toI );
141 template <
class TWeight>
142 void GCGraph<TWeight>::addTermWeights(
int i, TWeight sourceW, TWeight sinkW )
146 TWeight dw = vtcs[i].weight;
151 flow += (sourceW < sinkW) ? sourceW : sinkW;
152 vtcs[i].weight = sourceW - sinkW;
155 template <
class TWeight>
156 TWeight GCGraph<TWeight>::maxFlow()
160 const int TERMINAL = -1, ORPHAN = -2;
161 Vtx stub, *nilNode = &stub, *
first = nilNode, *last = nilNode;
164 Vtx *vtxPtr = &vtcs[0];
170 for(
int i = 0; i < (int)vtcs.size(); i++ )
176 last = last->next = v;
178 v->parent = TERMINAL;
179 v->t = v->weight < 0;
185 last->next = nilNode;
192 int e0 = -1, ei = 0, ej = 0;
193 TWeight minWeight, weight;
197 while(
first != nilNode )
203 for( ei = v->first; ei != 0; ei = edgePtr[ei].next )
205 if( edgePtr[ei^vt].weight == 0 )
207 u = vtxPtr+edgePtr[ei].dst;
213 u->dist = v->dist + 1;
217 last = last->next = u;
228 if( u->dist > v->dist+1 && u->ts <= v->ts )
233 u->dist = v->dist + 1;
248 minWeight = edgePtr[e0].weight;
251 for(
int k = 1;
k >= 0;
k-- )
253 for( v = vtxPtr+edgePtr[e0^
k].
dst;; v = vtxPtr+edgePtr[ei].dst )
255 if( (ei = v->parent) < 0 )
257 weight = edgePtr[ei^
k].weight;
258 minWeight =
MIN(minWeight, weight);
261 weight =
fabs(v->weight);
262 minWeight =
MIN(minWeight, weight);
267 edgePtr[e0].weight -= minWeight;
268 edgePtr[e0^1].weight += minWeight;
272 for(
int k = 1;
k >= 0;
k-- )
274 for( v = vtxPtr+edgePtr[e0^
k].
dst;; v = vtxPtr+edgePtr[ei].dst )
276 if( (ei = v->parent) < 0 )
278 edgePtr[ei^(
k^1)].weight += minWeight;
279 if( (edgePtr[ei^
k].weight -= minWeight) == 0 )
286 v->weight = v->weight + minWeight*(1-
k*2);
296 while( !orphans.
empty() )
298 Vtx* v2 = orphans.
back();
301 int d, minDist = INT_MAX;
305 for( ei = v2->first; ei != 0; ei = edgePtr[ei].next )
307 if( edgePtr[ei^(vt^1)].weight == 0 )
309 u = vtxPtr+edgePtr[ei].dst;
310 if( u->t != vt || u->parent == 0 )
315 if( u->ts == curr_ts )
333 u = vtxPtr+edgePtr[ej].dst;
344 for( u = vtxPtr+edgePtr[ei].
dst; u->ts != curr_ts; u = vtxPtr+edgePtr[u->parent].dst )
352 if( (v2->parent = e0) > 0 )
361 for( ei = v2->first; ei != 0; ei = edgePtr[ei].next )
363 u = vtxPtr+edgePtr[ei].dst;
365 if( u->t != vt || !ej )
367 if( edgePtr[ei^(vt^1)].weight && !u->next )
370 last = last->next = u;
372 if( ej > 0 && vtxPtr+edgePtr[ej].
dst == v2 )
383 template <
class TWeight>
384 bool GCGraph<TWeight>::inSourceSegment(
int i )
387 return vtcs[i].t == 0;
void * parent
Definition: core_c.h:1913
const void * first
Definition: core_c.h:1906
unsigned char uchar
Definition: interface.h:51
#define MIN(a, b)
Definition: cvdef.h:513
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition: base.hpp:342
CvArr * edges
Definition: imgproc_c.h:860
CV_EXPORTS OutputArray int double double InputArray OutputArray int int bool double k
Definition: imgproc.hpp:2133
InputArray int InputArray CV_IN_OUT Ptr< float > OutputArray flow
Definition: imgproc.hpp:3388
OutputArray dst
Definition: imgproc.hpp:3564
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441