Convert image to 8-bit unsigned integers


I2 = im2uint8(I1)
RGB2 = im2uint8(RGB1)
I = im2uint8(BW)
X2 = im2uint8(X1,'indexed')
gpuarrayB = im2uint8(gpuarrayA,___)


im2uint8 takes an image as input and returns an image of class uint8. If the input image is of class uint8, the output image is identical to the input image. If the input image is not uint8, im2uint8 returns the equivalent image of class uint8, rescaling or offsetting the data as necessary.

I2 = im2uint8(I1) converts the grayscale image I1 to uint8, rescaling the data if necessary.

RGB2 = im2uint8(RGB1) converts the truecolor image RGB1 to uint8, rescaling the data if necessary.

I = im2uint8(BW) converts the binary image BW to a uint8 grayscale image, changing 1-valued elements to 255.

X2 = im2uint8(X1,'indexed') converts the indexed image X1 to uint8, offsetting the data if necessary. Note that it is not always possible to convert an indexed image to uint8. If X1 is of class double, the maximum value of X1 must be 256 or less; if X1 is of class uint16, the maximum value of X1 must be 255 or less.

gpuarrayB = im2uint8(gpuarrayA,___) performs the conversion on a GPU. The input image, gpuarrayA, can be a grayscale, truecolor, binary, or index gpuArray image. The output image is a gpuArray. This syntax requires the Parallel Computing Toolbox™.

Code Generation

im2uint8 supports the generation of efficient, production-quality C/C++ code from MATLAB. Generated code for this function uses a precompiled platform-specific shared library. To see a complete list of toolbox functions that support code generation, see List of Supported Functions with Usage Notes.

Class Support

Grayscale and truecolor images can be uint8, uint16, int16, single, double, or logical. Indexed images can be uint8, uint16, double, or logical. Binary input images must be logical. The output image is uint8.

If input gpuArray gpuarrayA is an ntensity or truecolor image, it can be uint8, uint16, int16, logical, single, or double. If gpuarrayA is an indexed image, it can be uint8, uint16, double or logical. If gpuarrayA is a binary image, it must be logical. The output gpuArray image is uint8.


Convert array to uint8.

I1 = reshape(uint16(linspace(0,65535,25)),[5 5])
I2 = im2uint8(I1)
I1 =

      0  13653  27306  40959  54613
   2731  16384  30037  43690  57343
   5461  19114  32768  46421  60074
   8192  21845  35498  49151  62804
  10923  24576  38229  51882  65535

I2 =

    0   53  106  159  213
   11   64  117  170  223
   21   74  128  181  234
   32   85  138  191  244
   43   96  149  202  255

Convert array to uint8 on a GPU.

I1 = gpuArray(reshape(uint16(linspace(0,65535,25)),[5 5]))
I2 = im2uint8(I1)
Was this topic helpful?