Axes colour in figure changes after using saveas function to save as png

37 次查看(过去 30 天)
I am trying to save a figure as a png which contains an image where NaN pixels are shown as black. I have done this by setting the axes colour to black and creating an alpha map where NaN values in the amage are set to zero. This appears correctly when the figure is diplayed but when I use the saveas function to save the figure as a png the black pixels change to white. If I save the figure manually as a png they stay black but this needs to be repeated for many images so this is impractical.
How can I preserve the axes colour when saving as a png? See example code with dummy image and alphamap below:
a = rand(100,100); % create random array as test image
a(a<0.5) = nan; % threshold image to introduce nan pixels
figure()
testplot = axes(); % create axes
imagesc(testplot,a); % show test image
aMap = rand(100,100); % create random array for alpha map
aMap(isnan(a)) = 0; % set all nan pixels in a as zero in the alpha map
alpha(testplot,aMap); % apply alpha map to plot
set(gca,'color','k'); % set background colour to black
% save as png and as matlab figure
saveas(gcf,'testimage.png')
saveas(gcf,'testimage.fig')

采纳的回答

Dario Angelone
Dario Angelone 2023-4-3
A previously deleted answer suggested trying
exportgraphics(gcf,'testimage.png','ContentType','image','Resolution',600)
This is producing the desired output.

更多回答(1 个)

Jack
Jack 2023-3-30
When you save the figure using the saveas function, MATLAB uses a default resolution of 150 DPI (dots per inch) to create the PNG image. The default renderer used by MATLAB to create PNG images is the OpenGL renderer, which does not always produce consistent results when dealing with transparency and alpha maps. This is likely the cause of the issue you are experiencing, where the NaN pixels in the image are shown as white instead of black.
To fix this issue, you can try using a different renderer when saving the figure as a PNG. One option is to use the -r option with the print function to set a higher resolution and a different renderer. For example, you can try setting the renderer to 'Painters' and the resolution to 300 DPI using the following code:
% save as png using the print function with higher resolution and different renderer
print(gcf, 'testimage.png', '-dpng', '-r300', '-painters');
This code saves the figure with a resolution of 300 DPI and uses the 'Painters' renderer instead of the default OpenGL renderer. The 'Painters' renderer is a vector-based renderer that is better suited for creating high-quality images with transparency and alpha maps.
Alternatively, you can try using the exportgraphics function introduced in MATLAB R2020a to save the figure as a PNG with transparency. This function allows you to specify the format and resolution of the output image, as well as any additional options such as setting the background color. Here is an example of how to use the exportgraphics function to save the figure as a PNG with transparency:
% save as png using exportgraphics function with transparency
exportgraphics(gcf, 'testimage.png', 'Resolution', 300, 'BackgroundColor', 'k', 'ContentType', 'image/png');
This code saves the figure with a resolution of 300 DPI, sets the background color to black, and specifies the output format as PNG. The resulting PNG image should preserve the black background color and the NaN pixels in the image should be shown as transparent.
  2 个评论
Dario Angelone
Dario Angelone 2023-4-3
The first code still produces an image with a white background and the second generates the error:
Error using exportgraphics
'ContentType' value must be one of 'vector', 'image', or 'auto'.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by