简体   繁体   English

我如何在Python中使用ITK类

[英]How do I use an ITK class in Python

I have written a class using ITK in CPP which reads all files in a directory and then averages them. 我在CPP中使用ITK编写了一个类,它读取目录中的所有文件然后对它们进行平均。 I would like to use this class in a pipeline constructed using Python. 我想在使用Python构建的管道中使用此类。

I had previously tried to use Swig to wrap template code but according to the swig documenation , it doesn't have template support and the type names need to explicitly specified. 我之前曾尝试使用Swig来包装模板代码,但根据swig文档 ,它没有模板支持,并且需要明确指定类型名称。 But when I use ITK in Python , the interface is very different to that I expect from Swig-generated template code (the type name is not specified in the function/class name at all, which is contrary to what Swig documentation says). 但是当我在Python中使用ITK时 ,界面与我对Swig生成的模板代码的期望非常不同(类型名称根本没有在函数/类名中指定,这与Swig文档所说的相反)。

A small snippet from my code illustrating the usage of the class is shown below: 我的代码中的一个小片段说明了该类的用法如下所示:

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();

The code for the class can be seen in the Git repository . 可以在Git存储库中看到该类的代码。 I don't have an issue with increasing the dependencies of my project by using Boost::Python but I need a starting point to proceed. 我没有使用Boost :: Python增加项目依赖性的问题,但我需要一个起点继续。 Any help would be extremely appreciated. 任何帮助将非常感激。

Thanks. 谢谢。

UPDATE: 更新:

Expected usage in Python would be, 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())

The main idea is to use the WrapITK module. 主要思想是使用WrapITK模块。 It basically uses internal ITK wrapping and parsing mechanism (using GCCXML for C++ to XML parsing - to be moved to CastXML in the future) to generate the *.i files which SWIG uses to generate the Python wrapping code. 它基本上使用内部ITK包装和解析机制(使用GCCXML进行C ++到XML解析 - 将来移动到CastXML)来生成SWIG用来生成Python包装代码的* .i文件。

Basic idea: 基本理念:

  • Say you have an ITK filter 'itkDummy.h' which want to use from a Python script 假设您有一个想要在Python脚本中使用的ITK过滤器'itkDummy.h'
  • Write a file 'itkDummy.wrap' giving the information about the pixel types and template initializations. 写一个文件'itkDummy.wrap',提供有关像素类型和模板初始化的信息。
  • Use the WrapITK module (which is along with the ITK Python bindings) - needs to be verified because every time I tried this, I kept getting an error 使用WrapITK模块(与ITK Python绑定一起) - 需要进行验证,因为每次我尝试这个时,都会出现错误
  • Be happy that itkDummy can be called from Python 很高兴itkDummy可以从Python调用

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

This is what I was looking for. 这就是我想要的。

In Itk Software Guide vol. 在Itk软件指南卷。 1 ( http://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch3.html#x34-410003.7 ) they explain that they are using their own wrapping consisting of: 1( http://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch3.html#x34-410003.7 )他们解释说他们正在使用自己的包装:

  1. gccxml , that generates xml files from C++ program gccxml,从C ++程序生成xml文件
  2. a script called igenerator.py , which generates .i files for swig 一个名为igenerator.py的脚本,它为swig生成.i文件
  3. swig 痛饮

I've never done it before, but you could try to go through their wrapping pipeline to see how the generated *.i file looks like (or maybe even including your filter in your local ITK repository and see if the wrapping automatically works) 我以前从来没有这样做过,但你可以尝试通过他们的包装管道来查看生成的* .i文件是什么样的(或者甚至可能包括你的本地ITK存储​​库中的过滤器,看看包装是否自动运行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM