如何在 Python 中使用 ITK class

How do I use an ITK class in Python

我在 CPP 中使用 ITK 编写了一个 class,它读取目录中的所有文件,然后对它们进行平均。我想在使用 Python 构造的管道中使用此 class。

我之前曾尝试使用 Swig 来包装模板代码,但根据 swig documenation, it doesn't have template support and the type names need to explicitly specified. But when I use ITK in Python,界面与我对 Swig 生成的模板代码的预期非常不同(类型名称未在function/class 完全没有名称,这与 Swig 文档所说的相反)。

我的代码中的一小段说明了 class 的用法,如下所示:

typedef unsigned char PixelType;
typedef itk::Image<PixelType, 2> ImageType;
typedef itk::NaryMeanImageFilter< ImageType, ImageType > FilterType; // custom class
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageFileWriter<ImageType> WriterType;

ImageType::Pointer image = ImageType::New();
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
FilterType::Pointer filter = FilterType::New(); // custom class

for (unsigned int i = 0; i< fileNames.size(); ++i)
{
  reader->SetFileName(fileNames[i]);
  filter->SetInput(i, reader->GetOutput()); // custom class
}

writer->SetFileName(outName);
writer->SetInput(filter->GetOutput());
writer->Update();

class 的代码可以在 Git repository 中看到。我对使用 Boost::Python 增加项目的依赖性没有问题,但我需要一个起点才能继续。任何帮助将不胜感激。

谢谢。

更新:

Python 中的预期用法是,

readerType=itk.ImageFileReader[inputImageType]
reader=readerType.New()
filterType=itk.NaryMeanImageFilter[inputImageType,inputImageType]
filter=filterType.New()

for i in range(0, fileNames.size()):
    reader.SetFileName(fileNames[i])
    filter.SetInput(i, reader->GetOutput())

在 Itk 软件指南卷中。 1 (http://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch3.html#x34-410003.7 ) 他们解释说他们正在使用自己的包装,包括:

  1. gccxml ,从 C++ 程序
  2. 生成 xml 文件
  3. 一个名为 igenerator.py 的脚本,它为 swig
  4. 生成 .i 文件
  5. 痛饮

我以前从来没有这样做过,但你可以尝试通过他们的包装管道来查看生成的 *.i 文件的样子(或者甚至可能将你的过滤器包含在你的本地 ITK 存储库中,看看是否自动换行)

主要思路是使用WrapITK模块。它基本上使用内部 ITK 包装和解析机制(使用 GCCXML for C++ 到 XML 解析 - 将来移动到 CastXML)来生成 SWIG 的 *.i 文件用于生成 Python 包装代码。

基本思路:

  • 假设您有一个 ITK 过滤器 'itkDummy.h',它想从 Python 脚本中使用
  • 编写一个文件'itkDummy.wrap' 提供有关像素类型和模板初始化的信息。
  • 使用 WrapITK 模块(与 ITK Python 绑定一起使用)- 需要验证,因为每次我尝试这个时,我都会收到错误
  • 很高兴可以从 Python
  • 调用 itkDummy

参考:http://www.itk.org/Wiki/ITK/Release_4/Wrapping/BuildProcess

这就是我要找的。