Python: 在 ITK 中使用 3 维 numpy 数组
Python: Using a 3 dimensional numpy array in ITK
我有以下问题。我有一个充满浮点数的 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 class"
有什么需要注意的地方吗?
一个简短的博客post about itk<->Numpy conversions. You should combine it with information from this one。我认为将 scoreMatrixItk 写入文件是不必要的。在可能更改创建 thresholdFilter
时使用的类型之后,您应该能够执行 thresholdFilter.SetInput(scoreMatrixItk)
。由于您使用的是 unsigned char,因此不需要 rescaler
除非您想增加强度范围,例如0-50 变成 0-255。我不知道为什么你现在会收到错误。
除了@Dženan 的回复,还有一个问题:您没有为scoreMatrixItk
指定文件格式。格式根据后缀确定。所以下面一行:
itk.imwrite(scoreMatrixItk, "scoreMatrixItk.vtk")
可能会阻止您犯错误。
我有以下问题。我有一个充满浮点数的 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 class"
有什么需要注意的地方吗?
一个简短的博客post about itk<->Numpy conversions. You should combine it with information from this one。我认为将 scoreMatrixItk 写入文件是不必要的。在可能更改创建 thresholdFilter
时使用的类型之后,您应该能够执行 thresholdFilter.SetInput(scoreMatrixItk)
。由于您使用的是 unsigned char,因此不需要 rescaler
除非您想增加强度范围,例如0-50 变成 0-255。我不知道为什么你现在会收到错误。
除了@Dženan 的回复,还有一个问题:您没有为scoreMatrixItk
指定文件格式。格式根据后缀确定。所以下面一行:
itk.imwrite(scoreMatrixItk, "scoreMatrixItk.vtk")
可能会阻止您犯错误。