MATLAB: How to use function ‘ind2gray’

colormaperrorImage Processing Toolboxind2gray

I'm trying to convert uint8 image to grayscale image using the following command
a = imread('image.png'); % a dimension 18*24*3
b = ind2gray(a, map);
But I'm getting an error message saying
Undefined function or variable 'map'

Best Answer

You need to have some way to tell the function what color should get mapped to what gray level. The colors are a 2D array with columns for red green, and blue in the range 0-1, and one row for each gray level or range of gray levels. There are several built-in color maps to select from - see the help for colormap. For example you could do
rgbImage = ind2rgb(grayImage, jet(256));
colorbar;
jet() is a function that builds a colormap of a certain pattern with the number of gray levels you specify. There are others too. Or you could make up your own colormap completely customized.