How do you read the alpha channel of an indexed image?

Imread or open seems to give you only the cdata + colormap, however if there is transparency in the image, I can't read the information with matlab.
Is there any way to get the alpha channel?

 Accepted Answer

Hum, yes it does appear that you cannot retrieve the alpha value associated with the palette of a png image with imread. I think that is worthy of a bug report to mathworks.
You can still get the transparency info with imfinfo:
pnginfo = imfinfo('Dallas_Fuel_logo_std.png');
pnginfo.SimpleTransparencyData
Note:
The transparency for png indexed image is stored in a tRNS chunk. The length of the transparency array is at most the length of the colour map but can be shorter (as is the case in your image). In that case, the remaining colours all have 0 transparency. Consequently, I would read the transparency as follow:
[img, map] = imread(pngpath);
pnginfo = imfinfo(pngpath);
maptrans = zeros(size(map, 1), 1);
maptrans(1:numel(pnginfo.SimpleTransparencyData)) = pnginfo.SimpleTransparencyData;

2 Comments

Ah thanks works for me. Thanks. I will send them a bug report.
For information, the bug has been fixed in R2018b.
[img, map, alpha] = imread('Dallas_Fuel_logo_std.png')
now correctly returns the transparency in alpha.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 8 Dec 2017

Commented:

on 27 Nov 2018

Community Treasure Hunt

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

Start Hunting!