How do I fix a "Color data must be an m-by-n-by-3 or m-by-n matrix." error?

I am borrowing someone's code for a coloration project and I keep getting this error:
Error using image
Color data must be an m-by-n-by-3 or m-by-n matrix.
Error in linearization_2s (line 124)
image (imdata)
^^^^^^^^^^^^^^
This is part of the code I am using (I don't know how much is helpful, but the error is occurring in the middle):
%now we iterate over all the images
for this_image = 1:file_image_counter
%find the file I want to open
image_name = list_images{this_image};
%load the data
imdata = imread([folder_images '/' image_name]);
%imdata is the image that we will work on. If we want to do
%what I told you above we would iterate over a list of
%names instead of the specific file 'DSC_1820.tiff'.
fig_1 = figure(1);
set (fig_1, 'Units', 'normalized', 'Position', [0,0,1,1]);
image (imdata)
title ('Select 2 rectangles from bright to black','fontsize',20)
%find values of the squares for fit (you may want to change the number 2 in the title and the next 3 lines for a higher number of rectangles)
pos = zeros (2,4);
rec_data = zeros (2,4);
for rec_i = 1:2
It keeps popping up as my data failing at the image(imdata) above, but I'm not sure what to do next. I am trying to linearize a photo by using a color card I have in my pictures. Thank you for any help!

2 commentaires

Update: For some reason, my photo did not have any color space actually assigned to it. So when I tried to run it in the program, it didn't recognize it as RGB, greyscale, CMYK, or anything else. I ended up using Adobe Photoshop to "assign" it a color space and I haven't had issues since. Thanks everyone for your help!
Since it's a 4-D array, you can take 3 of the channels like this
if numel(size(imdata)) > 3
rgbImage = imdata(:, :, 1:3);
end

Connectez-vous pour commenter.

 Réponse acceptée

Before that image(imdata) line, do this and tell us what you see in the command window:
whos imdata
If you do that we'll see just what kind of data it is. Evidently it's not a gray scale or RGB image.
Also, you should not construct the filename that way. You should use fullfile
fullFileName = fullfile(folder_images, image_name);
if isfile(fullFileName)
imdata = imread(fullFileName);
else
warningMessage = sprintf('WARNING: file not found:\n"%s"', fullFileName);
uiwait(warndlg(warningMessage));
continue; % Can't proceed because this image does not exist. Skip to end of loop.
end
That is a more robust way of operating.

3 commentaires

Chris
Chris le 7 Mar 2025
Modifié(e) : Chris le 7 Mar 2025
This is the output from whos imdata
There are 1 images
Name Size Bytes Class Attributes
imdata 3000x4000x4 48000000 uint8
Error using image
Color data must be an m-by-n-by-3 or m-by-n matrix.
Error in linearization_2s (line 125)
image (imdata)
^^^^^^^^^^^^^^
You have a 4 channel image. Is it a hyperspectral image? You can only select 1 or 3 for display, for example:
fullFileName = fullfile(folder_images, image_name);
if isfile(fullFileName)
imdata = imread(fullFileName);
else
warningMessage = sprintf('WARNING: file not found:\n"%s"', fullFileName);
uiwait(warndlg(warningMessage));
continue; % Can't proceed because this image does not exist. Skip to end of loop.
end
rgbImage = imdata(:, :, 1:3);
subplot(1, 2, 1);
imshow(rgbImage);
title('Channels 1-3)')
subplot(1, 2, 2);
imshow(imdata(:, :, 4), [])
title('This is channel 4')
Ohhh okay that makes sense! It is a DNG photo from my phone that I've converted into a TIFF. Since it is a 4 channel image, what can I do to switch it to a 3 channel one (RGB)?

Connectez-vous pour commenter.

Plus de réponses (2)

  1. Check what is the content of image_name
  2. Check what is the content of the variable imdata
Note that the variable imdata must contain the directory (folder) and a image file name, e.g., Image_1.jpg or Image_1.png or etc. Note the file name must contain the file extension as well. If imdata contains something else, then the problem is:
image_name = list_images{this_image};
%load the data
imdata = imread([folder_images '/' image_name]);
Good luck!

2 commentaires

How do I check the content of image_name and imdata? I am still really new to coding, especially on something like MatLAB

Connectez-vous pour commenter.

Documented:
  • 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.
Undocumented:
  • If the image is grayscale + alpha, and the "map" and "transparency" outputs are not requested on output, then the output A might be a m-by-n-by-2 array.

9 commentaires

How do I know if these are CMYK color space rather than an RGB one?
Ask imread() for image, map, and transparency output. If the data was RGBA, the data will be split up into m-by-n-by-3 image, empty map, and m-by-n transparency data. If the data was CMYK, then the data will be m-by-n-by-4 image, empty map, empty transparency.
Note: TIFF files also have the possibility of being RGB + "extra samples" that are not marked as alpha data. In that case, then the data will be m-by-n-by-4 image, empty map, empty transparency, or even an m-by-n-by-5 image, empty map, empty transparency, However for more than two layers of "extra samples" the extra data is stored a different way that is not combined with RGB.
Note: If the image is a GEOTIFF then it would typically be read in as RGB; use readgeotable or readgeoraster for GEOTIFF files.
Ohh, okay cool! I'm so sorry for so many questions, how do I do the Ask imread()? I'm still new to coding, especially MatLAB, I found the page on imread but I am still confused on how to use it in my code
"how do I do the Ask imread()?"
According to the IMREAD documemtation you can call IMREAD with three outputs:
[imdata,map,transparency] = imread(fullfile(folder_images,image_name))
Check the array sizes using SIZE. Note that you can specify the specific dimension that you want to check, e.g.:
SIZE(imdata,3)
You might also find ISEMPTY useful:
isempty(map)
isempty(transparency)
Okay, thank you! MatLAB had me download an extension to be able to do the imread, and now I have a further problem where a new issue is popping up earlier in the code :( I am also having issues where I can't get these new lines you have given me to pop up at all.
@Chris what do you mean by "pop up"? You can just type the lines @Stephen23 gave you into a script.
To learn other fundamental concepts, invest 2 hours of your time here:
Apologies for the late response, I'll be honest I was super tired when I wrote this answer and I'm also not sure what I meant haha. I will go watch that video and try to change my photos to turn into 4-channel ones :) I'll let you know how it goes!
DGM
DGM le 14 Mar 2025
Modifié(e) : DGM le 14 Mar 2025
Do we know anything about the original file? As far as I can tell, a DNG may be raw sensor data, or it may be demosaiced data.
If it's a raw DNG, then I assume the channel arrangement is dependent on the sensor's filter array (RGGB? GRGB? RGBE?). It might help to know how exactly it was converted to TIFF, and whether that process has done any demosaicing or if the TIFF is still just raw.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by