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

匆匆过客 提交于 2019-12-04 07:48:30

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

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

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!