How can I change the colormap of an image?

I have an image I saved with the pink colormap and I would like to display it with jet. How can I do that?
The way I managed to do it seems like a really round about way:
I = imread('myimage.png');
I = ind2rgb(rgb2gray(I),jet);
imshow(I)

12 Commenti

Do you have the original colormap? Because then you could use rgb2ind to get to the indexed image.
The original map was pink(128), is this what you mean?
Did you try my solution below? Did it not work?
When you wrote it to png, did you write it as an indexed image with a colormap, or did you write it as an rgb image?
BGranato
BGranato il 16 Ago 2018
Modificato: BGranato il 16 Ago 2018
If I combine both suggestions it works :)
i = imread('myimage.png');
im = rgb2ind(i,pink(128));
colormap(gca, jet(256));
imshow(im)
I am not currently convinced it does work properly. Some testing I am doing suggests that this process can result in slightly different colors. The rgb2ind() of uint8 data against double pink(128) is returning values that are either the same, or off by 1, and (occasionally) off by 2.
I am testing to see if I can do better; I accidentally triggered a MATLAB bug along the way and stopped to report it.
Okay, to increase accuracy, I need to know exactly how you converted the original data to the rgb to write into the png file, including knowing the class of the original data and its value range.
I just confirmed that ind2rgb() of a double array in the range 0 to 127 does not produce exactly the same result as ind2rgb() of uint8() of that same array. I suspected that might be the case.
The original data was a sound wave which I saved as an image.
S = spectrogram(audiodata,400,300,[],fs);
imgData = flipud(abs(S));
imwrite(ind2rgb(im2uint8(mat2gray(imgData)), pink(128)), 'myimage.png')
I've since realized that I don't really need to do mat2gray, so I'm now saving the image this way:
imwrite(ind2rgb(im2uint8(imgData), pink(128)), 'myimage.png')
This also seems quite convoluted, so if you have a better suggestion I'm all ears.
Thank you!
That is probably one of the more compact ways of doing that.
Why are you saving it with a pink colormap when you said what you really wanted was a jet colormap?
Great, thank you! I found it on here somewhere, you guys are great! :)
Long story short, I saved thousands of images with pink but then realized some of the features are difficult to see, at least for some of the photos. So I wanted to change it to something else (jet) when I use imshow without having to re-do the whole extraction and saving process.
Thank you everyone!

Accedi per commentare.

Risposte (1)

Try this:
[indexedImage, pinkMap] = imread('myimage.png');
imshow(indexedImage);
colormap(gca, jet(256)); % Ignore pink map and use jet instead.
colorbar(gca);

2 Commenti

It didn't work, unfortunately, the image still appears as pink.
As long as you saved the gray scale image and the colormap, you can simply recall the grayscale image and use a different colormap when displaying, basically using the gray levels as indexes. This is preferred because gray levels are ordered in units of increasing intensity, whereas arbitrary indexes are not in any kind of order with intensity. This is the preferred way and what I'd hoped you did in my answer.
If you, unfortunately, got an indexed image from an RGB image by calling rgb2ind(rgbImage, pink) then those indexes are probably unique/special to the pink colormap and you can't simply use a different colormap with that same indexed image because they don't have any special ordering with respect to gray level intensity. To illustrate, just call imshow(), colormap(gray(256)), and colorbar() and check out the colorbar - you will see it's basically a bunch of random gray colors because they are not ordered. You'd have to get back to the original rgb image by using ind2rgb(indexedImage, pink) and then convert to grayscale with rgb2gray(). Once it's in grayscale, you can then use any other colormap you want, like jet.
I hope that explains it better.

Accedi per commentare.

Richiesto:

il 16 Ago 2018

Commentato:

il 20 Ago 2018

Community Treasure Hunt

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

Start Hunting!

Translated by