We need to use the command rgb2ind to do this.
A conversion like
[X,map] = rgb2ind(aa, n) %% here aa is a rgb color image. n is an integer less than or equal to 65536.
By using this operation we can reduce a RGB having almost 256*256*256 possible colors into an index image X with colormap map having the colors of RGB image mapped to the nearest color values.
Further making changes into the colour map.
Suppose we need to change the total colours into say n.
We can do the following:
[X, map] = imread('image.jpg');
[Y, newmap] = imapprox(X, map, n); % n is the new number of colours (a positive int value)
Converting the Ind Image into Grayscale:
ind2gray removes the hue and saturation information from the input image while retaining the intensity portion(i.e the luminiscence).
grayim = ind2gray(X,map);