This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Convert image to 8-bit unsigned integers


  • I2 = im2uint8(I)
  • RGB2 = im2uint8(RGB)
  • I = im2uint8(BW)
  • X2 = im2uint8(X,'indexed')
  • gpuarrayB = im2uint8(gpuarrayA,___)



I2 = im2uint8(I) converts the grayscale image I to 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.

RGB2 = im2uint8(RGB) converts the truecolor image RGB 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(X,'indexed') converts the indexed image X to uint8, offsetting the data if necessary. Note that it is not always possible to convert an indexed image to uint8. If X is of class double, the maximum value of X must be 256 or less. If X is of class uint16, the maximum value of X 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 indexed gpuArray image. The output image is a gpuArray. This syntax requires the Parallel Computing Toolbox™.

Code Generation support: Yes.

MATLAB Function Block support: Yes.


collapse all

Create an array of class uint16.

I = reshape(uint16(linspace(0,65535,25)),[5 5])
I =

  5×5 uint16 matrix

       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

Convert the array to class uint8 .

I2 = im2uint8(I)
I2 =

  5×5 uint8 matrix

     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

Create array of class uint16.

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

Convert array to uint8.

I2 = im2uint8(I1);

Input Arguments

collapse all

Input grayscale image, specified as a real, nonsparse, numeric array.

Example: I = imread('cameraman.tif');

Data Types: single | double | int16 | uint8

Truecolor image, specified as a real, nonsparse, numeric array.

Example: RGB = imread('peppers.png');

Data Types: single | double | int16 | uint8

Binary image, specified as a real, nonsparse, logical array.

Example: BW = imread('text.png');

Data Types: logical

Indexed image, specified as a real, nonsparse, numeric array.

Example: [X,map] = imread('trees.tif');

Data Types: double | uint8 | uint16

Input image, specified as a gpuArray.

Example: I = gpuArray(imread('cameraman.tif'));

Output Arguments

collapse all

Grayscale image, returned as a uint8 array.

Truecolor image, returned as a uint8 array.

Output indexed image, returned as a uint8 numeric array.

Output image, returned as a gpuArray.

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.

Introduced before R2006a

Was this topic helpful?