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!
How do I fix a "Color data must be an m-by-n-by-3 or m-by-n matrix." error?
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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
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')
更多回答(2 个)
Sulaymon Eshkabilov
2025-3-7
- Check what is the content of image_name
- 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!
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Basic Display 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
