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

10 ビュー (過去 30 日間)
Chris
Chris 2025 年 3 月 6 日
編集済み: Image Analyst 2025 年 4 月 9 日
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 件のコメント
Chris
Chris 2025 年 4 月 4 日
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!
Image Analyst
Image Analyst 2025 年 4 月 9 日
編集済み: Image Analyst 2025 年 4 月 9 日
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

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2025 年 3 月 7 日
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 件のコメント
Image Analyst
Image Analyst 2025 年 3 月 11 日
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')
Chris
Chris 2025 年 3 月 13 日
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)?

サインインしてコメントする。

その他の回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2025 年 3 月 7 日
  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 件のコメント
Chris
Chris 2025 年 3 月 7 日
How do I check the content of image_name and imdata? I am still really new to coding, especially on something like MatLAB

サインインしてコメントする。


Walter Roberson
Walter Roberson 2025 年 3 月 7 日
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 件のコメント
Chris
Chris 2025 年 3 月 13 日
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 2025 年 3 月 14 日
編集済み: DGM 2025 年 3 月 14 日
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by