Borrar filtros
Borrar filtros

How to plot png images with transparent background?

173 visualizaciones (últimos 30 días)
Mr M.
Mr M. el 12 de Abr. de 2018
Editada: DGM el 3 de Nov. de 2022
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Abr. de 2018
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 comentario
Walter Roberson
Walter Roberson el 12 de Abr. de 2018
[img, map, alphachannel] = imread('YourImage.png');
image(img, 'AlphaData', alphachannel);

Iniciar sesión para comentar.

Más respuestas (1)

Mr M.
Mr M. el 12 de Abr. de 2018
OK, but how to read the png to keep transparent background?
  2 comentarios
Cris LaPierre
Cris LaPierre el 11 de Sept. de 2019
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 el 3 de Nov. de 2022
Editada: DGM el 3 de Nov. de 2022
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.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by