[vtkusers] VTK and Numpy

Eric E. Monson emonson at cs.duke.edu
Mon May 18 09:07:10 EDT 2009


Hey Peter,

I think you really need to call Update() before your conversion to a  
numpy array, or the pipeline (your filters) won't execute. The writers  
will automatically update the pipeline before writing out their data,  
but the vtk_to_numpy() won't, so you'll have to call Update()  
explicitly before that. For example, here is basically your pipeline,  
but reading one of the standard VTK data sets (in case you don't have  
it, you can download the VTK data sets from the site http://www.vtk.org/VTK/resources/software.html 
  ).

=================

import vtk
import vtk.util.numpy_support as VN

reader = vtk.vtkRectilinearGridReader()
reader.SetFileName('/Users/emonson/Programming/VTK_cvs/VTKData/Data/ 
RectGrid2.vtk')

skinExtractor = vtk.vtkContourFilter()
skinExtractor.SetInputConnection(reader.GetOutputPort())
skinExtractor.SetValue(0, 5.2)

skinConnect=vtk.vtkPolyDataConnectivityFilter()
skinConnect.SetExtractionModeToLargestRegion()
skinConnect.SetInputConnection(skinExtractor.GetOutputPort())

skinNormals = vtk.vtkPolyDataNormals()
skinNormals.SetInputConnection(skinConnect.GetOutputPort())
skinNormals.SetFeatureAngle(60.0)
skinu = skinNormals.GetOutput()

# Force the pipeline to execute
skinu.Update()

ar = VN.vtk_to_numpy(skinu.GetPoints().GetData())

print ar

=================

Talk to you later,
-Eric

------------------------------------------------------
Eric E Monson
Duke Visualization Technology Group


On May 12, 2009, at 10:16 PM, Peter Halverson wrote:

> Thank you for the fast response. That seems to work really well.  
> Although it ignores my filters. My code is as follows
>
> reader=vtk.vtkDICOMImageReader()
> reader.SetDirectoryName('./SE1/')
> reader.Update()
>
> skinExtractor = vtk.vtkContourFilter()
> skinExtractor.SetInputConnection(reader.GetOutputPort())
> skinExtractor.SetValue(SkinLow, SkinHigh)
>
> skinConnect=vtk.vtkPolyDataConnectivityFilter()
> skinConnect.SetExtractionModeToLargestRegion()
> skinConnect.SetInputConnection(skinExtractor.GetOutputPort())
>
> skinNormals = vtk.vtkPolyDataNormals()
> skinNormals.SetInputConnection(skinConnect.GetOutputPort())
> skinNormals.SetFeatureAngle(60.0)
>
> ar=vtk_to_numpy(skinNormals.GetOutput().GetPoints().GetData())
>
>
> When I render the image (code shown below) my filters work fine and  
> if I export it as an stl my filters work fine (code also below) [the  
> latter being entirely unnecesarry, but a nice way to check]. As per  
> the help files I've tried decorating the code with Update()
> [ i.e.
> skinu= skinNormals.GetOutput()
> skinu.Update()
> skinu.GetPoints().GetData()
> ]
>
> but that hasn't done much...
>
> #Render
> skinMapper = vtk.vtkPolyDataMapper()
> skinMapper.SetInputConnection(skinNormals.GetOutputPort())
> skinMapper.ScalarVisibilityOff()
>
> skin = vtk.vtkActor()
> skin.SetMapper(skinMapper)
>
> #Export to STL
> stlout = vtk.vtkSTLWriter()
> stlout.SetFileName("hello.stl")
> stlout.SetInput(skinNormals.GetOutput())
> stlout.SetFileType(2)
> stlout.Write()
>
>
> On Tue, May 12, 2009 at 1:15 PM, Eric E. Monson  
> <emonson at cs.duke.edu> wrote:
> Hey Peter,
>
> Say pd is a vtkPolyData() object, then you can get the point  
> coordinates with:
>
> vtk_to_numpy(pd.GetPoints().GetData())
>
> -Eric
>
> ------------------------------------------------------
> Eric E Monson
> Duke Visualization Technology Group
>
>
>
> On May 12, 2009, at 12:12 PM, Peter Halverson wrote:
>
> I'm attempting to export VTK data into numpy using vtk_to_numpy.   
> The program reads in a set of DICOM files using vtkDICOMImageReader.  
> Filters the image using vtkContourFilter, and some other stuff and  
> then displays the rendered solid. I would then like to export the  
> data to Numpy so i do the following  
> scalars=skin.GetMapper().GetInput().GetPointData().GetScalars()
>
> Which gives me the scalars that I am looking for.  My question is  
> how do get the [x,y,z] data to go with the scalars into numpy?
>
> skin.GetMapper().GetInput().GetWholeExtent() gives me the correct  
> dimensions (0, 511, 0, 511, 0, 56) but I have exported less scalars  
> than this...
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090518/8e274f4a/attachment.htm>


More information about the vtkusers mailing list