Mapping A Maze Using MATLAB Image Processing Toolbox

Recently I saw an article about mapping a maze and I thought to myself; how could this be achieved using my favorite tool MATLAB :-)? Well it turns out that its a pretty simple task. Some functions used in this post require that you have MATLAB 2012a or higher installed, so if u do not then go away and get it.

Wow you are back, ok so lets begin, first we will look at the code that was used and then further down we will look at individual functions and what they were used to do.

The maze:
maze

The code:

%% Map Maze
clear
im = imread('maze.png');
bw = im2bw(im(1:287, 1:400), 0.45);
cc = bwconncomp(bw, 8);
obj = false(size(bw));
obj(cc.PixelIdxList{13}) = true;
sln = bwmorph(bwmorph(obj,'thin',Inf),'spur', Inf);
figure, imshow(imfuse(im,sln,'blend','Scaling','joint'))

Explanation:
clear Clears all variables from the MATLAB workspace.
im = imread(‘maze.png’);
Reads the maze image and stores it.
bw = im2bw(im(1:287, 1:400), 0.45); Converts the image to binary, scales it and stores it.
cc = bwconncomp(bw, 8); Find connecting components within the image.
obj(cc.PixelIdxList{13}) = true; Find the largest component and select it.
sln = bwmorph(bwmorph(obj,’thin’,Inf),’spur’,Inf); Use Morphological functions to refine image.
figure, imshow(imfuse(im,sln,’blend’,’Scaling’,’joint’)); Overlay and display images

Result:

solved maze

The end :-)

About these ads

3 thoughts on “Mapping A Maze Using MATLAB Image Processing Toolbox

  1. Pingback: GlobeGalaxy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s