Search code examples
image-processingwolfram-mathematica

How do I find Waldo with Mathematica?


This was bugging me over the weekend: What is a good way to solve those Where's Waldo? ['Wally' outside of North America] puzzles, using Mathematica (image-processing and other functionality)?

Here is what I have so far, a function which reduces the visual complexity a little bit by dimming some of the non-red colors:

whereIsWaldo[url_] := Module[{waldo, waldo2, waldoMask},
    waldo = Import[url];
    waldo2 = Image[ImageData[
        waldo] /. {{r_, g_, b_} /;
          Not[r > .7 && g < .3 && b < .3] :> {0, 0,
          0}, {r_, g_, b_} /; (r > .7 && g < .3 && b < .3) :> {1, 1,
          1}}];
    waldoMask = Closing[waldo2, 4];
    ImageCompose[waldo, {waldoMask, .5}]
]

And an example of a URL where this 'works':

whereIsWaldo["http://www.findwaldo.com/fankit/graphics/IntlManOfLiterature/Scenes/DepartmentStore.jpg"]

(Waldo is by the cash register):

Original image

Mathematica graphic


Solution

  • I've found Waldo!

    waldo had been found

    How I've done it

    First, I'm filtering out all colours that aren't red

    waldo = Import["http://www.findwaldo.com/fankit/graphics/IntlManOfLiterature/Scenes/DepartmentStore.jpg"];
    red = Fold[ImageSubtract, #[[1]], Rest[#]] &@ColorSeparate[waldo];
    

    Next, I'm calculating the correlation of this image with a simple black and white pattern to find the red and white transitions in the shirt.

    corr = ImageCorrelate[red, 
       Image@Join[ConstantArray[1, {2, 4}], ConstantArray[0, {2, 4}]], 
       NormalizedSquaredEuclideanDistance];
    

    I use Binarize to pick out the pixels in the image with a sufficiently high correlation and draw white circle around them to emphasize them using Dilation

    pos = Dilation[ColorNegate[Binarize[corr, .12]], DiskMatrix[30]];
    

    I had to play around a little with the level. If the level is too high, too many false positives are picked out.

    Finally I'm combining this result with the original image to get the result above

    found = ImageMultiply[waldo, ImageAdd[ColorConvert[pos, "GrayLevel"], .5]]