Can I save an image with different colormap? (Readable by Matlab)

14 views (last 30 days)
I used the command "imread" to get a matrix A of the image and its colormap.
I inverted the colormap with:
I want to apply the inverted map to an image B and save the result.
Is there a way to save the image B taking into account the new colormap, such that it's readable by the command "imread"?
If I use the command "imwrite":
I get an image in my folder which represents what I want (if I open it OUTSIDE Matlab), but the command "imread" gives me the original image B as a matrix.
Thank you, in any case

Accepted Answer

Sailesh Kalyanapu
Sailesh Kalyanapu on 18 May 2022
It is my understanding that you are looking to save an image with a different colormap and later read it using the function 'imread()
It is possible to do so using the ind2rgb() function.
Please add the following command in your code before calling the ‘imwrite()’ function and later use the imread() to get the true RGB format matrix
>> [X,cmap] = imread(filename);
>> Icmap = colormap(flipud(cmap));
>> Y = ind2rgb(X,Icmap);
>> imwrite(Y,filename1);
>> X_required = imread(filename1);
Please refer to the following link to a documentation for more information about ind2rgb() function:
  1 Comment
DGM on 18 May 2022
Why convert to RGB? PNG supports indexed images.
[inpict map0] = imread('canoe.tif');
map1 = 1-map0; % invert map
imwrite(inpict,map1,'invertedcanoe.png') % save
[newpict newmap] = imread('invertedcanoe.png'); % read
Note to OP: I think the obvious interpretation of "invert" is the unit complement of an image, so the inverse of cmap is 1-cmap. Flipping the map might be equivalent if the map is a simple linear RGB sweep. It all depends on what you actually want to happen. Note that in this case, flipping the map doesn't turn out so well.
[inpict map0] = imread('canoe.tif');
map1 = flipud(map0); % flip map
imwrite(inpict,map1,'invertedcanoe.png') % save
[newpict newmap] = imread('invertedcanoe.png'); % read

Sign in to comment.

More Answers (0)


Find more on Colormaps in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by