Main Content

Convert 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 a Bayer pattern encoded image to a 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.

im2gray

Convert an RGB image to a grayscale image.

The im2gray function also accepts single-channel grayscale input images and returns them unchanged. Use this function instead of rgb2gray when you want a preprocessing algorithm to accept both grayscale and RGB images.

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.

Unlike the im2rgb function, the rgb2gray function requires that the input image have three color channels.

rgb2ind

Convert a truecolor image to an indexed image.

Some images use color spaces other than the RGB color space, such as the HSV color space. To work with these images, first convert the image to the RGB color space, process the image, and then convert it back to the original color space. For more information about color space conversion routines, see Understanding Color Spaces and Color Space Conversion.

Related Topics