converters.py
3.03 KB
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
82
83
84
#--------------------------------------------------------------------------
# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
# Homepage: http://www.softwarepublico.gov.br
# Contact: invesalius@cti.gov.br
# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
#--------------------------------------------------------------------------
# Este programa e software livre; voce pode redistribui-lo e/ou
# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
# publicada pela Free Software Foundation; de acordo com a versao 2
# da Licenca.
#
# Este programa eh distribuido na expectativa de ser util, mas SEM
# QUALQUER GARANTIA; sem mesmo a garantia implicita de
# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
# detalhes.
#--------------------------------------------------------------------------
import numpy
import vtk
from vtk.util import numpy_support
def to_vtk(n_array, spacing, slice_number, orientation):
if orientation == "SAGITTAL":
orientation = "SAGITAL"
try:
dz, dy, dx = n_array.shape
except ValueError:
dy, dx = n_array.shape
dz = 1
v_image = numpy_support.numpy_to_vtk(n_array.flat)
if orientation == 'AXIAL':
extent = (0, dx -1, 0, dy -1, slice_number, slice_number + dz - 1)
elif orientation == 'SAGITAL':
dx, dy, dz = dz, dx, dy
extent = (slice_number, slice_number + dx - 1, 0, dy - 1, 0, dz - 1)
elif orientation == 'CORONAL':
dx, dy, dz = dx, dz, dy
extent = (0, dx - 1, slice_number, slice_number + dy - 1, 0, dz - 1)
# Generating the vtkImageData
image = vtk.vtkImageData()
image.SetOrigin(0, 0, 0)
image.SetSpacing(spacing)
image.SetDimensions(dx, dy, dz)
# SetNumberOfScalarComponents and SetScalrType were replaced by
# AllocateScalars
# image.SetNumberOfScalarComponents(1)
# image.SetScalarType(numpy_support.get_vtk_array_type(n_array.dtype))
image.AllocateScalars(numpy_support.get_vtk_array_type(n_array.dtype), 1)
image.SetExtent(extent)
image.GetPointData().SetScalars(v_image)
image_copy = vtk.vtkImageData()
image_copy.DeepCopy(image)
return image_copy
def np_rgba_to_vtk(n_array, spacing=(1.0, 1.0, 1.0)):
dy, dx, dc = n_array.shape
v_image = numpy_support.numpy_to_vtk(n_array.reshape(dy*dx, dc))
extent = (0, dx -1, 0, dy -1, 0, 0)
# Generating the vtkImageData
image = vtk.vtkImageData()
image.SetOrigin(0, 0, 0)
image.SetSpacing(spacing)
image.SetDimensions(dx, dy, 1)
# SetNumberOfScalarComponents and SetScalrType were replaced by
# AllocateScalars
# image.SetNumberOfScalarComponents(1)
# image.SetScalarType(numpy_support.get_vtk_array_type(n_array.dtype))
image.AllocateScalars(numpy_support.get_vtk_array_type(n_array.dtype), dc)
image.SetExtent(extent)
image.GetPointData().SetScalars(v_image)
return image