|
Alpha Expansion Library
C++ library for the Alpha-Expansion graph-cut algorithm with Python bindings
|
Stores the graph and energy costs for the Alpha-Expansion algorithm. More...
#include <EnergyModel.hpp>
Public Types | |
| using | UnaryCostFn = std::function< T(int node, int label)> |
Callable that returns the unary cost for assigning label to node. | |
| using | PairwiseCostFn = std::function< T(int node1, int node2, int label1, int label2)> |
Public Member Functions | |
| EnergyModel (int num_nodes, int num_labels) | |
| Constructs an energy model with all labels initialized to 0. | |
| int | num_nodes () const |
| Returns the total number of nodes. | |
| int | num_labels () const |
| Returns the total number of labels. | |
| int | get_label (int node) const |
Returns the current label assigned to node. | |
| void | set_label (int node, int label) |
Assigns label to node. | |
| const std::vector< int > & | get_labels () const |
| Returns the full label vector (one entry per node). | |
| void | set_labels (const std::vector< int > &labels) |
| Replaces the full label vector. | |
| void | set_unary_cost_fn (UnaryCostFn fn) |
| Sets a callback function for unary costs. | |
| void | set_pairwise_cost_fn (PairwiseCostFn fn) |
| Sets a callback function for pairwise costs. | |
| T | get_unary_cost (int node, int label) const |
Returns the unary cost for assigning label to node. | |
| T | get_pairwise_cost (int node1, int node2, int label1, int label2) const |
| Returns the pairwise cost for the given node–label pair. | |
| void | set_unary_costs (const std::vector< T > &costs) |
Sets unary costs from a flat row-major array of size num_nodes * num_labels. | |
| void | set_pairwise_costs (const std::vector< T > &costs) |
Sets a global pairwise cost matrix of size num_labels * num_labels. | |
| void | set_edge_weights (const std::vector< int > &n1s, const std::vector< int > &n2s, const std::vector< T > &weights) |
| Sets per-edge smoothness weights (Potts model). | |
| void | add_neighbor (int node1, int node2) |
Adds an undirected edge between node1 and node2. | |
| void | add_grid_edges (int width, int height) |
Populates a 4-connected grid neighbourhood for an image of size width × height. | |
| const std::vector< int > & | get_neighbors (int node) const |
Returns the neighbours of node. | |
| std::vector< int > | get_active_nodes (int alpha_label) const |
Returns the indices of all nodes that do not currently have alpha_label. | |
| T | evaluate_total_energy (const std::vector< int > &eval_labels) const |
| Evaluates the total energy for a given label assignment. | |
| T | evaluate_total_energy () const |
| Evaluates the total energy for the model's current label assignment. | |
Stores the graph and energy costs for the Alpha-Expansion algorithm.
An EnergyModel holds the nodes, the neighbor graph and the unary and pairwise cost functions that define the energy to be minimized. Costs can be set as dense arrays (good for image grids) or as callback functions (good for general graphs). If both are set, the array is used.
| T | Numeric type for costs (int32_t, float, or double). |
| using EnergyModel< T >::PairwiseCostFn = std::function<T(int node1, int node2, int label1, int label2)> |
Callable that returns the pairwise cost for assigning label1 to node1 and label2 to node2.
| using EnergyModel< T >::UnaryCostFn = std::function<T(int node, int label)> |
Callable that returns the unary cost for assigning label to node.
|
inline |
Constructs an energy model with all labels initialized to 0.
| num_nodes | Number of nodes (pixels, graph vertices, etc.). |
| num_labels | Number of labels (classes, segments, communities, etc.). |
|
inline |
Populates a 4-connected grid neighbourhood for an image of size width × height.
| std::invalid_argument | if width * height != num_nodes(). |
|
inline |
Adds an undirected edge between node1 and node2.
|
inline |
Evaluates the total energy for the model's current label assignment.
|
inline |
Evaluates the total energy for a given label assignment.
| eval_labels | Label vector of length num_nodes(). |
|
inline |
Returns the indices of all nodes that do not currently have alpha_label.
These are the nodes that take part in an alpha-expansion move for alpha_label.
|
inline |
Returns the current label assigned to node.
|
inline |
Returns the full label vector (one entry per node).
|
inline |
Returns the neighbours of node.
|
inline |
Returns the pairwise cost for the given node–label pair.
Priority: per-edge weights > dense pairwise array > callback.
|
inline |
Returns the unary cost for assigning label to node.
Checks the dense array first. If no array is set, it uses the callback function.
|
inline |
Returns the total number of labels.
|
inline |
Returns the total number of nodes.
|
inline |
Sets per-edge smoothness weights (Potts model).
For each edge (n1, n2), the pairwise cost is 0 if the two nodes have the same label, and weight if they have different labels. This overrides both the dense pairwise array and the callback for the specified edges.
| std::invalid_argument | if the three vectors have different sizes. |
|
inline |
Assigns label to node.
|
inline |
Replaces the full label vector.
| labels | Must have size equal to num_nodes(). |
|
inline |
Sets a callback function for pairwise costs.
If set_pairwise_costs() or set_edge_weights() is also called, those take priority.
|
inline |
Sets a global pairwise cost matrix of size num_labels * num_labels.
Entry [l1 * num_labels + l2] is the cost of placing label l1 and l2 on adjacent nodes. This overrides any callback, but per-edge weights set via set_edge_weights() take priority over this matrix.
| std::invalid_argument | if the array size does not match. |
|
inline |
Sets a callback function for unary costs.
If set_unary_costs() is also called, the dense array takes priority over this callback.
|
inline |
Sets unary costs from a flat row-major array of size num_nodes * num_labels.
Entry [node * num_labels + label] is the cost of assigning label to node. This overrides any callback set via set_unary_cost_fn().
| std::invalid_argument | if the array size does not match. |