Aging_MouthReplace / dlibs /docs /dlib /graph_cuts /min_cut_abstract.h.html
AshanGimhana's picture
Upload folder using huggingface_hub
9375c9a verified
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - min_cut_abstract.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2012 Davis E. King ([email protected])
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license.
</font><font color='#0000FF'>#undef</font> DLIB_MIN_CuT_ABSTRACT_Hh_
<font color='#0000FF'>#ifdef</font> DLIB_MIN_CuT_ABSTRACT_Hh_
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../graph_utils.h.html'>../graph_utils.h</a>"
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>namespace</font> dlib
<b>{</b>
<a name='node_label'/><font color='#009900'>/*!A node_label
The node_label type is the type used to label which part of a graph cut
a node is on. It is used by all the graph cut tools. The three possible
values of a node label are SOURCE_CUT, SINK_CUT, or FREE_NODE.
!*/</font>
<font color='#0000FF'>typedef</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> node_label;
<font color='#0000FF'>const</font> node_label SOURCE_CUT <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#0000FF'>const</font> node_label SINK_CUT <font color='#5555FF'>=</font> <font color='#979000'>254</font>;
<font color='#0000FF'>const</font> node_label FREE_NODE <font color='#5555FF'>=</font> <font color='#979000'>255</font>;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>class</font> <b><a name='flow_graph'></a>flow_graph</b>
<b>{</b>
<font color='#009900'>/*!
WHAT THIS OBJECT REPRESENTS
This object represents a flow capacity graph for use with the
min_cut algorithm defined below. In particular, this object
is a kind of directed graph where the edge weights specify the
flow capacities.
Note that there is no dlib::flow_graph object. What you are
looking at here is simply the interface definition for a graph
which can be used with the min_cut algorithm. You must implement
your own version of this object for the graph you wish to work with
and then pass it to the min_cut::operator() routine.
It's also worth pointing out that this graph has symmetric edge
connections. That is, if there is an edge from node A to node B
then there must also be an edge from node B to node A.
!*/</font>
<font color='#0000FF'>public</font>:
<font color='#0000FF'>class</font> <b><a name='out_edge_iterator'></a>out_edge_iterator</b>
<b>{</b>
<font color='#009900'>/*!
WHAT THIS OBJECT REPRESENTS
This is a simple forward iterator for iterating over the neighbors
of a node in the graph. It also represents the fact that the neighbors
are on the end of an outgoing edge. That is, the edge represents
the amount of flow which can flow towards the neighbor.
!*/</font>
<font color='#0000FF'>public</font>:
<b><a name='out_edge_iterator'></a>out_edge_iterator</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- constructs an iterator in an undefined state. It can't
be used until assigned with a valid iterator.
!*/</font>
<b><a name='out_edge_iterator'></a>out_edge_iterator</b><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> out_edge_iterator<font color='#5555FF'>&amp;</font> item
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- #*this is a copy of item
!*/</font>
out_edge_iterator<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> out_edge_iterator<font color='#5555FF'>&amp;</font> item
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- #*this is a copy of item
- returns #*this
!*/</font>
<font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> out_edge_iterator<font color='#5555FF'>&amp;</font> item
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- *this and item are iterators over the neighbors for the
same node.
ensures
- returns false if *this and item both reference the same
node in the graph and true otherwise.
!*/</font>
out_edge_iterator<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>+</font><font color='#5555FF'>+</font><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- advances *this to the next neighbor node.
- returns a reference to the updated *this
(i.e. this is the ++object form of the increment operator)
!*/</font>
<b>}</b>;
<font color='#0000FF'>class</font> <b><a name='in_edge_iterator'></a>in_edge_iterator</b>
<b>{</b>
<font color='#009900'>/*!
WHAT THIS OBJECT REPRESENTS
This is a simple forward iterator for iterating over the neighbors
of a node in the graph. It also represents the fact that the neighbors
are on the end of an incoming edge. That is, the edge represents
the amount of flow which can flow out of the neighbor node.
!*/</font>
<font color='#0000FF'>public</font>:
<b><a name='in_edge_iterator'></a>in_edge_iterator</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- constructs an iterator in an undefined state. It can't
be used until assigned with a valid iterator.
!*/</font>
<b><a name='in_edge_iterator'></a>in_edge_iterator</b><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> in_edge_iterator<font color='#5555FF'>&amp;</font> item
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- #*this is a copy of item
!*/</font>
in_edge_iterator<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> in_edge_iterator<font color='#5555FF'>&amp;</font> item
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- #*this is a copy of item
- returns #*this
!*/</font>
<font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> in_edge_iterator<font color='#5555FF'>&amp;</font> item
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- *this and item are iterators over the neighbors for the
same node.
ensures
- returns false if *this and item both reference the same
node in the graph and true otherwise.
!*/</font>
in_edge_iterator<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>+</font><font color='#5555FF'>+</font><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- advances *this to the next neighbor node.
- returns a reference to the updated *this
(i.e. this is the ++object form of the increment operator)
!*/</font>
<b>}</b>;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='number_of_nodes'></a>number_of_nodes</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
ensures
- returns the number of nodes in the graph.
!*/</font>
out_edge_iterator <b><a name='out_begin'></a>out_begin</b><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- idx &lt; number_of_nodes()
ensures
- returns an iterator pointing to the first neighboring node of
the idx-th node. If no such node exists then returns out_end(idx).
- The returned iterator also represents the directed edge going from
node idx to the neighbor.
!*/</font>
in_edge_iterator <b><a name='in_begin'></a>in_begin</b><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- idx &lt; number_of_nodes()
ensures
- returns an iterator pointing to the first neighboring node of
the idx-th node. If no such node exists then returns in_end(idx).
- The returned iterator also represents the directed edge going from
the neighbor to node idx.
!*/</font>
out_edge_iterator <b><a name='out_end'></a>out_end</b><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- idx &lt; number_of_nodes()
ensures
- returns an iterator to one past the last neighboring node of
the idx-th node.
!*/</font>
in_edge_iterator <b><a name='in_end'></a>in_end</b><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- idx &lt; number_of_nodes()
ensures
- returns an iterator to one past the last neighboring node of
the idx-th node.
!*/</font>
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='node_id'></a>node_id</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> out_edge_iterator<font color='#5555FF'>&amp;</font> it
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- it == a valid iterator (i.e. it must be in the range [out_begin(idx), out_end(idx))
for some valid idx)
ensures
- returns a number IDX such that:
- 0 &lt;= IDX &lt; number_of_nodes()
- IDX == The index which uniquely identifies the node pointed to by the
iterator it. This number can be used with any member function in this
object which expect a node index. (e.g. get_label(IDX) == the label for the
node pointed to by it)
!*/</font>
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='node_id'></a>node_id</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> in_edge_iterator<font color='#5555FF'>&amp;</font> it
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- it == a valid iterator (i.e. it must be in the range [in_begin(idx), in_end(idx))
for some valid idx)
ensures
- returns a number IDX such that:
- 0 &lt;= IDX &lt; number_of_nodes()
- IDX == The index which uniquely identifies the node pointed to by the
iterator it. This number can be used with any member function in this
object which expect a node index. (e.g. get_label(IDX) == the label for the
node pointed to by it)
!*/</font>
<font color='#009900'>// This typedef should be for a type like int or double. It
</font> <font color='#009900'>// must also be capable of representing signed values.
</font> <font color='#0000FF'>typedef</font> an_integer_or_real_type edge_type;
edge_type <b><a name='get_flow'></a>get_flow</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx1,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx2
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- idx1 &lt; number_of_nodes()
- idx2 &lt; number_of_nodes()
- idx1 and idx2 are neighbors in the graph
ensures
- returns the residual flow capacity from the idx1-th node to the idx2-th node.
- It is valid for this function to return a floating point value of infinity.
This value means this edge has an unlimited capacity.
!*/</font>
edge_type <b><a name='get_flow'></a>get_flow</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> out_edge_iterator<font color='#5555FF'>&amp;</font> it
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- it == a valid iterator (i.e. it must be in the range [out_begin(idx), out_end(idx))
for some valid idx)
ensures
- let IDX = node_id(it)
- it represents the directed edge from a node, call it H, to the node IDX. Therefore,
this function returns get_flow(H,IDX)
- It is valid for this function to return a floating point value of infinity.
This value means this edge has an unlimited capacity.
!*/</font>
edge_type <b><a name='get_flow'></a>get_flow</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> in_edge_iterator<font color='#5555FF'>&amp;</font> it
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- it == a valid iterator (i.e. it must be in the range [in_begin(idx), in_end(idx))
for some valid idx)
ensures
- let IDX = node_id(it)
- it represents the directed edge from node IDX to another node, call it H. Therefore,
this function returns get_flow(IDX,H)
- It is valid for this function to return a floating point value of infinity.
This value means this edge has an unlimited capacity.
!*/</font>
<font color='#0000FF'><u>void</u></font> <b><a name='adjust_flow'></a>adjust_flow</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx1,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx2,
<font color='#0000FF'>const</font> edge_type<font color='#5555FF'>&amp;</font> value
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
requires
- idx1 &lt; number_of_nodes()
- idx2 &lt; number_of_nodes()
- idx1 and idx2 are neighbors in the graph
ensures
- #get_flow(idx1,idx2) == get_flow(idx1,idx2) + value
- #get_flow(idx2,idx1) == get_flow(idx2,idx1) - value
!*/</font>
<font color='#0000FF'><u>void</u></font> <b><a name='set_label'></a>set_label</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx,
node_label value
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
requires
- idx &lt; number_of_nodes()
ensures
- #get_label(idx) == value
!*/</font>
node_label <b><a name='get_label'></a>get_label</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- idx &lt; number_of_nodes()
ensures
- returns the label for the idx-th node in the graph.
!*/</font>
<b>}</b>;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
<font color='#0000FF'>typename</font> flow_graph
<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>typename</font> flow_graph::edge_type <b><a name='graph_cut_score'></a>graph_cut_score</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> flow_graph<font color='#5555FF'>&amp;</font> g
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
requires
- flow_graph == an object with an interface compatible with the flow_graph
object defined at the top of this file, or, an implementation of
dlib/directed_graph/directed_graph_kernel_abstract.h.
ensures
- returns the sum of the outgoing flows from nodes with a label of SOURCE_CUT
to nodes with a label != SOURCE_CUT. Note that for a directed_graph object,
the labels are stored in the node's data field.
!*/</font>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>class</font> <b><a name='min_cut'></a>min_cut</b>
<b>{</b>
<font color='#009900'>/*!
WHAT THIS OBJECT REPRESENTS
This is a function object which can be used to find the min cut
on a graph.
The implementation is based on the method described in the following
paper:
An Experimental Comparison of Min-Cut/Max-Flow Algorithms for
Energy Minimization in Vision, by Yuri Boykov and Vladimir Kolmogorov,
in PAMI 2004.
!*/</font>
<font color='#0000FF'>public</font>:
<b><a name='min_cut'></a>min_cut</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*!
ensures
- this object is properly initialized
!*/</font>
<font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
<font color='#0000FF'>typename</font> flow_graph
<font color='#5555FF'>&gt;</font>
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
flow_graph<font color='#5555FF'>&amp;</font> g,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> source_node,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> sink_node
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- flow_graph == an object with an interface compatible with the flow_graph
object defined at the top of this file.
- source_node != sink_node
- source_node &lt; g.number_of_nodes()
- sink_node &lt; g.number_of_nodes()
- for all valid i and j:
- g.get_flow(i,j) &gt;= 0
(i.e. all the flow capacities/edge weights are non-negative)
- g does not contain any self loops. That is, no nodes are neighbors with
themselves.
ensures
- Finds the minimum cut on the given graph. That is, this function finds
a labeling of nodes in g such that graph_cut_score(g) would be minimized. Note
that the flow values in #g are modified by this algorithm so if you want
to obtain the min cut score you must call min_cut::operator(), then copy
the flow values back into #g, and then call graph_cut_score(#g). But in most
cases you don't care about the value of the min cut score, rather, you
just want the labels in #g.
- #g.get_label(source_node) == SOURCE_CUT
- #g.get_label(sink_node) == SINK_CUT
- for all valid i:
- #g.get_label(i) == SOURCE_CUT, SINK_CUT, or FREE_NODE
- if (#g.get_label(i) == SOURCE_CUT) then
- The minimum cut of g places node i into the source side of the cut.
- if (#g.get_label(i) == SINK_CUT) then
- The minimum cut of g places node i into the sink side of the cut.
- if (#g.get_label(i) == FREE_NODE) then
- Node i can be labeled SOURCE_CUT or SINK_CUT. Both labelings
result in the same cut score.
- When interpreting g as a graph of flow capacities from the source_node
to the sink_node we can say that the min cut problem is equivalent to
the max flow problem. This equivalent problem is to find out how to push
as much "flow" from the source node to the sink node as possible.
Upon termination, #g will contain the final flow residuals in addition to
the graph cut labels. That is, for all valid i and j:
- #g.get_flow(i,j) == the residual flow capacity left after the max
possible amount of flow is passing from the source node to the sink
node. For example, this means that #g.get_flow(i,j) == 0 whenever
node i is in the SOURCE_CUT and j is in the SINK_CUT.
- #g.get_flow(i,j) &gt;= 0
!*/</font>
<font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
<font color='#0000FF'>typename</font> directed_graph
<font color='#5555FF'>&gt;</font>
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
directed_graph<font color='#5555FF'>&amp;</font> g,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> source_node,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> sink_node
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
<font color='#009900'>/*!
requires
- directed_graph == an implementation of dlib/directed_graph/directed_graph_kernel_abstract.h
- directed_graph::type == node_label
- directed_graph::edge_type == and integer or double type
- source_node != sink_node
- source_node &lt; g.number_of_nodes()
- sink_node &lt; g.number_of_nodes()
- for all valid i and j:
- edge(g,i,j) &gt;= 0
(i.e. all the flow capacities/edge weights are positive)
- graph_contains_length_one_cycle(g) == false
- graph_has_symmetric_edges(g) == true
ensures
- This routine simply converts g into a flow graph and calls the version
of operator() defined above. Note that the conversion is done in O(1)
time, it's just an interface adaptor.
- edge weights in g correspond to network flows while the .data field of
each node in g corresponds to the graph node labels.
- upon termination, the flows and labels in g will have been modified
as described in the above operator() routine.
!*/</font>
<b>}</b>;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_MIN_CuT_ABSTRACT_Hh_
</font>
</pre></body></html>