在给定的图像中,表现和解决迷宫的最佳方法是什么?

Given an JPEG image (as seen above), what's the best way to read it in, parse it into some data structure and solve the maze? My first instinct is to read the image in pixel by pixel and store it in a list (array) of boolean values: True for a white pixel, and False for a non-white pixel (the colours can be discarded). The issue with this method, is that the image may not be "pixel perfect". By that I simply mean that if there is a white pixel somewhere on a wall it may create an unintended path.

另一种方法(经过思考后我想到的)是将图像转换为SVG文件——这是在画布上绘制的路径列表。这样,路径可以读入相同类型的列表(布尔值),其中True表示路径或墙壁,False表示可移动空间。如果转换不是100%准确,并且没有完全连接所有的墙壁,就会出现一个问题。

转换为SVG的另一个问题是,这些线不是“完全”直的。这导致路径是三次贝塞尔曲线。对于一个由整数索引的布尔值列表(数组),曲线将不容易转移,并且曲线上的所有点都必须计算,但不会与列表索引完全匹配。

我认为,虽然这些方法中的一种可能有效(虽然可能不是),但对于如此大的图像,它们的效率非常低,并且存在更好的方法。如何才能做到最好(最有效和/或最简单)?有没有最好的办法?

然后是迷宫的解决。如果我用前两种方法中的任何一种,我最终都会得到一个矩阵。根据这个答案,表示迷宫的好方法是使用树,解决迷宫的好方法是使用a *算法。如何从图像中创建树?什么好主意吗?

博士TL; 最好的解析方法?转换成什么样的数据结构?结构如何帮助/阻碍解决问题?

更新 正如@Thomas推荐的那样,我已经尝试用numpy实现@Mikhail在Python中编写的内容。我觉得这个算法是正确的,但它不像我希望的那样工作。下面(代码)。PNG库为PyPNG。

import png, numpy, Queue, operator, itertools

def is_white(coord, image):
  """ Returns whether (x, y) is approx. a white pixel."""
  a = True
  for i in xrange(3):
    if not a: break
    a = image[coord[1]][coord[0] * 3 + i] > 240
  return a

def bfs(s, e, i, visited):
  """ Perform a breadth-first search. """
  frontier = Queue.Queue()
  while s != e:
    for d in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
      np = tuple(map(operator.add, s, d))
      if is_white(np, i) and np not in visited:
        frontier.put(np)
    visited.append(s)
    s = frontier.get()
  return visited

def main():
  r = png.Reader(filename = "thescope-134.png")
  rows, cols, pixels, meta = r.asDirect()
  assert meta['planes'] == 3 # ensure the file is RGB
  image2d = numpy.vstack(itertools.imap(numpy.uint8, pixels))
  start, end = (402, 985), (398, 27)
  print bfs(start, end, image2d, [])

当前回答

来吧:迷宫-求解器-python (GitHub)

我玩得很开心,并扩展了约瑟夫·克恩的答案。不要贬低它;我只是为其他人做了一些小的补充,谁可能有兴趣玩这个。

这是一个基于python的求解器,它使用BFS来查找最短路径。当时我的主要补充内容是:

图像在搜索(即。转换为纯黑白) 自动生成GIF。 自动生成AVI。

就目前情况而言,这个示例迷宫的开始/结束点是硬编码的,但我计划扩展它,以便您可以选择适当的像素。

其他回答

树搜索太多了。迷宫在解的路径上本质上是可分离的。

(感谢Reddit的rainman002为我指出了这一点。)

正因为如此,你可以快速使用连接组件来识别迷宫墙壁的连接部分。这将在像素上迭代两次。

如果您希望将其转换为解决方案路径的漂亮图表,那么您可以使用带有结构化元素的二进制操作来填充每个连接区域的“死胡同”路径。

下面是MATLAB的演示代码。它可以通过调整来更好地清理结果,使其更一般化,并使其运行得更快。(不是凌晨两点半的时候)

% read in and invert the image
im = 255 - imread('maze.jpg');

% sharpen it to address small fuzzy channels
% threshold to binary 15%
% run connected components
result = bwlabel(im2bw(imfilter(im,fspecial('unsharp')),0.15));

% purge small components (e.g. letters)
for i = 1:max(reshape(result,1,1002*800))
    [count,~] = size(find(result==i));
    if count < 500
        result(result==i) = 0;
    end
end

% close dead-end channels
closed = zeros(1002,800);
for i = 1:max(reshape(result,1,1002*800))
    k = zeros(1002,800);
    k(result==i) = 1; k = imclose(k,strel('square',8));
    closed(k==1) = i;
end

% do output
out = 255 - im;
for x = 1:1002
    for y = 1:800
        if closed(x,y) == 0
            out(x,y,:) = 0;
        end
    end
end
imshow(out);

这里有一些想法。

(1。图像处理:)

1.1将图像加载为RGB像素图。在c#中,使用system.drawing.bitmap是很简单的。在不支持简单成像的语言中,只需将图像转换为可移植像素图格式(PPM)(一种Unix文本表示,产生大文件)或一些容易读取的简单二进制文件格式,如BMP或TGA。Unix中的ImageMagick或Windows中的IrfanView。

1.2如前所述,您可以将数据简化,将每个像素的(R+G+B)/3作为灰度调的指标,然后将其阈值设置为黑白表。假设0=黑,255=白,那么接近200的值就可以去掉JPEG工件。

