# How to make a stereogram in Mathematica? (2)

Given a tiled background and a depth map, both of the same dimension, I tried to produce a stereogram using the following codes which was adopted from the method given by Simon Woods, but in a simplified way. However, there are defects in the outcomes. What is wrong in the codes?

depMap = Import["http://i.stack.imgur.com/RvZZT.jpg"];
flower = Import["http://i.stack.imgur.com/9vmBl.jpg"];
{wid, hei} = ImageDimensions[flower]
imData = ConstantArray[{0, 0, 0}, {hei, wid}];
imc = ImageCrop[flower, {32, hei}, Right];
exflower = ImageData[ImageAssemble[{flower, imc}]];
shift = Round[32 ImageData[depMap]] ;

For[r = 1, r <= hei, r++,
For[c = 1, c <= wid, c++,
imData[[r, c]] = exflower[[r, c + shift[[r, c]]]]]];
Image[imData]


Depth map: (800x632) Tiled background: (800x632) Outcome: A pyramid of flowers is expected, but instead there are a triangular stack and a triangular hole.

• I see pyramid on left, pyramid hole on right. I don't see a triangle. Aug 13 '14 at 18:29
• There's a line in Simon Woods's code involving Accumulate which I don't see in your implementation.
– user484
Aug 14 '14 at 4:52
• Yes, the Accumulate seems for the y position shifting, but the outcome was much unexpected when I put it on the shift calculation. Aug 14 '14 at 6:03
• The shift needs to be a relative shift between a part of the image and its corresponding part in the tile to the left. So each vertical strip forms the "baseline" image to which shifts are applied for the next strip. That's why the Accumulate was there. In your code here you are shifting each pixel relative to the background, so for every "hill" in the 3D profile you will get a matching "hole" one tile to the right. Aug 14 '14 at 13:27
• In fact my original code was broken (though I'm not sure exactly what was wrong). The new version is similar in spirit to what you have here - copying pixels in a loop rather than attempting to do it all in one go with Part. Aug 14 '14 at 13:28

Inspired by Simon Woods' last revision of his code, here comes the solutions for my problem now:

depMap = Import["http://i.stack.imgur.com/RvZZT.jpg"];
flower = Import["http://i.stack.imgur.com/9vmBl.jpg"];

{wid, hei} = ImageDimensions[flower]
imc = ImageCrop[flower, {15, hei}, Right];
exflower = ImageData[ImageAssemble[{flower, imc}]];

(*  the dimension of the background pattern tile is 100x100  *)

shift = 50 - Round[15 ImageData[depMap]];

imData = exflower;
For[r = 1, r <= hei, r++,
For[c = 51, c <= wid - 50, c++,
imData[[r, c + shift[[r, c]]  ]] = imData[[r, c - shift[[r, c]]]]]];
Image[imData]


And here comes the perfect pyramid flower stack now:

And the Bunny rabbit hidden in the grass: