Image Processing Toolbox Previous page   Next Page

Reading and Writing DICOM Files

The Image Processing Toolbox includes support for working with image data in Digital Imaging and Communications in Medicine (DICOM) format. The following sections describe how to

To see an example that reads both the image data and metadata from a DICOM file, modifies the image data, and writes the modified data to a new DICOM file, see Example: Creating a New Series. The example shows how to use the dicomuid function to generate a DICOM unique identifier, which you need to create a new series.

Reading Image Data from a DICOM File

To read image data from a DICOM file, use the dicomread function. The dicomread function reads files that comply with the DICOM specification but can also read certain common noncomplying files.

This example reads an image from a sample DICOM file included with the toolbox.

To view the image data, use one of the toolbox image display functions, imshow or imview. (Because the image data is signed 16-bit data, you must use the autoscaling syntax with either display function.)

Reading Metadata from a DICOM File

DICOM files include information, called metadata, that describes characteristics of the image data it contains, such as size, dimensions, and bit depth. In addition, the DICOM specification defines numerous other metadata fields that describe many other characteristics of the data, such as the modality used to create the data, the equipment settings used to capture the image, and information about the study. The dicomread function can process almost all the metadata fields defined by the DICOM specification.

To read metadata from a DICOM file, use the dicominfo function. This function returns the metadata as a structure, where every field in the structure is a specific piece of DICOM metadata.

You can use the metadata structure returned by dicominfo to specify the DICOM file you want to read using dicomread. For example, you can use this code to read metadata from the sample DICOM file and then pass the metadata to dicomread to read the image from the file.

Writing Data to a DICOM File

To write image data to a file in DICOM format, use the dicomwrite function.

This example writes the image I to the DICOM file ankle.dcm.

Writing Metadata to a DICOM File

When you write image data to a DICOM file, dicomwrite includes the minimum set of metadata fields required by the type of DICOM information object (IOD) you are creating. dicomwrite supports three types of DICOM IODs:

You can also specify the metadata you want to write to the file by passing to dicomwrite an existing DICOM metadata structure that you retrieved using dicominfo.

In this case, the dicomwrite function writes the information in the metadata structure info to the new DICOM file. When writing metadata to a file, there are certain fields that dicomwrite must update. For example, dicomwrite must update the file modification date in the new file. To illustrate, compare the file modification date in the original metadata with the file modification date in the new file.

Using dicominfo, read the metadata from the newly written file and check the file modification date.

Example: Creating a New Series

When writing a modified image to a DICOM file, you might want to make the modified image the start of a new series. In the DICOM standard, images can be organized into series. When you write an image with metadata to a DICOM file, dicomwrite puts the image in the same series by default. To create a new series, you must assign a new DICOM unique identifier to the SeriesInstanceUID metadata field. This example illustrates this process.

  1. Read an image from a DICOM file into the MATLAB workspace.

  1. To view the image, use either of the toolbox display functions, imshow or imview. Because the DICOM image data is signed 16-bit data, you must use the autoscaling syntax.

  1. Read the metadata from the same DICOM file.

  1. To identify which series an image belongs to, view the value of the SeriesInstanceUID field.

  1. Because you typically only start a new DICOM series when you modify the image in some way, this example removes all the text from the image.

  1. The example finds the maximum and minimum values of a pixels in the image. The pixels that form the white text characters are set to the maximum pixel value.

    To remove them, the example sets all pixels with the maximum value to the minimum value.

    View the processed image.

  1. Generate a new DICOM unique identifier (UID) using the dicomuid function. You need a new UID to write the modified image as a new series.

  1. dicomuid is guaranteed to generate a unique UID.

  1. Set the value of the SeriesInstanceUID field in the metadata associated with the original DICOM file to the generated value.
  2. Write the modified image to a new DICOM file, specifying the modified metadata structure, info, as an argument. Because you set the SeriesInstanceUID value, the image you write is part of a new series.

  1. To verify this operation, view the image and the SeriesInstanceUID metadata field in the new file.


Previous page  Converting Graphics File Formats Image Arithmetic Next page

Learn more about the latest releases of MathWorks products:

 © 1994-2009 The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS