[Insight-users] Problem with wrapping itk with python 2.3

alex sando yejun.alex at gmail.com
Wed Nov 18 11:45:25 EST 2009


 hi Gaëtan , currently I am trying the registration function of ITK under
python :), Here is my Code:

        transformer = itk.TranslationTransform[itk.D, 2].New()
        optimizer = itk.RegularStepGradientDescentOptimizer.New()
        interpolator = itk.LinearInterpolateImageFunction[itk.Image.US2,
itk.D].New()
        metric =
itk.MeanSquaresImageToImageMetric[itk.Image.US2,itk.Image.US2].New()
        registration_object =
itk.ImageRegistrationMethod[itk.Image.US2,itk.Image.US2].New()
        registration_object.SetMetric(metric.GetPointer())
        registration_object.SetOptimizer(optimizer.GetPointer())
        registration_object.SetTransform(transformer.GetPointer())
        registration_object.SetInterpolator(interpolator.GetPointer())
        registration_object.SetFixedImage(convertor.GetOutput())
        registration_object.SetMovingImage(convertor1.GetOutput())

registration_object.SetFixedImageRegion(convertor.GetOutput().GetBufferedRegion())

        parameters =
itk.ImageRegistrationMethod[itk.Image.US2,itk.Image.US2].TranslationTransform[itk.D,
2].TransformParametersType(transformer.GetNumberOfParameters())
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~this line failed as i
previous mentioned...

        #parameters = [0.0, 0.0]
~~~~~~~~~~~~~~~~~~~~~~~~using parameters just as list seems works ,but it
will cause "registration_object.Update()" finished with last_para =
[0.0,0.0] and optimizer.GetValue() = 1, which definitely means the
registration failed... My problem is what should i do to set the parameters
correctly.... thanks again in advance...

        registration_object.SetInitialTransformParameters(parameters)
        optimizer.SetMaximumStepLength(4.00)
        optimizer.SetMinimumStepLength(0.01)
        optimizer.SetNumberOfIterations(200)
        try:
            registration_object.Update()
        except itkError:
            print itkError
        last_para = registration_object.GetLastTransformParameters()
        print last_para.GetElement(0), last_para.GetElement(1)
        print optimizer.GetCurrentIteration(), optimizer.GetValue()

Regards,

Alex

2009/11/19 Gaëtan Lehmann <gaetan.lehmann at jouy.inra.fr>

