Search code examples
wolfram-mathematicamosaic

Photo Mosaic in Mathematica: an example from 2008 doesn't work in Mathematica 9


I'm trying to get a Mathematica example working. It's the one on Theo Gray's blog. In Mathematica 9.0 It doesn't work. I already have search the answer on stackoverflow in mathematica 8.0 . I use the code that heike gave.

imagePool =Map[With[{i = Import[#]}, {i, N@Mean[Flatten[ImageData[i], 1]]}] &,FileNames["/Users/xunyanan/Desktop/webwx_img/*.jpg"]];
closeMatch[c_] :=RandomChoice[Nearest[imagePool[[All, 2]] -> imagePool[[All, 1]], c, 20]]
ImageAssemble[Map[closeMatch, ImageData[Import["/Users/xunyanan/Desktop/me.tif"]], {2}]]

I think it almost run successfully。 The response screenshot: out content

when I clicked “Show Full Output”. I would get the result as below or Mathematica 9.0 exit off-normal The screenshot:

enter image description here

I use Mathematica 9.0 right now, have not the experience.so Can anyone suggest a version of this code that works for Mathematica 9? I am appreciated that you can give me some suggest.

Thanks you guys to edit this question. My PC ENV : mac OS X version 10.9 and Mathematica 9.0


Solution

  • As the comments note, your problem is because the images you're using for the imagePool are not all the same number of channels, and that's upsetting the Nearest function. Probably the easy way to fix this is:

    imagePool = Map[With[{i = Import[#]}, {i, 
      N@Mean[Flatten[ImageData[RemoveAlphaChannel[i]], 1]]}] &, 
        FileNames["*.png", "/tmp"]]
    

    i.e. to apply RemoveAlphaChannel when you import the images. It would be sensible to apply the same precaution to your source image as well.

    Spot the difference:

    Before (without RemoveAlphaChannel):

    before

    After:

    after