Main Content

Write Image Data to DICOM Files

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 (such as X-ray, radiotherapy, or nuclear medicine) to a file. However, dicomwrite does not perform any validation of this data.

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.

Specify Value Representation

Each field of DICOM metadata (known as an attribute or data element), includes a tag that identifies the attribute, information about the length of the attribute, and the attribute data. The attribute optionally includes a two-letter value representation (VR) that identifies the format of the attribute data. For example, the format can be a single-precision binary floating point number, a character vector that represents a decimal integer, or a character vector in the format of a date-time.

To include the VR in the attribute when using dicomwrite, specify the VR name-value argument as "explicit". If you do not specify the VR, then dicomwrite infers the value representation from the data dictionary.

The figure shows an attribute with and without the VR.

See Also

Apps

Functions

Objects

Related Topics