フィルターのクリア

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

40 ビュー (過去 30 日間)
Mark
Mark 2014 年 11 月 20 日
回答済み: DGM 2022 年 4 月 11 日
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 日
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 4 月 7 日
Notice, though, that "icon is now only zeros", so something else was going wrong for the user.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by