How to Create & View 3D Graphs in MatLab (with 3D glasses)

Red Blue 3D Glasses

MatLab already has the ability to produce three-dimensional (3D) visualisations of array data such as ‘surf’ or ‘mesh’, but these still produce flat two-dimensional figures. Sometimes the best way to interpret these data is in genuine 3D with depth. I needed this capability for my own research and finding no clues online I’ve written some code myself.

This post will show you how to create your own figures in 3D (sometimes called anaglyphs) that can be saved, shared and viewed with 3D glasses. For this post I’ll be using red/blue 3D glasses (with red over the left eye) and the MatLab ‘peaks’ example data set.

As this is genuine 3D, and not just a 2D figure with perspective, the first stage is to generate two separate figures – one for each eye. There needs to be a small angle that is the difference in viewing direction for each eye, called the parallax angle. You may wish to tune this so that viewing is most comfortable for you, but I find 0.5° either side of centre works well for me. Also the point about which the figure rotates will determine where on the final 3D figure will be the ‘origin’ of the 3D perspective. Again I’ve found the best results with this as the back corner of my figures but you may wish to choose another point. Importantly the figures should be exactly the same dimensions (whether specified or left on default). Finally the shading of the 3D surface needs to be grey. If coloured shading is used it will cause problems with the red-blue glasses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 [X,Y,Z] = peaks(50) ;       % Create some dummy data.
 parallaxAngle = 0.5 ;       % You can change this to suit yourself.
 cameraAnchor = [  max(X(:))  max(Y(:))  min(Z(:)) ] ;
 
 leftImage = figure;
 surf(X,Y,Z,'FaceColor',[0.8 0.8 0.8]) % Create the surface figure.
 axis vis3d ;                   % Lock the aspect ration for rotation.
 camtarget(gca,[cameraAnchor])  % Point the camera at the anchor point.
 view((-45 - parallaxAngle),30) ; % Rotate the figre to the correct angle.
 title  ('Plot Title', 'FontWeight','Bold','FontSize',11) % Add labels...
 xlabel ('X Axis Label', 'FontWeight','Bold','FontSize',11)
 ylabel ('Y Axis Label', 'FontWeight','Bold','FontSize',11)
 zlabel ('Z Axis Label', 'FontWeight','Bold','FontSize',11)
 grid on ;                      % Switch on the figure grid, and
 set(gca,'GridLineStyle','-')   % set the grid to solid lines.

That completes the first figure. Next the same stages are repeated but with a new figure for the right eye. Note that the amount of parallax angle is applied in the opposite direction.

16
17
18
19
20
21
22
23
24
25
26
 rightImage = figure;
 surf(X,Y,Z,'FaceColor',[0.8 0.8 0.8]) % Create the surface figure.
 axis vis3d ;                   % Lock the aspect ration for rotation.
 camtarget(gca,[cameraAnchor])  % Point the camera at the anchor point.
 view((-45 + parallaxAngle),30) ; % Rotate the figre to the correct angle.
 title  ('Plot Title', 'FontWeight','Bold','FontSize',11) % Add labels...
 xlabel ('X Axis Label', 'FontWeight','Bold','FontSize',11)
 ylabel ('Y Axis Label', 'FontWeight','Bold','FontSize',11)
 zlabel ('Z Axis Label', 'FontWeight','Bold','FontSize',11)
 grid on ;                      % Switch on the figure grid, and
 set(gca,'GridLineStyle','-')   % set the grid to solid lines.

Now you should have two figures that look something like this. They will look pretty similar but are slightly different (I promise).

3D Figures

Next the two figures should be saved, giving them intuitive names something like this:

27
28
 print(leftImage ,'-dtiffn','-painters', 'Left_Eye.tif')
 print(rightImage,'-dtiffn','-painters','Right_Eye.tif')

Now we have the two separate images for each eye they simply need to be combined into a single anaglyph. To do this load the two images we just made into the workspace. You will see that they are m x n x 3 arrays. These correspond to the red, green and blue colour data that make up the images. For the 3D glasses I’m using in this example (red filter over the left eye) our left-eye image should be coloured red and vica-versa. We achieve this by striping out (setting to zero) the other colours we don’t want. After this the combined image is simply the sum of the two keeping the same dimensions as an individual image. You can now view the image and save it in your desired format.

29
30
31
32
33
34
35
36
 leftEyeImage  = imread('Left_Eye.tif')  ; % Load the left eye image.
 rightEyeImage = imread('Right_Eye.tif') ; % Load the right eye image.
 
 leftEyeImage(:,:,2:3) = 0 ;               % Removes green and blue from the left eye image.
 rightEyeImage(:,:,1)  = 0 ;               % Removes red from the right eye image.
 anaglyph = leftEyeImage + rightEyeImage ; % Combines the two to produce the finished anaglyph.
 imshow(anaglyph,'border','tight') ;       % Show the anaglyph image with no padding.
 print(gcf,'-dtiffn','-painters','Anaglyph.tif')  % Save the anaglyph image.

You should now have your finished image ready for viewing in 3D.

Anaglyph

16 thoughts on “How to Create & View 3D Graphs in MatLab (with 3D glasses)

  1. Aditya

    Thanks for the great post! What changes would I have to make for colored images instead of greyscale?

  2. Suhas

    Awesome! Awesome! Awesome!

    …just what I need and was looking for.

    Thank you so very much.

  3. Suhas

    Thanks Lewys… repeating Dimitri

    Awesome!

    …just what I need and was looking for.

    Thank you so very much.

  4. Jerry

    Hi

    how can you do this but using oculus rift? i have been looking for info but unable to find any.

    Thanks

    1. Lewys Jones Post author

      I’m afraid I don’t know much about how the Oculus Rift works, but in principal all you need to do is present the two different images one to each eye. Good luck, Lewys

  5. farah

    hi,i want some one to help me with my project about the creation of 3d graphs by using matlab

    1. Lewys Jones Post author

      Hi Farah, I hope this post was helpful and can be useful for your project.

  6. dimitri

    Awesome! Awesome! Awesome!

    …just what I need and was looking for.

    Thank you so very much.

  7. Jean-Louis Soulié

    ( I am French , excuse my english )
    Hi Jones,
    and thanks .
    I want to make 4D anaglyph graphics.
    I want to add the time “t”.
    I want to accomplish a series of cycles at a speed of movie.
    I want to display a timeline ;
    while adjusting the speed, with the possibility of still picture.
    How can i do ?
    best regards

    1. Lewys Jones Post author

      Dear Jean-Louis, you can achieve what you want by wrapping up my method in a loop where the time-step data is created one by one. Save the anaglyph for each time, and then rebuild these into a movie. You will want to make sure that the x,y,z limits of the axes do not change so that your movie comes out smooth. Good luck!

    1. Lewys Jones Post author

      Hi Marimuthu, can you explain more what you want to achieve? This example shows how to create an red-blue anaglyph from 3D data. It is not possible to ‘create’ a third dimension if your data doesn’t have it already.

    1. Lewys Jones Post author

      Hi Arvind, this method is for creating a red-blue anaglyph from a 3D plot and will not be able to create 3D information from a 2D JPEG.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>