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=5×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.8.274.1.1.2729954696.96242.3632970675.507" "1.2.826.0.1.3680043.8.274.1.1.7145442384.75872.7982248107.258" "Z:\29\jbernier.Bdoc23b.j2351022.1\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.2.1.1.322987881.621.736170080.681" "1.2.840.113619.2.1.2411.1031152382.365.736169244" "Z:\29\jbernier.Bdoc23b.j2351022.1\matlab\toolbox\images\imdata\CT-MONO2-16-ankle.dcm"
s3 {[03-Oct-2011 19:18:11]} {[03-Oct-2011 18:59:02]} "" "M" "MR" 512 512 1 1 "RIGHT KNEE" "" "1.3.6.1.4.1.9590.100.1.2.320418845013189618318250681693358291211" "1.3.6.1.4.1.9590.100.1.2.287740981712351622214874344032214809569" "Z:\29\jbernier.Bdoc23b.j2351022.1\matlab\toolbox\images\imdata\knee1.dcm"
s4 {[03-Oct-2011 19:18:11]} {[03-Oct-2011 19:05:04]} "" "M" "MR" 512 512 1 1 "RIGHT KNEE" "" "1.3.6.1.4.1.9590.100.1.2.320498134711034521212730362051554545799" "1.3.6.1.4.1.9590.100.1.2.316302984111738034326701385064023497963" "Z:\29\jbernier.Bdoc23b.j2351022.1\matlab\toolbox\images\imdata\knee2.dcm"
s5 {[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" "Z:\29\jbernier.Bdoc23b.j2351022.1\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") mkdir(matFileDir) end
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))," ","_"); else [~,matFileName] = fileparts(dicomFileName); end 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.
try data = dicomreadVolume(collection,collection.Row{idx}); catch ME data = dicomread(dicomFileName); if ndims(data)<4 % Skip files that are not volumes continue; end end
For complete volumes returned in a 4-D array, write the data and the absolute file name to a MAT file.
save(matFileName,"data","dicomFileName");
End the loop over the studies in the collection.
end
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", ... ReadFcn=@matRead);
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); volshow(V);
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}); end
See Also
dicominfo
| dicomread
| dicomreadVolume
| dicomCollection
| imageDatastore
| volshow
Related Topics
- Create Image Datastore Containing DICOM Images
- Preprocess Volumes for Deep Learning (Deep Learning Toolbox)