Skip to content

Use Converter.numpy2vtkImage in setInputAsNumpy for viewer3D #478

Description

@paskino

Currently the code at setInputAsNumpy creates a new VTK image, and also not really correct.

It should really call the converter which will not copy the data from the numpy array to the VTK image.

from ccpi.viewer.utils.conversion import Converter
vtkdata = Converter.numpy2vtkImage(data)
v.setInputData(vtkdata)

if (len(numpy.shape(numpyarray)) == 3):
doubleImg = vtk.vtkImageData()
shape = numpy.shape(numpyarray)
doubleImg.SetDimensions(shape[0], shape[1], shape[2])
doubleImg.SetOrigin(0, 0, 0)
doubleImg.SetSpacing(1, 1, 1)
doubleImg.SetExtent(0, shape[0] - 1, 0, shape[1] - 1, 0, shape[2] - 1)
doubleImg.AllocateScalars(vtk.VTK_DOUBLE, 1)
for i in range(shape[0]):
for j in range(shape[1]):
for k in range(shape[2]):
doubleImg.SetScalarComponentFromDouble(i, j, k, 0, numpyarray[i][j][k])
# rescale to appropriate VTK_UNSIGNED_SHORT
stats = vtk.vtkImageAccumulate()
stats.SetInputData(doubleImg)
stats.Update()
iMin = stats.GetMin()[0]
iMax = stats.GetMax()[0]
scale = vtk.VTK_UNSIGNED_SHORT_MAX / (iMax - iMin)
shiftScaler = vtk.vtkImageShiftScale()
shiftScaler.SetInputData(doubleImg)
shiftScaler.SetScale(scale)
shiftScaler.SetShift(iMin)
shiftScaler.SetOutputScalarType(vtk.VTK_UNSIGNED_SHORT)
shiftScaler.Update()
self.img3D = shiftScaler.GetOutput()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions