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 :-)

Wheatstone Bridge In MATLAB Simulink

The diagram below describes a Wheatstone bridge designed with MATLAB Simulink, The resistor designated [R x] can be replaced with a variable resistance material and used in the construction of a strain gauge.

Wheatstone bridge is an electrical circuit used to measure an unknown electrical resistance by balancing two legs of a bridge circuit, one leg of which includes the unknown component. Its operation is similar to the original potentiometer. It was invented by Samuel Hunter Christie in 1833 and improved and popularized by Sir Charles Wheatstone in 1843. One of the Wheatstone bridge’s initial uses was for the purpose of soils analysis and comparison read more

Simulink Circuit Schematic:
wheatstone bridge

DC Voltage = 100 v
R 1 = 10 ohm, R 2 = 10 ohm, R 3 = 10 ohm, R x = 5 ohm
Connect the components as seen in the diagram then run the simulation, let [R x] be the variable resistance material; vary the resistance to see the difference in current.

NB: Resistors were taken from the  powerlib Simulink components [Parallel RLC Branch]

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

In this the second part of the crossword puzzle solver series, we will look at writing to the video input stream. The image acquisition toolbox allows the programmer to configure a function called the UpdatePreviewWindowFcn . Once configured, we can use this function along with others from the vision toolbox like vision.TextInserter and vision.ShapeInserter to add artifacts to the video stream before calling the preview function.

Lets see some code:

vid = videoinput('winvideo', 1, 'YUY2_1280x720');
src = getselectedsource(vid);

vid.FramesPerTrigger = 1;
vid.ReturnedColorspace = 'rgb';

vidRes   = get(vid, 'VideoResolution');
imWidth  = vidRes(1);
imHeight = vidRes(2);
nBands   = get(vid, 'NumberOfBands');
hImage   = image(zeros(imHeight, imWidth, nBands));

setappdata(hImage,'UpdatePreviewWindowFcn',@mypreview_fcn); % configure update udf.

preview(vid, hImage) % display updated stream.

Update Preview UDF code:

function mypreview_fcn(obj,event,himage)
    % display image data.
    tx = vision.TextInserter('Hello World!');
    tx.FontSize = 40;

    set(himage, 'cdata', step(tx, event.Data))
end

Result: See Hello World! printed in the top left hand corner of the stream.

This functionality will be very important when we get to actually superimposing the solution to the crossword puzzle on the video output stream.

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

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

Note:
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());

wr.write(data);
rd   = BufferedReader(InputStreamReader(con.getInputStream()));

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

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.

matlab

Designing A Custom Neural Network In MATLAB

The MATLAB Neural Network toolbox ships with numerous predefined and canonical neural nets, however sometimes you may need to create a custom net with just the right connections, biases and hidden layers to suite your particular problem domain. To achieve this goal we can use the matlab network object. The  network object allows granular design of neural networks by exposing all properties of the net that we are designing. The preceding code demonstrates how to build a simple neural to learn the truth table for Logical AND.

First lets look at the Logical AND truth table:

p q p ∧ q
T T T
T F F
F T F
F F F

Open a new edit window in MATLAB and enter the following code:

    1. This creates an empty network object and assigns it to the net variable, sets up the number of inputs and uses cell array syntax to index into its properties.
      %% Design Custom Neural Network
      net = network;                                  % create network
      net.numInputs = 2;                              % set number of inputs
      net.inputs{1}.size = 1;                         % assign 2 to input size
      net.inputs{2}.size = 1;
      net.numLayers = 1;                              % add 1 layer to network
      net.layers{1}.size = 1;                         % assign number of neurons in layer
      net.inputConnect(1) = 1;                        % connet input to layer 1
      net.inputConnect(2) = 1;
      net.biasConnect(1) = 1;                         % connect bias to layer 1
      net.biases{1}.learnFcn = 'learnp';              % set bias learning function
      net.biases{1}.initFcn = 'initzero';             % set bias init function
      
      net.outputConnect(1) = 1;
      
      net.layers{1}.transferFcn = 'hardlim';          % set layer transfer function [hard limit]
      net.inputWeights{1}.initFcn = 'initzero';       % set input wieghts init function
      net.inputWeights{1}.learnFcn = 'learnp';        % set input weight learning function
      net.inputWeights{2}.learnFcn = 'learnp';
      net.inputWeights{2}.initFcn = 'initzero';
      
      net.initFcn = 'initlay';                        % set network init function
      net.trainFcn = 'trainc';                        % set network training function
      net.performFcn = 'mae';                         % set network perf evaluation function
      
      view(net)
      net = train(net,[0 0 1 1;0 1 0 1],[0 0 0 1]) ;  % train network
      
    2. Custom Network Diagram:
    3. Test Network
      In the command window type
      net([1;1])
      

      This should output a 1 to the command window indicating 1 AND 1 = 1

vision

Image Classification Using MATLAB SOM/LVQ

I like to think of myself as a hacker :-), not in today’s sense of the word [person who breaks into secured computer areas] but as a hacker in the sense of first two definitions found here. I like to experiment with things especially related to computers and Artificial Intelligence in particular. MATLAB happens to be a virtual toolbox for the trade pun intended  :-), using some of its toolboxes we will see how we can solve a ubiquitous problem that faces most persons with a nice camera and a voracious appetite for taking pictures.

Now, I don’t have an expensive Nikon, but I do have loads of pictures; and one day i was trying to find this particular picture when it occurred to me that if i could not remember the name or date when i took the picture  it would require me to search blindly every picture I had in order to find that special one. Now what if i had some way of finding the picture based on what i could remember of it? ie. environment in which it was taken, colours and objects along with some other visual specifications, wouldn’t that be cool.

