Photo Mosaic in Mathematica: an example from 2008 doesn't work in Mathematica 8
Asked Answered
L

2

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}]
Lycanthropy answered 22/10, 2011 at 15:37 Comment(0)
R
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

Ricks answered 22/10, 2011 at 16:15 Comment(8)
Man, that southern hemisphere... always flipping things aroundOates
@yoda Is it upside-down? I can't make sense of anything in that image!Ricks
@yoda If it IS upside-down remove the Reverse[]Ricks
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.Oates
I saw the Reverse... It's less funny now, because I initially thought you had done it on purpose :)Oates
Did something change between versions, or was it a path problem?Zannini
@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"]Yam
@Heike, IMHO, this should be spelled out in the answer.Zannini
Y
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).

Yam answered 22/10, 2011 at 16:44 Comment(0)
R
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

Ricks answered 22/10, 2011 at 16:15 Comment(8)
Man, that southern hemisphere... always flipping things aroundOates
@yoda Is it upside-down? I can't make sense of anything in that image!Ricks
@yoda If it IS upside-down remove the Reverse[]Ricks
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.Oates
I saw the Reverse... It's less funny now, because I initially thought you had done it on purpose :)Oates
Did something change between versions, or was it a path problem?Zannini
@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"]Yam
@Heike, IMHO, this should be spelled out in the answer.Zannini

© 2022 - 2024 — McMap. All rights reserved.