可以python sitk.ReadImage读取列表/系列图像吗?

时间:2017-07-14 07:11:06

标签: python itk simpleitk

我不明白sitk.ReadImage是否可以读取图像列表?我没有设法找到一个示例,显示如何将图像列表输入到函数中。 但是在功能文件中它说:

ReadImage(**VectorString fileNames**, itk::simple::PixelIDValueEnum outputPixelType) -> Image
ReadImage(std::string const & filename, itk::simple::PixelIDValueEnum outputPixelType) -> Image



ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image reading tasks.


Note that when reading a series of images that have meta-data
associated with them (e.g. a DICOM series) the resulting image will
have an empty meta-data dictionary. It is possible to programmatically
add a meta-data dictionary to the compounded image by reading in one
or more images from the series using the ImageFileReader class,
analyzing the meta-dictionary associated with each of those images and
creating one that is relevant for the compounded image.

所以从文件中可以看出它是可能的。有人能告诉我一个简单的例子。

编辑: 我尝试了以下方法:

sitk.ReadImage(['volume00001.mhd','volume00002.mhd'])

但这是我得到的错误:

RuntimeErrorTraceback (most recent call last)
<ipython-input-42-85abf82c3afa> in <module>()
      1 files = [f for f in os.listdir('.') if 'mhd' in f]
      2 print(sorted_files[1:25])
----> 3 sitk.ReadImage(['volume00001.mhd','volume00002.mhd'])

/gpfs/bbp.cscs.ch/home/amsalem/anaconda2/lib/python2.7/site-packages/SimpleITK/SimpleITK.pyc in ReadImage(*args)
   8330 
   8331     """
-> 8332     return _SimpleITK.ReadImage(*args)
   8333 class HashImageFilter(ProcessObject):
   8334     """

RuntimeError: Exception thrown in SimpleITK ReadImage: /tmp/SimpleITK/Code/IO/src/sitkImageSeriesReader.cxx:145:
sitk::ERROR: The file in the series have unsupported 3 dimensions.

感谢。

1 个答案:

答案 0 :(得分:0)

SimpleITK使用SWIG将C ++接口包装到Insight Segmentation and Registration Toolkit(ITK)。因此,内联python文档应该补充C ++ Doxygen文档。有一个C ++类型到Python类型的映射,它们之间有强大的隐式转换。你可以在这里找到sitk :: ReadImage方法的文档:  https://itk.org/SimpleITKDoxygen/html/namespaceitk_1_1simple.html#ae3b678b5b043c5a8c93aa616d5ee574c

请注意,有2个ReadImage方法,您列出的Python文档字符串就是其中之一。

从SimpleITK示例中可以看到读取DICOM系列的片段:

Language

这使用类接口而不是程序。这只是:

print( "Reading Dicom directory:", sys.argv[1] )
reader = sitk.ImageSeriesReader()

dicom_names = reader.GetGDCMSeriesFileNames( sys.argv[1] )
reader.SetFileNames(dicom_names)

image = reader.Execute()

或通常带有字符串文件名列表:

image = sitk.ReadImage(dicom_names)

许多常见的数组类似字符串将被隐式转换为SWIG image = sitk.ReadImage(["image1.png", "image2.png"...]) 类型。