6

I'm trying to get a Mathematica example working. It's the one on Theo Gray's blog.

I think that Mathematica must have changed since he wrote that code (May 2008), since I'm unable to get anything reasonable out of it, despite changing nearly everything. Do I use ImageData instead of Import? Can anyone suggest a version of this code that works for Mathematica 8?

imagePool = 
 Map[With[{i = Import[#]}, {i, Mean[Flatten[N[i[[1, 1]]], 1]]}] &, 
  FileNames["Pool/*.jpg"]];
closeMatch[c_] := 
  RandomChoice[Take[SortBy[imagePool, Norm[c - #[[2]]] &], 20]][[1]];
Grid[Reverse[
  Map[closeMatch, Import["MendeleevIcon.tif"][[1, 1]], {2}]], 
  Spacings -> {0, 0}]
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
cormullion
  • 1,672
  • 1
  • 10
  • 24

2 Answers2

7

Maybe slightly more streamlined:

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

closeMatch[c_] := RandomChoice[
   Nearest[imagePool[[All, 2]] -> imagePool[[All, 1]], c, 20]]

ImageAssemble[Map[closeMatch, ImageData[Import["mendeleevIcon.tif"]], {2}]]

mosaic

Edit

The reason that the original code stopped working in version 8 is that up until version 6 of Mathematica, Import["file.jpg"] would return a Graphics[Raster[]] object. To extract the image data itself you could simply do Import["file.jpg"][[1,1]]. However, in version 8 (and I suspect version 7) raster images are imported as an Image by default which means that you need ImageData to extract the image data from the imported files. You can still import raster images as a Graphics[Raster[]] by using Import["file.jpg","Graphics"] so the original code should still work if you adapt the Import statements, but the advantage of using Image objects is that you can use functions such as ImageAssemble (plus a whole range of other image processing tools that comes with Mathematica 8).

Heike
  • 24,102
  • 2
  • 31
  • 45
5

The following works (Thanks to @yoda for pointing out the Reverse[] thing in the comments):

f = FileNames["*.jpg", {"c:\\test\\pool\\Pool"}];
m = Import["c:\\test\\pool\\Pool\\MendeleevIcon.tif"];
imagePool =
  Map[
   With[{i = Import[#]},
     {i, Mean[Flatten[ImageData@i, 1]]}] &, f];
closeMatch[c_] := 
  RandomChoice[Take[SortBy[imagePool, Norm[c - #[[2]]] &], 20]][[1]];
Grid[Map[closeMatch, ImageData@m, {2}], Spacings -> {0, 0}]

enter image description here

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • 1
    Man, that southern hemisphere... always flipping things around – abcd Oct 22 '11 at 16:17
  • @yoda Is it upside-down? I can't make sense of anything in that image! – Dr. belisarius Oct 22 '11 at 16:19
  • @yoda If it IS upside-down remove the `Reverse[]` – Dr. belisarius Oct 22 '11 at 16:22
  • Yeah, it's upside down. The two dark patches on either side of the "Fm-Es-Fm---Es` strip in the middle of the lower third are the eyes. Starting with that, if you follow the darker images going upward from the left eye (picture left), it forms the shadow of the nose and eventually diverges in the top third fo become the moustache. – abcd Oct 22 '11 at 16:26
  • 1
    I saw the `Reverse`... It's less funny now, because I initially thought you had done it on purpose :) – abcd Oct 22 '11 at 16:27
  • Did something change between versions, or was it a path problem? – Mr.Wizard Oct 22 '11 at 18:25
  • @Mr.Wizard: I think that what has changed is that in Mathematica 8 (and possibly 7), `Import["file.jpg"]` returns an `Image[]` whereas in older versions it would return a `Graphics[Raster[]]`. You can still import a file as a `Graphics[Raster[]]` with `Import["file.jpg","Graphics"] – Heike Oct 22 '11 at 18:54
  • @Heike, IMHO, this should be spelled out in the answer. – Mr.Wizard Oct 22 '11 at 19:11