3

Problem : The image loses its transparency when plot using surf

I have already figured out how to create a png file with a transparent background as described in numerous other threads.here

However, when plot using surf.m, the image is not transparent

Here is the code that I have so far:

img = imread('image.png');

A1 = ones(size(img));A2 = ones(size(img));A3 = ones(size(img));
A1(img(:,:,1)==0)=0;A2(img(:,:,2)==0)=0;A3(img(:,:,3)==0)=0;

A = A1+A2+A3;
A= A(:,:,1);

imwrite(img,'test.png','alpha',A);
[img,map,alpha] = imread('test.png');

ximage = [-.5,.5;-.5,.5];
yimage = [0,0;0,0];
zimage = [.5,.5;-.5,-.5];

surf(ximage,yimage,zimage,'Cdata',img,'Facecolor','texturemap','Edgecolor','none','alphadata',alpha);
axis vis3d

The code converts image.png (blue square) to a test.png with a transparent background (get rid of the black background). test.png is then used to generate a surf plot which turns out to be not transparent. Any idea what I am doing wrong?

blue square with black background - need to plot this as a 3D slice without the black background

Community
  • 1
  • 1
hkf
  • 663
  • 2
  • 11
  • 23

1 Answers1

3

There is a function called alpha in MATLAB which sets transparency for the objects in current axes. I suggest to change the variable named alpha to another name by replacing the following line

[img,map,alpha] = imread('test.png');

to

[img,map,alphaChannel] = imread('test.png');

Now, after running surf, you can set the transparency for your plot through alpha function.

Using alpha function

surf(ximage,yimage,zimage,'Cdata',img,'Facecolor','texturemap','Edgecolor', ...
                          'none','alphadata',alpha);
alpha(0.5); %# line added
axis vis3d

Using surf function

If you want to set the transparency through the surf function, you need to add 'FaceAlpha' parameter:

surf(ximage,yimage,zimage,'Cdata',img,'Facecolor','texturemap','Edgecolor', ... 
                          'none','AlphaData',alphaChannel,'FaceAlpha',0.5);

Result

Image obtained through through any example above

More information about alpha and surf functions.


Setting a matrix transparency

But those functions above sets the transparency for the entire plot. If you want to set your original matrix of transparency, you need to pass 'FaceAlpha','texture' parameter to surf:

handler = surf( ximage , yimage , zimage , 'Cdata', img , ...
    'FaceColor','texturemap',                             ...
    'EdgeColor','none',                                   ... 
    'FaceAlpha','texture',                                ...
    'AlphaData', alphaChannel);
axis vis3d

Result

Image obtained by setting 'FaceAlpha'

More details. Hope it helps!

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
  • 1
    Thanks for the response Victor Hugo. However, the result I want is such that the transparency is set to 0 in the black border and 1 everywhere else (blue box). Alpha and/or Facealpha set the transparency of the entire matrix to the same value. I want to somehow tell surf to use the alpha matrix and not just a scalar value. Hope that makes sense. – hkf Aug 16 '12 at 19:14
  • So I am trying to use this technique for placing an image on an actual surface plot with elevation data similar to this: http://stackoverflow.com/questions/3719502/how-can-i-plot-an-image-jpg-in-matlab-in-both-2-d-and-3-d How do I prevent the elevation data from being affected? – user1854628 Oct 29 '13 at 20:36
  • @user1854628 what is the "elevation data" in the example you sent me? Is it the sphere around the image? – Yamaneko Oct 29 '13 at 21:39