2

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

Community
  • 1
  • 1
xunyn
  • 23
  • 4
  • @Cole Do you know how to post the question to these guys – xunyn Nov 25 '13 at 06:50
  • I'm not sure whether you can do that on stack overflow, other than @ mentioning their names – Cole Nov 25 '13 at 07:43
  • make sure all images are same color type (make sure Dimensions@Imagedata is all the same) – agentp Nov 25 '13 at 12:54
  • in fact it looks like your tiff has an alpha channel. try closematch[#[[1;;3]]&, in Map[] – agentp Nov 25 '13 at 13:04
  • @george "same colour type" means black and white ? – xunyn Nov 25 '13 at 15:44
  • I mean they all need the same number of "channels". See my other comment i think your tiff is 4 channel ( 3 colors + opacity). The jpg's are most likely just 3. If you mix b/w (grayscale) and color that would be a problem as well, ( though often grayscale images are really color with all 3 channels equal. ) . The fix in my above comment needs one more `]` in front of the `&` by the way.. – agentp Nov 25 '13 at 23:47
  • You might want to add the `ImageAssemble` part of the code back into your question, in case future seekers wonder why it doesn't work... – cormullion Nov 26 '13 at 16:46
  • @george you point the issue, I save tiff picture without the alpha channel .It can work – xunyn Nov 27 '13 at 01:59
  • @cormullion thanks,have changed – xunyn Nov 27 '13 at 02:04

1 Answers1

1

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

cormullion
  • 1,672
  • 1
  • 10
  • 24