Skip to content

Commit e203358

Browse files
committed
ENH: Read DICOM directory with Python imread
1 parent 3aec776 commit e203358

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

Wrapping/Generators/Python/Tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ itk_python_add_test(NAME PythonComplex COMMAND complex.py)
1919
itk_python_add_test(NAME PythonHelperFunctions COMMAND helpers.py)
2020
itk_python_add_test(NAME PythonLazyModule COMMAND lazy.py)
2121
itk_python_add_test(NAME PythonNoLazyModule COMMAND nolazy.py)
22+
itk_python_add_test(NAME PythonDICOMSeries COMMAND dicomSeries.py
23+
DATA{${ITK_DATA_ROOT}/Input/DicomSeries/Image0075.dcm}
24+
DATA{${ITK_DATA_ROOT}/Input/DicomSeries/Image0076.dcm}
25+
DATA{${ITK_DATA_ROOT}/Input/DicomSeries/Image0077.dcm})
2226

2327
# some tests will fail if dim=2 and unsigned short are not wrapped
2428
INTERSECTION(WRAP_2 2 "${ITK_WRAP_IMAGE_DIMS}")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#==========================================================================
2+
#
3+
# Copyright NumFOCUS
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0.txt
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
#==========================================================================*/
18+
19+
import itk
20+
import os
21+
import argparse
22+
23+
parser = argparse.ArgumentParser(description='dicomSeries.py dicom_volume_series_files')
24+
parser.add_argument('files', metavar='file', type=str, nargs='+', help='a file in a DICOM volume series')
25+
args = parser.parse_args()
26+
dicom_dir = os.path.dirname(args.files[0])
27+
image = itk.imread(dicom_dir)

Wrapping/Generators/Python/itkExtras.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,21 @@ def imread(filename, pixel_type=None, fallback_only=False):
642642
643643
The reader is instantiated with the image type of the image file if
644644
`pixel_type` is not provided (default). The dimension of the image is
645-
automatically found. If the given filename is a list or a tuple, the
646-
reader will use an itk.ImageSeriesReader object to read the files.
645+
automatically found
646+
647+
If the filename provided is a directory then the directory is assumed to
648+
be for a DICOM series volume. If there is exactly one DICOM series
649+
volume in that directory, the reader will use an itk.ImageSeriesReader
650+
object to read the the DICOM filenames within that directory.
651+
652+
If the given filename is a list or a tuple of file names, the reader
653+
will use an itk.ImageSeriesReader object to read the files.
647654
648655
If `fallback_only` is set to `True`, `imread()` will first try to
649656
automatically deduce the image pixel_type, and only use the given
650-
`pixel_type` if automatic deduction fails. Failures typically
651-
happen if the pixel type is not supported (e.g. it is not currently
652-
wrapped).
657+
`pixel_type` if automatic deduction fails. Failures typically happen if
658+
the pixel type is not supported (e.g. it is not currently wrapped).
659+
653660
"""
654661
import itk
655662
import itkTemplate
@@ -663,6 +670,21 @@ def imread(filename, pixel_type=None, fallback_only=False):
663670
return imread(filename)
664671
except (KeyError, itkTemplate.TemplateTypeError):
665672
pass
673+
if type(filename) not in [list, tuple]:
674+
import os
675+
if os.path.isdir(filename):
676+
# read DICOM series of 1 image in a folder, refer to: https://github.com/RSIP-Vision/medio
677+
names_generator = itk.GDCMSeriesFileNames.New()
678+
names_generator.SetUseSeriesDetails(True)
679+
names_generator.AddSeriesRestriction("0008|0021") # Series Date
680+
names_generator.SetDirectory(filename)
681+
series_uid = names_generator.GetSeriesUIDs()
682+
if len(series_uid) == 0:
683+
raise FileNotFoundError(f"no DICOMs in: {name}.")
684+
if len(series_uid) > 1:
685+
raise OSError(f"the directory: {name} contains more than one DICOM series.")
686+
series_identifier = series_uid[0]
687+
filename = names_generator.GetFileNames(series_identifier)
666688
if type(filename) in [list, tuple]:
667689
template_reader_type = itk.ImageSeriesReader
668690
io_filename = filename[0]

0 commit comments

Comments
 (0)