forked from 2MRD/animatedicom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimatedicom.py
82 lines (59 loc) · 2.39 KB
/
animatedicom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# animatedicom - Visualise and Animate a DICOM data series
#
# Author: Iwan Cornelius <[email protected]> and Christopher Poole <[email protected]>
# Date: 7 April 2015
import argparse
import os
import vtk
import tvtk
import numpy as np
from mayavi import mlab
# Process command line arguments.
parser = argparse.ArgumentParser()
parser.add_argument("--dicomdir",
type=str,
default="./",
help="The directory containing the dicom series (default:./)")
parser.add_argument("--output",
type=str,
default="./animation/",
help="The generated animation output directory (default:./animation/)")
parser.add_argument("--animate",
action="store_true",
default=False,
help="Animate camera rotation about the azimuthal axis and save each frame")
args = parser.parse_args()
# VTK is used to read in the DICOM data series. By default will look in
# the current directory.
reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName(args.dicomdir)
reader.Update()
# Convert the VTK object to a TVTK object. This is what mayavi uses.
data = tvtk.api.tvtk.to_tvtk(reader.GetOutput())
# Create our viewing window.
fig = mlab.figure(size=(640, 360), bgcolor=(1,1,1))
# Show the data by adding it to the pipeline, and using a
# matplotlib colour map name.
contours = mlab.pipeline.contour_surface(data,contours=5, colormap="Oranges", opacity=1.0)
# Perform our rotation animation.
if args.animate is True:
# We create the output directory if it doesn't already exist.
odir = os.path.join(args.output, "png")
if not os.path.exists(odir): os.makedirs(odir)
# Tweak these to values to set the starting angles of elevation and
# azimuth. If you run interactively, you can type mlab.view(figure=fig) to get
# current settings of the camera.
#mlab.view(40, 70, focalpoint=(86, 110, 30), distance=200, figure=fig)
mlab.view(0, 270, focalpoint=(100, 120, 70), distance=400, figure=fig)
# Configure the number of frames we require.
frames = 100
increment = 360.0 / np.float(frames)
for i in range(frames):
print("Rendering frame number: {0}".format(i))
fig.scene.camera.azimuth(increment)
fig.scene.render()
# Save the current frame.
filename = os.path.join(odir, "frame{:03}.png".format(i))
fig.scene.save_png(filename)
# Allow interaction with the viewer.
#mlab.show()