【问题标题】:Python: Using a 3 dimensional numpy array in ITKPython:在 ITK 中使用 3 维 numpy 数组
【发布时间】:2018-01-31 13:09:27
【问题描述】:

我有以下问题。我有一个充满浮点数的 3D numpy 数组,称为 scoreMatrix。现在我想在它上面使用 Itk 过滤器,比如阈值过滤器。这一般可以吗?我试图将 numpy 数组转换为 itk 图像,然后对其进行处理。还有其他方法吗?:

    scoreMatrixItk = itk.GetImageViewFromArray(scoreMatrix)

    itk.imwrite(scoreMatrixItk, "scoreMatrixItk")
    PixelType = itk.UC
    Dimension = 3

    ImageType = itk.Image[PixelType, Dimension]

    reader = itk.ImageFileReader[ImageType].New()
    reader.SetFileName('scoreMatrixItk')

    thresholdFilter = itk.OtsuMultipleThresholdsImageFilter[
            ImageType,
            ImageType].New()
    thresholdFilter.SetInput(reader.GetOutput())

    thresholdFilter.SetNumberOfHistogramBins(3)
    thresholdFilter.SetNumberOfThresholds(2)
    thresholdFilter.SetLabelOffset(4)

    rescaler = itk.RescaleIntensityImageFilter[ImageType, ImageType].New()
    rescaler.SetInput(thresholdFilter.GetOutput())
    rescaler.SetOutputMinimum(0)
    rescaler.SetOutputMaximum(255)

    writer = itk.ImageFileWriter[ImageType].New()
    writer.SetFileName('outputImage.tiff')
    writer.SetInput(rescaler.GetOutput())

    writer.Update()

但我收到错误:KeyError: "itkTemplate : No template [ >] for the itk::ImageFileWriter 类"

我需要注意一些事情吗?

【问题讨论】:

    标签: python arrays numpy scipy itk


    【解决方案1】:

    关于 itkNumpy 转换的简短博客 post。您应该将它与来自此one 的信息结合起来。我认为将 scoreMatrixItk 写入文件是不必要的。你应该可以做thresholdFilter.SetInput(scoreMatrixItk),也许在改变thresholdFilter的创建所使用的类型之后。由于您使用的是无符号字符,因此您不需要rescaler,除非您想增加强度范围,例如0-50 变为 0-255。我不知道您为什么目前收到错误消息。

    【讨论】:

    • 我认为它有效,至少他创建了一个输出图像。但不幸的是,我不能确定,因为如果我尝试打印 thresholdFilter.GetThresholds() 它会显示空括号。
    【解决方案2】:

    除了@Dženan 的回复,还有一个问题:您没有为scoreMatrixItk 指定文件格式。格式是根据后缀确定的。所以下面一行:

    itk.imwrite(scoreMatrixItk, "scoreMatrixItk.vtk")
    

    可能会阻止您出错。

    【讨论】:

      猜你喜欢
      • 2014-05-23
      • 1970-01-01
      • 2014-12-08
      • 2017-08-18
      • 2016-12-30
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多