[Paraview] Programmable filter VTK/numpy

Edoardo Pasca edo.paskino at gmail.com
Tue Oct 17 10:38:10 EDT 2017


Hi there,

In my programmable filter I want to use numpy to perform some operations on
the pixels (in this case a simple thresholding). I've come up with this
solution which works. I'm wondering if there are other ways to do the
conversion from numpy array to vtkImageData.

in the following code I get a numpy array out of the input vtkImageData as
B. Then I do something on the content an store the result in a numpy array,
A.

Then I convert the numpy array to a vtkImageData with
vtkImageImportFromArray importer class.
Finally I copy the importer output to the output of the programmable filter.

Thanks for any suggestions

Edo

import numpy

from vtk.util import numpy_support, vtkImageImportFromArray

import vtk.numpy_interface.dataset_adapter as dsa


B = numpy_support.vtk_to_numpy(

                inputs[0].GetPointData().GetScalars())


ifLarger = lambda x,M: x if x<=M else 0

fgt = numpy.frompyfunc(ifLarger,2,1)

#A = fgt(B,int(2**16*0.8))


ifSmaller = lambda x,M: x if x>M else 0

flt = numpy.frompyfunc(ifSmaller,2,1)

#A = flt(B,int(2**16*0.3))


ifBetween = lambda x,m,M: ifSmaller(x,m) if x<=M else 0

fbetw = numpy.frompyfunc(ifBetween,3,1)


A = fbetw(B,int(2**16*0.3), int(2**16*0.95))



A = numpy.asarray(A,dtype=uint16)

dims = inputs[0].GetDimensions()

A = numpy.reshape(A, (dims[2],dims[1],dims[0]), order='C')

A = numpy.ascontiguousarray(A)

importer = vtkImageImportFromArray.vtkImageImportFromArray()

importer.SetArray(A)

importer.SetDataSpacing(inputs[0].GetSpacing())

importer.SetDataOrigin(inputs[0].GetOrigin())

importer.Update()


output.DeepCopy(importer.GetOutput())



-- 
Edo
I know you think you understand what you thought I said, but I'm not sure
you realize that what you heard is not what I meant (prob. Alan Greenspan)
:wq
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/paraview/attachments/20171017/092563c3/attachment.html>


More information about the ParaView mailing list