How to use "image" function to view JP2 images with alpha channel?
19 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
MathWorks Support Team
2025-3-14
编辑:MathWorks Support Team
2025-3-20
When reading in a JP2 image, the fourth page represents 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.
1 个评论
DGM
2025-3-22
Note that when displaying images with transparency, their appearance is dictated by what's beneath the image. In this case, it's the default axes background, which is white. It's conceivable that such a setup would be very confusing for viewing certain images.
% a PNG with alpha will not read the same as a JP2 with alpha
[inpict,~,alpha] = imread('bbwr.png');
% image() accepts 'alphadata' specification directly
image(inpict,'alphadata',alpha)
% but imshow() needs it to be set after creating the object
figure
hi = imshow(inpict);
hi.AlphaData = alpha;
更多回答(1 个)
Stephen23
2025-3-7
编辑:Stephen23
2025-3-8
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 个评论
Stephen23
2025-3-22
编辑:Stephen23
2025-3-22
@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
2025-3-22
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!