Why does imread read the alpha channel, but not the image itself?

40 次查看(过去 30 天)
Hi all,
I would like to read a png with transparent background in MATLAB (2014b, Windows 7) using imread:
[icon, map, alpha] = imread('icon.png');
However, icon is now only zeros, but the alpha channel is read correctly (the map is empty as well). I made the png with Photoshop and I tried several options, such as making the image indexed, RGB, grayscale, 8 bits, 32 bits, et cetera.
Does anyone have an idea about why my alpha channel is read correctly, but the image data is black? How should I save the image from Photoshop such that it is read in MATLAB? Thanks in advance!
Kind regards, Mark

采纳的回答

DGM
DGM 2022-4-11
Old question, I know, but I answer them when I'm bored.
The reason that the image looks all black is because that's what it is. The color content is a uniform black field. The object content of the image is defined only by the alpha channel. There isn't any reason to suspect that transparent image regions are any particular color.
It's worth noting that this might not always seem to be the case. Consider a modified version of the same image. In this case, the color channels are a uniform purple field. The alpha channel is the same as before. I've scaled the image to make it easier to see. What happens if you try to just load the color content? You should get a uniform purple field, right?
A = imread('icon.png');
imshow(A)
Nope. This doesn't mean that the color content is black and purple. There is no black in the image, but when called with this syntax, imread() internally composes the transparent image with a black field, resulting in this. It may be a convenience, but it's inflexible and misleading, so pay attention. If your images have alpha content, you must call imread with three output arguments in order to get unadulterated color content for the image, even if you don't need to use the alpha for anything.

更多回答(1 个)

Raphael
Raphael 2018-4-6
编辑:Walter Roberson 2018-4-7

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by