def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkImagePermute(), 'Processing.', ('vtkImageData', 'vtkImageStencilData'), ('vtkImageData',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def getPlane(self, data, plane, xCoordinate, yCoordinate, zCoordinate, applyZScaling = 0): """ Get a plane from given the volume """ xAxis, yAxis, zAxis = 0, 1, 2 dataWidth, dataHeight, dataDepth = data.GetDimensions() if not self.voi: self.voi = vtk.vtkExtractVOI() else: self.voi.RemoveAllInputs() if not self.permute: self.permute = vtk.vtkImagePermute() else: self.permute.RemoveAllInputs() self.permute.SetInputConnection(data.GetProducerPort()) spacing = data.GetSpacing() xscale = 1 yscale = 1 if plane == "zy": data.SetUpdateExtent(xCoordinate, xCoordinate, 0, dataHeight - 1, 0, dataDepth - 1) self.permute.SetFilteredAxes(zAxis, yAxis, xAxis) self.permute.Update() data = self.permute.GetOutput() self.voi.SetInput(data) self.voi.SetVOI(0, dataDepth - 1, 0, dataHeight - 1, xCoordinate, xCoordinate) xdim = dataDepth ydim = dataHeight if applyZScaling: xdim *= spacing[2] xscale = spacing[2] elif plane == "xz": data.SetUpdateExtent(0, dataWidth - 1, yCoordinate, yCoordinate, 0, dataDepth - 1) self.permute.SetFilteredAxes(xAxis, zAxis, yAxis) self.permute.Update() data = self.permute.GetOutput() self.voi.SetInput(data) self.voi.SetVOI(0, dataWidth - 1, 0, dataDepth - 1, yCoordinate, yCoordinate) xdim = dataWidth ydim = dataDepth if applyZScaling: ydim *= spacing[2] yscale = 1 self.voi.Update() if applyZScaling: self.voi.Update() return lib.ImageOperations.scaleImage(self.voi.GetOutput(), interpolation = 2, xfactor = xscale, yfactor = yscale) self.voi.Update() return self.voi.GetOutput()
def permuteAxes(self, x, y, z): """Reorder the axes of the Volume by specifying the input axes which are supposed to become the new X, Y, and Z.""" imp = vtk.vtkImagePermute() imp.SetFilteredAxes(x, y, z) imp.SetInputData(self.imagedata()) imp.Update() return self._update(imp.GetOutput())
#!/usr/bin/env python import vtk from vtk.test import Testing from vtk.util.misc import vtkGetDataRoot VTK_DATA_ROOT = vtkGetDataRoot() # Simple viewer for images. # Image pipeline reader = vtk.vtkImageReader() reader.ReleaseDataFlagOff() reader.SetDataByteOrderToLittleEndian() reader.SetDataExtent(0,63,0,63,1,93) reader.SetFilePrefix("" + str(VTK_DATA_ROOT) + "/Data/headsq/quarter") reader.SetDataMask(0x7fff) permute = vtk.vtkImagePermute() permute.SetInputConnection(reader.GetOutputPort()) permute.SetFilteredAxes(1,2,0) viewer = vtk.vtkImageViewer() viewer.SetInputConnection(permute.GetOutputPort()) viewer.SetZSlice(32) viewer.SetColorWindow(2000) viewer.SetColorLevel(1000) #viewer DebugOn #viewer Render #make interface viewer.Render() # --- end of script --
def imageDataFromNumpy(npArr, spacing, origin, shape, dims=1, outputFilename = None): ds, o, p = shape, origin, spacing print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr\tinitializing volume..." d = vtk.vtkImageData() d.SetDimensions(*ds) d.SetOrigin(o[2],o[1],o[0]) d.SetSpacing(p[2],p[1],p[0]) d.SetNumberOfScalarComponents(dims) if dims==3: d.SetScalarTypeToUnsignedChar() else: d.SetScalarTypeToUnsignedInt() d.AllocateScalars() print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: origin: " + str(d.GetOrigin()) print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: spacing: " + str(d.GetSpacing()) print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: data type: " + str(d.GetScalarTypeAsString()) print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: components no.: " + str(d.GetNumberOfScalarComponents()) print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: points no.: " + str(d.GetNumberOfPoints()) print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: setting image data..." print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: iteration sequence: %d %d %d" % (ds[2], ds[1], ds[0]) dScalars = d.GetPointData().GetScalars() dComputePointId = d.ComputePointId if dims==3: for i in range(ds[2]): for j in range(ds[1]): for k in range(ds[0]): id = d.ComputePointId((k,j,i)) dScalars.SetTuple3(id, *map(float,npArr[k,j,i])) if dims==1: for i in range(ds[2]): for j in range(ds[1]): for k in range(ds[0]): id = d.ComputePointId((k,j,i)) dScalars.SetTuple1(id, int(npArr[k,j,i])) print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: applying vtkImagePermute()" permute = vtk.vtkImagePermute() permute.SetFilteredAxes(2,1,0) permute.SetInput(d) print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: applying vtk.vtkImageCast()" cast = vtk.vtkImageCast() cast.SetInput(permute.GetOutput()) cast.SetOutputScalarTypeToFloat() cast.Update() print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: applying vtk.vtkImageCast()" cast2 = vtk.vtkImageCast() cast2.SetInput(cast.GetOutput()) cast2.SetOutputScalarTypeToUnsignedInt() cast2.Update() print >>sys.stderr, cast2.GetOutput() if outputFilename: print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: writing to: %s" % outputFilename w = vtk.vtkStructuredPointsWriter() w.SetInput(cast2.GetOutput()) w.SetFileName(outputFilename) w.Update() print >>sys.stderr, "\timageDataFrom3dRGBNumpyArr: done." return cast2.GetOutput()
#!/usr/bin/env python import vtk from vtk.test import Testing from vtk.util.misc import vtkGetDataRoot VTK_DATA_ROOT = vtkGetDataRoot() # Simple viewer for images. # Image pipeline reader = vtk.vtkImageReader() reader.ReleaseDataFlagOff() reader.SetDataByteOrderToLittleEndian() reader.SetDataExtent(0, 63, 0, 63, 1, 93) reader.SetFilePrefix("" + str(VTK_DATA_ROOT) + "/Data/headsq/quarter") reader.SetDataMask(0x7fff) permute = vtk.vtkImagePermute() permute.SetInputConnection(reader.GetOutputPort()) permute.SetFilteredAxes(1, 2, 0) viewer = vtk.vtkImageViewer() viewer.SetInputConnection(permute.GetOutputPort()) viewer.SetZSlice(32) viewer.SetColorWindow(2000) viewer.SetColorLevel(1000) #viewer DebugOn #viewer Render #make interface viewer.Render() # --- end of script --
def permute(image,ax0,ax1,ax2): permute = vtk.vtkImagePermute() permute.SetInput(image) permute.SetFilteredAxes(ax0,ax1,ax2) return permute.GetOutput()
def getPlane(self, data, plane, xCoordinate, yCoordinate, zCoordinate, applyZScaling=0): """ Get a plane from given the volume """ xAxis, yAxis, zAxis = 0, 1, 2 dataWidth, dataHeight, dataDepth = data.GetDimensions() if not self.voi: self.voi = vtk.vtkExtractVOI() else: self.voi.RemoveAllInputs() if not self.permute: self.permute = vtk.vtkImagePermute() else: self.permute.RemoveAllInputs() self.permute.SetInputConnection(data.GetProducerPort()) spacing = data.GetSpacing() xscale = 1 yscale = 1 if plane == "zy": data.SetUpdateExtent(xCoordinate, xCoordinate, 0, dataHeight - 1, 0, dataDepth - 1) self.permute.SetFilteredAxes(zAxis, yAxis, xAxis) self.permute.Update() data = self.permute.GetOutput() self.voi.SetInput(data) self.voi.SetVOI(0, dataDepth - 1, 0, dataHeight - 1, xCoordinate, xCoordinate) xdim = dataDepth ydim = dataHeight if applyZScaling: xdim *= spacing[2] xscale = spacing[2] elif plane == "xz": data.SetUpdateExtent(0, dataWidth - 1, yCoordinate, yCoordinate, 0, dataDepth - 1) self.permute.SetFilteredAxes(xAxis, zAxis, yAxis) self.permute.Update() data = self.permute.GetOutput() self.voi.SetInput(data) self.voi.SetVOI(0, dataWidth - 1, 0, dataDepth - 1, yCoordinate, yCoordinate) xdim = dataWidth ydim = dataDepth if applyZScaling: ydim *= spacing[2] yscale = 1 self.voi.Update() if applyZScaling: self.voi.Update() return lib.ImageOperations.scaleImage(self.voi.GetOutput(), interpolation=2, xfactor=xscale, yfactor=yscale) self.voi.Update() return self.voi.GetOutput()