image AlphaData chops off some borders

3 views (last 30 days)
Ahmad
Ahmad on 23 Apr 2013
I have two images: im2 needs to be over-layed over im1 with a constant alpha map of 0.5. Whenever I set the AlphaData for im2 (to reveal im1 below), the size of the figure/axes changes slightly. If my axes occupies the whole figure, some border pixels go missing. The following example demonstrates this problem by two example images, where im2 has a yellow border so you can clearly notice this problem:
close all;
frame_sz = [106 777];
% create a white im1
im1 = ones([frame_sz 3]);
% create im2 with a yellow border
im2 = zeros([frame_sz 3]);
im2(:,1,[1 2]) = 1;
im2(:,end,[1 2]) = 1;
im2(1,:,[1 2]) = 1;
im2(end,:,[1 2]) = 1;
% create the figure at a specific location with size equal to image
screen_sz = get(0,'ScreenSize');
fg_h = figure('units', 'pixels', 'position', ...
[screen_sz([3 4])-frame_sz([2 1])-40 frame_sz([2 1])], ...
'paperpositionmode', 'auto');
ax_h = axes('Parent',fg_h);
% show first image
imshow(im1, 'Parent',ax_h, 'InitialMagnification','fit');
hold on;
% display im2 ontop of im1
h = image(im2, 'Parent',ax_h);
% set the axis such that image occupies the whole figure
set(ax_h, 'Units','normalized', ...
'position', [0 0 1 1], 'visible', 'off');
axis off;
%%%%%%%%%%%%PROBLEM HAPPENS HERE %%%%%%%%%%%%%
% as soon as I set the AlphaData couple of pixels
% go missing from the right and bottom side
set(h, 'AlphaData', ones(frame_sz([1 2]))*0.5);
The problem occurs regardless of if I am overlaying using image() or just simply using image() to display an image. As soon as I set the AlphaData, the sizing of the axes slightly changes and chops off some borders.
How do I fix this?

Answers (1)

Walter Roberson
Walter Roberson on 23 Apr 2013
This is an OpenGL bug. OpenGL is the only renderer that supports Alpha, so switching to a different renderer will not work. However if you are not on OS-X, you can give the command
opengl software
Alternately you can nudge the size of your figure to be slightly bigger. (I think it was figure instead of axis... Jan did the relevant testing.)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!