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

조회 수: 13 (최근 30일)
Martino Riccardi
Martino Riccardi 2022년 5월 14일
댓글: DGM 2022년 5월 18일
I used the command "imread" to get a matrix A of the image and its colormap.
I inverted the colormap with:
Icmap=colormap(flipud(cmap));
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":
imwrite(B,Icmap,"image_name.png")
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

채택된 답변

Sailesh Kalyanapu
Sailesh Kalyanapu 2022년 5월 18일
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
DGM
DGM 2022년 5월 18일
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
imshow(newpict,newmap)
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
imshow(newpict,newmap)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by