[vtkusers] Python: numpy array->vtkImage->paraview
David Gobbi
david.gobbi at gmail.com
Fri Mar 9 13:15:38 EST 2012
Hi Justin,
Please keep your replies on the list (though I know gmail doesn't
reply-all by default).
The data doesn't have to be a cube. I suspect the problem is the
ordering of the dimensions. VTK uses x,y,z as the order while C
(and probably numpy as well) use [z][y][x] as the index ordering.
- David
On Fri, Mar 9, 2012 at 11:08 AM, Justin Weber <onlyjus at gmail.com> wrote:
> David,
>
> Thanks for your help! That did the trick, I have my data working in paraview
> now!
>
> Quick question though, with the vtk.vtkImageImport(), does the SetDataExtent
> and SetWholeExtent have to be be a cube? My numpy array is 20x20x40 however
> if I set the Extents to (0,19,0,19,0,39), the vtk data is not correct.
> Instead if I turn my numpy array into 40x40x40 and my extents
> (0,39,0,39,0,39) it works fine.
>
> Thanks!
> Justin
>
>
> On Fri, Mar 9, 2012 at 12:33 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>>
>> Hi Justin,
>>
>> You don't need to convert the array to a string, the VTK importer can
>> access the numpy array directly. So instead of doing this:
>>
>> data_string = data_matrix.tostring()
>> dataImporter.CopyImportVoidPointer(data_string, len(data_string))
>>
>> you can do this:
>>
>> dataImporter.CopyImportVoidPointer(data_matrix, data_matrix.nbytes)
>>
>> or you can avoid the copy of the data altogether, though this is a bit
>> dangerous because the importer will need to be able to access the
>> numpy array's memory each time the importer executes:
>>
>> dataImporter.SetImportVoidPointer(data_matrix)
>>
>>
>> About the writer, the vtkImageWriter is for writing raw, unformatted
>> data. It doesn't write .vtk files. Try using vtkDataSetWriter
>> instead, which writes in the .vtk format, or vtkXMLImageDataWriter,
>> which writes in the newer XML format.
>>
>> - David
>>
>> On Fri, Mar 9, 2012 at 10:17 AM, Justin Weber <onlyjus at gmail.com> wrote:
>> > Greetings,
>> >
>> > The story: I started using python/numpy to perform data manipulation (3D
>> > connected components, etc..) and processing of 3D arrays. Next I managed
>> > to
>> > use vtk to perform volume rendering etc. to visualize the data. Now, I
>> > want
>> > to save the vtk data to a format that can be used in paraview so that a
>> > side
>> > by side comparison with CFD simulations can be performed. I am having
>> > trouble with saving the vtk data to a file to be used in paraview.
>> >
>> > System: Windows 7, Python 2.7, vtkVersion:04859120.
>> >
>> > 1) Numpy array to vtk (example here) works fine:
>> >
>> > data=np.array() #sum numpy array dim=20x20x40
>> > dataImporter = vtk.vtkImageImport()
>> > data_string = data_matrix.tostring()
>> > dataImporter.CopyImportVoidPointer(data_string, len(data_string))
>> > dataImporter.SetDataScalarTypeToUnsignedChar()
>> > dataImporter.SetNumberOfScalarComponents(1)
>> > dataImporter.SetDataExtent(0, 20, 0, 20, 0, 40)
>> > dataImporter.SetWholeExtent(0, 20, 0, 20, 0, 40)
>> >
>> > 2) Displaying the volume (example here) works fine:
>> > # This class describes how the volume is rendered (through ray tracing).
>> > compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
>> > # We can finally create our volume. We also have to specify the data for
>> > it,
>> > as well as how the data will be rendered.
>> > volumeMapper = vtk.vtkVolumeRayCastMapper()
>> > volumeMapper.SetVolumeRayCastFunction(compositeFunction)
>> > volumeMapper.SetInputConnection(dataImporter.GetOutputPort())
>> >
>> > # The class vtkVolume is used to pair the preaviusly declared volume as
>> > well
>> > as the properties to be used when rendering that volume.
>> > volume = vtk.vtkVolume()
>> > volume.SetMapper(volumeMapper)
>> > volume.SetProperty(volumeProperty)
>> >
>> > etc....
>> >
>> > 3) Writing data to some vtk file (can't figure out how to do this):
>> > writer=vtk.vtkImageWriter()
>> > writer.SetInputConnection(dataImporter.GetOutputPort())
>> > writer.SetFileName('./test.vtk')
>> > writer.Update()
>> > writer.Write()
>> >
>> > When i try opening the test.vtk file with paraview, there is no
>> > information
>> > in it.
>> >
>> > Any help would be much appreciated!
>> >
>> > Thanks
>> > Justin
>
>
More information about the vtkusers
mailing list