當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。