[vtkusers] 3D numpy array to vtkDataSet

Cory Quammen cory.quammen at kitware.com
Sat Jul 18 10:09:06 EDT 2015


vtkImageData is in fact a subclass of vtkDataSet, so no conversion is
needed. You said, "vtkImageData scales all points to positive integer
values", but this should not be the case. Something strange is going on.

Could you expand the code above into a complete example that can be run
through vtkpython? That will make it easier to take a look. In particular,
please add a part that shows the vtkImageData are being scaled to positive
integers. It's fine if you fill the array with random values.


On Sat, Jul 18, 2015 at 6:08 AM, Quentan Qi <quentan at gmail.com> wrote:

> Hi there,
> I am going to read a set of discrete 3D points (float value, not int
> value) and show both the points and the fitting surface via VTK’s implicit
> functions, say vtkImplicitVolume or vtkImplicitDataSet.
> It works fine of the numpy arrary to vtkImageData conversion in the
> following code, but is out of my mind how to convert it to vtkDataSet,
> which is my requirement.
> ```Python
>     # Convert numpy array to VTK array (vtkFloatArray)
>     vtk_data_array = numpy_support.numpy_to_vtk(
>         num_array=ndarray.transpose(2, 1, 0).ravel(),  # ndarray contains
> the fitting result from the points. It is a 3D array
>         deep=True,
>         array_type=vtk.VTK_FLOAT)
>     # Convert the VTK array to vtkImageData
>     img_vtk = vtk.vtkImageData()
>     img_vtk.SetDimensions(ndarray.shape)
>     img_vtk.SetSpacing(spacing[::-1])
>     img_vtk.GetPointData().SetScalars(vtk_data_array)
>     # …
>     implicit_volume = vtk.vtkImplicitVolume()  # I want a
> vtkImplicitDataSet, whose input is a vtkDataSet
>     implicit_volume.SetVolume(img_vtk)
> ```
> Because vtkImageData scales all points to positive integer values so that
> the rendering result mismatches the original points, I deem vtkDataSet or
> its subclass can do it. Anybody knows how to convert?
> Cheers,
> Quentan
> _______________________________________________
> 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
> Search the list archives at: http://markmail.org/search/?q=vtkusers
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers

Cory Quammen
R&D Engineer
Kitware, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150718/a64d10c1/attachment.html>

More information about the vtkusers mailing list