用Mathematica制作马赛克拼图

xiaoee posted @ 2009年3月05日 11:58 in This is My Life with tags Mathematica 图片 , 4329 阅读

Mathematica真的是很强大的工具,你一定见过用各种图片拼起来的马赛克图片,现在你用Mathematica就能实现了。

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["MasterImage.tif"][[1, 1]], {2}]], Spacings -> {0, 0}]

Pool/*.jpg是你图库里的所有jpg图片,imagePool是载入所有这些图片,然后算出他们的平均RGB值。所以我先把我的图片都用PS做成了25*25,并调成灰度显示。closeMatch会把把你的图片的每一个像素从颜色最接近20张图片中的随机选一个代替。所以我把我的照片裁剪后压成 35*40的。最后Grid会输出我的马赛克拼图了。

 

imagePool =
  Map[With[{i = Import[#]}, {i, Mean[Flatten[N[i[[1, 1]]], 1]]}] &,
   FileNames["F:\\temp\\*.jpg"]];
closeMatch[c_] :=
  RandomChoice[Take[SortBy[imagePool, Norm[c - #[[2]]] &], 20]][[1]];
Grid[Reverse[
  Map[closeMatch, Import["F:\\temp\\temp\\me.tif"][[1, 1]], {2}]],
 Spacings -> {0, 0}]

我只在mathematica6.x上才能成功,上面的代码在mathematica7.x上好像不行。

来源:http://blog.wolfram.com/2008/05/02/making-photo-mosaics/


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter