Read Image Data into the Workspace

This example shows to read image data from a graphics file into the MATLAB workspace using the imread function.

Read a truecolor image into the workspace. The example reads the image data from a graphics file that uses JPEG format.

RGB = imread('football.jpg');

If the image file format uses 8-bit pixels, imread returns the image data as an m-by-n-by-3 array of uint8 values. For graphics file formats that support 16-bit data, such as PNG and TIFF, imread returns an array of uint16 values.

whos
  Name        Size                Bytes  Class    Attributes

  RGB       256x320x3            245760  uint8              

Read a grayscale image into the workspace. The example reads the image data from a graphics file that uses the TIFF format. imread returns the grayscale image as an m-by-n array of uint8 values.

I = imread('cameraman.tif');
whos
  Name        Size                Bytes  Class    Attributes

  I         256x256               65536  uint8              
  RGB       256x320x3            245760  uint8              

Read an indexed image into the workspace. imread uses two variables to store an indexed image in the workspace: one for the image and another for its associated colormap. imread always reads the colormap into a matrix of class double, even though the image array itself may be of class uint8 or uint16.

[X,map] = imread('trees.tif');
whos
  Name        Size                Bytes  Class     Attributes

  I         256x256               65536  uint8               
  RGB       256x320x3            245760  uint8               
  X         258x350               90300  uint8               
  map       256x3                  6144  double              

In these examples, imread infers the file format to use from the contents of the file. You can also specify the file format as an argument to imread. imread supports many common graphics file formats, such as the Graphics Interchange Format (GIF), Joint Photographic Experts Group (JPEG), Portable Network Graphics (PNG), and Tagged Image File Format (TIFF) formats. For the latest information concerning the bit depths and image formats supported, see imread and imformats reference pages.

pep = imread('peppers.png','png');
whos
  Name        Size                Bytes  Class     Attributes

  I         256x256               65536  uint8               
  RGB       256x320x3            245760  uint8               
  X         258x350               90300  uint8               
  map       256x3                  6144  double              
  pep       384x512x3            589824  uint8               

See Also

Related Topics