Image Processing Toolbox    

Converting The Storage Class of Images

If you want to perform operations that are not supported for uint8 or uint16 arrays, you can convert the data to double precision using the MATLAB function, double. For example,

However, converting between storage classes changes the way MATLAB and the toolbox interpret 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.

For easier conversion of storage classes, use one of these toolbox functions: im2double, im2uint8, and im2uint16. These functions automatically handle the rescaling and offsetting of the original data. For example, this command converts a double-precision RGB image with data in the range [0,1] to a uint8 RGB image with data in the range [0,255].

Note that when you convert from one class to another that uses fewer bits to represent numbers, you generally lose some of the information in your image. For example, a uint16 intensity image is capable of storing up to 65,536 distinct shades of gray, but a uint8 intensity image can store only 256 distinct shades of gray. If you convert a uint16 intensity image to a uint8 intensity image, im2uint8 must quantize the gray shades in the original image. In other words, all values from 0 to 128 in the original image become 0 in the uint8 image, values from 129 to 385 all become 1, and so on. This loss of information is often not a problem, however, since 256 still exceeds the number of shades of gray that your eye is likely to discern.

In an indexed image, the image matrix contains only indices into a colormap, rather than the color data itself, so there is no quantization of the color data possible during the conversion. Therefore, it is not always possible to convert an indexed image from one storage class to another. For example, a uint16 or double indexed image with 300 colors cannot be converted to uint8, because uint8 arrays have only 256 distinct values. If you want to perform this conversion, you must first reduce the number of the colors in the image using the imapprox function. This function performs the quantization on the colors in the colormap, to reduce the number of distinct colors in the image. See Using imapprox for more information. For more information on the storage class conversion functions see the reference pages for im2double, im2uint8, im2uint16.

Turning the Logical Flag on or off

As discussed in Binary Images, a uint8 binary image must have its logical flag on. If you use im2uint8 to convert a binary image of class double to uint8, this flag is turned on automatically. If you do the conversion manually, however, you must use the logical function to turn on the logical flag. For example,

To turn the logical flag off, you can use the unary plus operator. For example, if A is a uint8 logical array,


 Working with uint8 and uint16 Data Converting the Graphics File Format of an Image