guide.txt
来自「DTMK软件开发包,此为开源软件,是一款很好的医学图像开发资源.」· 文本 代码 · 共 179 行 · 第 1/2 页
TXT
179 行
WrapITK README
--------------
WrapITK is a project designed to allow classes from ITK (and custom, classes that interact with ITK) to be "wrapped" for use with scripting languages like Python, Tcl, and Java.
Note that ITK already has a wrapping infrastructure. This project aims to be address the following deficits of the existing wrappers (and others):
- The ITK wrapping system is difficult to understand and maintain. WrapITK was written -- and thoroughly documented -- to be as easy as possible to understand, maintain, and extend.
- It is non-trivial to add wrappers for different ITK classes to the system. In WrapITK, adding a wrapper can be as simple as adding a single file containing a few well-documented cmake macros.
- It is difficult if not impossible to add original-style ITK wrappers for external C++ classes that interact with ITK. WrapITK provides explicit hooks for external C++ classes to be wrapped and even installed in the WrapITK tree so that they interact seamlessly with the other wrapped classes.
- Once the ITK wrappers are built, using them from within the target languages is in many cases painful, forcing Python code (for example) to look a lot like C++ code, but nastier. In particular, template types must be hard-coded into every function name. WrapITK attempts to address this in Python by providing run-time lookup of templated types. Additionally, WrapITK ensures that SmartPointers are always returned and acceptable as input, so no bare pointers are ever exposed to Python. This is not the case in the standard ITK wrappers.
An example:
C++:
typedef itk::Image<unsigned short, 2> ImageI2;
ImageI2::Pointer image = ImageUS2::New();
typedef itk::ImageFileWriter<ImageUS2> Writer;
Writer::Pointer writer = Writer::New();
writer->SetInput(image);
writer->SetFileName("foo.tif");
Python with current ITK wrappers:
image = itk.ImageUS2_Pointer(itk.ImageUS2_New())
witer = itk.ImageFileWriterIUS2_Pointer(itk.ImageFileWriterIUS2_New())
writer.SetInput(image);
writer.SetFileName("foo.tif");
Python with WrapITK:
image = itk.Image[itk.US, 2].New()
writer = itk.ImageFileWriter[image].New(Input=image, FileName="foo.tif")
BUILDING
--------
WrapITK requires ITK and CableSwig to have been previously downloaded and built. To get CableSwig, simply run:
cvs -d:pserver:anonymous@public.kitware.com:/cvsroot/CableSwig co CableSwig
(Note that no cvs login is needed here.)
If you check out CableSwig into the Insight/Utilities directory, then it will be built as a part of ITK, and will be automatically detected by WrapITK when ITK is found.
WrapITK will work properly with a CVS checkout of ITK from 2006-1-31 or later, or with the ITK 2.4.1 release. If you are using 2.4.1, there are several required patches to correct bugs in ITK that must be applied. Follow the directions in WrapITK/patches to do so. Additionally, there are some optional patches to the ITK source in WrapITK/patches/optional which can be applied to either a CVS checkout of ITK or to version 2.4.1. These optional patches provide better support for python by providing __str__ methods and the like.
After CableSwig and ITK have been (possibly patched) and built, building WrapITK with cmake is simple. Run ccmake in a new directory with the path to the WrapITK source tree as the first argument, and provide the locations of the ITK and CableSwig build trees if ccmake so requests. Build options are relatively self-explanatory.
Note that each individual filter that is wrapped can declare which dimensions it should be wrapped for, and what image types it can accept. For example, a filter could declare that it should only be wrapped for 3D images with floating-point typed pixels. In this case, then wrappers will only be created if the user has selected to build 3-dimensional image wrappers and has selected one or more floating point types (e.g. double or float) in ccmake. Thus, the ccmake configuration specifies the maximum possible range of image and filter types to be created, and each filter is wrapped for some subset of that range.
INSTALLING
----------
(0) Philosophy
WrapITK is both a tool for users and for developers who wish to create wrappers for additional classes from ITK or classes that interact with ITK. The best way to both use WrapITK and extend it is by installing it into a known location. Once installed, using WrapITK is easy because the installation process informs Python where to look for the itk libraries. Installation makes extending WrapITK easy as well because extensions can be subsequently installed to the same place and used seamlessly.
The install location is specified in ccmake with the CMAKE_INSTALL_PREFIX variable. Simply run 'make install' (if you are using make as the build tool) to install the package.
(1) FindWrapITK.cmake and WrapITK.pth
In addition to the libraries and script files installed into CMAKE_INSTALL_PREFIX/WRAP_ITK_INSTALL_LOCATION, a cmake script will be placed in the proper directory in the CMake tree so that the command FIND_PACKAGE(WrapITK) will work with no additional configuration, making developing external projects (see below) very easy.
Moreover (as described above), a file called 'WrapITK.pth' will be installed in the python site-packages directory which points python at the WrapITK scripts, so that they can be imported from within python with no additional configuration.
In some cases, it is not desirable to install these files (which require superuser/administrator privileges to install). If so, turn off the INSTALL_WRAP_ITK_COMPATIBILITY option in ccmake. If this option is off, these files can be manually installed by building the 'install_wrapitk_compatibility' target. (E.g. 'make install_wrapitk_compatibility' if using makefiles.) If INSTALL_WRAP_ITK_COMPATIBILITY is on, manually building the install_wrapitk_compatibility target does nothing.
PYTHON USAGE
------------
(0) Configuring Python and Importing the Libraries
If WrapITK has been installed, then using it from within python is trivial: simply issue the command "import itk", and you are ready to go. This is because WrapITK installs a pth file in the python site-packages directory so that python knows where to find the itk scripts.
If WrapITK has not been installed, then you will either need to set the PYTHONPATH environment variable to contain the directory /path-to-WrapITK-build/Python, add this path to sys.paths within python, or start python from that directory. After this, "import itk" will work properly.
(1) Basic Usage
Most class in the itk python module are "template proxy classes" that encapsulate all of the template instantiations that were created at build time. If three-dimensional unsigned char and unsigned short image types were created, they can be accessed as follows:
itk.Image[itk.UC, 3] -or- itk.Image.UC3
itk.Image[itk.US, 3] -or- itk.Image.US3
Filters templated on images can be similarly accessed:
itk.ImageFileReader[itk.Image[itk.UC,3]]
or
itk.ImageFileReader[itk.Image.UC3]
or even
itk.ImageFileReader[image]
if 'image' is of type UC3. This makes it easy to write generic routines which can deal with any input image type.
Each class has a New() method which returns a smart pointer to that class. The New() method in python has some additional features:
- Arguments to the new method are assumed to be filter inputs. So you could write:
adder = itk.AddImageFilter[...].New()
adder.SetInput1(inputA)
adder.SetInput2(inputB)
or you could write
adder = itk.AddImageFilter[...].New(inputA, inputB)
- Additionally, keyword arguments are allowed as well. Keyword arguments cause the corresponding "Set..." method to be called, so you could write the following:
itk.ImageFileWriter[image].New(image, FileName="foo.tif")
or
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?