我如何使用 Mathematica 找到 Waldo? [英] How do I find Waldo with Mathematica?

查看:26
本文介绍了我如何使用 Mathematica 找到 Waldo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个周末一直困扰着我:解决这些问题的好方法是什么Waldo 在哪里? ['Wally' 在北美以外] 使用 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}]
]

还有一个有效"的 URL 示例:

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

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

(Waldo 在收银台旁边):

(Waldo is by the cash register):

推荐答案

我找到了 Waldo!

I've found Waldo!

我是怎么做到的

首先,我过滤掉所有不是红色的颜色

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];

我使用 Binarize 挑选出图像中具有足够高相关性的像素,并在它们周围绘制白色圆圈以使用 Dilation

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]]

这篇关于我如何使用 Mathematica 找到 Waldo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