Main Content

write

Create selectively modified copy of DICOM file

Since R2023a

    Description

    write(dFile,filename) writes the contents of the dicomFile object dFile to the new DICOM file specified by filename.

    write(dFile,filename,pixelData) writes pixelData as the pixel data of the new DICOM file.

    write(dFile,filename,metadata) modifies the attributes of the new DICOM file as specified by metadata.

    example

    write(dFile,filename,pixelData,metadata) modifies the pixel data and the attributes of the new DICOM file as specified by pixelData and metadata, respectively. You can specify pixelData and metadata in any order.

    Examples

    collapse all

    Import a DICOM file into the workspace. The DICOM file is part of a data set containing three CT volumes. The size of the entire data set is approximately 81 MB. Download the data set from the MathWorks® website, then unzip the folder.

    zipFile = matlab.internal.examples.downloadSupportFile("medical","MedicalVolumeDICOMData.zip");
    filepath = fileparts(zipFile);
    unzip(zipFile,filepath)
    datapath = fullfile(filepath,"MedicalVolumeDICOMData/LungCT01/CT000000.dcm");
    dFile = dicomFile(datapath);

    Get the raw pixel data of the DICOM file by specifying rescale as false.

    pixelData = getPixelData(dFile,rescale=false);

    Filter the pixel data. The filtered pixel data is the pixel data for the new DICOM file. Making the modifications on the raw pixel data instead of the rescaled pixel data ensures that the pixel data is not rescaled repeatedly while getting pixel data from the new DICOM file.

    filteredPixelData = imgaussfilt(pixelData,2);

    Visualize and compare the original and filtered pixel data.

    figure
    imshow([pixelData filteredPixelData],[])

    Create a study description for the new DICOM file. Then, create a structure metadata with a StudyDescription field that contains your new study description.

    newStudyDescription = 'CT CARDIAC CALCIUM SCORING FILTERED';
    metadata = struct("StudyDescription",newStudyDescription)
    metadata = struct with fields:
        StudyDescription: 'CT CARDIAC CALCIUM SCORING FILTERED'
    
    

    Create a modified copy of the CT image DICOM file, replacing its pixel data with the filtered pixel data and its metadata with the structure containing your new study description.

    write(dFile,"CT00000_filtered.dcm",filteredPixelData,metadata)

    Input Arguments

    collapse all

    DICOM file from which to create the selectively modified copy, specified as a dicomFile object.

    Name of the modified DICOM file, specified as a string scalar or character vector.

    Data Types: char | string

    Pixel data for the modified DICOM file, specified as a 2-D numeric matrix or 3-D numeric array.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Metadata for the modified DICOM file, specified as a structure. The names of the fields of the structure must match the names of the attributes to be modified.

    Data Types: struct

    Version History

    Introduced in R2023a