Hi Edo, Documentation for the VTK/Numpy adapter can be found here:
https://www.paraview.org/ParaView/Doc/Nightly/www/py-doc/paraview.vtk.numpy_interface.html?highlight=numpy_interface#numpy-interface-package You will likely be interested in the dataset_adapter module that provides a convenient way to wrap VTK data objects in a Python class that provides easy read/write access to VTK data arrays as Numpy arrays. You could simplify your code quite a bit using this adapter as in the following: import numpy from vtk.util import numpy_support, vtkImageImportFromArray import vtk.numpy_interface.dataset_adapter as dsa inData = dsa.WrapDataObject(inputs[0]) B = inData.PointData['array_name'] # best practice is to name the array #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)) outData = dsa.WrapDataObject(output) outData.PointData.append(A,'new_array_name') #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()) Hope that helps, Cory On Tue, Oct 17, 2017 at 10:38 AM, Edoardo Pasca <edo.pask...@gmail.com> wrote: > 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 > > _______________________________________________ > 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 ParaView Wiki at: > http://paraview.org/Wiki/ParaView > > Search the list archives at: http://markmail.org/search/?q=ParaView > > Follow this link to subscribe/unsubscribe: > http://public.kitware.com/mailman/listinfo/paraview > > -- Cory Quammen Staff R&D Engineer Kitware, Inc.
_______________________________________________ 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 ParaView Wiki at: http://paraview.org/Wiki/ParaView Search the list archives at: http://markmail.org/search/?q=ParaView Follow this link to subscribe/unsubscribe: http://public.kitware.com/mailman/listinfo/paraview