title: Representing and solving a maze given an image
body: <p>What is the best way to represent and solve a maze given an image?</p>
  
  <p><img src="https://i.stack.imgur.com/TqKCM.jpg" alt="The cover image of The Scope Issue 134"></p>
  
  <p>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: <code>True</code> for a white pixel, and <code>False</code> 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.</p>
  
  <p>Another method (which came to me after a bit of thought) is to convert the image to an SVG file - which is a list of paths drawn on a canvas. This way, the paths could be read into the same sort of list (boolean values) where <code>True</code> indicates a path or wall, <code>False</code> indicating a travel-able space. An issue with this method arises if the conversion is not 100% accurate, and does not fully connect all of the walls, creating gaps.</p>
  
  <p>Also an issue with converting to SVG is that the lines are not "perfectly" straight. This results in the paths being cubic bezier curves. With a list (array) of boolean values indexed by integers, the curves would not transfer easily, and all the points that line on the curve would have to be calculated, but won't exactly match to list indices.</p>
  
  <p>I assume that while one of these methods may work (though probably not) that they are woefully inefficient given such a large image, and that there exists a better way. How is this best (most efficiently and/or with the least complexity) done? Is there even a best way?</p>
  
  <p>Then comes the solving of the maze. If I use either of the first two methods, I will essentially end up with a matrix. According to <a href="https://stackoverflow.com/a/3097677/1267663">this answer</a>, a good way to represent a maze is using a tree, and a good way to solve it is using the <a href="http://en.wikipedia.org/wiki/A%2a_search_algorithm" rel="noreferrer">A* algorithm</a>. How would one create a tree from the image? Any ideas?</p>
  
  <p><strong>TL;DR</strong><br>
  Best way to parse? Into what data structure? How would said structure help/hinder solving?</p>
  
  <p><strong>UPDATE</strong><br>
  I've tried my hand at implementing what @Mikhail has written in Python, using <code>numpy</code>, as @Thomas recommended. I feel that the algorithm is correct, but it's not working as hoped. (Code below.) The PNG library is <a href="https://raw.github.com/drj11/pypng/master/code/png.py" rel="noreferrer">PyPNG</a>.</p>
  
  <pre><code>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] &gt; 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, [])
  </code></pre>