MATLAB: How to use “image” function to view images (JP2, PNG, BMP, etc) with alpha channel

alphaimageMATLABrbga

I have an image file to view in MATLAB. When I do "a = imread(filename)" and "image(a)", it gives the following errors:
Error using image
Color data must be an m-by-n-by-3 or m-by-n matrix.
Error in display_jp2 (line 5)
image(a)
The variable "a" is an m-by-n-by-4 matrix.

Best Answer

The images with the fourth column are typical with an alpha channel that stores the transparency data of each pixel. The color space in this case is in RGBA (Red, Green, Blue, Alpha) format, not traditional RGB format.
To display the image without transparency information
Execute the following command:
>> filename = '<Your_Image_Location>';
>> a = imread(filename);
>> image(a(:,:,1:3))
To display the image with transparency information
Execute the following command:
>> filename = '<Your_Image_Location>';
>> a = imread(filename);
>> image(a(:,:,1:3),'alphadata',a(:,:,4));
The "alphadata" property of an image is the one storing transparency information.