Skip to content

Instantly share code, notes, and snippets.

@cglosser
Created June 1, 2016 18:49
Show Gist options
  • Save cglosser/ec398ede219dec64cdc7cd6525eac505 to your computer and use it in GitHub Desktop.
Save cglosser/ec398ede219dec64cdc7cd6525eac505 to your computer and use it in GitHub Desktop.
(*Grab the image off the web*)
img = Import["http://i.imgur.com/XgrV1va.jpg"];
(*Cut it down to just the maze*)
croppedImg = ImageTrim[
img,
{
{3097.716894977169`, 288.36529680365334`},
{3033.4246575342463`, 1346.2648401826486`},
{432.5114155251141`, 1355.0319634703199`},
{350.68493150684935`, 273.7534246575344`}
}
];
(*Make the maze black & white so it's easier to process*)
bwImg = Binarize[croppedImg];
(*WatershedComponents[] will give the basins of attraction; the maze
solution lies on a boundary between basins, so this picks out that boundary*)
boundary = Image[Clip[1 - WatershedComponents[bwImg], {0, 1}]];
(*Now just draw the path on top of the original image*)
HighlightImage[croppedImg, Dilation[boundary, 5], "HighlightColor" -> Purple]
@straubm3
Copy link

straubm3 commented Jun 1, 2016

Additional Comments: mazes may need borders, and thicker lines. Additional research on how the watershed algorithm actually works to create the boundary between basins would also be helpful. Additional note- tested with a maze that used loops rather than dead ends- disastrous results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment