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.m302 chars14 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