Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Christian Butz
VTK
Commits
d64c256c
Commit
d64c256c
authored
Apr 21, 2011
by
David Partyka
Browse files
Merge branch '09224-image-from-numpy' into release
parents
ddd73ea6
febc3a96
Changes
2
Hide whitespace changes
Inline
Side-by-side
Wrapping/Python/vtk/util/vtkImageExportToArray.py
View file @
d64c256c
"""
vtkImageExportToArray - a NumPy front-end to vtkImageExport
This class converts a VTK image to a Numeric Python array.
This class converts a VTK image to a numpy array. The output
array will always have 3 dimensions (or 4, if the image had
multiple scalar components).
To use this class, you must have the LLNL Numeric Python distribution
(http://numpy.sf.net)
To use this class, you must have numpy installed (http://numpy.scipy.org)
Methods
SetInput(input) -- connect to VTK image pipeline
GetArray() -- execute pipeline and return a Numeric array
SetInputConnection(vtkAlgorithmOutput) -- connect to VTK image pipeline
SetInput(vtkImageData) -- set an vtkImageData to export
GetArray() -- execute pipeline and return a numpy array
Convert VTK_UNSIGNED_SHORT to python Int
(this might be necessary because Python doesn't support unsigned short,
the default is to cast unsigned short to signed short).
SetConvertUnsignedShortToInt(yesno)
ConvertUnsignedShortToIntOn()
ConvertUnsignedShortToIntOff()
From vtkImageExport
Methods from vtkImageExport
GetDataExtent()
GetDataSpacing()
GetDataOrigin()
"""
import
Numeric
import
umath
from
vtk
import
vtkImageExport
from
vtkConstants
import
*
import
numpy
_NEW_NUMERIC
=
0
try
:
val
=
float
(
Numeric
.
__version__
)
except
ValueError
:
_NEW_NUMERIC
=
0
else
:
if
val
>
20.0
:
_NEW_NUMERIC
=
1
else
:
_NEW_NUMERIC
=
0
from
vtk
import
vtkImageExport
from
vtk
import
VTK_SIGNED_CHAR
from
vtk
import
VTK_UNSIGNED_CHAR
from
vtk
import
VTK_SHORT
from
vtk
import
VTK_UNSIGNED_SHORT
from
vtk
import
VTK_INT
from
vtk
import
VTK_UNSIGNED_INT
from
vtk
import
VTK_LONG
from
vtk
import
VTK_UNSIGNED_LONG
from
vtk
import
VTK_FLOAT
from
vtk
import
VTK_DOUBLE
class
vtkImageExportToArray
:
def
__init__
(
self
):
self
.
__export
=
vtkImageExport
()
self
.
__ConvertUnsignedShortToInt
=
0
self
.
__ConvertUnsignedShortToInt
=
False
# type dictionary: note that python doesn't support
# unsigned integers!
# type dictionary
__typeDict
=
{
VTK_CHAR
:
Numeric
.
Int8
,
VTK_UNSIGNED_CHAR
:
Numeric
.
UnsignedInt8
,
VTK_SHORT
:
Numeric
.
Int16
,
VTK_UNSIGNED_SHORT
:
Numeric
.
Int16
,
VTK_INT
:
Numeric
.
Int32
,
VTK_FLOAT
:
Numeric
.
Float32
,
VTK_DOUBLE
:
Numeric
.
Float64
}
__typeDict
=
{
VTK_SIGNED_CHAR
:
'b'
,
VTK_UNSIGNED_CHAR
:
'B'
,
VTK_SHORT
:
'h'
,
VTK_UNSIGNED_SHORT
:
'H'
,
VTK_INT
:
'i'
,
VTK_UNSIGNED_INT
:
'I'
,
VTK_FLOAT
:
'f'
,
VTK_DOUBLE
:
'd'
}
__sizeDict
=
{
VTK_CHAR
:
1
,
__sizeDict
=
{
VTK_
SIGNED_
CHAR
:
1
,
VTK_UNSIGNED_CHAR
:
1
,
VTK_SHORT
:
2
,
VTK_UNSIGNED_SHORT
:
2
,
VTK_INT
:
4
,
VTK_UNSIGNED_INT
:
4
,
VTK_FLOAT
:
4
,
VTK_DOUBLE
:
8
}
...
...
@@ -75,12 +69,15 @@ class vtkImageExportToArray:
return
self
.
__ConvertUnsignedShortToInt
def
ConvertUnsignedShortToIntOn
(
self
):
self
.
__ConvertUnsignedShortToInt
=
1
self
.
__ConvertUnsignedShortToInt
=
True
def
ConvertUnsignedShortToIntOff
(
self
):
self
.
__ConvertUnsignedShortToInt
=
0
self
.
__ConvertUnsignedShortToInt
=
False
# set the input
def
SetInputConnection
(
self
,
input
):
return
self
.
__export
.
SetInputConnection
(
input
)
def
SetInput
(
self
,
input
):
return
self
.
__export
.
SetInput
(
input
)
...
...
@@ -98,24 +95,13 @@ class vtkImageExportToArray:
extent
[
1
]
-
extent
[
0
]
+
1
)
if
(
numComponents
>
1
):
dim
=
dim
+
(
numComponents
,)
size
=
dim
[
0
]
*
dim
[
1
]
*
dim
[
2
]
*
numComponents
*
self
.
__sizeDict
[
type
]
if
_NEW_NUMERIC
:
imArray
=
Numeric
.
zeros
((
size
,),
Numeric
.
UnsignedInt8
)
self
.
__export
.
Export
(
imArray
)
else
:
imString
=
Numeric
.
zeros
((
size
,),
Numeric
.
UnsignedInt8
).
tostring
()
self
.
__export
.
Export
(
imString
)
imArray
=
Numeric
.
fromstring
(
imString
,
self
.
__typeDict
[
type
])
# just to remind myself of the dangers of memory management
del
imString
# reshape array appropriately.
imArray
=
Numeric
.
reshape
(
imArray
,
dim
)
imArray
=
numpy
.
zeros
(
dim
,
self
.
__typeDict
[
type
])
self
.
__export
.
Export
(
imArray
)
# convert unsigned short to int to avoid sign issues
if
(
type
==
VTK_UNSIGNED_SHORT
and
self
.
__ConvertUnsignedShortToInt
):
imArray
=
umath
.
bitwise_and
(
imArray
.
astype
(
Numeric
.
Int32
),
0xffff
)
imArray
=
umath
.
bitwise_and
(
imArray
.
astype
(
'i'
),
0xffff
)
return
imArray
...
...
Wrapping/Python/vtk/util/vtkImageImportFromArray.py
View file @
d64c256c
"""
vtkImageImportFromArray: a NumPy front-end to vtkImageImport
Load a Numeric Python array into a VTK image.
To use this class, you must have the LLNL Numeric Python distribution
(http://numpy.sf.net)
Load a python array into a vtk image.
To use this class, you must have NumPy installed (http://numpy.scipy.org/)
Methods:
GetOutput() -- connect to VTK image pipeline
SetArray() -- set the array to load in
Convert python 'Int' to VTK_UNSIGNED_SHORT:
(python doesn't support unsigned short, so this might be necessary)
SetConvertIntToUnsignedShort(yesno)
ConvertIntToUnsignedShortOn()
ConvertIntToUnsignedShortOff()
SetArray() -- set the numpy array to load
Update() -- generate the output
GetOutput() -- get the image as vtkImageData
GetOutputPort() -- connect to VTK pipeline
Methods from vtkImageImport:
(if you don't set these, sensible defaults will be used)
...
...
@@ -25,47 +19,44 @@ Methods from vtkImageImport:
SetDataOrigin()
"""
import
Numeric
from
vtk
import
vtkImageImport
from
vtkConstants
import
*
_NEW_NUMERIC
=
0
try
:
val
=
float
(
Numeric
.
__version__
)
except
ValueError
:
_NEW_NUMERIC
=
0
else
:
if
val
>
20.0
:
_NEW_NUMERIC
=
1
else
:
_NEW_NUMERIC
=
0
from
vtk
import
VTK_SIGNED_CHAR
from
vtk
import
VTK_UNSIGNED_CHAR
from
vtk
import
VTK_SHORT
from
vtk
import
VTK_UNSIGNED_SHORT
from
vtk
import
VTK_INT
from
vtk
import
VTK_UNSIGNED_INT
from
vtk
import
VTK_LONG
from
vtk
import
VTK_UNSIGNED_LONG
from
vtk
import
VTK_FLOAT
from
vtk
import
VTK_DOUBLE
class
vtkImageImportFromArray
:
def
__init__
(
self
):
self
.
__import
=
vtkImageImport
()
self
.
__ConvertIntToUnsignedShort
=
0
self
.
__ConvertIntToUnsignedShort
=
False
self
.
__Array
=
None
# type dictionary: note that python doesn't support
# unsigned integers properly!
__typeDict
=
{
'c'
:
VTK_UNSIGNED_CHAR
,
'b'
:
VTK_UNSIGNED_CHAR
,
'1'
:
VTK_CHAR
,
's'
:
VTK_SHORT
,
'i'
:
VTK_INT
,
'l'
:
VTK_LONG
,
'f'
:
VTK_FLOAT
,
'd'
:
VTK_DOUBLE
,
'F'
:
VTK_FLOAT
,
'D'
:
VTK_DOUBLE
}
__sizeDict
=
{
VTK_CHAR
:
1
,
__typeDict
=
{
'b'
:
VTK_SIGNED_CHAR
,
# int8
'B'
:
VTK_UNSIGNED_CHAR
,
# uint8
'h'
:
VTK_SHORT
,
# int16
'H'
:
VTK_UNSIGNED_SHORT
,
# uint16
'i'
:
VTK_INT
,
# int32
'I'
:
VTK_UNSIGNED_INT
,
# uint32
'f'
:
VTK_FLOAT
,
# float32
'd'
:
VTK_DOUBLE
,
# float64
'F'
:
VTK_FLOAT
,
# float32
'D'
:
VTK_DOUBLE
,
# float64
}
__sizeDict
=
{
VTK_SIGNED_CHAR
:
1
,
VTK_UNSIGNED_CHAR
:
1
,
VTK_SHORT
:
2
,
VTK_UNSIGNED_SHORT
:
2
,
VTK_INT
:
4
,
VTK_
LONG
:
4
,
VTK_
UNSIGNED_INT
:
4
,
VTK_FLOAT
:
4
,
VTK_DOUBLE
:
8
}
...
...
@@ -77,10 +68,17 @@ class vtkImageImportFromArray:
return
self
.
__ConvertIntToUnsignedShort
def
ConvertIntToUnsignedShortOn
(
self
):
self
.
__ConvertIntToUnsignedShort
=
1
self
.
__ConvertIntToUnsignedShort
=
True
def
ConvertIntToUnsignedShortOff
(
self
):
self
.
__ConvertIntToUnsignedShort
=
0
self
.
__ConvertIntToUnsignedShort
=
False
def
Update
(
self
):
self
.
__import
.
Update
()
# get the output
def
GetOutputPort
(
self
):
return
self
.
__import
.
GetOutputPort
()
# get the output
def
GetOutput
(
self
):
...
...
@@ -91,31 +89,32 @@ class vtkImageImportFromArray:
self
.
__Array
=
imArray
numComponents
=
1
dim
=
imArray
.
shape
if
(
len
(
dim
)
==
4
):
if
len
(
dim
)
==
0
:
dim
=
(
1
,
1
,
1
)
elif
len
(
dim
)
==
1
:
dim
=
(
1
,
1
,
dim
[
0
])
elif
len
(
dim
)
==
2
:
dim
=
(
1
,
dim
[
0
],
dim
[
1
])
elif
len
(
dim
)
==
4
:
numComponents
=
dim
[
3
]
dim
=
(
dim
[
0
],
dim
[
1
],
dim
[
2
])
type
=
self
.
__typeDict
[
imArray
.
type
code
()]
type
code
=
imArray
.
d
type
.
char
if
(
imArray
.
typecode
()
==
'F'
or
imArray
.
typecode
==
'D'
):
ar_type
=
self
.
__typeDict
[
typecode
]
complexComponents
=
1
if
(
typecode
==
'F'
or
typecode
==
'D'
):
numComponents
=
numComponents
*
2
complexComponents
=
2
if
(
self
.
__ConvertIntToUnsignedShort
and
typecode
==
'i'
):
imArray
=
imArray
.
astype
(
'h'
)
ar_type
=
VTK_UNSIGNED_SHORT
if
(
self
.
__ConvertIntToUnsignedShort
and
imArray
.
typecode
()
==
'i'
):
if
_NEW_NUMERIC
:
imTmpArr
=
imArray
.
astype
(
Numeric
.
Int16
).
flat
else
:
imTmpArr
=
imArray
.
astype
(
Numeric
.
Int16
).
tostring
()
type
=
VTK_UNSIGNED_SHORT
else
:
if
_NEW_NUMERIC
:
imTmpArr
=
imArray
.
flat
else
:
imTmpArr
=
imArray
.
tostring
()
size
=
len
(
imTmpArr
)
*
self
.
__sizeDict
[
type
]
self
.
__import
.
CopyImportVoidPointer
(
imTmpArr
,
size
)
self
.
__import
.
SetDataScalarType
(
type
)
size
=
len
(
imArray
.
flat
)
*
self
.
__sizeDict
[
ar_type
]
*
complexComponents
self
.
__import
.
CopyImportVoidPointer
(
imArray
,
size
)
self
.
__import
.
SetDataScalarType
(
ar_type
)
self
.
__import
.
SetNumberOfScalarComponents
(
numComponents
)
extent
=
self
.
__import
.
GetDataExtent
()
self
.
__import
.
SetDataExtent
(
extent
[
0
],
extent
[
0
]
+
dim
[
2
]
-
1
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment