Transparency causes nonsmooth lines

When I set transparency in the figure, the original smooth line becomes nonsmooth.
[x,y] = meshgrid(-2:.2:2);
z = x.*exp(-x.^2-y.^2);
a = gradient(z);
figure; surf(x,y,z,'AlphaData',a,'FaceAlpha','flat','FaceColor','blue');
figure; surf(x,y,z)

2 comentarios

KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Jul. de 2018
What is your question?
bai da
bai da el 26 de Jul. de 2018
Jan has pointed out the problem. The line becomes nonsmooth after I set transparency to the figure

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 26 de Jul. de 2018

0 votos

Transparency requires the OpenGL renderer. The line-smoothing requires the Painters renderer. As far as I can see, you observe the expected behavior.

5 comentarios

bai da
bai da el 26 de Jul. de 2018
Thanks! Are there any trick to avoid this dilemma?
Jan
Jan el 26 de Jul. de 2018
Editada: Jan el 26 de Jul. de 2018
"Dilemma" means that it is not a "lemma", or in other words: It cannot be decided. This implies, that you cannot avoid it, because it is the nature of the different renderers.
But you can reduce the effects or find a work-around. It depends on what you want to do with the displayed graphics. Does your problem concerns the output to the screen or do you want to export a smoothed graphics file? KALYAN ACHARJYA asked already, what your question is.
Which Matlab version are you using and do you have the Image Processing toolbox?
bai da
bai da el 26 de Jul. de 2018
Yes, I want to export a smoothed file. I'm using version 2017a. I have the image processing toolbox
Jan
Jan el 26 de Jul. de 2018
Editada: Jan el 26 de Jul. de 2018
Try this:
RGB = AntiAlias(gcf, 4, 'lanczos3');
imwrite(RGB, 'File.png');
Using this function:
function Out = AntiAlias(FigH, K, Method)
% Anti-aliasing of a figure
% Reply = AntiAliasFig(FigH, K)
% FigH: Figure handle.
% K: Integer subsampling factor >= 1: The created pictures uses a K-times
% higher resolution.
% For K = 1 the output equals the original picture.
% Method: See Method of IMRESIZE: E.g. 'lanczos3'
%
% OUTPUT:
% Reply: RGB data are replied as [M x N x 3] UINT8 array.
%
% Tested: Matlab/64 9.1, Win10
% Author: Jan Simon, Heidelberg, (C) 2018
% Initialize: ==================================================================
screen_DPI = get(groot, 'ScreenPixelsPerInch');
% Do the work: =================================================================
% Wanted resolution as string:
ResolutionStr = sprintf('-r%d', round(screen_DPI * K));
% Prepare figure for hardcopy:
drawnow;
fig_Renderer = get(FigH, 'Renderer');
fig_Paperposmode = get(FigH, 'PaperPositionMode');
fig_PaperOrient = get(FigH, 'PaperOrientation');
fig_Invhardcopy = get(FigH, 'InvertHardcopy');
set(FigH, ...
'PaperPositionMode', 'auto', ...
'PaperOrientation', 'portrait', ...
'InvertHardcopy', 'off');
% Create hard copy in high resolution: -----------------------------------------
% Simulate PRINT command (save time for writing and reading image file):
% Set units of axes and text from PIXELS to POINTS to keep their sizes
% independent from from the output resolution:
% See: graphics/private/preparehg.m
root_SHH = get(groot, 'ShowHiddenHandles');
set(groot, 'ShowHiddenHandles', 'on');
text_axes_H = [findobj(FigH, 'Type', 'axes'); ...
findobj(FigH, 'Type', 'text')];
pixelObj = findobj(text_axes_H, 'Units', 'pixels');
fontPixelObj = findobj(text_axes_H, 'FontUnits', 'pixels');
set(pixelObj, 'Units', 'points');
set(fontPixelObj, 'FontUnits', 'points');
fig_ResizeFcn = get(FigH, 'ResizeFcn');
set(FigH, 'ResizeFcn', '');
% Get image as RGB array:
high = print('-RGBImage', ResolutionStr);
% Restore units of axes and text objects, and EraseMode:
set(pixelObj, 'Units', 'pixels');
set(fontPixelObj, 'FontUnits', 'pixels');
set(groot, 'ShowHiddenHandles', root_SHH);
set(FigH, 'ResizeFcn', fig_ResizeFcn);
% Restore properties of the original image:
set(FigH, ...
'InvertHardcopy', fig_Invhardcopy, ...
'PaperPositionMode', fig_Paperposmode, ...
'PaperOrientation', fig_PaperOrient, ...
'Renderer', fig_Renderer);
% Create the low resolution data and axes properties:
try
low = imresize(high, 1 / K, Method); % Hide from CheckSyntax
catch ME
if isempty(which('imresize'))
error(['*** %s: Signal-Processing-Toolbox is required.', ...
char(10), '%s'], mfilename, Mode, ME.message);
else
error(['*** %s: Unknown [Mode]: [%s]', ...
char(10), '%s'], mfilename, Mode, ME.message);
end
end
% Limit range of data:
% Slightly slower: lowres = max(min(lowres, 1), 0);
if isa(low, 'double')
low(low < 0) = 0;
low(low > 1) = 1;
end
Out = low;
end
bai da
bai da el 30 de Jul. de 2018
Editada: bai da el 30 de Jul. de 2018
Thank you very much! It works! It also works in Linux (Ubuntu).

Iniciar sesión para comentar.

Más respuestas (1)

Indrajit Wadgaonkar
Indrajit Wadgaonkar el 10 de Ag. de 2021
Editada: Indrajit Wadgaonkar el 10 de Ag. de 2021

0 votos

Did this issue get resolved? What is the solution if I need transparency as well as smooth lines?

Categorías

Etiquetas

Preguntada:

el 26 de Jul. de 2018

Editada:

el 10 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by