MATLAB: How to convert the RGB image to grayscale without using the Image Processing Toolbox

grayscaleimageImage Processing Toolboxintensityrgbrgb2grayrgb2ind

I read a TrueColor image into MATLAB as a 3D RGB matrix. I would like to convert it to grayscale so that I can analyze the intensities, or modify the colormap according to intensity.

Best Answer

To convert an RGB image to grayscale, you can use the RGB2GRAY command from the Image Processing Toolbox. If you do not have this toolbox, then you can use the standard NTSC conversion formula that is used for calculating the effective luminance of a pixel:
intensity = 0.2989*red + 0.5870*green + 0.1140*blue
The following code can be used as an example:
% Assume you have an RGB image of class double, or create a random one
rgb = rand(200,200,3);
% Convert it
gray = 0.2989 * rgb(:,:,1) + 0.5870 * rgb(:,:,2) + 0.1140 * rgb(:,:,3);