colour image to grayscale in matlab

To convert a color image to grayscale in Matlab, you can use the rgb2gray function. This function converts an RGB image to a grayscale image by taking the weighted average of the red, green, and blue values of each pixel.

Here's an example code snippet that converts a color image to grayscale:

main.m
% Read in a color image
colorImage = imread('myimage.jpg');

% Convert to grayscale
grayImage = rgb2gray(colorImage);

% Display the original and grayscale images side by side
subplot(1, 2, 1);
imshow(colorImage);
title('Original Image');
subplot(1, 2, 2);
imshow(grayImage);
title('Grayscale Image');
302 chars
14 lines

In the code above, we first read in a color image using the imread function. We then call the rgb2gray function to convert the image to grayscale. Finally, we display the original and grayscale images side by side using the subplot and imshow functions.

Note that the rgb2gray function works with RGB images, so if your image is in a different color space (such as CMYK), you'll need to convert it to RGB first using the appropriate function (such as cmyk2rgb).

gistlibby LogSnag