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