DICOM Files

Overview of DICOM Support

The Digital Imaging and Communications in Medicine (DICOM) Standard is a joint project of the American College of Radiology (ACR) and the National Electrical Manufacturers Association (NEMA). The standard facilitates interoperability of medical imaging equipment by specifying a set of media storage services, a file format, and a medical directory structure to facilitate access to the images and related information stored on interchange media. For detailed information about the standard, see the official DICOM web site.

MATLAB® provides the following support for working with files in the DICOM format:

  • Reading any file that complies with the DICOM standard

  • Writing three different types of DICOM files, or DICOM information objects (IOD), with validation:

    • Secondary capture (default)

    • Magnetic resonance

    • Computed tomography

  • Writing many more types of DICOM files without validation, by setting the createmode flag to 'copy' when writing data to a file.

    Note:   MATLAB supports working with DICOM files. There is no support for working with DICOM network capabilities.

Read Metadata from a DICOM File

DICOM files contain metadata that provide information about the image data, such as the size, dimensions, bit depth, modality used to create the data, and equipment settings used to capture the image. To read metadata from a DICOM file, use the dicominfo function. dicominfo returns the information in a MATLAB structure where every field contains 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 — see Read Image Data from DICOM Files.

The following example reads the metadata from a sample DICOM file that is included with the toolbox.

info = dicominfo('CT-MONO2-16-ankle.dcm')

info = 

                          Filename: [1x89 char]
                       FileModDate: '18-Dec-2000 11:06:43'
                          FileSize: 525436
                            Format: 'DICOM'
                     FormatVersion: 3
                             Width: 512
                            Height: 512
                          BitDepth: 16
                         ColorType: 'grayscale'
    FileMetaInformationGroupLength: 192
        FileMetaInformationVersion: [2x1 uint8]
           MediaStorageSOPClassUID: '1.2.840.10008.5.1.4.1.1.7'
        MediaStorageSOPInstanceUID: [1x50 char]
                 TransferSyntaxUID: '1.2.840.10008.1.2'
            ImplementationClassUID: '1.2.840.113619.6.5'
                                .
                                .
                                .

Private DICOM Metadata

The DICOM specification defines many of these metadata fields, but files can contain additional fields, called private metadata. This private metadata is typically defined by equipment vendors to provide additional information about the data they provide.

When dicominfo encounters a private metadata field in a DICOM file, it returns the metadata creating a generic name for the field based on the group and element tags of the metadata. For example, if the file contained private metadata at group 0009 and element 0006, dicominfo creates the name:Private_0009_0006. dicominfo attempts to interpret the private metadata, if it can. For example, if the metadata is a text string, dicominfo processes the data. If it can't interpret the data, dicominfo returns a sequence of bytes.

If you need to process a DICOM file created by a manufacturer that uses private metadata, and you prefer to view the correct name of the field as well as the data, you can create your own copy of the DICOM data dictionary and update it to include definitions of the private metadata. You will need information about the private metadata that vendors typically provide in DICOM compliance statements. For more information about updating DICOM dictionary, see Create Your Own Copy of DICOM Dictionary.

Create Your Own Copy of DICOM Dictionary

MathWorks uses a DICOM dictionary that contains definitions of thousands of standard DICOM metadata fields. If your DICOM file contains metadata that is not defined this dictionary, you can update the dictionary, creating your own copy that it includes these private metadata fields.

To create your own dictionary, perform this procedure:

  1. Make a copy of the text version of the DICOM dictionary that is included with MATLAB. This file, called dicom-dict.txt is located in matlabroot/toolbox/images/medformats or matlabroot/toolbox/images/iptformats depending on which version of the Image Processing Toolbox software you are working with. Do not attempt to edit the MAT-file version of the dictionary, dicom-dict.mat.

  2. Edit your copy of the DICOM dictionary, adding entries for the metadata. Insert the new metadata field using the group and element tag, type, and other information. Follow the format of the other entries in the file. The creator of the metadata (e.g., an equipment vendor) must provide you with the information.

  3. Save your copy of the dictionary.

  4. Set MATLAB to use your copy of the DICOM dictionary, dicomdict function.

Read Image Data from DICOM Files

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.

When using dicomread, you can specify the filename as an argument, as in the following example. The example reads the sample DICOM file that is included with the toolbox.

