I have an image that is 234x432x4 size wise. I tried imshow() and image(), but it doesn't work. What do I use?

17 views (last 30 days)
I have an image that is 234x432x4 size wise. I tried imshow() and image(), but it doesn't work. What do I use?

Answers (2)

John D'Errico
John D'Errico on 29 Sep 2022
You have a 4 channel image, most likely CMYK, certainly not RGB.
For example:
imshow(rand(234,432,3))
As you can see, there is no problem. However,
imshow(rand(234,432,4))
Error using images.internal.imageDisplayValidateParams>validateCData
Multi-plane image inputs must be RGB images of size MxNx3.

Error in images.internal.imageDisplayValidateParams (line 30)
common_args.CData = validateCData(common_args.CData,image_type);

Error in images.internal.imageDisplayParseInputs (line 79)
common_args = images.internal.imageDisplayValidateParams(common_args);

Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
So assuming you have a CMYK image, you will need to convert it into equivalent RGB code values. Of course, I would not be surprised if there may be some gamut issues involved, so some colors will not be represented as well as you might desire.
If, in fact, you have a multispectral image of some other ilk, again, you need to convert it into an appropriate 3 channel color space.
  3 Comments

Sign in to comment.


DGM
DGM on 1 Oct 2022
Edited: DGM on 1 Oct 2022
If the image is CMYK, hyperspectral, or simply a volumetric image, then this answer really doesn't apply. Admittedly, it's more likely that an imported 4-channel image is not RGBA, but I'm going to assume you somehow managed to get an RGBA image assembled.
If the image is RGBA, then MIMT has tools for handling it. The basic idea is to just composite the image with a standard opaque background. At that point, it's just an RGBA image and the display tools can handle it just fine.
Using alphasafe() and imshow():
% a PNG with alpha (RGB + A)
[inpict,~,alpha] = imread('sources/standardmods/peppers_rgba.png');
% put the image back together (RGBA)
inpict = joinalpha(inpict,alpha);
% composite the image with checkerboard padding for the purpose of viewing
safepict = alphasafe(inpict); % using default options
% display the RGB representation of the RGBA image
imshow(safepict)
Alternatively, MIMT imshow2() handles RGBA images, as well as other things that imshow() doesn't. It's a bit janky since the default view controls were changed a few years ago, but maybe I'll fix that someday.
% a PNG with alpha (RGB + A)
[inpict,~,alpha] = imread('sources/standardmods/peppers_rgba.png');
% put the image back together (RGBA)
inpict = joinalpha(inpict,alpha);
% display the RGB representation of the RGBA image
imshow2(inpict)
The idea behind the first example can certainly be done without MIMT tools. This is one of several answers describing how to do basic image composition without specialized tools. I chose this one because it includes a simple example showing how the checkerboard matting can be generated.
When doing composition "manually" like this, it'll be important to pay attention to the class and scale of the image data.

Community Treasure Hunt

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

Start Hunting!

Translated by