(2)。解决方案:)

2.1深度优先搜索:初始化一个空堆栈起始位置,收集可用的后续移动,随机选择一个并推入堆栈,直到到达终点或死角。在通过弹出堆栈的死胡同回溯中,你需要跟踪地图上访问的位置,这样当你收集可用的移动时,你就不会重复相同的路径。制作动画很有趣。

2.2广度优先搜索:前面提到过,类似于上面,但只是使用队列。动画也很有趣。这就像图像编辑软件中的洪水填充。我认为你可以用这个技巧在Photoshop中解决一个迷宫。

2.3墙的追随者:从几何上讲,迷宫是一个折叠/弯曲的管子。如果你把手放在墙上,你最终会找到出口。这并不总是有效。有一些假设关于:完美的迷宫,等等,例如,某些迷宫包含岛屿。一定要查一下;这很吸引人。

(3)。评论:)

This is the tricky one. It is easy to solve mazes if represented in some simple array formal with each element being a cell type with north, east, south and west walls and a visited flag field. However given that you are trying to do this given a hand drawn sketch it becomes messy. I honestly think that trying to rationalize the sketch will drive you nuts. This is akin to computer vision problems which are fairly involved. Perhaps going directly onto the image map may be easier yet more wasteful.

这个解决方案是用Python编写的。感谢米哈伊尔对图像准备的指导。

一个动画宽度优先搜索:

完成的迷宫:

#!/usr/bin/env python

import sys

from Queue import Queue
from PIL import Image

start = (400,984)
end = (398,25)

def iswhite(value):
    if value == (255,255,255):
        return True

def getadjacent(n):
    x,y = n
    return [(x-1,y),(x,y-1),(x+1,y),(x,y+1)]

def BFS(start, end, pixels):

    queue = Queue()
    queue.put([start]) # Wrapping the start tuple in a list

    while not queue.empty():

        path = queue.get() 
        pixel = path[-1]

        if pixel == end:
            return path

        for adjacent in getadjacent(pixel):
            x,y = adjacent
            if iswhite(pixels[x,y]):
                pixels[x,y] = (127,127,127) # see note
                new_path = list(path)
                new_path.append(adjacent)
                queue.put(new_path)

    print "Queue has been exhausted. No answer was found."


if __name__ == '__main__':

    # invoke: python mazesolver.py <mazefile> <outputfile>[.jpg|.png|etc.]
    base_img = Image.open(sys.argv[1])
    base_pixels = base_img.load()

    path = BFS(start, end, base_pixels)

    path_img = Image.open(sys.argv[1])
    path_pixels = path_img.load()

    for position in path:
        x,y = position
        path_pixels[x,y] = (255,0,0) # red

    path_img.save(sys.argv[2])

注意:标记白色访问像素灰色。这消除了访问列表的需要,但这需要在绘制路径之前从磁盘上第二次加载图像文件(如果您不想要最终路径和所有路径的合成图像)。

我用过的空白迷宫。

这里有一个解决方案。

Convert image to grayscale (not yet binary), adjusting weights for the colors so that final grayscale image is approximately uniform. You can do it simply by controlling sliders in Photoshop in Image -> Adjustments -> Black & White. Convert image to binary by setting appropriate threshold in Photoshop in Image -> Adjustments -> Threshold. Make sure threshold is selected right. Use the Magic Wand Tool with 0 tolerance, point sample, contiguous, no anti-aliasing. Check that edges at which selection breaks are not false edges introduced by wrong threshold. In fact, all interior points of this maze are accessible from the start. Add artificial borders on the maze to make sure virtual traveler will not walk around it :) Implement breadth-first search (BFS) in your favorite language and run it from the start. I prefer MATLAB for this task. As @Thomas already mentioned, there is no need to mess with regular representation of graphs. You can work with binarized image directly.

下面是BFS的MATLAB代码:

function path = solve_maze(img_file)
  %% Init data
  img = imread(img_file);
  img = rgb2gray(img);
  maze = img > 0;
  start = [985 398];
  finish = [26 399];

  %% Init BFS
  n = numel(maze);
  Q = zeros(n, 2);
  M = zeros([size(maze) 2]);
  front = 0;
  back = 1;

  function push(p, d)
    q = p + d;
    if maze(q(1), q(2)) && M(q(1), q(2), 1) == 0
      front = front + 1;
      Q(front, :) = q;
      M(q(1), q(2), :) = reshape(p, [1 1 2]);
    end
  end

  push(start, [0 0]);

  d = [0 1; 0 -1; 1 0; -1 0];

  %% Run BFS
  while back <= front
    p = Q(back, :);
    back = back + 1;
    for i = 1:4
      push(p, d(i, :));
    end
  end

  %% Extracting path
  path = finish;
  while true
    q = path(end, :);
    p = reshape(M(q(1), q(2), :), 1, 2);
    path(end + 1, :) = p;
    if isequal(p, start) 
      break;
    end
  end
end

它真的非常简单和标准,在Python或其他语言中实现它应该没有困难。

这就是答案:

好的解决方案是,不是通过像素来寻找邻居,而是通过单元格来完成,因为一个走廊可以有15px,所以在同一个走廊中,它可以采取向左或向右的动作,而如果它像一个立方体一样进行位移,它将是一个简单的动作,如向上,向下,向左或向右