Main Content

Importing Images

To import data into the MATLAB® workspace from a graphics file, use the imread function. Using this function, you can import data from files in many standard file formats, including the Tagged Image File Format (TIFF), Graphics Interchange Format (GIF), Joint Photographic Experts Group (JPEG), and Portable Network Graphics (PNG) formats. For a complete list of supported formats, see the imread reference page.

This example reads the image data stored in a file in JPEG format into the MATLAB workspace as the array I:

I = imread('ngc6543a.jpg');

imread represents the image in the workspace as a multidimensional array of class uint8. The dimensions of the array depend on the format of the data. For example, imread uses three dimensions to represent RGB color images:

whos I
  Name      Size                           Bytes  Class

  I       650x600x3                      1170000  uint8 array

Grand total is 1170000 elements using 1170000 bytes

For more control over reading TIFF files, use the Tiff object—see Reading Image Data and Metadata from TIFF Files for more information.

Getting Information About Image Files

If you have a file in a standard graphics format, use the imfinfo function to get information about its contents. The imfinfo function returns a structure containing information about the file. The fields in the structure vary with the file format, but imfinfo always returns some basic information including the file name, last modification date, file size, and format.

This example returns information about a file in Joint Photographic Experts Group (JPEG) format:

info = imfinfo('ngc6543a.jpg')
info = 

           Filename: 'matlabroot\toolbox\matlab\demos\ngc6543a.jpg'
        FileModDate: '01-Oct-1996 16:19:44'
           FileSize: 27387
             Format: 'jpg'
      FormatVersion: ''
              Width: 600
             Height: 650
           BitDepth: 24
          ColorType: 'truecolor'
    FormatSignature: ''
    NumberOfSamples: 3
       CodingMethod: 'Huffman'
      CodingProcess: 'Sequential'
            Comment: {'CREATOR: XV Version 3.00b  Rev: 6/15/94  Quality =...'}

Reading Image Data and Metadata from TIFF Files

While you can use imread to import image data and metadata from TIFF files, the function does have some limitations. For example, a TIFF file can contain multiple images and each images can have multiple subimages. While you can read all the images from a multi-image TIFF file with imread, you cannot access the subimages. Using the Tiff object, you can read image data, metadata, and subimages from a TIFF file. When you construct a Tiff object, it represents your connection with a TIFF file and provides access to many of the routines in the LibTIFF library.

A step-by-step example of using Tiff object methods and properties to read subimages from a TIFF file follows. To get the most out of the Tiff object, familiarize yourself with the TIFF specification and technical notes. See LibTIFF - TIFF Library and Utilities.

Reading Subimages from a TIFF File

A TIFF file can contain one or more image file directories (IFD). Each IFD contains image data and the metadata (tags) associated with the image. Each IFD can contain one or more subIFDs, which also can contain image data and metadata. These subimages are typically reduced-resolution (thumbnail) versions of the image data in the IFD containing the subIFDs.

To read the subimages in an IFD, you must get the location of the subimage from the SubIFD tag. The SubIFD tag contains an array of byte offsets that point to the subimages. You then can pass the address of the subIFD to the setSubDirectory method to make the subIFD the current IFD. Most Tiff object methods operate on the current IFD.

  1. Open a TIFF file that contains images and subimages using the Tiff object constructor. This example uses the TIFF file created in Creating TIFF File Subdirectories, which contains one IFD directory with two subIFDs. The Tiff constructor opens the TIFF file, and makes the first subIFD in the file the current IFD:

    t = Tiff('my_subimage_file.tif','r');
  2. Retrieve the locations of subIFDs associated with the current IFD. Use the getTag method to get the value of the SubIFD tag. This method returns an array of byte offsets that specify the location of subIFDs:

    offsets = getTag(t,'SubIFD')
  3. Navigate to the first subimage. First, set the currentIFD to the directory containing the first subimage:

    dirNum = 1; 
    setDirectory(t,dirNum);
  4. Then, navigate to the first subIFD using the setSubDirectory method. Specify the byte offset of the subIFD as an argument. This call makes the subIFD the current IFD:

    setSubDirectory(t,offsets(1));
    
  5. Read the image data from the current IFD (the first subIFD) the same way you read any other IFD in the file:

    subimage_one = read(t);
    
  6. View the first subimage:

    imagesc(subimage_one)
    
  7. Navigate to the second subimage. First, reset the currentIFD to the directory containing the second subimage:

    setDirectory(t,dirNum);
  8. Then, navigate to the second subIFD using the setSubDirectory method. Specify the byte offset of the second subIFD:

    setSubDirectory(t,offsets(2));
    
  9. Read the image data from the current IFD (the second subIFD) as you would with any other IFD in the file:

    subimage_two = read(t);
    
  10. View the second subimage:

    imagesc(subimage_two)
    
  11. Close the Tiff object:

    close(t);
    

See Also

External Websites