1

Today I wanted to experiment with semi-transparent polygons (specifically triangles) in Matlab. What I need to do is to draw several (semi-transparent) polygons (which can overlap) into figure and then get matrix (of size H x W ) of RGB pixel values.

Example of what I have done so far:

H = 100; % Desired height (in pixels)
W = 150; % Desired width  (in pixels)

f = figure('Position',[0,0,W,H],'Resize', 'off', 'DockControls', 'off','PaperUnits','inches','PaperPosition',[0,0,W,H]);
axis off; 
hold on;

% Background
fill([0,W,W,0,0], [0,0,H,H,0], 'black', 'FaceAlpha',1, 'EdgeColor', 'none'); 

% Other polygons
fill([20,60,95], [40,80,10], 'r', 'FaceAlpha',0.5, 'EdgeColor', 'none');
fill([10,45,80], [10,90,25], 'g', 'FaceAlpha',0.5, 'EdgeColor', 'none');
% ... there will be more of them

set(gca,'Position',[0,0,1,1]) % This seems to be a problem

pixels = print('-RGBImage','-r1'); % Get RGB matrix

% Try to show the result
figure
imshow(pixels)

When the figure f is visible, I can see:

this

which is desired output. But as soon as I call print(...) and display the pixels, I see:

this

which has a white area on the right side, which is undesired.

I suppose, that problem is in calling of:

set(gca,'Position',[0,0,1,1])

but as I am not so experienced with Matlab, I don't know, how to fix it. Any ideas?

Note: Another approach could be directly using matrices. I found a function poly2mask, which could fill the triangles for me, but using that, I would need to compute all RGB(A) values of overlaying triangles myself. Furthermore poly2mask won't give 'fluent transitions' around the edges of triangles (as the output is boolean 0/1). Is there some function similar to poly2mask, but returning values in interval [0,1], which should give some smoothness on the edges?

P.S.: I am working on Ubuntu 14.04, using Matlab R2015b.

EBH
  • 10,350
  • 3
  • 34
  • 59
Andrew
  • 11
  • 2

1 Answers1

0

It seems, that replacing

axis off; 

with

axis tight;

somehow solved the problem.

Andrew
  • 11
  • 2