I = dicomread('CT-MONO2-16-ankle.dcm');

You can also use the metadata structure returned by dicominfo to specify the file you want to read, as in the following example.

info = dicominfo('CT-MONO2-16-ankle.dcm');
I = dicomread(info);

View DICOM Images

To view the image data imported from a DICOM file, use one of the toolbox image display functions imshow or imtool. Note, however, that because the image data in this DICOM file is signed 16-bit data, you must use the autoscaling syntax with either display function to make the image viewable.

imshow(I,'DisplayRange',[])

Write Data to DICOM File

To write image data or metadata to a file in DICOM format, use the dicomwrite function. This example writes the image I to the DICOM file ankle.dcm.

dicomwrite(I,'ankle.dcm')

Include Metadata with Image Data

When writing image data to a DICOM file, dicomwrite automatically includes the minimum set of metadata fields required by the type of DICOM information object (IOD) you are creating. dicomwrite supports the following DICOM IODs with full validation.

  • Secondary capture (default)

  • Magnetic resonance

  • Computed tomography

dicomwrite can write many other types of DICOM data (e.g., X-ray, radiotherapy, nuclear medicine) to a file; however, dicomwrite does not perform any validation of this data. See dicomwrite for more information.

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 the following example, the dicomwrite function writes the relevant information in the metadata structure info to the new DICOM file.

info = dicominfo('CT-MONO2-16-ankle.dcm');
I = dicomread(info);
dicomwrite(I,'ankle.dcm',info)

Note that the metadata written to the file is not identical to the metadata in the info structure. When writing metadata to a file, there are certain fields that dicomwrite must update. To illustrate, look at the instance ID in the original metadata and compare it with the ID in the new file.

info.SOPInstanceUID
ans =

1.2.840.113619.2.1.2411.1031152382.365.1.736169244

Now, read the metadata from the newly created DICOM file, using dicominfo, and check the SOPInstanceUID field.

info2 = dicominfo('ankle.dcm');

info2.SOPInstanceUID

ans =

1.2.841.113411.2.1.2411.10311244477.365.1.63874544

Note that the instance ID in the newly created file differs from the ID in the original file.

Explicit Versus Implicit VR Attributes

DICOM attributes provide the length and then the data. When writing data to a file, you can include a two-letter value representation (VR) with the attribute or you can let DICOM infer the value representation from the data dictionary. When you specify the VR option with the value 'explicit', the dicomwrite function includes the VR in the attribute. The following figure shows the attributes with and without the VR.

Remove Confidential Information from a DICOM File

When using a DICOM file as part of a training set, blinded study, or a presentation, you might want to remove confidential patient information, a process called anonymizing the file. To do this, use the dicomanon function.

The dicomanon function creates a new series with new study values, changes some of the metadata, and then writes the file. For example, you could replace steps 4, 5, and 6 in the example in Create a New DICOM Series with a call to the dicomanon function.

Create a New DICOM 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. The following example illustrates this process.

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

    I = dicomread('CT-MONO2-16-ankle.dcm');

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

    imtool(I,'DisplayRange',[])

  2. Read the metadata from the same DICOM file.

    info = dicominfo('CT-MONO2-16-ankle.dcm');

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

    info.SeriesInstanceUID
    
    ans =
    
    1.2.840.113619.2.1.2411.1031152382.365.736169244
  3. 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.

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

    max(I(:))
    ans =
    
        4080
    
    min(I(:))
    
    ans =
    
        32

    To remove these text characters, the example sets all pixels with the maximum value to the minimum value.

    Imodified = I;
    Imodified(Imodified == 4080) = 32;

    View the processed image.

    imshow(Imodified,[])

  4. 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.

    uid = dicomuid
    
    uid =
    
    1.3.6.1.4.1.9590.100.1.1.56461980611264497732341403390561061497

    dicomuid is guaranteed to generate a unique UID.

  5. Set the value of the SeriesInstanceUID field in the metadata associated with the original DICOM file to the generated value.

    info.SeriesInstanceUID = uid;
  6. 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.

    dicomwrite(Imodified,'ankle_newseries.dcm',info);

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

For information about the syntax variations that specify nondefault spatial coordinates, see the reference page for imshow.

Was this topic helpful?