Converting Between Image Types

The toolbox includes many functions that you can use to convert an image from one type to another, listed in the following table. For example, if you want to filter a color image that is stored as an indexed image, you must first convert it to truecolor format. When you apply the filter to the truecolor image, MATLAB® filters the intensity values in the image, as is appropriate. If you attempt to filter the indexed image, MATLAB simply applies the filter to the indices in the indexed image matrix, and the results might not be meaningful.

You can perform certain conversions just using MATLAB syntax. For example, you can convert a grayscale image to truecolor format by concatenating three copies of the original matrix along the third dimension.

RGB = cat(3,I,I,I);

The resulting truecolor image has identical matrices for the red, green, and blue planes, so the image displays as shades of gray.

In addition to these image type conversion functions, there are other functions that return a different image type as part of the operation they perform. For example, the region of interest functions return a binary image that you can use to mask an image for filtering or for other operations.

    Note   When you convert an image from one format to another, the resulting image might look different from the original. For example, if you convert a color indexed image to a grayscale image, the resulting image displays as shades of grays, not color.

Function

Description

demosaicConvert Bayer pattern encoded image to truecolor (RGB) image.

dither

Use dithering to convert a grayscale image to a binary image or to convert a truecolor image to an indexed image.

gray2ind

Convert a grayscale image to an indexed image.

grayslice

Convert a grayscale image to an indexed image by using multilevel thresholding.

im2bw

Convert a grayscale image, indexed image, or truecolor image, to a binary image, based on a luminance threshold.

ind2gray

Convert an indexed image to a grayscale image.

ind2rgb

Convert an indexed image to a truecolor image.

mat2gray

Convert a data matrix to a grayscale image, by scaling the data.

rgb2gray

Convert a truecolor image to a grayscale image.

Note: To work with images that use other color spaces, such as HSV, first convert the image to RGB, process the image, and then convert it back to the original color space. For more information about color space conversion routines, see Color.

rgb2ind

Convert a truecolor image to an indexed image.

Was this topic helpful?