How do I fix a "Color data must be an m-by-n-by-3 or m-by-n matrix." error?
Mostrar comentarios más antiguos
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 comentarios
Chris
el 4 de Abr. de 2025
Image Analyst
el 9 de Abr. de 2025
Editada: Image Analyst
el 9 de Abr. de 2025
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
Respuesta aceptada
Más respuestas (2)
Sulaymon Eshkabilov
el 7 de Mzo. de 2025
- 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
el 7 de Mzo. de 2025
0 votos
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 comentarios
Chris
el 7 de Mzo. de 2025
Walter Roberson
el 7 de Mzo. de 2025
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.
Chris
el 7 de Mzo. de 2025
"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)
Walter Roberson
el 7 de Mzo. de 2025
See https://www.mathworks.com/matlabcentral/answers/101194-how-can-i-convert-cmyk-image-to-rgb for information about converting CMYK to RGB
Chris
el 11 de Mzo. de 2025
Image Analyst
el 11 de Mzo. de 2025
To learn other fundamental concepts, invest 2 hours of your time here:
Chris
el 13 de Mzo. de 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.
Categorías
Más información sobre Convert Image Type en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
