Configuration: VS2008 (32-bit)
Python2.7.8 (32-bit)
Cmake 3.0
ITK4.5.2
VTK version requirements 5.9 and above
Note: And Python in conjunction with the proposed use of VS2008, in the VS2010 is likely to compile, Bo Master after many tests found in VS2010 always error. First, preliminary preparation
1.1 Installation VS2008
1.2 Installation CMake
1.3 Installation Python2.7.8
1.4 Installation VTK 5.10 (method see previous blog post)
1.5 Download Pygccxml and install: Download and then run the setup.py file in the Pygccxml folder using the Python command.
1.6 Download ITK Source
Create a new directory #itk_soucce#, download ITK to the directory and extract to the current folder. For example, if #itk_soucce#=d:\itk_source, then ITK source files are placed under this directory after decompression: D:\ITK_source\InsightToolkit-4.5.2
1.7 New Two folders
Storage of CMake generated ITK project files #solution_path#
Store files such as Bin,lib,include after successful compilation #build_output#
For example, my settings are #solution_path#=c:\ivtk\itk32py_install #BUILD_OUTPUT # =c:\ivtk\itk32py Two, cmake in the settings
Where is the source code selects #itk_soucce#/insighttoolkit-4.5.2
Where to build the binaries select #solution_path#
Compiler Selection vs 2008
Wait for configure to finish.
Buildtesting and build examples are not selected in the CMake option.
Tick build_shared_libs.
After checking advanced, change the Cmake_install_prefix (the installation path of the Lib and include that is generated after compiling ITK) is set to #build_output#
Tick itkv3_compatibility
Tick Itk_wrap_python
Tick Module_itkvtkglue
Check to see if the Vtk_dir is correct, for example c:/ivtk/vtk510_32py/lib/vtk-5.10
Click Configure again until all entries are dimmed to indicate a successful configuration and click Generate to confirm. third, compile with VS2008
3.1 Setting environment variables
Gccxml_pah= #SOLUTION_PATH #\wrapping\generators\gccxml\gccxml\bin
Add%gccxml_path% to the end of the PATH environment variable 3.2 compilation
Open the ITK project file located under #solution_path# with VS2008 and select release for compilation.
3.3 Installation
Once the compilation succeeds, the install project is compiled, and the associated include, Lib, and bin files are extracted to the #build_output# directory. Four, follow-up
4.1 Copy the DLL files from #build_output#\bin to: #BUILD_OUTPUT #\ Lib\itk-4.5\python
4.2 Add the #build_output#\lib\itk-4.5\python path to Eclipse's Pythonpath Five, example program
5.1 Using ITK to open and save a pair of images
Import itk
Import sys
import VTK
inputfilename= "d:/data/small/0200.dcm
outputfilename=" d:/data/ 0200.png "# #
reads a 2D image in with signed short (16bits/pixel) pixel type
# and save it as unsigned char ( 8bits/pixel) pixel type
#
Inputimagetype = itk. Image.ss2
outputimagetype = itk. IMAGE.UC2
reader = itk. Imagefilereader[inputimagetype]. New ()
writer = itk. Imagefilewriter[outputimagetype]. New ()
filter = ITK. Rescaleintensityimagefilter[inputimagetype, Outputimagetype]. New ()
filter. Setoutputminimum (0)
filter. Setoutputmaximum (255)
filter. SetInput (reader. GetOutput ())
writer. SetInput (filter. GetOutput ())
reader. Setfilename (InputFileName)
writer. Setfilename (OutputFileName)
writer. Update ()
5.2 Open the image with ITK and display with VTK
Add after the above code:
IVFILTER=ITK. Imagetovtkimagefilter[outputimagetype]. New ()
ivfilter. SetInput (filter. GetOutput ())
viewer=vtk.vtkimageviewer ()
iren=vtk.vtkrenderwindowinteractor ()
Viewer. Setupinteractor (Iren)
Viewer. SetInput (Ivfilter. GetOutput ())
Viewer. Render ()
Viewer. Setcolorwindow (255)
Viewer. Setcolorlevel (128)
Iren. Start ()
Examples of 5.3 ITK registration
From insighttoolkit Import * from sys import argv fixedimagename= "D:/data/brain.png movingimagename=" d:/data/
Brainshifted13x17y.png "Outputimagename=" d:/data/brainresult2.png "Fixedimagereader = ItkImageFileReaderIF2_New ()
Movingimagereader = Itkimagefilereaderif2_new () fixedimagereader.setfilename (Fixedimagename) Movingimagereader.setfilename (Movingimagename) fixedimagereader.update () movingimagereader.update () FixedImage = Fixedimagereader.getoutput () Movingimage = Movingimagereader.getoutput () # Instantiate the classes for the Registratio N Framework # registration = itkimageregistrationmethodif2if2_new () Imagemetric = Itkmeansquaresimagetoimagemetricif2if2_new () transform = itktranslationtransformd2_new () optimizer = ITKREGULARSTEPG Radientdescentoptimizer_new () Interpolator = Itklinearinterpolateimagefunctionif2d_new () registration. Setoptimizer (Optimizer. Getpointer ()) registration. SetTransform (transform. Getpointer ()) registration. SetinterpolAtor (interpolator. Getpointer ()) registration. Setmetric (Imagemetric.getpointer ()) registration. Setfixedimage (fixedimage) registration. Setmovingimage (movingimage) registration. Setfixedimageregion (Fixedimage.getbufferedregion ()) transform. Setidentity () Initialparameters = transform. GetParameters () registration. Setinitialtransformparameters (initialparameters) # Iteration Observer # def iterationupdate (): Currentparameter = Transform. GetParameters () print "M:%f P:%f%f"% (optimizer. GetValue (), currentparameter.getelement (0), currentparameter.getelement (1) ) Iterationcommand = Itkpycommand_new () iterationcommand.setcommandcallable (iterationupdate) optimizer. Addobserver (Itkiterationevent (), Iterationcommand.getpointer ()) # # Define optimizer parameters # Optimizer. Setmaximumsteplength (4.00) Optimizer. Setminimumsteplength (0.01) optimizer. Setnumberofiterations print "Starting RegisTration "# # Start the registration process # registration. Update () # Get the final parameters of the transformation # finalparameters = registration. Getlasttransformparameters () print "Final registration Parameters" print "Translation X =%f"% (Finalparameters.getele ment (0),) print "Translation Y =%f"% (finalparameters.getelement (1),) # Now, we use the final transform for Resampli
ng the # Moving image. # Resampler = Itkresampleimagefilterif2if2_new () Resampler. SetTransform (transform. Getpointer ()) Resampler. SetInput (movingimage) region = Fixedimage.getlargestpossibleregion () Resampler. SetSize (region. GetSize ()) Resampler. Setoutputspacing (Fixedimage.getspacing ()) Resampler. Setoutputorigin (Fixedimage.getorigin ()) Resampler. Setoutputdirection (Fixedimage.getdirection ()) Resampler. Setdefaultpixelvalue (MB) outputcast = Itkrescaleintensityimagefilterif2iuc2_new () outputcast.setoutputminimum (0 ) Outputcast.setoutputmaximum (255) OUTPUTCASt. SetInput (Resampler. GetOutput ()) # Write The resampled image # writer = Itkimagefilewriteriuc2_new () writer. Setfilename (outputimagename) writer. SetInput (Outputcast.getoutput ()) writer. Update () print "Image registration has been finished"
Note: To run this example, you may have an error at the Observer definition:
Iterationcommand = Itkpycommand_new ()
Nameerror:name ' Itkpycommand_new ' is notdefined
Locate the insighttoolkit.py file (below #BUILD_OUTPUT #\lib\itk-4.5\python), add the From Itkpyutilspython import * on the last line, and run the example program again.
Next you will encounter a new error:
Traceback (most recent call last):
File "C:\Users\User\workspace\python_itk\registration2D.py", line in<module>,
Optimizer. Addobserver (Itkiterationevent (), Iterationcommand.getpointer ())
File "C:\IVTK\ITK32Py\lib\ITK-4.5\Python\ITKCommonBasePython.py", line 1464, in Addobserver
Elif len (args) = = 2 and not Issubclass (args[1].__class__, Itk.command) and callable (args[1)):
File "C:\IVTK\ITK32Py\lib\ITK-4.5\Python\itkLazy.py", line, in__getattribute__
Itkbase.loadmodule (module, namespace)
File "C:\IVTK\ITK32Py\lib\ITK-4.5\Python\itkBase.py", line 119, Inloadmodule
For K, v. in Module.__dict__.items ():
Unboundlocalerror:local variable ' module ' referenced before assignment
Workaround: In itkbase.py (#BUILD_OUTPUT #\lib\itk-4.5\python below), add the following code after line 103:
module = loader.load (swigmodulename)
Run the example program again to succeed.