Graphics Previous page   Next Page

Converting the Data or Graphic Type of Images

Converting between data types changes the way MATLAB interprets the image data. If you want the resulting array to be interpreted properly as image data, you need to rescale or offset the data when you convert it. (See the earlier sections Image Types and 8-Bit and 16-Bit Indexed Images for more information about offsets.)

For certain operations, it is helpful to convert an image to a different image type. For example, if you want to filter a color image that is stored as an indexed image, you should first convert it to RGB format. To do this efficiently, use the ind2rgb function, which originated in the Image Processing Toolbox. When you apply the filter to the RGB 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 may not be meaningful.

You can also perform certain conversions just using MATLAB syntax. For example, if you want to convert a grayscale image to RGB, you can concatenate three copies of the original matrix along the third dimension.

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

Sometimes you will want to change the graphics format of an image, perhaps for compatibility with another software product. This process is very straightforward. For example, to convert an image from a BMP to a PNG, load the BMP using imread, set the data type to uint8, uint16, or double, and then save the image using imwrite, with 'PNG' specified as your target format. See imread and imwrite for the specifics of which bit depths are supported for the different graphics formats, and for how to specify the format type when writing an image to file.


Previous page  Printing Images Printing and Exporting Next page

© 1994-2005 The MathWorks, Inc.