如何在 Python 中扩展 ITK class?

How to extend an ITK class in Python?

SimpleITK 提供易于使用的 Python 界面。我可以从那里扩展 class 吗?

我需要解决一个注册问题,这需要我编写自定义注册class,尤其是相似度指标。如何在 Python 中扩展 SimpleITK 供我使用?

Python 的包装 SimpleITK 接口不提供扩展或派生的接口。 SimpleITK ImageRegistrationMethods 的选项是可用的选项。

派生 类 和调整算法最好使用 C++ 级别的 ITK 来完成。

您可以使用 SimpleITK 和 Python 的组件组合一个小的注册框架。例如,您可以使用 SimpleITK 的 ResampleImageFilter 和 Transform 类 以及 scipy 优化器和自定义指标。

SimpleElastix 包是 SimpleITK 的扩展,除了标准的 SimpleITK 方法外,还包括 elastix 注册库。您可以在 elastix 中编写您的指标,并使用 SimpleElastix 将您的指标包装在 Python(或 SimpleITK 支持的任何其他语言)中。然后您将使用 SimpleElastix 来执行注册。要在 SimpleElastix 中使用您想要的指标:

import SimpleITK as sitk

SimpleElastix = sitk.SimpleElastix()
SimpleElastix.SetParameter("Metric", "NameOfYourMetric")
SimpleElastix.SetFixedImage(sitk.ReadImage("fixedImage.nii"))
SimpleElastix.SetMovingImage(sitk.ReadImage("movingImage.nii"))
resultImage = SimpleElastix.Execute()

Elastix 本身是 ITK v3 注册库的扩展,所以如果您熟悉 ITK,elastix 代码库对您来说不会陌生。您可以通过查看 src/Components/Metrics directory. Take a look at the AdvancedMeanSqaures metric for instance. Basically you only need to modify the GetValue() and GetValueAndDerivative() functions in the files prefixed with itk to implement your metric. You then need to modify the files prefixed with elx if you want to pass parameters to your metric via the parameter maps and the CMakeLists file to register the metric with the build system. Parameter maps are key-value pairs that allows you to configure registration components including your metric. You can read more about parameter maps in the SimpleElastix documentation or elastix manual.

中的示例了解如何实施指标

要用 SimpleElastix 包装您的指标,您可以克隆 the repository and follow the guide how to build SimpleElastix manually,除非您将 ELASTIX_DIR 设置为您修改后的 elastix 版本。 elastix 构建系统将负责将您的指标包装在 elastix 中,而 SimpleElastix 构建系统将负责将 elastix 包装在 python.

免责声明:我是 SimpleElastix 的作者。