How to use "image" function to view JP2 images with alpha channel?
Mostra commenti meno recenti
I have a JP2 image file to view in MATLAB. When I run the following code:
a = imread(filename);
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. How can I fix this error?
Risposta accettata
Più risposte (1)
The accepted answer is incorrect.
"When I do "a = imread(filename)" and "image(a)", it gives the following errors..."
"The variable "a" is an m-by-n-by-4 matrix"
"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."
The only documented case where IMREAD's first output has 4 pages** is when the image is stored with CMYK colorspace (i.e. only when importing TIFF files):
"Image data, returned as an array. If the image data has m rows and n columns, then:"
- "If the file contains a grayscale image, then A is an m-by-n array of values that represent the intensity of the pixels in the image."
- "If the file contains an indexed image, then A is an m-by-n array of index values that refer to the rows of map."
- "If the file contains an RGB (truecolor) image, then A is an m-by-n-by-3 array."
- "If the file is a TIFF file containing color images that use the CMYK color space, then A is an m-by-n-by-4 array." (bold added)
If the image contains an alpha channel then this is returned as the third output from IMREAD:
"For PNG files:"
- "If the alpha channel is present and you do not specify the BackgroundColor name-value argument, then transparency is the alpha channel."
- ...
Documentation source: https://www.mathworks.com/help/matlab/ref/imread.html
Lets test it right now. First we create a PNG file with an alpha channel:
I = rand(5,7,3);
A = rand(5,7,1);
image(I,'AlphaData',A)
imwrite(I,'mytest.png','Alpha',A)
imfinfo mytest.png
Why the inconsistent option names ALPHA vs ALPHADATA? Anyway, lets now check the size of the image imported by IMREAD. Does it have 4 pages as the accepted answer incorrectly claims? (hint: no)
J = imread('mytest.png');
size(J)
Does it have 3 pages as the documentation states? (hint: yes)
[J,~,B] = imread('mytest.png');
size(J)
size(B) % alpha channel
For CMYK we must create a TIFF image:
K = rand(5,7,4);
imwrite(K,'mytest.tiff')
imfinfo mytest.tiff
Lets now check the size of the CMYK image imported by IMREAD. Does it have 4 pages as the documentation states? (hint: yes)
L = imread('mytest.tiff');
size(L)
Conclusion: the only documented case of IMREAD returning image data with 4 pages also matches the test results, i.e. that of a TIFF image using a CMYK colorspace. @TMW: perhaps getting letting interns post untested answers without anyone reviewing them is not the best way to leave the impression that you know your own product.
** Rather amusingly "Mathworks Support Team" also does not know what columns and pages are:
3 Commenti
A PNG written with alpha reads as RGB+A
% since JP2 can't be attached on the forum ...
unzip stuff.zip
% read an RGBA PNG
fname = 'stuff/peppers_rgba.png';
[inpict,~,alpha] = imread(fname);
imfinfo(fname).ColorType % it's apparently RGB
[size(inpict,3) size(alpha,3)-isempty(alpha)] % 3+1 channels (RGB+A)
% generate some matting
bsf = 70; % block size scaling factor
sc = size(inpict,1:2);
blocksize = sqrt(sum(sc.^2))/bsf;
mat = 0.5*freecb(sc,blocksize) + 0.25; % make it gray
% display it on checkered matting
himat = imshow(mat); hold on;
hi1 = imshow(inpict);
hi1.AlphaData = alpha;
But a JP2 with alpha reads as a 4-channel image.
% read an RGBA JP2 file
fname = 'stuff/peppers_rgba.jp2';
[inpict,~,alpha] = imread(fname);
imfinfo(fname).ColorSpace % it's apparently RGB
[size(inpict,3) size(alpha,3)-isempty(alpha)] % 4 channels (RGBA)
% generate some matting as before
% ...
% display it on checkered matting
himat = imshow(mat); hold on;
hi1 = imshow(inpict(:,:,1:3));
hi1.AlphaData = inpict(:,:,4);
The PNG was generated using imwrite(), but imwrite() can't write a JP2 with alpha. The JP2 was converted from the PNG using ImageMagick. So imread() might return either RGB+A or RGBA, depending on the format. I don't know the reason for the inconsistency. It certainly doesn't appear that the JP2 is CMYK.
If imwrite() is asked to create a TIFF, it will accept a 4-ch image and treat it as CMYK. I abuse that all the time, but that's just my bad habits.
@DGM: this is exactly why I emphasized "documented" in my answer. The documentation for the first IMREAD output is either incorrect or incomplete: nowhere does it state that any image file format returns a four-page RGB+A array. Although the TRANSPARENCY output does state that it "...applies only to PNG, CUR, and ICO files", it should not be required for a user to perform experiments to discover how IMREAD works with an image format that it is documented to work with.
Incomplete documentation... which TMW tries to cover with posting random threads written by staff who do not know the difference between columns and pages.
My point is about the documentation, which does not match what the function actually does. Amusingly there are also some other active threads on unrelated TMW functions which are making exactly the same point about the documentation.
** see also MATLAB.
DGM
il 22 Mar 2025
FWIW, I think the imread() webdocs said something about the decoder just dumping all available channels, so I'm not even sure that it's even considering that the content contains alpha data. I thought about trying to create a malformed JP2 with a silly number of channels, but then I realized that I'd be better off going to sleep.
Categorie
Scopri di più su Convert Image Type in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



