When using ind2rgb, how do you use map as output argument?
7 views (last 30 days)
I am confused on what the 'map' is and how to create it. When I use the ind2rgb fucntion, I get an error becuase it says I did not use 'map.' I already have searched up the documentation for this and I am still confused on how to use it.
Steven Lord on 1 Jul 2022
The map input to ind2rgb is the colormap for the image. The first input to ind2rgb represents indices into that colormap. So for example if I had a colormap:
map = [1 0 0; 0 1 0; 0 0 1]; % Color 1 is red, 2 is green, 3 is blue
Then my image will be values 1, 2, and 3 indicating whether this part of the image is red, green, or blue respectively.
im = randi(3, [4 4]) % Random image
rgb = ind2rgb(im, map)
All the 1's in im are translated to a value of 1 in the first page of rgb and 0's in the second and third pages. Similarly the 2's in im set the value of the corresponding elements of the second page of rgb to 1 and leave the first and third pages as 0. You can see if you look at im that the first row is blue, green, blue, and blue which you can see if you visualize the image.
More Answers (1)
Cris LaPierre on 1 Jul 2022
It is the colormap to apply to your indexed image. See here. The indeces in the indexed image indicate which row of the colormap to use color that pixel. For a uint8 image, that means your colormap must have 256 rows.
[X,cmap] = imread('corn.tif');
X(1,1) % index of pixel 1
cmap(X(1,1),:) % corresponding color
The easiest way to create it is to use the built in colormaps, though you can create your own in you want.
map = jet(256)