Skip to content

Commit

Permalink
ENH: Add itk.imseriesread() to match itk.imread() for image series
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Budin committed Dec 4, 2018
1 parent 54789c2 commit 65c4a6a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
22 changes: 22 additions & 0 deletions Wrapping/Generators/Python/Tests/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ def custom_callback(name, progress):
series_reader.Update()
assert series_reader.GetOutput().GetImageDimension() == 3

# test reading image series with itk.imread()
image_series = itk.imread([fileName, fileName])
assert image_series.GetImageDimension() == 3

# Numeric series filename generation without any integer index. It is
# only to produce an ITK object that users could set as an input to
# `itk.ImageSeriesReader.New()` or `itk.imread()` and test that it works.
numeric_series_filename = itk.NumericSeriesFileNames.New()
numeric_series_filename.SetStartIndex(0)
numeric_series_filename.SetEndIndex(3)
numeric_series_filename.SetIncrementIndex(1)
numeric_series_filename.SetSeriesFormat(fileName)
image_series = itk.imread(numeric_series_filename.GetFileNames())
number_of_files = len(numeric_series_filename.GetFileNames())
assert image_series.GetImageDimension() == 3
assert image_series.GetLargestPossibleRegion().GetSize()[2] == number_of_files

# test reading image series with `itk.imread()` and check that dimension is
# not increased if last dimension is 1.
image_series = itk.imread([image_series3d_filename, image_series3d_filename])
assert image_series.GetImageDimension() == 3

# pipeline, auto_pipeline and templated class are tested in other files

# BridgeNumPy
Expand Down
28 changes: 22 additions & 6 deletions Wrapping/Generators/Python/itkExtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,22 +454,38 @@ def imwrite(image_or_filter, filename, compression=False):
writer.Update()

def imread(filename, pixel_type=None):
"""Read an image from a file and return an itk.Image.
"""Read an image from a file or series of files and return an itk.Image.
The reader is instantiated with the image type of the image file.
The reader is instantiated with the image type of the image file if
`pixelType` is not provided (default). The dimension of the image is
automatically found. If the given filename is a list or a tuple, the
reader will use an itk.ImageSeriesReader object to read the files.
"""
import itk
if type(filename) in [list, tuple]:
TemplateReaderType=itk.ImageSeriesReader
io_filename=filename[0]
increase_dimension=True
kwargs={'FileNames':filename}
else:
TemplateReaderType=itk.ImageFileReader
io_filename=filename
increase_dimension=False
kwargs={'FileName':filename}
if pixel_type:
imageIO = itk.ImageIOFactory.CreateImageIO(filename, itk.ImageIOFactory.ReadMode)
imageIO = itk.ImageIOFactory.CreateImageIO(io_filename, itk.ImageIOFactory.ReadMode)
if not imageIO:
raise RuntimeError("No ImageIO is registered to handle the given file.")
imageIO.SetFileName( filename )
imageIO.SetFileName(io_filename)
imageIO.ReadImageInformation()
dimension = imageIO.GetNumberOfDimensions()
# Increase dimension if last dimension is not of size one.
if increase_dimension and imageIO.GetDimensions(dimension-1) != 1:
dimension += 1
ImageType=itk.Image[pixel_type,dimension]
reader = itk.ImageFileReader[ImageType].New(FileName=filename)
reader = TemplateReaderType[ImageType].New(**kwargs)
else:
reader = itk.ImageFileReader.New(FileName=filename)
reader = TemplateReaderType.New(**kwargs)
reader.Update()
return reader.GetOutput()

Expand Down

0 comments on commit 65c4a6a

Please sign in to comment.