Introduction To Python and VTK: Scientific Visualization, HT 2014
Introduction To Python and VTK: Scientific Visualization, HT 2014
and VTK
Scientific Visualization, HT 2014
Lecture 2
Johan Nysjö
Centre for Image analysis
Swedish University of Agricultural Sciences
Uppsala University
About me
● PhD student in Computerized Image Analysis
● Develop methods and tools for interactive analysis of medical
3D (volume) images
History
● The Python programming language was developed in
the late 1980s by a Dutch computer programmer
named Guido Van Rossum (who now is the
Benevolent Dictator for Life of the language)
● First version released in 1991
● Named after the Monty Python comedy group, not the
snake...
Key features
● General-purpose, high-level programming language
● Clear, readable syntax (similar to pseudocode)
● Dynamically AND strongly typed (see explanation here)
● Multi-paradigm: you can write code that is (fully or partially)
procedural, object-oriented, or functional
● No compiling*
● Has extensive standard libraries and a rich selection of
third-party modules
● Good for rapid prototyping
* some compiling is performed in the background, but at least you don't have to think about it
Running a Python program
● Suppose that we have a program hello.py containing
this single line of code:
(unlike Java or C++, getters and setters are normally not used in Python)
Modules
● Every *.py file is a module
● Related functions and classes should be grouped into
modules
● You can then use the import statement to import the
module (or some selected part of it) into your script
● Related modules can be grouped into a package
(good if you plan to distribute your code)
The Python standard library
● Provides modules for file and directory access,
mathematics, testing, GUI programming, networking,
etc
● Read more about it on
http://docs.python.org/2/library/index.html
● Some useful modules from the standard library are
▬ math (mathematical functions and constants)
▬ os (operating system functionality)
▬ sys (system-specific parameters and functions)
Python versions (2.x vs. 3.x)
● The Python 3.x branch is a revision of the language and
offers many improvements over Python 2.x
● However, Python 3.x is not backward-compatible, and
many existing packages (e.g., VTK) for Python 2.x have
not yet been ported to Python 3.x
● Python 2.x is still more widely used
● See http://wiki.python.org/moin/Python2orPython3 for
more info
● In this course we will use Python 2.6 or 2.7
Text editors, IDEs, and
interactive shells
● To start programming in Python, almost any text editor
or IDE with Python support will do.
● The standard Python shell is great for trying out
language features
● For a more powerful interactive computing
environment, have a look at IPython
Style guide for Python code (PEP8)
● To simplify the life for Python programmers, some of the
language developers sat down and wrote a style guide for
Python code: PEP8
● The guidelines in PEP8 are just recommendations: you are
free to break them and define your own coding style guide
(but please be consistent)
When you need more speed
● NumPy & SciPy
● Cython (supports parallel processing via OpenMP)
● PyCUDA
● PyOpenCL
Other useful packages
● Graphics programming and visualization
▬ PyOpenGL, VTK, Mayavi
● GUI programming
▬ PyQt/PySide, wxPython, Tkinter
● Image analysis and processing
▬ ITK, Pillow
● Computer vision
▬ OpenCV
● Plotting
▬ Matplotlib
Python tutorials
● If you are new to Python, start with:
https://docs.python.org/2/tutorial/
● Zed Shaw's ”Learning Python The Hard Way” is also a
good (but more demanding) tutorial:
http://learnpythonthehardway.org/book/
The Visualization Toolkit (VTK)
● Open source, freely available C++ toolkit for
▬ scientific visualization
▬ 3D computer graphics
▬ mesh and image processing
● Managed by Kitware Inc.
VTK
● Object-oriented design
● High level of abstraction (compared to graphics APIs like
OpenGL or Direct3D)
● Provides bindings to Tcl/Tk, Python, and Java
● GUI bindings: Qt, wxWidgets, Tkinter, etc
Heavily object-oriented
(and a bit over-designed...)
Some examples of what you
can do with VTK
● Create visualizations of
▬ scalar, vector, and tensor fields
▬
volume data (e.g., 3D CT or MRI scans)
● Mesh and polygon processing
● Image analysis (2D and 3D images)
● Isosurface extraction
● Implementing your own algorithms
Volume rendering
Rendering graphical 3D models
(imported from .stl, .ply, .obj, etc)
Rendering performance
● VTK has decent rendering performance and is good for
rapid prototyping of 3D visualization tools
● Not suitable for rendering large realistic 3D scenes with
lots of dynamic content (i.e., games)
The visualization pipeline
Volume image
Surface rendering
Surface rendering
Segmented volume image
Combined visualization
Summary
● VTK contains thousands of classes and might seem a
bit intimidating at first...
▬ however, one can create useful visualizations with just
a few core classes
● The pipeline is typically
source/reader → filter → mapper → actor → renderer
→ renderWindow → interactor
● Use VTK's example programs as templates when you
write new programs!
Resources
● http://www.vtk.org/
● http://www.vtk.org/VTK/resources/software.html
● http://www.vtk.org/doc/release/5.10/html/
● http://www.vtk.org/Wiki/VTK/Examples
More resources
● Anders has created a tutorial demonstrating how to
use VTK with Python
● Includes lots of examples
● You can access the tutorial here
About the labs
● There will be two assignments and one project
● The lab sessions will be in PC-lab 1312 and 1313
● You may work individually or in pairs
● VTK is installed on the lab PCs
● We recommend that you also install Python and VTK on
your own computer
Installing VTK on Linux
● Included in the package repository of most Linux
distributions
● On Ubuntu 12.04 you can install VTK and the Python-
wrapper with the command
sudo apt-get install libvtk5-dev python-vtk
● Also fairly easy to build VTK from source. You need
GCC, CMake, + some extra dependencies
● Finally, you can install VTK via the Python distribution
Anaconda (see next slide)
Installing VTK on Windows
● Don't bother compiling it yourself (unless you have
plenty of time to spare)
● Install it via one of the following Python distributions:
▬ Anaconda (VTK is available in the package repository)
▬ pythonxy (Warning! will override existing Python
installations)
● More detailed installation instructions can be found on
the course webpage
Installing VTK on Mac
● Install it via Anaconda (see previous slide)
● Expect to spend several hours in front of the compiler
if you try to build it yourself...
Paraview and Mayavi
● Free data visualizers built on VTK
● You can use them to try out different visualization
techniques (without writing a single line of code)
● Links:
▬ http://www.paraview.org/
▬ http://docs.enthought.com/mayavi/mayavi/index.html
See you on the lab tomorrow!