teaLeef
teaLeef

Reputation: 1989

Adjusting colors map in matlab

I am printing an image on matlab using imwrite:

imagesc(imageM);
imwrite(imageM, jet(N), 'fileName.jpg');

I can't get the same colors as those I get using imagesc on the saved figure. I tried playing with different values for N in jet (the values of the matrix imageM are between 2 and 180). However, I can't get the same result. How can I choose the range in jet to get the same colors as those using imagesc?

Upvotes: 1

Views: 72

Answers (2)

Amro
Amro

Reputation: 124543

Try this:

imagesc(img)

cmap = get(gcf,'Colormap');
X = ind2rgb(img, cmap);
imwrite(X, 'out.png');

Upvotes: 0

hitzg
hitzg

Reputation: 12691

You need to scale either the values in imageM or the colormap, because that is excatly what imagescdoes. In your image you have 180 - 2 = 178 steps, so

imgmin = min(imageM(:));
imgrange = range(imageM(:));

imwrite(imageM-imgmin, jet(imgrange) , 'fileName.jpg')

should do the trick.

Otherwise you could go with a standard colormap with 64 entries:

imwrite((imageM-imgmin)*64/imgrange, jet, 'fileName.jpg')

Upvotes: 1

Related Questions