Mapping A Maze Using MATLAB Image Processing Toolbox

Posted on Updated on

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:

The code:

%% Map Maze
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'))

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


solved maze

The end 🙂

Solving Crossword Puzzles Using Image Acquisition And Data Scraping (Part 1)

Posted on

Crossword puzzles are a very popular pastime in most parts of the western world, and provide a quick means of escape for those on the commute or on break from regular work. The solution to the more interesting puzzles require a fairly good vocabulary and the ability to spot patterns and word arrangements. I recently saw an article on using MATLAB to solve a Sudoku puzzle using image acquisition toolbox and a Sudoku solver algorithm. This series of articles will take a slightly different approach, we will still use the image acquisition toolbox; however, instead of an algorithm to solve the puzzle we will use data scraping form a puzzle website to get the most suitable word for a particular place in the crossword.

The website we will be using to get solutions is  One Across

MATLAB is built on the java platform and is able to execute java code written at the command line or inserted in .m files. This feature will be exploited in future articles for doing tasks more suitable to the java toolset.

Example Java and MATLAB code in one .m file

import java.net.*;
import java.io.*;

data = URLEncoder.encode('http://www.oneacross.com/cgi-bin/search_banner.cgi?c0=&p0=r%3Foogle&s=+Go%21+');
url  = URL('http://www.oneacross.com/');
con  = url.openConnection(); con.setDoOutput(true);
wr   = OutputStreamWriter(con.getOutputStream());

rd   = BufferedReader(InputStreamReader(con.getInputStream()));

while rd.readLine() ~= '' %MATLAB CODE
    line = ans;

In part two of this series we will look at using image acquisition to capture a live video feed for the puzzle we want to solve and overlaying the possible solution unto the video feed, stay tuned.