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 个评论

What is your question?
Jan has pointed out the problem. The line becomes nonsmooth after I set transparency to the figure

请先登录,再进行评论。

 采纳的回答

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

5 个评论

Thanks! Are there any trick to avoid this dilemma?
Jan
Jan 2018-7-26
编辑:Jan 2018-7-26
"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?
Yes, I want to export a smoothed file. I'm using version 2017a. I have the image processing toolbox
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 2018-7-30
编辑:bai da 2018-7-30
Thank you very much! It works! It also works in Linux (Ubuntu).

请先登录,再进行评论。

更多回答(1 个)

Indrajit Wadgaonkar
编辑:Indrajit Wadgaonkar 2021-8-10
Did this issue get resolved? What is the solution if I need transparency as well as smooth lines?

类别

帮助中心File Exchange 中查找有关 Spline Postprocessing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by