Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python计算位图中两点之间的最短路径_Python_Image Processing_Bitmap_Distance - Fatal编程技术网

用python计算位图中两点之间的最短路径

用python计算位图中两点之间的最短路径,python,image-processing,bitmap,distance,Python,Image Processing,Bitmap,Distance,我有一个黑白位图图像显示在这里: 图像大小为200158 我想选取落在白色路径上的两个点,并计算仅跟随白色像素的这两个点之间的最短距离。我不知道如何处理这个问题。我想创建一个函数,我用两组x,y坐标调用它,它只返回沿着白色像素最短路径的像素数 如果有任何建议,我都将不胜感激。我不是这类算法的专家,但至少我可以通过给你一些线索来帮助你。 去年我在学校做过一个类似的项目。当时,我们正在考虑两种算法:蚁群优化算法和遗传算法 蚁群优化 这些算法是基于蚂蚁的行为,当他们试图找到最简单和最短的路线,一些食

我有一个黑白位图图像显示在这里:

图像大小为200158

我想选取落在白色路径上的两个点,并计算仅跟随白色像素的这两个点之间的最短距离。我不知道如何处理这个问题。我想创建一个函数,我用两组x,y坐标调用它,它只返回沿着白色像素最短路径的像素数


如果有任何建议,我都将不胜感激。

我不是这类算法的专家,但至少我可以通过给你一些线索来帮助你。 去年我在学校做过一个类似的项目。当时,我们正在考虑两种算法:蚁群优化算法遗传算法

蚁群优化 这些算法是基于蚂蚁的行为,当他们试图找到最简单和最短的路线,一些食物。更多信息请点击此处:

遗传算法 它们更通用,可以解决很多问题。 根据你的情况,你可能会感兴趣



很抱歉,我无法为您提供更详细的帮助。我让别人回答,给你你需要的宝贵建议。无论如何,祝你的项目好运 如评论中所述,这个问题可以简化为

解决方案背后的关键概念是将图像表示为图形,然后使用最短路径算法的预先实现

首先,观察尺寸为4x4的图像的简单表示:

T F F T
T T F T
F T T F
T T T T
其中,
T
为白点,
F
为黑点。在这种情况下,路径是相邻白点之间的一组移动

假设一个图是一组节点
{1,2,…,16}
,我们可以将每个点
(i,j)
映射到编号
i*4+j
。在图形中,边是相邻点的反射,这意味着如果
(i1,j1)
(i2,j2)
在图像中相邻,则
i1*4+j1
i2*4+j2
在图形中相邻

在这一点上,我们有一个可以计算最短路径的图

幸运的是,python为图像加载和最短路径实现提供了简单的实现。以下代码处理路径计算,并将结果可视化:

import itertools

from scipy import misc
from scipy.sparse.dok import dok_matrix
from scipy.sparse.csgraph import dijkstra

# Load the image from disk as a numpy ndarray
original_img = misc.imread('path_t_image')

# Create a flat color image for graph building:
img = original_img[:, :, 0] + original_img[:, :, 1] + original_img[:, :, 2]


# Defines a translation from 2 coordinates to a single number
def to_index(y, x):
    return y * img.shape[1] + x


# Defines a reversed translation from index to 2 coordinates
def to_coordinates(index):
    return index / img.shape[1], index % img.shape[1]


# A sparse adjacency matrix.
# Two pixels are adjacent in the graph if both are painted.
adjacency = dok_matrix((img.shape[0] * img.shape[1],
                        img.shape[0] * img.shape[1]), dtype=bool)

# The following lines fills the adjacency matrix by
directions = list(itertools.product([0, 1, -1], [0, 1, -1]))
for i in range(1, img.shape[0] - 1):
    for j in range(1, img.shape[1] - 1):
        if not img[i, j]:
            continue

        for y_diff, x_diff in directions:
            if img[i + y_diff, j + x_diff]:
                adjacency[to_index(i, j),
                          to_index(i + y_diff, j + x_diff)] = True

# We chose two arbitrary points, which we know are connected
source = to_index(14, 47)
target = to_index(151, 122)

# Compute the shortest path between the source and all other points in the image
_, predecessors = dijkstra(adjacency, directed=False, indices=[source],
                           unweighted=True, return_predecessors=True)

# Constructs the path between source and target
pixel_index = target
pixels_path = []
while pixel_index != source:
    pixels_path.append(pixel_index)
    pixel_index = predecessors[0, pixel_index]


# The following code is just for debugging and it visualizes the chosen path
import matplotlib.pyplot as plt

for pixel_index in pixels_path:
    i, j = to_coordinates(pixel_index)
    original_img[i, j, 0] = original_img[i, j, 1] = 0

plt.imshow(original_img)
plt.show()

免责声明:

  • 我在图像处理方面没有经验,所以我怀疑解决方案中的每一步
  • 该解决方案假设一个非常简单的邻接谓词。对于这一部分,在计算几何中可能有一些更好的方法

skimage.graph有一个专门针对图像的Dijkstra实现,只需几行即可解决您的问题:

import numpy as np
import skimage.graph

T,F = True,False
array = np.asarray(
    [[T, F, F, T],
     [T, T, F, T],
     [F, T, T, F],
     [T, T, T, T]])
costs = np.where(array, 1, 1000)
path, cost = skimage.graph.route_through_array(
    costs, start=(0,0), end=(3,3), fully_connected=True)

在本例中,
路径
将等于[(0,0),(1,1),(2,2),(3,3)],这确实是最短路径。

看起来像Dijkstra,有点开销。顶点是两条或多条直线相交的地方,如果目标点恰好位于一条直线上,则需要为其添加vertix。您需要通过确定每个边上的白色像素数来计算每个边的权重。感谢您的回复。我不知道那个算法。Dijkstra的算法需要一个图,这意味着你需要以某种方式定义一个图。我首先为每个白色像素包含一个节点,然后如果像素在图像中“相邻”,则在两个像素节点之间添加一条边。如果两个像素的x坐标之间的绝对差为1,y坐标之间的绝对差为1,则两个像素可能相邻。ofc,每个像素一个节点可以节省计算每条线长度的时间。好主意。使它成为一个更大的网络。。。很有意思的是,看看你需要做多少搜索来抵消较小的网络结构。这些算法通常解决(近似)更复杂的旅行推销员问题,这是NP完全问题。Elisha,谢谢你的解决方案。这是一个非常有趣的方法,效果非常好。我没想到有人会深入细节,并实际发布一个完整的答案,做得好!