>
> Le 18 nov. 09 à 17:28, alex sando a écrit :
>
>  hi Gaëtan ,
>>
>> the RegistrationType should be ImageRegistrationMethod :) But when i
>> tried:
>>
>> parameters =
>> itk.ImageRegistrationMethod[itk.Image.US2,itk.Image.US2].ParametersType(transformer.GetNumberOfParameters())
>>
>> it gave me the error:
>> AttributeError: type object 'itkImageRegistrationMethodIUS2IUS2' has no
>> attribute 'ParametersType'
>>
>> And in ITK's source file, i found that:
>>
>> typedef  typename MetricType::TransformParametersType    ParametersType;
>>
>
> ParametersType is a typedef, so it is not available in python
>
>
>
>> So I guess that the correct invoke maybe like:
>>
>> parameters =
>> itk.ImageRegistrationMethod[itk.Image.US2,itk.Image.US2].TranslationTransform[itk.D,
>> 2].TransformParametersType(transformer.GetNumberOfParameters())
>>
>> But it failed with:
>>
>> AttributeError: type object 'itkImageRegistrationMethodIUS2IUS2' has no
>> attribute 'TranslationTransform'
>>
>>
> I'm not sure what you're trying to do there. What is TranslationTransform?
>
>
>  Would you give me any suggestion about this issue?.... thanks:)
>>
>> Alex
>>
>>
>>
>> 2009/11/18 Gaëtan Lehmann <gaetan.lehmann at jouy.inra.fr>
>>
>> Le 18 nov. 09 à 16:17, alex sando a écrit :
>>
>>
>> hi Gaëtan, it works again:). But i got two new questions:)
>>
>>       transformer = itk.TranslationTransform[itk.D, 2].New()
>>       optimizer = itk.RegularStepGradientDescentOptimizer.New()
>>       interpolator = itk.LinearInterpolateImageFunction[itk.Image.US2,
>> itk.D].New()
>>       metric =
>> itk.MeanSquaresImageToImageMetric[itk.Image.US2,itk.Image.US2].New()
>>       registration_object =
>> itk.ImageRegistrationMethod[itk.Image.US2,itk.Image.US2].New()
>>       registration_object.SetMetric(metric.GetPointer())
>>       registration_object.SetOptimizer(optimizer.GetPointer())
>>       registration_object.SetTransform(transformer.GetPointer())
>>       registration_object.SetInterpolator(interpolator.GetPointer())
>>       registration_object.SetFixedImage(convertor.GetOutput())
>>       registration_object.SetMovingImage(convertor1.GetOutput())
>>
>> registration_object.SetFixedImageRegion(convertor.GetOutput().GetBufferedRegion())
>>
>>       #parameters =
>> itk.RegistrationType.ParametersType(transformer.GetNumberOfParameters())
>>                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~this failed in
>> the python, what should i do to construct a ParametersType object in python?
>>
>> typedefs available in c++ are not available in python.
>> What type ParametersType is supposed to be?
>>
>>
>>
>>
>>       parameters = [0.0, 0.0]
>>       ~~~~~~~~~~~~~~~~~just treat parameters as a list seems works, am i
>> correct?
>>
>>       registration_object.SetInitialTransformParameters(parameters)
>>       optimizer.SetMaximumStepLength(4.00)
>>       optimizer.SetMinimumStepLength(0.01)
>>       optimizer.SetNumberOfIterations(200)
>>
>>       try:
>>           registration_object.Update()
>>       except itkError:
>>           print itkError
>>
>>       last_para = registration_object.GetLastTransformParameters()
>>       ~~~~~~~this returns a itkArray pointer and it seems  unindexable,
>> cause it failed when i using "last_para[0]" how can i access the actual
>> transform parameters? thanks in advance again;)
>>
>> Usually, there is a SetElement() and GetElement() method for this kind of
>> class. It seems to be the case as well for itk::Array :-)
>>
>>
>> Gaëtan
>>
>>
>>
>> Regards,
>>
>> Alex
>>
>> 2009/11/18 Gaëtan Lehmann <gaetan.lehmann at jouy.inra.fr>
>>
>> Le 18 nov. 09 à 14:17, alex sando a écrit :
>>
>>
>> Great! It works! thank you Gaëtan :)
>>
>> But i got an error when i tried to use ITK python to finish some
>> Registration work....Here is my code:
>>
>>      transformer = itk.TranslationTransform[itk.D, 3].New()
>>      optimizer = itk.RegularStepGradientDescentOptimizer.New()
>>      interpolator = itk.LinearInterpolateImageFunction[itk.Image.US3,
>> itk.D].New()
>>      metric =
>> itk.MeanSquaresImageToImageMetric[itk.Image.US3,itk.Image.US3].New()
>>      registration_object =
>> itk.ImageRegistrationMethod[itk.Image.US3,itk.Image.US3].New()
>>      registration_object.SetMetric(metric)
>>      ~~~~~~~~~~~~~~~~~~~~~~~~~~~ I got an error from this
>> TypeError: Type error. Got
>> _p_itk__SmartPointerTitk__MeanSquaresImageToImageMetricTitk__ImageTunsigned_short_3u_t_itk__ImageTunsigned_short_3u_t_t_t,
>> expected
>> _p_itk__SmartPointerTitk__ImageToImageMetricTitk__ImageTunsigned_short_3u_t_itk__ImageTunsigned_short_3u_t_t_t
>>
>> Is there something i do wrong when invorking the method? The error may
>> indicate the SetMetric function requires a base class... could you tell how
>> to solve it.... many many thanks
>>
>>
>> Usually, when a base class is requested, you have to pass a raw pointer
>> instead of a smart pointer. You can get it with the method GetPointer()
>>
>>  registration_object.SetMetric(metric.GetPointer())
>>
>> may work.
>>
>>
>> Gaëtan
>>
>>
>> Alex
>>
>>
>>
>> 2009/11/18 Gaëtan Lehmann <gaetan.lehmann at jouy.inra.fr>
>>
>> Le 18 nov. 09 à 09:30, alex sando a écrit :
>>
>>
>> hi Gaëtan, my wrapITK is installed in:
>>
>> C:\Program Files\WrapITK
>>
>> itkvtkGlue is installed in:
>>
>> C:\Program Files\ItkVtkGlue
>>
>> Do you mean that i should copy ItkVtkGlue into the WrapITK directory?
>>
>>
>> I was thinking to change the base path in CMAKE_INSTALL_PREFIX with cmake
>> gui, and to rebuild and reinstall ItkVtkGlue.
>>
>>
>>
>> Regards,
>>
>> Alex
>>
>>
>> 2009/11/18 Gaëtan Lehmann <gaetan.lehmann at jouy.inra.fr>
>>
>> Le 18 nov. 09 à 09:11, alex sando a écrit :
>>
>>
>> Thanks Gaëtan,  I built itkvtkglue project successfully:
>>
>> 1>------ Build started: Project: INSTALL, Configuration: Release Win32
>> ------
>> 1>
>> 1>Performing Post-Build Event...
>> 1>-- Install configuration: "Release"
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/include/InsightToolkit/BasicFilters/itkImageToVTKImageFilter.h
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/include/InsightToolkit/BasicFilters/itkImageToVTKImageFilter.txx
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/include/InsightToolkit/BasicFilters/itkVTKImageToImageFilter.h
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/include/InsightToolkit/BasicFilters/itkVTKImageToImageFilter.txx
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/ClassIndex/ItkVtkGlue.mdx
>> 1>-- Installing: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/lib/_ItkVtkGluePython.pyd
>> 1>-- Installing: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/ClassIndex/wrap_ItkVtkGluePython.idx
>> 1>-- Installing: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/lib/ItkVtkGluePython.py
>> 1>-- Installing: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/ClassIndex/wrap_itkImageToVTKImageFilter.idx
>> 1>-- Installing: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/lib/itkImageToVTKImageFilter.py
>> 1>-- Installing: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/ClassIndex/wrap_itkVTKImageToImageFilter.idx
>> 1>-- Installing: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/lib/itkVTKImageToImageFilter.py
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/SWIG/ItkVtkGlue.swg
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/Python/Configuration/ItkVtkGlueConfig.py
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/Python/ItkVtkGlue.py
>> 1>-- Installing external project  into the WrapITK installation directory.
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/Python/itkvtk.py
>> 1>-- Up-to-date: C:/Program
>> Files/ItkVtkGlue/lib/InsightToolkit/WrapITK/Python/itkExtras/itkvtkExtras.py
>> 1>Build log was saved at
>> "file://d:\ITKVTKBuild\INSTALL.dir\Release\BuildLog.htm"
>> 1>INSTALL - 0 error(s), 0 warning(s)
>> ========== Build: 1 succeeded, 0 failed, 4 up-to-date, 0 skipped
>> ==========
>>
>> But i still get error message when import the module:
>>
>> import ItkvtkGlue
>> ImportError: No module named ItkvtkGlue
>> or
>> import itkvtk
>> ImportError: No module named itkvtk
>>
>> So I guess VTKImageToImageFilter may be built into the itk module, so i
>> try the filter directly in the source code without the extra "import"
>> expressions:
>>
>> print "Start to OpenFile"
>> convertor = itk.VTKImageToImageFilter[itk.Image.US3].New(imageData)
>> img = convertor.GetOutput()
>> convertor.Update();
>> print "Finished OpenFile"
>>
>> , but only get:
>>
>> AttributeError: 'LazyITKModule' object has no attribute
>> 'VTKImageToImageFilter'
>>
>> How can i fix this.... many many thanks for suggestions...
>>
>> Hi Alex,
>>
>> The base path for the installation must be the same than the one used for
>> WrapITK. I don't think that WrapITK is installed in C:/Program
>> Files/ItkVtkGlue.
>> Can you try to install ItkVtkGlue in the same dir than WrapITK?
>>
>> Gaëtan
>>
>>
>>
>>
>> Alex
>>
>>
>> 2009/11/18 Gaëtan Lehmann <gaetan.lehmann at jouy.inra.fr>
>>
>> Le 17 nov. 09 à 07:59, alex sando a écrit :
>>
>>
>> hi, currently i want to import a vtkImageData to itkImage so that the data
>> could be processed using some ITK filters, but when i execute the following
>> code:
>>
>>   imageData = reader.GetOutput()
>>   print "Start to OpenFile"
>>   convertor = itk.VTKImageToImageFilter[itk.Image.S3].New(imageData)
>>   img = convertor.GetOutput()
>>   convertor.Update();
>>   print "Finished OpenFile"
>>
>> always get the following error message:
>>  File "D:\Lab Affairs\src\control\Common.py", line 448, in _simpleProcess
>>  convertor = itk.VTKImageToImageFilter[...].New(imageData)
>>  File "C:\Program
>> Files\WrapITK\lib\InsightToolkit\WrapITK\Python\itkLazy.py", line 14, in
>> __getattribute__
>>  value = types.ModuleType.__getattribute__(self, attr)
>> AttributeError: 'LazyITKModule' object has no attribute
>> 'VTKImageToImageFilter'
>>
>> VTKImageToImageFilter is not wrapped in the WrapITK? how can i convert the
>> image from & to between vtkImageData and itkImage? thanks...
>>
>> It is in WrapITK, but in an external project.
>> See Wrapping/WrapITK/ExternalProjects/ItkVtkGlue
>>
>> Gaëtan
>>
>>
>>
>> Regards,
>>
>> Alex
>>
>> On Tue, Nov 17, 2009 at 1:20 PM, alex sando <yejun.alex at gmail.com> wrote:
>> Thanks Gaëtan! It seems work well on my platform:). Some little issues i
>> encountered in the progress, just a record for later's reference :).
>> 1. _BasePython project in WrapITK failed because /bigobj compile flag is
>> not set defaultly.
>> 2. Dont forget to add path which contains SwigRuntimePython.dll to PATH
>>
>> Regards,
>>
>> Alex
>>
>> 2009/11/17 Gaëtan Lehmann <gaetan.lehmann at jouy.inra.fr>
>>
>>
>> Le 16 nov. 09 à 19:14, alex sando a écrit :
>>
>>
>> hello Gaëtan
>>
>> Currently i use itk 3.14 and Cableswig 3.14 and wrapitk0.2 in the source
>> of itk 3.14. But when i build wrapitk, it failed with a lot of
>> "itkFlatStructuringElement.h: No such file or directory" error.... can you
>> give me any suggestion to solve this? many thanks, my platform is xp with
>> vs2005 and cmake 2.8.
>>
>> Sure: turning on ITK_USE_REVIEW in cmake gui should help.
>> Please let me know if that fix your problem.
>>
>> Regards,
>>
>> Gaëtan
>>
>>
>>
>>
>> Regards,
>>
>> Alex
>>
>> 2009/11/16 Gaëtan Lehmann <gaetan.lehmann at jouy.inra.fr>
>>
>> Le 16 nov. 09 à 13:32, alex sando a écrit :
>>
>>
>> hi all,
>>  I am currently working on an legacy system which is composed under python
>> 2.3. Is there any method that could help me to create the itk python wrap
>> under python 2.3? I search for a while on the internet and find the wrapitk
>> is a great tool for using itk under python environment, but its requirement
>> for  the version of python is at least 2.4...I also tried to compile the
>> wrap using cmake, but failed with
>>
>>
>> CMake Warning at CMakeLists.txt:173 (FIND_PACKAGE):
>> Could not find module FindCableSwig.cmake or a configuration file for
>> package CableSwig.
>> Adjust CMAKE_MODULE_PATH to find FindCableSwig.cmake or set CableSwig_DIR
>> to the directory containing a CMake configuration file for CableSwig. The
>> file will have one of the following names:
>> CableSwigConfig.cmake
>> cableswig-config.cmake
>> CMake Error at CMakeLists.txt:185 (MESSAGE):
>> CableSwig is required for CSwig Wrapping.
>> Configuring incomplete, errors occurred!
>>
>> But in fact i set the CableSwig_DIR directory correctly... Is there any
>> suggestion to get itk work under python 2.3.....great appreciate....
>>
>>
>> Hi Alex,
>>
>> I think that WrapITK 0.2 - in ITK sources - *should* work with python 2.3.
>>
>> Can you give more informations on your system? OS, cmake version,
>> compiler, ITK version, CableSwig version, …
>>
>>
>> Regards,
>>
>> Gaëtan
>>
>>
>> --
>> Gaëtan Lehmann
>> Biologie du Développement et de la Reproduction
>> INRA de Jouy-en-Josas (France)
>> tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
>> http://voxel.jouy.inra.fr  http://www.itk.org
>> http://www.mandriva.org  http://www.bepo.fr
>>
>>
>>
>> --
>> Gaëtan Lehmann
>> Biologie du Développement et de la Reproduction
>> INRA de Jouy-en-Josas (France)
>> tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
>> http://voxel.jouy.inra.fr  http://www.itk.org
>> http://www.mandriva.org  http://www.bepo.fr
>>
>>
>>
>>
>> --
>> Gaëtan Lehmann
>> Biologie du Développement et de la Reproduction
>> INRA de Jouy-en-Josas (France)
>> tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
>> http://voxel.jouy.inra.fr  http://www.itk.org
>> http://www.mandriva.org  http://www.bepo.fr
>>
>>
>>
>> --
>> Gaëtan Lehmann
>> Biologie du Développement et de la Reproduction
>> INRA de Jouy-en-Josas (France)
>> tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
>> http://voxel.jouy.inra.fr  http://www.itk.org
>> http://www.mandriva.org  http://www.bepo.fr
>>
>>
>>
>> --
>> Gaëtan Lehmann
>> Biologie du Développement et de la Reproduction
>> INRA de Jouy-en-Josas (France)
>> tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
>> http://voxel.jouy.inra.fr  http://www.itk.org
>> http://www.mandriva.org  http://www.bepo.fr
>>
>>
>>
>> --
>> Gaëtan Lehmann
>> Biologie du Développement et de la Reproduction
>> INRA de Jouy-en-Josas (France)
>> tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
>> http://voxel.jouy.inra.fr  http://www.itk.org
>> http://www.mandriva.org  http://www.bepo.fr
>>
>>
>>
>> --
>> Gaëtan Lehmann
>> Biologie du Développement et de la Reproduction
>> INRA de Jouy-en-Josas (France)
>> tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
>> http://voxel.jouy.inra.fr  http://www.itk.org
>> http://www.mandriva.org  http://www.bepo.fr
>>
>>
>>
> --
> Gaëtan Lehmann
> Biologie du Développement et de la Reproduction
> INRA de Jouy-en-Josas (France)
> tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
> http://voxel.jouy.inra.fr  http://www.itk.org
> http://www.mandriva.org  http://www.bepo.fr
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20091119/426b1a38/attachment-0001.htm>


More information about the Insight-users mailing list