Write Image Data to DICOM Files
To write image data or metadata to a file in DICOM format, use thedicomwrite
function. This example writes the imageI
to the DICOM fileankle.dcm
.
dicomwrite(I,'ankle.dcm')
Include Metadata with Image Data
在编写图像数据到DICOM文件时,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 todicomwrite
an existing DICOM metadata structure that you retrieved usingdicominfo
. In the following example, thedicomwrite
函数写有关备用ion in the metadata structureinfo
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 theinfo
structure. When writing metadata to a file, there are certain fields thatdicomwrite
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, usingdicominfo
, and check theSOPInstanceUID
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 anattributeordata 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 usingdicomwrite
, specify the'VR'
name-value pair argument as'explicit'
. If you do not specify the VR, thendicomwrite
infers the value representation from the data dictionary.
The figure shows an attribute with and without the VR.