So I went for the proverbial toolbox MATLAB, which tools will I need?

  1. Neural Network    -> selforgmap, lvqnet, vec2ind, ind2vec
  2. Image Processing -> imhist, imresize, rgb2gray, imread

Other:

  1. mspaint

Note: For this demonstration I will be using:

  • MATLAB R2011b on Windows 7
  • Pictures found at  C:\Users\Public\Pictures\Sample Pictures

Ok lets do it,  start up MATLAB, copy and paste all the pics from the above directory to MATLAB’s current directory. {Chrysanthemum, Desert, Hydrangeas, Jellyfish, Koala, Lighthouse, Penguins, Tulips}.

  1. In the new edit window copy and paste the code as given below. Save file as scan.m
    function scan(img)
    files = dir('*.jpg');
    hist = [];
    for n = 1 : length(files)
        filename = files(n).name;
        file = imread(filename);
    
        hist = [hist, imhist(rgb2gray(imresize(file,[ 50 50])))]; %#ok
    end
    
    som = selforgmap([10 10]);
    som = train(som, hist);
    t   = som(hist); %extract class data
    
    net = lvqnet(10);
    net = train(net, hist, t);
    
    like(img, hist, files, net)
    end
                   

    Links to the functions used were provided above therefore i will not be going into the details of how they work, however there is a narrative with regard to the workings of the code:

    The code starts by searching the current MATLAB directory for all files with a .jpg extension. On each iteration of the loop an image is loaded and resized to 50 x 50, it is then converted to greyscale and a histogram measurement is taken of its pixels [feature vector]; the results are then appended to a 256 x n matrix with n been the number of images scanned.

    A self organizing map network is then used to identify classes into which the images fall. The feature matrix and class data is used to train a Learning Vector Quantization neural network, that will be used for classification of images presented to it.

  2. Next we will create a function to display all matching images for an image we submit to the LVQ network.
    function like(im, hist, files , net)
        hs = imhist(rgb2gray(imresize(im,[50 50])));
        cls = vec2ind(net(hs));
    
        [~, n] = size(hist);
        for i = 1 : n
            if(cls == vec2ind(net(hist(:, i))))
                figure('name', files(i).name);
                imshow(imresize(imread(files(i).name), [100 100]))
            end
        end
    end
    
  3. Download a picture of a koala and save it outside your MATLAB path as koalatest.jpg
  4. At the MATLAB command prompt type scan(imread(‘[replace with path to koalatest]\koalatest.jpg’)
  5. After a minute or two the networks should have been trained and a figure displaying the matching koala.jpg image shown to you.

NOTE: As explained above this is hacking, not production code I wrote this up in about 20 minutes as a demonstration for classification of images, with imagination this could be extended to classify things like sound for example using a feature map crated from humming a tune to find a song with a similar melody.

LVQ Been trained:

How To Call Functions In a MATLAB Generated .NET DLL From C#

This Tutorial does not represent a practical use of  a MATLAB generated .NET DLL since it requires the installation and use of MATLAB and the MATLAB Compiler Runtime (MCR) for a scenario that could be easily handled by a more elegant algorithm such as the one discussed in my earlier post on RPN expression parsing. However, it does seek to demonstrate the principles behind developing an algorithm in MATLAB which could be then exported to a .NET component and reused in any managed language. MATLAB provides a fast prototyping environment which gives the programmer a vast array of tools which she can then use to design and test her algorithms. A subset of these functionalities are provided in the MCR which is basically an installable, free, headless version of MATLAB. Since the MCR is a MATLAB instance it requires the same amount of time for initial loading into memory and will also be as memory intensive as its GUI cousin.

That said,  MATLAB allows the packaging of its code into an encrypted Dll file which can then be executed by the MCR. In addition to .NET, MATLAB code can also be exported to C libraries , Java libraries or an executable file.

The first thing we will do is fire up MATLAB, we will access its computational engine through the use of  of  the eval  function which will return the result of a mathematical expression passed to it as a string.  There are the steps.

  1. Launch MATLAB
  2. Type edit in the MATLAB command window then press enter/return. 
    edit
  3. In the new window create the function as seen below then hit the save button.
    function result = calculator(input)
    result = eval(input);
    end
    
  4. Click File->New->Deployment Project then type calculator in the name box.
  5. Select a location to save the project and .NET Assembly from the type drop down box.
  6. In the .NET Assembly window, under the build tab change the default name of the class to “demo” then drag and drop the calculator.m file unto the class to add it as a method of the class, then hit the compile button. This should create the calculator.dll which can be referenced from your c# application.
  7. Fire up Visual Studio and create a new C# Windows Forms Application, to this application add a reference to the calculator.dll file, you will also need to add a reference to the MWArray.Dll which is located at “C:\Program Files\MATLAB\MATLAB Compiler Runtime\v716\toolbox\dotnetbuilder\bin\win64\v2.0\MWArray.dll” (version dependent path) in order to facilitate the conversion of .NET types to MATLAB types.
  8. GUI for expression parser application.
  9. C# Application code.
    using System;
    using System.Windows.Forms;
    using MathWorks.MATLAB.NET.Arrays;
    using calculator;
    
    namespace DemoCalculator
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var calc= new demo();            
                MessageBox.Show(calc.calculator((MWCharArray)textBox1.Text)[1].ToString());
            }
            
        }
    }
    
  10. Run the application then enter a mathematical expression and hit the Calculate button, this will send the string to be interpreted by the MCR and return a result to the user, the end.