通过直接与之交互,这应该可以更有效地实现 skimage.graph.MCP Cython课程。便利包装 route_through_array 不够通用。假设我正确理解你的问题,你所寻找的基本上是 MCP.find_costs() 方法。
skimage.graph.MCP
route_through_array
MCP.find_costs()
您的代码看起来会像(忽略导入)
img = np.random.rand(400,400) img = img.astype(dtype=int) starts = [[1,1], [2,2], [3,3], [4,5], [6,17]] ends = [[301,201], [300,300], [305,305], [304,328], [336,317]] # Pass full set of start and end points to `MCP.find_costs` from skimage.graph import MCP m = MCP(img) cost_array, tracebacks_array = m.find_costs(starts, ends) # Transpose `ends` so can be used to index in NumPy ends_idx = tuple(np.asarray(ends).T.tolist()) costs = cost_array[ends_idx] # Compute exact minimum cost path to each endpoint tracebacks = [m.traceback(end) for end in ends]
请注意原始输出 cost_array 实际上是一个完全密集的阵列,形状相同 img ,仅在您要求终点时才有有限值。这种方法唯一可能的问题是,来自多个起始点的最小路径是否收敛到相同的终点。您只能通过上面的代码获得这些收敛路径中较低者的完整回溯。
cost_array
img
回溯步骤仍然有一个循环。这很可能通过使用删除 tracebacks_array 并与`m.offsets交互,这也将消除上面提到的歧义。但是,如果您只想要最小成本和最佳路径,则可以省略此循环 - 只需使用argmin查找最低成本,并跟踪该单个端点(或多个端点,如果多个端点并列最低) ) 背部。
tracebacks_array