Is it possible to produce anaglyphs with Mathematica?

I'd like to prepare some presentations in Mathematica to help students visualize functions of two variables (it's a usual calculus course). I thought it would be both cool and useful to have the graphs as red/cyan anaglyphs. Is it possible to do that, and if yes, how?

Edit: Simon Woods' answer below is great, but it produces a static image. I'd prefer an interactive version (rotatable - is it a word? - with a mouse); if this is not possible, then I'd like to have at least an animation. (I guess the latter shouldn't be too hard - I'd only have to put suitable commands in some loop, export the images and mount them as an animation; the point is, I'm a Mathemathica newbie and don't know (yet) how to do it - but I can probably figure that out on my own.)

• by anaglyph, do you mean this?
– acl
Aug 11, 2012 at 19:52
• Yes, I'll clarify that in the question. Aug 11, 2012 at 19:54
• Check out the StereoImagery package by Mark Fisher: markfisher.net/~mefisher/mma/mathematica.html and a nice example by @Vitaly vimeo.com/15262935 Aug 11, 2012 at 20:07

I think the basic idea is to create two slightly different views and combine them in the red and (green + blue) channels.

p = Plot3D[Sin[x y]^2, {x, -2, 2}, {y, -2, 2}];

{r, g} = ColorConvert[
Image[Show[p, ViewPoint -> {3 Sin[#], 3 Cos[#], 2} &[# Degree]],
ImageSize -> {360, 275}], "Grayscale"] & /@ {141, 139};

ColorCombine[{r, g, g}]


A simple way to animate is just to change the ViewPoint in a loop and Export the individual frames. I use some software called VirtualDub to combine the images into a movie or animated gif:

Do[{r, g} = ColorConvert[
Image[Show[p, SphericalRegion -> True,
ViewPoint -> {3 Sin[#], 3 Cos[#], 2} &[# Degree]],
ImageSize -> {360, 275}], "Grayscale"] & /@ {2 a + 1, 2 a - 1};
Export["frame" <> ToString[a] <> ".bmp", ColorCombine[{r, g, g}]]
, {a, 0, 44}]


• It works for me, but I think you should remove the tick labels as they are vertically displaced somewhat. Aug 11, 2012 at 20:06
• Also, shouldn't you keep the ViewCenter the same in both pictures? Aug 11, 2012 at 20:09
• Thanks, this is very nice. However, it is "just an image", I cannot e.g. rotate it interactively. I'll modify the question once again;). Aug 11, 2012 at 20:30
• @SjoerdC.deVries, the ViewCenter is the same in both images I think. Aug 11, 2012 at 21:22
• @mbork, interactive rotation is probably possible, but I don't know how to do it! Aug 11, 2012 at 21:26

The idea of interactive rotation of the anaglyph has caught my attention. I propose the following:

Manipulate[
ColorCombine@
Flatten@(ColorSeparate[
Image[Show[pl, ViewPoint -> {2 Sin[(\[Alpha] + #[[1]]) Degree],
2 Cos[(\[Alpha] + #[[1]]) Degree],
3 Cos[\[Beta] Degree]}],
ImageSize -> {360, 275}]][[#[[2]]]] & /@ {{2, 1}, {0,
2 ;; 3}}), {{\[Alpha], 45}, -90, 90}, {{\[Beta], 60}, 0, 180},
ContinuousAction -> True,
Initialization :> (pl =
Plot3D[Sin[(x y)]^2, {x, -2, 2}, {y, -2, 2}, Boxed -> False,
Axes -> False, SphericalRegion -> True, PlotRange -> All,
ColorFunction -> "GrayTones", ColorFunctionScaling -> True])]


and my result is the following:

The main problem with anaglyphs are the combination of colors in the image. Try to avoid many explicit reds and blues, or your pseudo-stereo image (plot) will suffer from ghost parts. I recommend a color scheme based on grey tones.

• Edit update

I have edited the code to be a bit more faster.

• For generality it'd be better to use ImageSize -> ImageDimensions[pl], so that one could generate pl with its size and aspect ratio, and then just display in stereo here in the same size. Jul 13, 2022 at 16:58