skimage.graph.route_through_array (array, ...) | Simple example of how to use the MCP and MCP_Geometric classes. |
skimage.graph.shortest_path (arr[, reach, ...]) | Find the shortest path through an n-d array from one side to another. |
skimage.graph.MCP (costs[, offsets, ...]) | A class for finding the minimum cost path through a given n-d costs array. |
skimage.graph.MCP_Connect (costs[, offsets, ...]) | Connect source points using the distance-weighted minimum cost function. |
skimage.graph.MCP_Flexible (costs[, offsets, ...]) | Find minimum cost paths through an N-d costs array. |
skimage.graph.MCP_Geometric (costs[, ...]) | Find distance-weighted minimum cost paths through an n-d costs array. |
skimage.graph.route_through_array(array, start, end, fully_connected=True, geometric=True)
[source]
Simple example of how to use the MCP and MCP_Geometric classes.
See the MCP and MCP_Geometric class documentation for explanation of the path-finding algorithm.
Parameters: |
array : ndarray Array of costs. start : iterable n-d index into end : iterable n-d index into fully_connected : bool (optional) If True, diagonal moves are permitted, if False, only axial moves. geometric : bool (optional) If True, the MCP_Geometric class is used to calculate costs, if False, the MCP base class is used. See the class documentation for an explanation of the differences between MCP and MCP_Geometric. |
---|---|
Returns: |
path : list List of n-d index tuples defining the path from cost : float Cost of the path. If |
See also
>>> import numpy as np >>> from skimage.graph import route_through_array >>> >>> image = np.array([[1, 3], [10, 12]]) >>> image array([[ 1, 3], [10, 12]]) >>> # Forbid diagonal steps >>> route_through_array(image, [0, 0], [1, 1], fully_connected=False) ([(0, 0), (0, 1), (1, 1)], 9.5) >>> # Now allow diagonal steps: the path goes directly from start to end >>> route_through_array(image, [0, 0], [1, 1]) ([(0, 0), (1, 1)], 9.1923881554251192) >>> # Cost is the sum of array values along the path (16 = 1 + 3 + 12) >>> route_through_array(image, [0, 0], [1, 1], fully_connected=False, ... geometric=False) ([(0, 0), (0, 1), (1, 1)], 16.0) >>> # Larger array where we display the path that is selected >>> image = np.arange((36)).reshape((6, 6)) >>> image array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35]]) >>> # Find the path with lowest cost >>> indices, weight = route_through_array(image, (0, 0), (5, 5)) >>> indices = np.array(indices).T >>> path = np.zeros_like(image) >>> path[indices[0], indices[1]] = 1 >>> path array([[1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1]])
skimage.graph.shortest_path(arr, reach=1, axis=-1, output_indexlist=False)
[source]
Find the shortest path through an n-d array from one side to another.
Parameters: |
arr : ndarray of float64 reach : int, optional By default ( axis : int, optional The axis along which the path must always move forward (default -1) output_indexlist: bool, optional See return value |
---|---|
Returns: |
p : iterable of int For each step along cost : float Cost of path. This is the absolute sum of all the differences along the path. |
class skimage.graph.MCP(costs, offsets=None, fully_connected=True, sampling=None)
Bases: object
A class for finding the minimum cost path through a given n-d costs array.
Given an n-d costs array, this class can be used to find the minimum-cost path through that array from any set of points to any other set of points. Basic usage is to initialize the class and call find_costs() with a one or more starting indices (and an optional list of end indices). After that, call traceback() one or more times to find the path from any given end-position to the closest starting index. New paths through the same costs array can be found by calling find_costs() repeatedly.
The cost of a path is calculated simply as the sum of the values of the costs
array at each point on the path. The class MCP_Geometric, on the other hand, accounts for the fact that diagonal vs. axial moves are of different lengths, and weights the path cost accordingly.
Array elements with infinite or negative costs will simply be ignored, as will paths whose cumulative cost overflows to infinite.
Parameters: |
costs : ndarray offsets : iterable, optional A list of offset tuples: each offset specifies a valid move from a given n-d position. If not provided, offsets corresponding to a singly- or fully-connected n-d neighborhood will be constructed with make_offsets(), using the fully_connected : bool, optional If no sampling : tuple, optional For each dimension, specifies the distance between two cells/voxels. If not given or None, the distance is assumed unit. |
---|
offsets | (ndarray) Equivalent to the offsets provided to the constructor, or if none were so provided, the offsets created for the requested n-d neighborhood. These are useful for interpreting the traceback array returned by the find_costs() method. |
__init__(costs, offsets=None, fully_connected=True, sampling=None)
See class documentation.
find_costs()
Find the minimum-cost path from the given starting points.
This method finds the minimum-cost path to the specified ending indices from any one of the specified starting indices. If no end positions are given, then the minimum-cost path to every position in the costs array will be found.
Parameters: |
starts : iterable A list of n-d starting indices (where n is the dimension of the ends : iterable, optional A list of n-d ending indices. find_all_ends : bool, optional If ‘True’ (default), the minimum-cost-path to every specified end-position will be found; otherwise the algorithm will stop when a a path is found to any end-position. (If no |
---|---|
Returns: |
cumulative_costs : ndarray Same shape as the traceback : ndarray Same shape as the |
goal_reached()
int goal_reached(int index, float cumcost) This method is called each iteration after popping an index from the heap, before examining the neighbours.
This method can be overloaded to modify the behavior of the MCP algorithm. An example might be to stop the algorithm when a certain cumulative cost is reached, or when the front is a certain distance away from the seed point.
This method should return 1 if the algorithm should not check the current point’s neighbours and 2 if the algorithm is now done.
traceback(end)
Trace a minimum cost path through the pre-calculated traceback array.
This convenience function reconstructs the the minimum cost path to a given end position from one of the starting indices provided to find_costs(), which must have been called previously. This function can be called as many times as desired after find_costs() has been run.
Parameters: |
end : iterable An n-d index into the |
---|---|
Returns: |
traceback : list of n-d tuples A list of indices into the |
class skimage.graph.MCP_Connect(costs, offsets=None, fully_connected=True)
Bases: skimage.graph._mcp.MCP
Connect source points using the distance-weighted minimum cost function.
A front is grown from each seed point simultaneously, while the origin of the front is tracked as well. When two fronts meet, create_connection() is called. This method must be overloaded to deal with the found edges in a way that is appropriate for the application.
__init__()
Initialize self. See help(type(self)) for accurate signature.
create_connection()
create_connection id1, id2, pos1, pos2, cost1, cost2)
Overload this method to keep track of the connections that are found during MCP processing. Note that a connection with the same ids can be found multiple times (but with different positions and costs).
At the time that this method is called, both points are “frozen” and will not be visited again by the MCP algorithm.
Parameters: |
id1 : int The seed point id where the first neighbor originated from. id2 : int The seed point id where the second neighbor originated from. pos1 : tuple The index of of the first neighbour in the connection. pos2 : tuple The index of of the second neighbour in the connection. cost1 : float The cumulative cost at cost2 : float The cumulative costs at |
---|
class skimage.graph.MCP_Flexible(costs, offsets=None, fully_connected=True)
Bases: skimage.graph._mcp.MCP
Find minimum cost paths through an N-d costs array.
See the documentation for MCP for full details. This class differs from MCP in that several methods can be overloaded (from pure Python) to modify the behavior of the algorithm and/or create custom algorithms based on MCP. Note that goal_reached can also be overloaded in the MCP class.
__init__(costs, offsets=None, fully_connected=True, sampling=None)
See class documentation.
examine_neighbor(index, new_index, offset_length)
This method is called once for every pair of neighboring nodes, as soon as both nodes are frozen.
This method can be overloaded to obtain information about neightboring nodes, and/or to modify the behavior of the MCP algorithm. One example is the MCP_Connect class, which checks for meeting fronts using this hook.
travel_cost(old_cost, new_cost, offset_length)
This method calculates the travel cost for going from the current node to the next. The default implementation returns new_cost. Overload this method to adapt the behaviour of the algorithm.
update_node(index, new_index, offset_length)
This method is called when a node is updated, right after new_index is pushed onto the heap and the traceback map is updated.
This method can be overloaded to keep track of other arrays that are used by a specific implementation of the algorithm. For instance the MCP_Connect class uses it to update an id map.
class skimage.graph.MCP_Geometric(costs, offsets=None, fully_connected=True)
Bases: skimage.graph._mcp.MCP
Find distance-weighted minimum cost paths through an n-d costs array.
See the documentation for MCP for full details. This class differs from MCP in that the cost of a path is not simply the sum of the costs along that path.
This class instead assumes that the costs array contains at each position the “cost” of a unit distance of travel through that position. For example, a move (in 2-d) from (1, 1) to (1, 2) is assumed to originate in the center of the pixel (1, 1) and terminate in the center of (1, 2). The entire move is of distance 1, half through (1, 1) and half through (1, 2); thus the cost of that move is (1/2)*costs[1,1] + (1/2)*costs[1,2]
.
On the other hand, a move from (1, 1) to (2, 2) is along the diagonal and is sqrt(2) in length. Half of this move is within the pixel (1, 1) and the other half in (2, 2), so the cost of this move is calculated as (sqrt(2)/2)*costs[1,1] + (sqrt(2)/2)*costs[2,2]
.
These calculations don’t make a lot of sense with offsets of magnitude greater than 1. Use the sampling
argument in order to deal with anisotropic data.
__init__(costs, offsets=None, fully_connected=True, sampling=None)
See class documentation.
© 2011 the scikit-image team
Licensed under the BSD 3-clause License.
http://scikit-image.org/docs/0.13.x/api/skimage.graph.html