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

47 Ansichten (letzte 30 Tage)
Mark
Mark am 20 Nov. 2014
Beantwortet: DGM am 11 Apr. 2022
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

Akzeptierte Antwort

DGM
DGM am 11 Apr. 2022
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.

Weitere Antworten (1)

Raphael
Raphael am 6 Apr. 2018
Bearbeitet: Walter Roberson am 7 Apr. 2018
  1 Kommentar
Walter Roberson
Walter Roberson am 7 Apr. 2018
Notice, though, that "icon is now only zeros", so something else was going wrong for the user.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by