How to plot png images with transparent background?

199 次查看(过去 30 天)
I've just used image(), and the transparent area are shown with black colour by default, however I need white background. I tried to use set(gcf,'color','white') but this cannot help.

采纳的回答

Walter Roberson
Walter Roberson 2018-4-12
When you image() pass in 'AlphaData', and the transparency data. The alpha needs to be floating point (not logical) in the range 0 to 1 and the same size as the image.

更多回答(1 个)

Mr M.
Mr M. 2018-4-12
OK, but how to read the png to keep transparent background?
  2 个评论
Cris LaPierre
Cris LaPierre 2019-9-11
Images get displayed in an axes. Even if the image doesn't have a background, it will at least originally display in an axes with a background color set to white. You can turn that off using
axis off
of
set(gca,'Color','none')
DGM
DGM 2022-11-3
编辑:DGM 2022-11-3
If you're trying to retain the original alpha content from the PNG file:
% read the image and its alpha
[inpict,~,alpha] = imread('peppers_rgba.png');
% show the image with the alpha from the file
image(inpict,'alphadata',im2double(alpha))
% set the axes background color if desired
set(gca,'color',[0.8 0.3 1])
If instead, your concern is how the transparent regions are visualized, then consider this example regarding display matting for transparent images.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by