2
$\begingroup$

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) enter image description here Tiled background: (800x632) enter image description here Outcome: A pyramid of flowers is expected, but instead there are a triangular stack and a triangular hole. enter image description here

$\endgroup$
6
  • $\begingroup$ I see pyramid on left, pyramid hole on right. I don't see a triangle. $\endgroup$
    – Fred Kline
    Aug 13 '14 at 18:29
  • $\begingroup$ There's a line in Simon Woods's code involving Accumulate which I don't see in your implementation. $\endgroup$
    – user484
    Aug 14 '14 at 4:52
  • $\begingroup$ Yes, the Accumulate seems for the y position shifting, but the outcome was much unexpected when I put it on the shift calculation. $\endgroup$
    – Putterboy
    Aug 14 '14 at 6:03
  • $\begingroup$ 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. $\endgroup$ Aug 14 '14 at 13:27
  • $\begingroup$ 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. $\endgroup$ Aug 14 '14 at 13:28
2
$\begingroup$

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: enter image description here

And the Bunny rabbit hidden in the grass: enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.