当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python skimage.graph.route_through_array用法及代码示例

用法:

skimage.graph.route_through_array(array, start, end, fully_connected=True, geometric=True)

如何使用 MCP 和 MCP_Geometric 类的简单示例。

有关 path-finding 算法的说明,请参阅 MCP 和 MCP_Geometric 类文档。

参数

arrayndarray

一系列成本。

start可迭代的

n-d 索引到定义起点的 array

end可迭代的

n-d 索引到定义终点的 array

fully_connected布尔(可选)

如果为 True,则允许对角线移动,如果为 False,则仅允许轴向移动。

geometric布尔(可选)

如果为 True,则使用 MCP_Geometric 类来计算成本,如果为 False,则使用 MCP 基类。有关 MCP 和 MCP_Geometric 之间差异的说明,请参阅类文档。

返回

path列表

n-d 索引元组列表定义从开始到结束的路径。

cost浮点数

路径的成本。如果几何的是假的,路径的成本是值的总和array沿着路径。如果几何的为真,进行更精细的计算(参见MCP_Geometric 类的文档)。

例子

>>> 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.19238815542512)
>>> # 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.stack(indices, axis=-1)
>>> 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]])

相关用法


注:本文由纯净天空筛选整理自scikit-image.org大神的英文原创作品 skimage.graph.route_through_array。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。