[vtkusers] about vtkImageData
Eric E. Monson
emonson at cs.duke.edu
Tue Jun 30 13:12:04 EDT 2009
Hello,
You can manually create a vtkImageData and then you have a couple of
options for populating the array data values (and I am sure there are
more that I don't know). Here is an example where "chem" is a 3d numpy
array that I'm grabbing from an HDF5 file (I also needed to swap the X
and Z axes in this case"):
# ----
import vtk
import numpy as N
# ...code omitted...
gridSpacing = 6.25
# reading chemical array and stripping off outer zero/ghost planes
chem = h5file.getNode(snapGroup._v_pathname + '/' + snapName + '/' +
chemArrayNames[cc]).read()[1:-1,1:-1,1:-1]
sideDim = chem.shape[0]
numPts = chem.size
vol = vtk.vtkImageData()
vol.SetDimensions(sideDim,sideDim,sideDim)
vol.SetOrigin(0,0,0)
vol.SetSpacing(gridSpacing,gridSpacing,gridSpacing)
sc = vtk.vtkFloatArray()
sc.SetNumberOfValues(numPts)
sc.SetNumberOfComponents(1)
sc.SetName('tnf')
for ii,tmp in enumerate(N.ravel(chem.swapaxes(0,2))):
sc.SetValue(ii,tmp)
vol.GetPointData().SetScalars(sc)
# ----
Instead of creating a vtkFloatArray and then manually populating its
entries, you can also use the VTK numpy_support module to create one
directly from a numpy array:
# ----
import vtk
import vtk.util.numpy_support as VN
import numpy as N
x = N.random.random(10)
y = VN.numpy_to_vtk(x)
# ----
and y is now a vtkDoubleArray containing the 10 random values, and in
principle could be used in a SetScalars(y) or AddArray(y) if it was
the correct size.
Talk to you later,
-Eric
------------------------------------------------------
Eric E Monson
Duke Visualization Technology Group
On Jun 30, 2009, at 11:40 AM, Lic. José M. Rodriguez Bacallao wrote:
> as I said before, until now, I have been using vtkGDCMImageReader but
> now I wan't to use pydicom, this is how I read the image with pydicom:
>
> import dicom
> dataset = dicom.read_file('./path/to/file.dcm')
> pixel_array = ds.pixel_array #numpy array
> pixel_data = ds.PixelData #string of bytes
>
> question is: How do I visualize any of these with vtk to see a dicom
> image?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090630/d20d8a62/attachment.htm>
More information about the vtkusers
mailing list