Difference between revisions of "MITK Python Wrapping"

From mitk.org
Jump to navigation Jump to search
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
= MITK Python Wrapping =
+
<pre><nowiki>
 +
Deprecated
 +
 
 +
The information on this page is deprecated. Please look for up-to-date
 +
information about Python support at http://docs.mitk.org/nightly-qt4
 +
</nowiki></pre>
 +
 
 +
 
 
== Introduction ==
 
== Introduction ==
 
This how-to will show you how to use MITK classes from a Python environment. The main purpose of wrapping MITK Code in Python is to accelerate algorithm development by using the interpreted Python environment and still have access to existing fast C++ algorithms. Besides that, there are many more scenarios in which these wrapper may proof to be useful. The creational workflow as well as the infrastructure was developed in the Diploma Thesis of Danial Saruji, completed in December 2010.
 
This how-to will show you how to use MITK classes from a Python environment. The main purpose of wrapping MITK Code in Python is to accelerate algorithm development by using the interpreted Python environment and still have access to existing fast C++ algorithms. Besides that, there are many more scenarios in which these wrapper may proof to be useful. The creational workflow as well as the infrastructure was developed in the Diploma Thesis of Danial Saruji, completed in December 2010.
Line 6: Line 13:
 
The wrapping process is very similar to ITK. [http://www.itk.org/ITK/resources/CableSwig.html CableSwig] is used to generate the wrappers. Currently only Python wrappers can be generated for the core part of MITK. It is also possible to specify wrappers for other parts or own classes.
 
The wrapping process is very similar to ITK. [http://www.itk.org/ITK/resources/CableSwig.html CableSwig] is used to generate the wrappers. Currently only Python wrappers can be generated for the core part of MITK. It is also possible to specify wrappers for other parts or own classes.
  
Finally, we have a very nice bundle intergrated in the MITK BlueBerry application which provides a Python console, a variable stack, a command history and a script editor. All in all, everything you need to access the running MITK application and all instantiated objects interactively through Python. You can easily drag and drop Data Nodes from the DataManager to the Python variable stack and vice versa. This way you can create DataNodes with Python Code and show them instantly in the MITK [[RenderWindows]].
+
Finally, we have a very nice bundle intergrated in the MITK BlueBerry application which provides a Python console, a variable stack, a command history and a script editor. All in all, everything you need to access the running MITK application and all instantiated objects interactively through Python. You can easily drag and drop Data Nodes from the DataManager to the Python variable stack and vice versa. This way you can create DataNodes with Python Code and show them instantly in the MITK RenderWindows.
 +
 
  
 
== Contents ==
 
== Contents ==
 
This document contains instructions on how to build MITK, ITK and VTK with Python support. Moreover, a description on how to build the Python plugin for the MITK BlueBerry Application is included. Last but not least, there are some troubleshooting paragraphs describing the fixes or workarounds that we needed when we encountered problems.
 
This document contains instructions on how to build MITK, ITK and VTK with Python support. Moreover, a description on how to build the Python plugin for the MITK BlueBerry Application is included. Last but not least, there are some troubleshooting paragraphs describing the fixes or workarounds that we needed when we encountered problems.
  
<<[[TableOfContents]](2)>>
+
__TOC__
  
= General Prerequisites =
+
 
The document also describes how to enable Python Wrapping for other toolkits (ITK, VTK, OpenCV). Together with the MITK wrappers you will have the whole code base accessible through Python. '''In general you will need Python 2.6 or 2.7''' installed on your computer. You can download it [http://www.python.org/download/releases/ here]. For the MITK wrapping process Python 3.x may not be used yet. Of course, you will need everything else to built the Toolkits if you would built them without Python support, especially [[CMake]].
+
== General Prerequisites ==
 +
The document also describes how to enable Python Wrapping for other toolkits (ITK, VTK, OpenCV). Together with the MITK wrappers you will have the whole code base accessible through Python. '''In general you will need Python 2.6 or 2.7''' installed on your computer. You can download it [http://www.python.org/download/releases/ here]. For the MITK wrapping process Python 3.x may not be used yet. Of course, you will need everything else to built the Toolkits if you would built them without Python support, especially CMake.
  
 
As an optional but useful software you may want to install [http://ipython.scipy.org/moin/ IPython] which is an enhanced interactive Python shell for Linux, Mac and Windows which we found to be very handy.
 
As an optional but useful software you may want to install [http://ipython.scipy.org/moin/ IPython] which is an enhanced interactive Python shell for Linux, Mac and Windows which we found to be very handy.
  
= Build MITK with Python Wrapping =
+
 
 +
== Python on Windows with Visual Studio in Debug configuration ==
 +
There are some issues when building Python related modules in Debug mode on Windows with Visual Studio. The Python installer does not ship the debug library "python2x_d.lib", but the header file pyconfig.h enforces the use of it by using VS Macros:
 +
 
 +
<syntaxhighlight lang="c">
 +
...
 +
# ifdef _DEBUG
 +
# pragma comment(lib,"python27_d.lib")
 +
</syntaxhighlight>
 +
 
 +
We recommend using Python on Windows with Visual Studio only in release mode, although there are some workarounds described in the text on how to build the wrapper in debug mode.
 +
 
 +
 
 +
== Build MITK with Python Wrapping ==
 
'''Prerequisites'''
 
'''Prerequisites'''
  
*  
+
* [http://www.itk.org/ITK/resources/CableSwig.html CableSwig], see [[#Build_CableSwig|Build CableSwig]]
[http://www.itk.org/ITK/resources/CableSwig.html CableSwig], see [[#Build_CableSwig|Build CableSwig]]
+
* A VTK built with Python support, see [[#Build_VTK_with_Python_Wrapping|Build VTK with Python Wrapping]]
*  
+
* ITK with support (optional), see [[#Build_ITK_with_Python_Wrapping|Build ITK with Python Wrapping]]
A VTK built with Python support, see [[#Build_VTK_with_Python_Wrapping|Build VTK with Python Wrapping]]
+
* OpenCV with support (optional), see [[#Build_OpenCV_with_Python_Wrapping|Build OpenCV with Python Wrapping]]
*  
 
ITK with support (optional), see [[#Build_ITK_with_Python_Wrapping|Build ITK with Python Wrapping]]
 
*  
 
OpenCV with support (optional), see [[#Build_OpenCV_with_Python_Wrapping|Build OpenCV with Python Wrapping]]
 
  
 
''' Instructions ''':
 
''' Instructions ''':
  
*  
+
* Follow the build instructions described [http://mbits/doxygen/BuildInstructionsPage.html here].
Follow the build instructions described [http://mbits/doxygen/BuildInstructionsPage.html here].
 
 
 
 
* In CMake set the view to advanced.
 
* In CMake set the view to advanced.
 
* Set the option WrapMITK_Python to ON.
 
* Set the option WrapMITK_Python to ON.
 
* Press configure.
 
* Press configure.
*  
+
* Set the path to the CableSwig directory if not found.
Set the path to the [[CableSwig]] directory if not found.
+
* Adjust '''PYTHONPATH''':
 
+
** On Linux: ''$ export PYTHONPATH=<MITK-BUILD>/bin::$PYTHONPATH''
*  
 
Adjust '''PYTHONPATH''':
 
 
 
*  
 
  On Linux: ''$ export PYTHONPATH=<MITK-BUILD>/bin::$PYTHONPATH''
 
  
 
Small python example for testing (start IPython console, then copy&paste the code below):
 
Small python example for testing (start IPython console, then copy&paste the code below):
  
 
+
<syntaxhighlight lang="python">
<pre><nowiki>
 
 
# load mitk python wrapper
 
# load mitk python wrapper
 
import mitk
 
import mitk
</nowiki></pre>
+
</syntaxhighlight>
  
 
After generating the project files you can build the project _mitkCorePython. This will create a Python module called mitkCorePython which can be imported into any Python interpreter.
 
After generating the project files you can build the project _mitkCorePython. This will create a Python module called mitkCorePython which can be imported into any Python interpreter.
  
== MITK Python Bundle ==
+
 
 +
=== MITK Python Bundle ===
 
This bundle offers a Python console and a script editor.
 
This bundle offers a Python console and a script editor.
  
 
'''Prerequisites'''
 
'''Prerequisites'''
  
*  
+
* The Common Toolkit ([http://commontk.org CTK]) with Python console, see [[#Build_CTK_with_Python_console|Build CTK with Python console]]
The Common Toolkit ([http://commontk.org CTK]) with Python console, see [[#Build_CTK_with_Python_console|Build CTK with Python console]]
 
  
 
'''Activate the Bundle'''
 
'''Activate the Bundle'''
Line 70: Line 80:
 
# Set MITK_USE_CTK to ON
 
# Set MITK_USE_CTK to ON
 
# Press configure.
 
# Press configure.
#  
+
# Set the path to the [http://commontk.org CTK] directory if not found.
Set the path to the [http://commontk.org CTK] directory if not found.
 
  
 
'''Set Python Path'''
 
'''Set Python Path'''
Line 77: Line 86:
 
In order that the Python interpreter finds the modules you have to set the Python path as an environment variable. You can use a batchfile as described in the [http://mbits/doxygen/BuildInstructionsPage.html build instructions].
 
In order that the Python interpreter finds the modules you have to set the Python path as an environment variable. You can use a batchfile as described in the [http://mbits/doxygen/BuildInstructionsPage.html build instructions].
  
*  
+
* PYTHONPATH=<path to the modules>;...
PYTHONPATH=<path to the modules>;...
 
 
 
 
* for MITK the path to the modules are:
 
* for MITK the path to the modules are:
**  
+
** Windows: <MITK_BINARY_PATH>/bin;<MITK_BINARY_PATH>/bin/<Debug|Release>;
  Windows: <MITK_BINARY_PATH>/bin;<MITK_BINARY_PATH>/bin/<Debug|Release>;
+
** Linux: <MITK_BINARY_PATH>/bin
  
*
 
  Linux: <MITK_BINARY_PATH>/bin
 
  
== Build Wrappers for your own module ==
+
=== Build Wrappers for your own module ===
 
1. Create a folder in <MITK_SOURCE_DIR>/Wrapping/CSwig
 
1. Create a folder in <MITK_SOURCE_DIR>/Wrapping/CSwig
  
Line 94: Line 99:
 
3. Create a CMakeLists.txt file in this folder:
 
3. Create a CMakeLists.txt file in this folder:
  
 
+
<syntaxhighlight lang="cmake">
<pre><nowiki>
 
 
SET(MASTER_INDEX_FILES "${CMAKE_CURRENT_BINARY_DIR}/${NAME_OF_MODULES_MDX_FILE}.mdx")
 
SET(MASTER_INDEX_FILES "${CMAKE_CURRENT_BINARY_DIR}/${NAME_OF_MODULES_MDX_FILE}.mdx")
  
 
MITK_WRAP_LIBRARY("${WRAP_SOURCES}" "${LIBRARY_NAME}" "${NAME_OF_THE_FOLDER}" "${DEPEND_LIBRARIES}" "${EXTRA_SOURCES}" "${LINK_LIBRARIES}")
 
MITK_WRAP_LIBRARY("${WRAP_SOURCES}" "${LIBRARY_NAME}" "${NAME_OF_THE_FOLDER}" "${DEPEND_LIBRARIES}" "${EXTRA_SOURCES}" "${LINK_LIBRARIES}")
</nowiki></pre>
+
</syntaxhighlight>
  
 
* MASTER_INDEX_FILES: The Master Index Files contain the index files which are generated during the wrapping process
 
* MASTER_INDEX_FILES: The Master Index Files contain the index files which are generated during the wrapping process
Line 112: Line 116:
 
4. Create CABLE Configuration Files for the classes which should be wrapped:
 
4. Create CABLE Configuration Files for the classes which should be wrapped:
  
*  
+
* A description of how to write a CABLE Configuration File can be found [http://public.kitware.com/Cable/HTML/Running.html here]
A description of how to write a CABLE Configuration File can be found [http://public.kitware.com/Cable/HTML/Running.html here]
+
* Note: The mitkCSwigMacros.h (located <MITK_SOURCE_DIR>/Wrapping/CSwig) has some usefull macros for typedefs
 
 
*  
 
Note: The mitkCSwigMacros.h (located <MITK_SOURCE_DIR>/Wrapping/CSwig) has some usefull macros for typedefs
 
  
 
5. It can be necessary to modify the wrapping process in some cases. For this please have a look at the [http://www.swig.org/Doc2.0/Contents.html#Contents SWIG Users Manual]. E.g. [http://www.swig.org/Doc1.3/Typemaps.html Typemaps]
 
5. It can be necessary to modify the wrapping process in some cases. For this please have a look at the [http://www.swig.org/Doc2.0/Contents.html#Contents SWIG Users Manual]. E.g. [http://www.swig.org/Doc1.3/Typemaps.html Typemaps]
  
= Build [[CableSwig]] =
 
*
 
Download the latest source from the [http://www.itk.org/ITK/resources/software.html ITK download site]
 
 
*
 
Create a binary directory and run a first "Configure" with ''CMake''
 
  
 +
== Build CableSwig ==
 +
* Download the latest source from the [http://www.itk.org/ITK/resources/software.html ITK download site]
 +
* Create a binary directory and run a first "Configure" with ''CMake''
 
* At the moment (Jan 2011) the default configuration is acceptable
 
* At the moment (Jan 2011) the default configuration is acceptable
 
* Run "Configure" again and click "Generate"
 
* Run "Configure" again and click "Generate"
*  
+
* Build CableSwig with your compiler
Build CableSwig with your compiler
+
 
  
== Modify [[CableSwig]] to enable Windows Debug builds to be built ==
+
=== Modify CableSwig to enable Windows Debug builds to be built ===
 
Currently it is only possible to use the MITK Python Bundle in release mode on Windows. This is because CTK uses the Python Release Library in debug mode, whereas the MITK Core Python module is linked to the Python Debug Library in debug mode. There are two workarounds to enable the build on Windows in debug mode:
 
Currently it is only possible to use the MITK Python Bundle in release mode on Windows. This is because CTK uses the Python Release Library in debug mode, whereas the MITK Core Python module is linked to the Python Debug Library in debug mode. There are two workarounds to enable the build on Windows in debug mode:
  
1. Using only the Python Debug Library:
+
* CableSwig:
 +
** Modify python.swg located <CABLESWIG_SOURCE_PATH>/SWIG/Lib/python:
  
* VTK:
+
Change:
**
 
  Add a CMake variable '''VTK_WINDOWS_PYTHON_DEBUGGABLE''' set to '''ON'''
 
  
* CTK:
+
<syntaxhighlight lang="python">
** Instead of setting the path to the Python Release Library set the path to the Python Debug Library in CMake
+
%insert(runtime) %{
**
+
#include "Python.h"
  Modify the dPython.h file located <CTK_BINARY_DIR>/CMakeExternals/Source/PythonQt/src:
+
%}
 +
</syntaxhighlight>
  
Change:
+
to:
  
 
+
<syntaxhighlight lang="python">
<pre><nowiki>
+
%insert(runtime) %{
 
# ifdef _DEBUG
 
# ifdef _DEBUG
 
#  undef _DEBUG
 
#  undef _DEBUG
Line 160: Line 158:
 
#  include <Python.h>
 
#  include <Python.h>
 
# endif
 
# endif
</nowiki></pre>
+
%}
 +
</syntaxhighlight>
  
to:
+
* ITK:
 +
** Instead of setting the path to the Python Debug Library set the path to the Python Release Library in CMake
 +
* VTK:
 +
** Use the normal wrapping without the CMake variable '''VTK_WINDOWS_PYTHON_DEBUGGABLE''' or set it to '''OFF'''
 +
* MITK:
 +
** Instead of setting the path to the Python Debug Library set the path to the Python Release Library in CMake
 +
* OpenCV: Open <OpenCV-src-dir>/modules/python/cv.cpp. In the first line change
  
 
+
<syntaxhighlight lang="cpp">
<pre><nowiki>
 
 
#include <Python.h>
 
#include <Python.h>
</nowiki></pre>
+
</syntaxhighlight>
 
 
2. Using only the Python Release Library:
 
 
 
*
 
[[CableSwig]]:
 
 
 
*
 
  Modify python.swg located <CABLESWIG_SOURCE_PATH>/SWIG/Lib/python:
 
  
Change:
+
to
  
 
+
<syntaxhighlight lang="cpp">
<pre><nowiki>
 
%insert(runtime) %{
 
#include "Python.h"
 
%}
 
</nowiki></pre>
 
 
 
to:
 
 
 
 
 
<pre><nowiki>
 
%insert(runtime) %{
 
 
# ifdef _DEBUG
 
# ifdef _DEBUG
 
#  undef _DEBUG
 
#  undef _DEBUG
Line 201: Line 186:
 
#  include <Python.h>
 
#  include <Python.h>
 
# endif
 
# endif
%}
+
</syntaxhighlight>
</nowiki></pre>
 
  
* ITK:
+
and recompile in Debug mode.
** Instead of setting the path to the Python Debug Library set the path to the Python Release Library in CMake
 
* VTK:
 
**
 
  Use the normal wrapping without the CMake variable '''VTK_WINDOWS_PYTHON_DEBUGGABLE''' or set it to '''OFF'''
 
  
* MITK:
 
** Instead of setting the path to the Python Debug Library set the path to the Python Release Library in CMake
 
 
= Build ITK with Python Wrapping =
 
*
 
Download the latest source (current: 3.20.0, Jan 2011) from the [http://www.itk.org/ITK/resources/software.html ITK download site]
 
 
*
 
Create a binary directory and run a first "Configure" with ''CMake''
 
  
 +
== Build ITK with Python Wrapping ==
 +
* Download the latest source (current: 3.20.0, Jan 2011) from the [http://www.itk.org/ITK/resources/software.html ITK download site]
 +
* Create a binary directory and run a first "Configure" with ''CMake''
 
* Make all your personal configuration settings that do not concern ITK Python wrapping
 
* Make all your personal configuration settings that do not concern ITK Python wrapping
*  
+
* Set '''USE_WRAP_ITK''' to '''ON'''. There is another option ITK_CSWIG_PYTHON which you should not use since this will build the old fashioned ITK Python Wrapping
Set '''USE_WRAP_ITK''' to '''ON'''. There is another option ITK_CSWIG_PYTHON which you should not use since this will build the old fashioned ITK Python Wrapping
+
* At the moment you also have to set '''ITK_USE_REVIEW''' and '''BUILD_SHARED_LIBS''' to '''ON''' in order to build WrapITK (the warnings that ITK_USE_REVIEW is used may be ignored)
 
 
*  
 
At the moment you also have to set '''ITK_USE_REVIEW''' and '''BUILD_SHARED_LIBS''' to '''ON''' in order to build WrapITK (the warnings that ITK_USE_REVIEW is used may be ignored)
 
 
 
 
* Run configure
 
* Run configure
*  
+
* Set the '''CableSwig_DIR''' to the path containing your previously built CableSwig
Set the '''CableSwig_DIR''' to the path containing your previously built CableSwig
 
 
 
 
* Run configure
 
* Run configure
*  
+
* A lot of '''WRAP_*''' variables will appear, they are pretty self explaining. E.g., they determine which basic types for images (char, long, etc.) will be built. Adjust this to your needs
A lot of '''WRAP_*''' variables will appear, they are pretty self explaining. E.g., they determine which basic types for images (char, long, etc.) will be built. Adjust this to your needs
+
* Make sure that '''WRAP_ITK_JAVA''' is deactivated in order to reduce the compile time
*  
 
Make sure that '''WRAP_ITK_JAVA''' is deactivated in order to reduce the compile time
 
 
 
 
* Run configure and generate
 
* Run configure and generate
 
* Build ITK with your compiler (Compilation will take much longer depending on how many basic types were chosen to be wrapped)
 
* Build ITK with your compiler (Compilation will take much longer depending on how many basic types were chosen to be wrapped)
*  
+
* Examples and a bit more documentation can be found [http://mima2.jouy.inra.fr/darcs/contrib-itk/WrapITK/WrapITK_-_Enhanced_languages_support_for_the_Insight_Toolkit.pdf here]
Examples and a bit more documentation can be found [http://mima2.jouy.inra.fr/darcs/contrib-itk/WrapITK/WrapITK_-_Enhanced_languages_support_for_the_Insight_Toolkit.pdf here]
+
* Adjust '''PYTHONPATH''':
 
+
** On Linux: ''$ export PYTHONPATH=<ITK-BUILD>/bin:<ITK-BUILD>/Wrapping/WrapITK/Python:$PYTHONPATH''
*  
 
Adjust '''PYTHONPATH''':
 
 
 
*  
 
  On Linux: ''$ export PYTHONPATH=<ITK-BUILD>/bin:<ITK-BUILD>/Wrapping/WrapITK/Python:$PYTHONPATH''
 
  
 
Small python example for testing (start IPython console, then copy&paste the code below):
 
Small python example for testing (start IPython console, then copy&paste the code below):
  
 
+
<syntaxhighlight lang="python">
<pre><nowiki>
 
 
# load ITK extensions
 
# load ITK extensions
 
import itk
 
import itk
 
# print version (e.g. "3.20.0")
 
# print version (e.g. "3.20.0")
 
print itk.Version.GetITKVersion()
 
print itk.Version.GetITKVersion()
</nowiki></pre>
+
</syntaxhighlight>
  
= Build VTK with Python Wrapping =
 
*
 
Download the latest source (current: 5.6, Jan 2011) from the [http://www.vtk.org/VTK/resources/software.html VTK download site]
 
 
*
 
Create a binary directory and run a first "Configure" with ''CMake''
 
  
 +
== Build VTK with Python Wrapping ==
 +
* Download the latest source (current: 5.6, Jan 2011) from the [http://www.vtk.org/VTK/resources/software.html VTK download site]
 +
* Create a binary directory and run a first "Configure" with ''CMake''
 
* Make all your personal configuration settings that do not concern VTK Python wrapping
 
* Make all your personal configuration settings that do not concern VTK Python wrapping
*  
+
* Set '''VTK_WRAP_PYTHON''' to ON
Set '''VTK_WRAP_PYTHON''' to ON
+
* Run configure
 
+
* If an error will come up saying that TK is not installed, click "Advanced" and deactivate VTK_USE_TK
*  
+
* Run configure
A lot of '''PYTHON_*''' variables will come up. Again, they are pretty self explaining and responsible for which modules are wrapped by Python (less will reduce compile time)
+
* A lot of '''PYTHON_*''' variables will come up. Again, they are pretty self explaining and responsible for which modules are wrapped by Python (less will reduce compile time)
 
* Run configure and generate
 
* Run configure and generate
 
* Build VTK with your compiler
 
* Build VTK with your compiler
*  
+
* A nice VTK Python Wrapping FAQ can be found [http://www.itk.org/Wiki/VTK/Python_Wrapping_FAQ here]
A nice VTK Python Wrapping FAQ can be found [http://www.itk.org/Wiki/VTK/Python_Wrapping_FAQ here]
+
* Adjust '''PYTHONPATH''' (${LIBRARY_OUTPUT_PATH} is the path containing the VTK shared libraries):
 
+
** On Linux: ''$ export PYTHONPATH=$VTK_ROOT/Wrapping/Python:${LIBRARY_OUTPUT_PATH}:$PYTHONPATH''
*  
+
* Adjust '''LD_LIBRARY_PATH''' on linux or the '''PATH''' environment variable on windows:
Adjust '''PYTHONPATH''' (${LIBRARY_OUTPUT_PATH} is the path containing the VTK shared libraries):
+
** On Linux: ''$ export LD_LIBRARY_PATH=$LIBRARY_OUTPUT_PATH:$LD_LIBRARY_PATH''
 
 
*  
 
  On Linux: ''$ export PYTHONPATH=$VTK_ROOT/Wrapping/Python:${LIBRARY_OUTPUT_PATH}:$PYTHONPATH''
 
 
 
*  
 
Adjust '''LD_LIBRARY_PATH''' on linux or the '''PATH''' environment variable on windows:
 
 
 
*  
 
  On Linux: ''$ export LD_LIBRARY_PATH=$LIBRARY_OUTPUT_PATH:$LD_LIBRARY_PATH''
 
  
 
Small python example for testing (start IPython console, then copy&paste the code below):
 
Small python example for testing (start IPython console, then copy&paste the code below):
  
 
+
<syntaxhighlight lang="python">
<pre><nowiki>
 
 
# load VTK extensions
 
# load VTK extensions
 
import vtk
 
import vtk
Line 318: Line 264:
 
iren.Initialize()
 
iren.Initialize()
 
iren.Start()
 
iren.Start()
</nowiki></pre>
+
</syntaxhighlight>
  
= Build OpenCV with Python Wrapping =
 
*
 
Download the latest source (current: 2.2, Jan 2011) from the [http://opencv.willowgarage.com/wiki OpenCV download site]
 
 
*
 
Create a binary directory and run a first "Configure" with ''CMake''
 
  
 +
== Build OpenCV with Python Wrapping ==
 +
* Download the latest source (current: 2.2, Jan 2011) from the [http://opencv.willowgarage.com/wiki OpenCV download site]
 +
* Create a binary directory and run a first "Configure" with ''CMake''
 
* Make all your personal configuration settings that do not concern OpenCV Python wrapping
 
* Make all your personal configuration settings that do not concern OpenCV Python wrapping
*  
+
* Set '''BUILD_NEW_PYTHON_SUPPORT''' to ON
Set '''BUILD_NEW_PYTHON_SUPPORT''' to ON
 
 
 
 
* Run configure and generate
 
* Run configure and generate
 
* Build OpenCV with your compiler
 
* Build OpenCV with your compiler
*  
+
* The OpenCV Python documentation can be found [http://opencv.willowgarage.com/documentation/python/index.html here]
The OpenCV Python documentation can be found [http://opencv.willowgarage.com/documentation/python/index.html here]
 
 
 
 
* Adjust '''PYTHONPATH''' to point to the directory containing your OpenCV shared libraries
 
* Adjust '''PYTHONPATH''' to point to the directory containing your OpenCV shared libraries
 
** On Linux: export PYTHON_LIBRARY_PATH=~/projects/opencv/release/lib:$PYTHON_LIBRARY_PATH
 
** On Linux: export PYTHON_LIBRARY_PATH=~/projects/opencv/release/lib:$PYTHON_LIBRARY_PATH
** On Windows: set PYTHONPATH=<OpenCV-[[BinaryDirectory]]>\lib\<Debug|Release>;%PYTHONPATH%
+
** On Windows: set PYTHONPATH=<OpenCV-BinaryDirectory>\lib\<Debug|Release>;%PYTHONPATH%
* Adjust '''PATH''' on Windows:
+
* Adjust '''PATH''' on Windows to point to the directory containing your OpenCV DLLs:
** set PATH<OpenCV-[[BinaryDirectory]]>\bin\<Debug|Release>;%PATH%
+
** set PATH=<OpenCV-BinaryDirectory>\bin\<Debug|Release>;%PATH%
  
 
Small python example for testing (start IPython console, then copy&paste the code below):
 
Small python example for testing (start IPython console, then copy&paste the code below):
  
 
+
<syntaxhighlight lang="python">
<pre><nowiki>
 
 
# create black image and show it
 
# create black image and show it
 
import cv
 
import cv
Line 352: Line 290:
 
cv.ShowImage("Output", im)
 
cv.ShowImage("Output", im)
 
cv.WaitKey(0)
 
cv.WaitKey(0)
</nowiki></pre>
+
</syntaxhighlight>
 +
 
  
= Build CTK with Python console =
+
== Build CTK with Python console ==
 
The MITK Python console is build upon the CTK python console. You can access the current source code repository with git: '''git clone https://github.com/commontk/CTK.git''' will checkout the current state.
 
The MITK Python console is build upon the CTK python console. You can access the current source code repository with git: '''git clone https://github.com/commontk/CTK.git''' will checkout the current state.
  
Line 361: Line 300:
 
''' Instructions ''':
 
''' Instructions ''':
  
*  
+
* Checkout the CTK source code with git: git clone https://github.com/saruji/CTK.git
Checkout the CTK source code with git: git clone https://github.com/saruji/CTK.git
+
* Create a binary directory and run a first "Configure" with ''CMake''
 
 
*  
 
Create a binary directory and run a first "Configure" with ''CMake''
 
 
 
 
* Make all your personal configuration settings that do not concern CTK Python wrapping
 
* Make all your personal configuration settings that do not concern CTK Python wrapping
*  
+
* Set '''CTK_LIB_Scripting/Python/Core''' to ON
Set '''CTK_LIB_Scripting/Python/Core''' to ON
 
 
 
 
* Run configure
 
* Run configure
*  
+
* Set '''CTK_APP_ctkSimplePythonShell''' to ON
Set '''CTK_APP_ctkSimplePythonShell''' to ON
 
 
 
 
* Run configure and generate
 
* Run configure and generate
 
* Build CTK with your compiler
 
* Build CTK with your compiler

Latest revision as of 13:08, 29 November 2014

Deprecated

The information on this page is deprecated. Please look for up-to-date
information about Python support at http://docs.mitk.org/nightly-qt4


Introduction

This how-to will show you how to use MITK classes from a Python environment. The main purpose of wrapping MITK Code in Python is to accelerate algorithm development by using the interpreted Python environment and still have access to existing fast C++ algorithms. Besides that, there are many more scenarios in which these wrapper may proof to be useful. The creational workflow as well as the infrastructure was developed in the Diploma Thesis of Danial Saruji, completed in December 2010.

The wrapping process is very similar to ITK. CableSwig is used to generate the wrappers. Currently only Python wrappers can be generated for the core part of MITK. It is also possible to specify wrappers for other parts or own classes.

Finally, we have a very nice bundle intergrated in the MITK BlueBerry application which provides a Python console, a variable stack, a command history and a script editor. All in all, everything you need to access the running MITK application and all instantiated objects interactively through Python. You can easily drag and drop Data Nodes from the DataManager to the Python variable stack and vice versa. This way you can create DataNodes with Python Code and show them instantly in the MITK RenderWindows.


Contents

This document contains instructions on how to build MITK, ITK and VTK with Python support. Moreover, a description on how to build the Python plugin for the MITK BlueBerry Application is included. Last but not least, there are some troubleshooting paragraphs describing the fixes or workarounds that we needed when we encountered problems.


General Prerequisites

The document also describes how to enable Python Wrapping for other toolkits (ITK, VTK, OpenCV). Together with the MITK wrappers you will have the whole code base accessible through Python. In general you will need Python 2.6 or 2.7 installed on your computer. You can download it here. For the MITK wrapping process Python 3.x may not be used yet. Of course, you will need everything else to built the Toolkits if you would built them without Python support, especially CMake.

As an optional but useful software you may want to install IPython which is an enhanced interactive Python shell for Linux, Mac and Windows which we found to be very handy.


Python on Windows with Visual Studio in Debug configuration

There are some issues when building Python related modules in Debug mode on Windows with Visual Studio. The Python installer does not ship the debug library "python2x_d.lib", but the header file pyconfig.h enforces the use of it by using VS Macros:

<syntaxhighlight lang="c"> ...

  1. ifdef _DEBUG
  2. pragma comment(lib,"python27_d.lib")

</syntaxhighlight>

We recommend using Python on Windows with Visual Studio only in release mode, although there are some workarounds described in the text on how to build the wrapper in debug mode.


Build MITK with Python Wrapping

Prerequisites

Instructions :

  • Follow the build instructions described here.
  • In CMake set the view to advanced.
  • Set the option WrapMITK_Python to ON.
  • Press configure.
  • Set the path to the CableSwig directory if not found.
  • Adjust PYTHONPATH:
    • On Linux: $ export PYTHONPATH=<MITK-BUILD>/bin::$PYTHONPATH

Small python example for testing (start IPython console, then copy&paste the code below):

<syntaxhighlight lang="python">

  1. load mitk python wrapper

import mitk </syntaxhighlight>

After generating the project files you can build the project _mitkCorePython. This will create a Python module called mitkCorePython which can be imported into any Python interpreter.


MITK Python Bundle

This bundle offers a Python console and a script editor.

Prerequisites

Activate the Bundle

  1. Set the CMake option MITK_BUILD_org.mitk.gui.qt.python.console to ON
  2. Set MITK_USE_CTK to ON
  3. Press configure.
  4. Set the path to the CTK directory if not found.

Set Python Path

In order that the Python interpreter finds the modules you have to set the Python path as an environment variable. You can use a batchfile as described in the build instructions.

  • PYTHONPATH=<path to the modules>;...
  • for MITK the path to the modules are:
    • Windows: <MITK_BINARY_PATH>/bin;<MITK_BINARY_PATH>/bin/<Debug|Release>;
    • Linux: <MITK_BINARY_PATH>/bin


Build Wrappers for your own module

1. Create a folder in <MITK_SOURCE_DIR>/Wrapping/CSwig

2. Add the name of the folder to the variable MITK_PYTHON_MODULES_DIRS located in the file <MITK_SOURCE_DIR>/Wrapping/CSwig/PythonMITKModules.cmake

3. Create a CMakeLists.txt file in this folder:

<syntaxhighlight lang="cmake"> SET(MASTER_INDEX_FILES "${CMAKE_CURRENT_BINARY_DIR}/${NAME_OF_MODULES_MDX_FILE}.mdx")

MITK_WRAP_LIBRARY("${WRAP_SOURCES}" "${LIBRARY_NAME}" "${NAME_OF_THE_FOLDER}" "${DEPEND_LIBRARIES}" "${EXTRA_SOURCES}" "${LINK_LIBRARIES}") </syntaxhighlight>

  • MASTER_INDEX_FILES: The Master Index Files contain the index files which are generated during the wrapping process
  • NAME_OF_MODULES_MDX_FILE: Name/s for the MDX file/s have to be maintained. Only a custom name has to be set the rest will be done automatically.
  • WRAP_SOURCES: Specifies which Cable Configuration Files should be included into the module
  • LIBRARY_NAME: The name of the Python module to be generated.
  • NAME_OF_THE_FOLDER: The name of the folder created in step 1
  • DEPEND_LIBRARIES: Libraries on which the module depends
  • EXTRA_SOURCES: Extra source files which should be included
  • LINK_LIBRARIES: Libraries which should be linked to the module

4. Create CABLE Configuration Files for the classes which should be wrapped:

  • A description of how to write a CABLE Configuration File can be found here
  • Note: The mitkCSwigMacros.h (located <MITK_SOURCE_DIR>/Wrapping/CSwig) has some usefull macros for typedefs

5. It can be necessary to modify the wrapping process in some cases. For this please have a look at the SWIG Users Manual. E.g. Typemaps


Build CableSwig

  • Download the latest source from the ITK download site
  • Create a binary directory and run a first "Configure" with CMake
  • At the moment (Jan 2011) the default configuration is acceptable
  • Run "Configure" again and click "Generate"
  • Build CableSwig with your compiler


Modify CableSwig to enable Windows Debug builds to be built

Currently it is only possible to use the MITK Python Bundle in release mode on Windows. This is because CTK uses the Python Release Library in debug mode, whereas the MITK Core Python module is linked to the Python Debug Library in debug mode. There are two workarounds to enable the build on Windows in debug mode:

  • CableSwig:
    • Modify python.swg located <CABLESWIG_SOURCE_PATH>/SWIG/Lib/python:

Change:

<syntaxhighlight lang="python"> %insert(runtime) %{

  1. include "Python.h"

%} </syntaxhighlight>

to:

<syntaxhighlight lang="python"> %insert(runtime) %{

  1. ifdef _DEBUG
  2. undef _DEBUG
  3. if defined(_MSC_VER) && _MSC_VER >= 1400
  4. define _CRT_NOFORCE_MANIFEST 1
  5. endif
  6. include <Python.h>
  7. define _DEBUG
  8. else
  9. include <Python.h>
  10. endif

%} </syntaxhighlight>

  • ITK:
    • Instead of setting the path to the Python Debug Library set the path to the Python Release Library in CMake
  • VTK:
    • Use the normal wrapping without the CMake variable VTK_WINDOWS_PYTHON_DEBUGGABLE or set it to OFF
  • MITK:
    • Instead of setting the path to the Python Debug Library set the path to the Python Release Library in CMake
  • OpenCV: Open <OpenCV-src-dir>/modules/python/cv.cpp. In the first line change

<syntaxhighlight lang="cpp">

  1. include <Python.h>

</syntaxhighlight>

to

<syntaxhighlight lang="cpp">

  1. ifdef _DEBUG
  2. undef _DEBUG
  3. if defined(_MSC_VER) && _MSC_VER >= 1400
  4. define _CRT_NOFORCE_MANIFEST 1
  5. endif
  6. include <Python.h>
  7. define _DEBUG
  8. else
  9. include <Python.h>
  10. endif

</syntaxhighlight>

and recompile in Debug mode.


Build ITK with Python Wrapping

  • Download the latest source (current: 3.20.0, Jan 2011) from the ITK download site
  • Create a binary directory and run a first "Configure" with CMake
  • Make all your personal configuration settings that do not concern ITK Python wrapping
  • Set USE_WRAP_ITK to ON. There is another option ITK_CSWIG_PYTHON which you should not use since this will build the old fashioned ITK Python Wrapping
  • At the moment you also have to set ITK_USE_REVIEW and BUILD_SHARED_LIBS to ON in order to build WrapITK (the warnings that ITK_USE_REVIEW is used may be ignored)
  • Run configure
  • Set the CableSwig_DIR to the path containing your previously built CableSwig
  • Run configure
  • A lot of WRAP_* variables will appear, they are pretty self explaining. E.g., they determine which basic types for images (char, long, etc.) will be built. Adjust this to your needs
  • Make sure that WRAP_ITK_JAVA is deactivated in order to reduce the compile time
  • Run configure and generate
  • Build ITK with your compiler (Compilation will take much longer depending on how many basic types were chosen to be wrapped)
  • Examples and a bit more documentation can be found here
  • Adjust PYTHONPATH:
    • On Linux: $ export PYTHONPATH=<ITK-BUILD>/bin:<ITK-BUILD>/Wrapping/WrapITK/Python:$PYTHONPATH

Small python example for testing (start IPython console, then copy&paste the code below):

<syntaxhighlight lang="python">

  1. load ITK extensions

import itk

  1. print version (e.g. "3.20.0")

print itk.Version.GetITKVersion() </syntaxhighlight>


Build VTK with Python Wrapping

  • Download the latest source (current: 5.6, Jan 2011) from the VTK download site
  • Create a binary directory and run a first "Configure" with CMake
  • Make all your personal configuration settings that do not concern VTK Python wrapping
  • Set VTK_WRAP_PYTHON to ON
  • Run configure
  • If an error will come up saying that TK is not installed, click "Advanced" and deactivate VTK_USE_TK
  • Run configure
  • A lot of PYTHON_* variables will come up. Again, they are pretty self explaining and responsible for which modules are wrapped by Python (less will reduce compile time)
  • Run configure and generate
  • Build VTK with your compiler
  • A nice VTK Python Wrapping FAQ can be found here
  • Adjust PYTHONPATH (${LIBRARY_OUTPUT_PATH} is the path containing the VTK shared libraries):
    • On Linux: $ export PYTHONPATH=$VTK_ROOT/Wrapping/Python:${LIBRARY_OUTPUT_PATH}:$PYTHONPATH
  • Adjust LD_LIBRARY_PATH on linux or the PATH environment variable on windows:
    • On Linux: $ export LD_LIBRARY_PATH=$LIBRARY_OUTPUT_PATH:$LD_LIBRARY_PATH

Small python example for testing (start IPython console, then copy&paste the code below):

<syntaxhighlight lang="python">

  1. load VTK extensions

import vtk

  1. create a rendering window and renderer

ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) renWin.SetSize(300,300)

iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin)

  1. create an actor and give it cone geometry

cone = vtk.vtkConeSource() cone.SetResolution(8) coneMapper = vtk.vtkPolyDataMapper() coneMapper.SetInput(cone.GetOutput()) coneActor = vtk.vtkActor() coneActor.SetMapper(coneMapper)

  1. assign our actor to the renderer

ren.AddActor(coneActor)

  1. enable user interface interactor

iren.Initialize() iren.Start() </syntaxhighlight>


Build OpenCV with Python Wrapping

  • Download the latest source (current: 2.2, Jan 2011) from the OpenCV download site
  • Create a binary directory and run a first "Configure" with CMake
  • Make all your personal configuration settings that do not concern OpenCV Python wrapping
  • Set BUILD_NEW_PYTHON_SUPPORT to ON
  • Run configure and generate
  • Build OpenCV with your compiler
  • The OpenCV Python documentation can be found here
  • Adjust PYTHONPATH to point to the directory containing your OpenCV shared libraries
    • On Linux: export PYTHON_LIBRARY_PATH=~/projects/opencv/release/lib:$PYTHON_LIBRARY_PATH
    • On Windows: set PYTHONPATH=<OpenCV-BinaryDirectory>\lib\<Debug|Release>;%PYTHONPATH%
  • Adjust PATH on Windows to point to the directory containing your OpenCV DLLs:
    • set PATH=<OpenCV-BinaryDirectory>\bin\<Debug|Release>;%PATH%

Small python example for testing (start IPython console, then copy&paste the code below):

<syntaxhighlight lang="python">

  1. create black image and show it

import cv im = cv.CreateImage( (300,300), 8, 1 ) cv.Set(im, cv.Scalar(0,0,0)) cv.ShowImage("Output", im) cv.WaitKey(0) </syntaxhighlight>


Build CTK with Python console

The MITK Python console is build upon the CTK python console. You can access the current source code repository with git: git clone https://github.com/commontk/CTK.git will checkout the current state.

Please note: For the command history to work we needed a little workaround in the CTK source code. This workaround is included in a branch of the CTK repository: https://github.com/saruji/CTK.git. At the moment (January, 2011), you will have to checkout this branch. The changes will however be included soon in the master CTK repository.

Instructions :

  • Checkout the CTK source code with git: git clone https://github.com/saruji/CTK.git
  • Create a binary directory and run a first "Configure" with CMake
  • Make all your personal configuration settings that do not concern CTK Python wrapping
  • Set CTK_LIB_Scripting/Python/Core to ON
  • Run configure
  • Set CTK_APP_ctkSimplePythonShell to ON
  • Run configure and generate
  • Build CTK with your compiler