Main Content

Create Image Datastore Containing Single and Multi-File DICOM Volumes

This example shows how to create an image datastore containing volumetric DICOM data stored as a single file and as multiple files.

Specify the location of a directory containing DICOM data. The data includes 2-D images, a 3-D volume in a single file, and a multi-file 3-D volume.

dicomDir = fullfile(matlabroot,'toolbox/images/imdata');

Gather details about the DICOM files by using the dicomCollection function. This function returns the details as a table, where each row represents a single study. For multi-file DICOM volumes, the function aggregates the files into a single study.

collection = dicomCollection(dicomDir,"IncludeSubfolders",true)
collection=6×14 table
               StudyDateTime               SeriesDateTime           PatientName      PatientSex     Modality     Rows    Columns    Channels    Frames    StudyDescription    SeriesDescription                             StudyInstanceUID                                                     SeriesInstanceUID                                                                                  Filenames                                                     
          ________________________    ________________________    _______________    __________    __________    ____    _______    ________    ______    ________________    _________________    __________________________________________________________________    __________________________________________________________________    ___________________________________________________________________________________________________________________

    s1    {0×0 double            }    {0×0 double            }    ""                    ""         "RTSTRUCT"      0         0         0           1      ""                  ""                   "1.2.826.0.1.3680043."       "1.2.826.0.1.3680043."       {["E:\jobarchive\Bdoc20b\2020_04_16_h05m48s37_job1364129_pass\matlab\toolbox\images\imdata\rtstruct.dcm"         ]}
    s2    {[30-Apr-1993 11:27:24]}    {[30-Apr-1993 11:27:24]}    "Anonymized"          ""         "CT"          512       512         1           1      "RT ANKLE"          ""                   "1.2.840.113619."                    "1.2.840.113619.2.1.2411.1031152382.365.736169244"                    {["E:\jobarchive\Bdoc20b\2020_04_16_h05m48s37_job1364129_pass\matlab\toolbox\images\imdata\CT-MONO2-16-ankle.dcm"]}
    s3    {[14-Dec-2013 15:47:31]}    {[14-Dec-2013 15:54:33]}    "GORBERG MITZI"       "F"        "MR"          512       512         1          22      "CSP"               "AX T2"              "1.2.840.113619.2.244.3596.11880862.13689.1386517653.214"             "1.2.840.113619.2.244.3596.11880862.13689.1386517653.217"             {22×1 string                                                                                                      }
    s4    {[03-Oct-2011 19:18:11]}    {[03-Oct-2011 18:59:02]}    ""                    "M"        "MR"          512       512         1           1      "RIGHT KNEE"        ""                   ""    ""    {["E:\jobarchive\Bdoc20b\2020_04_16_h05m48s37_job1364129_pass\matlab\toolbox\images\imdata\knee1.dcm"            ]}
    s5    {[03-Oct-2011 19:18:11]}    {[03-Oct-2011 19:05:04]}    ""                    "M"        "MR"          512       512         1           1      "RIGHT KNEE"        ""                   ""    ""    {["E:\jobarchive\Bdoc20b\2020_04_16_h05m48s37_job1364129_pass\matlab\toolbox\images\imdata\knee2.dcm"            ]}
    s6    {[30-Jan-1994 11:25:01]}    {0×0 double            }    "Anonymized"          ""         "US"          430       600         1          10      "Echocardiogram"    "PS LAX MR & AI"     "999.999.3859744"                                                     "999.999.94827453"                                                    {["E:\jobarchive\Bdoc20b\2020_04_16_h05m48s37_job1364129_pass\matlab\toolbox\images\imdata\US-PAL-8-10x-echo.dcm"]}

Create a temporary directory to store the processed DICOM volumes.

matFileDir = fullfile(pwd,'MATFiles');
if ~exist(matFileDir,'dir')

Loop through each study in the collection.

for idx = 1:size(collection,1)

Get the file names that comprise the study. For multi-file DICOM volumes, the file names are listed as a string array.

    dicomFileName = collection.Filenames{idx};
    if length(dicomFileName) > 1
        matFileName = fileparts(dicomFileName(1));
        matFileName = split(matFileName,filesep);
        matFileName = replace(strtrim(matFileName(end))," ","_");
        [~,matFileName] = fileparts(dicomFileName);
    matFileName = fullfile(matFileDir,matFileName);

Read the data. Try different read functions because the images have a different number of dimensions and are stored in different formats.

1) Try reading the data of the study by using the dicomreadVolume function.

  • If the data is a multi-file volume, then dicomreadVolume runs successfully and returns the complete volume in a single 4-D array. You can add this data to the datastore.

  • If the data is contained in a single file, then dicomreadVolume does not run successfully.

2) Try reading the data of the study by using the dicomread function.

  • If dicomread returns a 4-D array, then the study contains a complete 3-D volume. You can add this data to the datastore.

  • If dicomread returns a 2-D matrix or 3-D array, then the study contains a single 2-D image. Skip this data and continue to the next study in the collection.

        data = dicomreadVolume(collection,collection.Row{idx});
    catch ME
        data = dicomread(dicomFileName);
        if ndims(data)<4
            % Skip files that are not volumes

For complete volumes returned in a 4-D array, write the data and the absolute file name to a MAT file.


End the loop over the studies in the collection.


Create an imageDatastore from the MAT files containing the volumetric DICOM data. Specify the ReadFcn property as the helper function matRead, defined at the end of this example.

imdsdicom = imageDatastore(matFileDir,'FileExtensions','.mat', ...

Read the first DICOM volume from the image datastore.

[V,Vinfo] = read(imdsdicom);
[~,VFileName] = fileparts(Vinfo.Filename);

The DICOM volume is grayscale. Remove the singleton channel dimension by using the squeeze function, then display the volume by using the volshow function.

V = squeeze(V);
ViewPnl = uipanel(figure,'Title',VFileName);

Supporting Functions

The matRead function loads data from the first variable of a MAT file with file name filename.

function data = matRead(filename)
    inp = load(filename);
    f = fields(inp);
    data = inp.(f{1});

See Also

| | | | |

Related Topics