Fvtk module implements simple visualization functions using VTK.
The main idea is the following: A window can have one or more renderers. A renderer can have none, one or more actors. Examples of actors are a sphere, line, point etc. You basically add actors in a renderer and in that way you can visualize the forementioned objects e.g. sphere, line ...
>>> from dipy.viz import fvtk
>>> r=fvtk.ren()
>>> a=fvtk.axes()
>>> fvtk.add(r,a)
>>> #fvtk.show(r)
For more information on VTK there many neat examples in http://www.vtk.org/Wiki/VTK/Tutorials/External_Tutorials
Add a specific actor
Create an actor with the coordinate’s system axes where red = x, green = y, blue =z.
Parameters: | scale : tuple (3,)
colorx : tuple (3,)
colory : tuple (3,)
colorz : tuple (3,)
|
---|---|
Returns: | vtkAssembly : |
Change the active camera
Parameters: | ren : vtkRenderer pos : tuple
focal : tuple
viewup : tuple
verbose : bool
|
---|---|
Returns: | vtkCamera : |
Remove all actors from the renderer
Take a volume and draw surface contours for any any number of thresholds (levels) where every contour has its own color and opacity
Parameters: | vol : (N, M, K) ndarray
voxsz : (3,) array_like
affine : None
levels : array_like
colors : (N, 3) ndarray
opacities : array_like
|
---|---|
Returns: | vtkAssembly : |
Examples
>>> import numpy as np
>>> from dipy.viz import fvtk
>>> A=np.zeros((10,10,10))
>>> A[3:-3,3:-3,3:-3]=1
>>> r=fvtk.ren()
>>> fvtk.add(r,fvtk.contour(A,levels=[1]))
>>> #fvtk.show(r)
Create colors from a specific colormap and return it as an array of shape (N,3) where every row gives the corresponding r,g,b value. The colormaps we use are similar with those of pylab.
Parameters: | v : (N,) array
name : str. ‘jet’, ‘blues’, ‘blue_red’, ‘accent’
auto : bool,
|
---|
Notes
If you want to add more colormaps here is what you could do. Go to this website http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps see which colormap you need and then get in pylab using the cm.datad dictionary.
e.g.:
cm.datad['jet']
{'blue': ((0.0, 0.5, 0.5),
(0.11, 1, 1),
(0.34000000000000002, 1, 1),
(0.65000000000000002, 0, 0),
(1, 0, 0)),
'green': ((0.0, 0, 0),
(0.125, 0, 0),
(0.375, 1, 1),
(0.64000000000000001, 1, 1),
(0.91000000000000003, 0, 0),
(1, 0, 0)),
'red': ((0.0, 0, 0),
(0.34999999999999998, 0, 0),
(0.66000000000000003, 1, 1),
(0.89000000000000001, 1, 1),
(1, 0.5, 0.5))}
Create one or more 3d points
Parameters: | points : ndarray, (N, 3) color : tuple (3,) opacity : float dot_size : int |
---|---|
Returns: | vtkActor : |
See also
Create a label actor.
This actor will always face the camera
Parameters: | ren : vtkRenderer() object
text : str
pos : (3,) array_like, optional
scale : (3,) array_like
color : (3,) array_like
|
---|---|
Returns: | l : vtkActor object
|
Examples
>>> from dipy.viz import fvtk
>>> r=fvtk.ren()
>>> l=fvtk.label(r)
>>> fvtk.add(r,l)
>>> #fvtk.show(r)
Create an actor for one or more lines.
Parameters: | lines : list of arrays representing lines as 3d points for example
colors : array, shape (N,3)
opacity : float, optional
linewidth : float, optional
|
---|---|
Returns: | v : vtkActor object
|
Examples
>>> from dipy.viz import fvtk
>>> r=fvtk.ren()
>>> lines=[np.random.rand(10,3), np.random.rand(20,3)]
>>> colors=np.random.rand(2,3)
>>> c=fvtk.line(lines, colors)
>>> fvtk.add(r,c)
>>> #fvtk.show(r)
Visualize peak directions as given from peaks_from_model
Parameters: | peaks_dirs : ndarray
peaks_values : ndarray
scale : float
colors : ndarray or tuple
|
---|---|
Returns: | vtkActor : |
See also
Visualize points as sphere glyphs
Parameters: | points : ndarray, shape (N, 3) colors : ndarray (N,3) or tuple (3,) point_radius : float theta : int phi : int |
---|---|
Returns: | vtkActor : |
Examples
>>> from dipy.viz import fvtk
>>> ren = fvtk.ren()
>>> pts = np.random.rand(5, 3)
>>> point_actor = fvtk.point(pts, fvtk.colors.coral)
>>> fvtk.add(ren, point_actor)
>>> #fvtk.show(ren)
This will record a video of your scene
Records a video as a series of .png files of your scene by rotating the azimuth angle az_angle in every frame.
Parameters: | ren : vtkRenderer() object
cam_pos : None or sequence (3,), optional
cam_focal : None or sequence (3,), optional
cam_view : None or sequence (3,), optional
out_path : str, optional
path_numbering : bool
n_frames : int, optional
az_ang : float, optional
magnification : int, optional
|
---|
Examples
>>> from dipy.viz import fvtk
>>> r=fvtk.ren()
>>> a=fvtk.axes()
>>> fvtk.add(r,a)
>>> #uncomment below to record
>>> #fvtk.record(r)
>>> #check for new images in current directory
Create a renderer.
Returns: | v : vtkRenderer() object
|
---|
Examples
>>> from dipy.viz import fvtk
>>> import numpy as np
>>> r=fvtk.ren()
>>> lines=[np.random.rand(10,3)]
>>> c=fvtk.line(lines, fvtk.colors.red)
>>> fvtk.add(r,c)
>>> #fvtk.show(r)
Remove a specific actor
Remove all actors from the renderer
Show window
Parameters: | ren : vtkRenderer() object
title : string
size : (int, int)
png_magnify : int
|
---|
See also
Notes
If you want to:
Examples
>>> import numpy as np
>>> from dipy.viz import fvtk
>>> r=fvtk.ren()
>>> lines=[np.random.rand(10,3),np.random.rand(20,3)]
>>> colors=np.array([[0.2,0.2,0.2],[0.8,0.8,0.8]])
>>> c=fvtk.line(lines,colors)
>>> fvtk.add(r,c)
>>> l=fvtk.label(r)
>>> fvtk.add(r,l)
>>> #fvtk.show(r)
Slice a 3D volume
Parameters: | vol : array, shape (N, M, K)
voxsz : sequence of 3 floats
plane_i : sequence of ints
plane_j : sequence of ints
plane_k : sequence of ints
outline : bool
|
---|
Examples
>>> import numpy as np
>>> from dipy.viz import fvtk
>>> x, y, z = np.ogrid[-10:10:80j, -10:10:80j, -10:10:80j]
>>> s = np.sin(x * y * z) / (x * y * z)
>>> r = fvtk.ren()
>>> fvtk.add(r, fvtk.slicer(s, plane_i=[0, 5]))
>>> #fvtk.show(r)
Plot many morphed spherical functions simultaneously.
Parameters: | sphere_values : (M,) or (X, M) or (X, Y, M) or (X, Y, Z, M) ndarray
sphere : Sphere image : None,
colormap : None or ‘jet’
scale : float,
norm : bool,
radial_scale : bool,
|
---|---|
Returns: | actor : vtkActor
|
Examples
>>> from dipy.viz import fvtk
>>> r = fvtk.ren()
>>> odfs = np.ones((5, 5, 724))
>>> odfs[..., 0] = 2.
>>> from dipy.data import get_sphere
>>> sphere = get_sphere('symmetric724')
>>> fvtk.add(r, fvtk.sphere_funcs(odfs, sphere))
>>> #fvtk.show(r)
Uses streamtubes to visualize polylines
Parameters: | lines : list
colors : array (N, 3) or tuple (3,) opacity : float linewidth : float tube_sides : int lod : bool
lod_points : int
lod_points_size : int
|
---|
Notes
Streamtubes can be heavy on GPU when loading many streamlines and therefore, you may experience slow rendering time depending on system GPU. A solution to this problem is to reduce the number of points in each streamline. In Dipy we provide an algorithm that will reduce the number of points on the straighter parts of the streamline but keep more points on the curvier parts. This can be used in the following way
from dipy.tracking.distances import approx_polygon_track lines = [approx_polygon_track(line, 0.2) for line in lines]
Examples
>>> from dipy.viz import fvtk
>>> r=fvtk.ren()
>>> lines=[np.random.rand(10, 3), np.random.rand(20, 3)]
>>> colors=np.random.rand(2, 3)
>>> c=fvtk.streamtube(lines, colors)
>>> fvtk.add(r,c)
>>> #fvtk.show(r)
Plot many tensors as ellipsoids simultaneously.
Parameters: | evals : (3,) or (X, 3) or (X, Y, 3) or (X, Y, Z, 3) ndarray
evecs : (3, 3) or (X, 3, 3) or (X, Y, 3, 3) or (X, Y, Z, 3, 3) ndarray
scalar_colors : (3,) or (X, 3) or (X, Y, 3) or (X, Y, Z, 3) ndarray
sphere : Sphere,
scale : float,
norm : boolean,
|
---|---|
Returns: | actor : vtkActor
|
Examples
>>> from dipy.viz import fvtk
>>> r = fvtk.ren()
>>> evals = np.array([1.4, .35, .35]) * 10 ** (-3)
>>> evecs = np.eye(3)
>>> from dipy.data import get_sphere
>>> sphere = get_sphere('symmetric724')
>>> fvtk.add(r, fvtk.tensor(evals, evecs, sphere=sphere))
>>> #fvtk.show(r)
Create a volume and return a volumetric actor using volumetric rendering.
This function has many different interesting capabilities. The maptype, opacitymap and colormap are the most crucial parameters here.
Parameters: | vol : array, shape (N, M, K), dtype uint8
voxsz : (3,) array_like
affine : (4, 4) ndarray
center_origin : int {0,1}
info : int {0,1}
trilinear : int {0,1}
maptype : int {0,1}
iso : int {0,1}
iso_thr : int
opacitymap : (2, 2) ndarray
colormap : (4, 4) ndarray
|
---|---|
Returns: | v : vtkVolume
|
Notes
What is the difference between TextureMapper2D and RayCastFunction? Coming soon... See VTK user’s guide [book] & The Visualization Toolkit [book] and VTK’s online documentation & online docs.
What is the difference between RayCastIsosurfaceFunction and RayCastCompositeFunction? Coming soon... See VTK user’s guide [book] & The Visualization Toolkit [book] and VTK’s online documentation & online docs.
What about trilinear interpolation? Coming soon... well when time permits really ... :-)
Examples
First example random points.
>>> from dipy.viz import fvtk
>>> import numpy as np
>>> vol=100*np.random.rand(100,100,100)
>>> vol=vol.astype('uint8')
>>> vol.min(), vol.max()
(0, 99)
>>> r = fvtk.ren()
>>> v = fvtk.volume(vol)
>>> fvtk.add(r,v)
>>> #fvtk.show(r)
Second example with a more complicated function
>>> from dipy.viz import fvtk
>>> import numpy as np
>>> x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
>>> s = np.sin(x*y*z)/(x*y*z)
>>> r = fvtk.ren()
>>> v = fvtk.volume(s)
>>> fvtk.add(r,v)
>>> #fvtk.show(r)
If you find this function too complicated you can always use mayavi. Please do not forget to use the -wthread switch in ipython if you are running mayavi.
from enthought.mayavi import mlab import numpy as np x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j] s = np.sin(x*y*z)/(x*y*z) mlab.pipeline.volume(mlab.pipeline.scalar_field(s)) mlab.show()
More mayavi demos are available here:
http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html