Documentation Center

  • Trial Software
  • Product Updates

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.

The following section provides a step-by-step example of using Tiff object methods and properties to read subimages from a TIFF file. To get the most out of the Tiff object, you must be familiar with the TIFF specification and technical notes. View this documentation at 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 can also 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 can then 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 Subdirectories in a TIFF File, 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 returns an array of byte offsets that specify the location of subIFDs:

    offsets = t.getTag('SubIFD')
  3. 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:

    t.setSubDirectory(offsets(1));
    
  4. Read the image data from the current IFD (the first subIFD) as you would with any other IFD in the file:

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

    imagesc(subimage_one)
    
  6. To view the second subimage, call the setSubDirectory method again, specifying the byte offset of the second subIFD:

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

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

    imagesc(subimage_two)
    
  9. Close the Tiff object.

    t.close();
    
Was this topic helpful?