-1

I'd like to read an image in matlab and convert it to an indexed image

Here is my code:

[I map] = imread('image.tif');
I = rgb2ind(I, map);

figure(1);
imagesc(I);axis('equal');

When I just read the image, it looks fine (but it is an rgb image). Then I convert it to an indexed image, I have the following picture: enter image description here

What is wrong with this piece of code?

user2738748
  • 1,106
  • 2
  • 19
  • 36
  • What's wrong with the image? – Dan Sep 25 '15 at 09:33
  • This is not the original image. The original image is a picture of a person. – user2738748 Sep 25 '15 at 09:41
  • Which is rather vital information to the question. You should add the original image. – Dan Sep 25 '15 at 09:51
  • This is a picture of me, so I don't want to post it. I'm assuring you it has nothing to do with the rainbow I you can see above. It isn't very colorful, rather dark (but is also isn't black and white). – user2738748 Sep 25 '15 at 12:08

2 Answers2

2

Your syntax is slightly off. This should work:

[I, map] = imread('autumn.tif');
[I, map] = rgb2ind(I, map);

figure(1);
image(I);
colormap(map);
axis('equal');

See documentation of rgb2ind.

lhcgeneva
  • 1,981
  • 2
  • 21
  • 29
1

Your output is the result of a misuse of the matlab functions.

%read a non-indexed image. I is your RGB image, map is empty
[I,map] = imread('board.tif');
%rgb2ind has two output arguments, get both, otherwise your unchanged code
[I2,map2] = rgb2ind(I, map);
%Now I2 is a indexed image and map2 the corresponding map

Now you display your indexed image I2 without applying a colormap:

imagesc(I2)

Your image contains values 1:n and colormap jet is activated, so you get a rainbow.

Possibilities to display the correct image are using the map:

imagesc(I2)
colormap(map2)

Or displaying I, which is the original RGB image

imagesc(I)
Daniel
  • 36,610
  • 3
  • 36
  • 69