def probeVolume(self, volumeNode, rulerNode, numOfPoints): #get ruler end points in RAS coordinates system p0ras = rulerNode.GetPolyData().GetPoint(0) + (1,) p1ras = rulerNode.GetPolyData().GetPoint(1) + (1,) import math, numpy lineLength = math.sqrt((p0ras[0]-p1ras[0])*(p0ras[0]-p1ras[0]) + (p0ras[1]-p1ras[1])*(p0ras[1]-p1ras[1]) + (p0ras[2]-p1ras[2])*(p0ras[2]-p1ras[2])) distanceArray = [0] if (numOfPoints > 1): distanceArray = numpy.linspace(0,lineLength,numOfPoints) #The transformation matrix from RAS to IJK coordinates systems ras2ijk = vtk.vtkMatrix4x4() volumeNode.GetRASToIJKMatrix(ras2ijk) p0ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p0ras)[:3]] p1ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p1ras)[:3]] #Create the VTK sampling line line = vtk.vtkLineSource() line.SetResolution(numOfPoints) line.SetPoint1(p0ijk[0], p0ijk[1], p0ijk[2]) line.SetPoint2(p1ijk[0], p1ijk[1], p1ijk[2]) #Creat the VTK probe filter probe = vtk.vtkProbeFilter() probe.SetInputConnection(line.GetOutputPort()) probe.SetSourceData(volumeNode.GetImageData()) probe.Update() # return the sampled array return probe.GetOutput().GetPointData().GetArray('ImageScalars'), distanceArray
def probeVolume(self,volumeNode,rulerNode): # get ruler endpoints coordinates in RAS p0ras = rulerNode.GetPolyData().GetPoint(0)+(1,) p1ras = rulerNode.GetPolyData().GetPoint(1)+(1,) # convert RAS to IJK coordinates of the vtkImageData ras2ijk = vtk.vtkMatrix4x4() volumeNode.GetRASToIJKMatrix(ras2ijk) p0ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p0ras)[:3]] p1ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p1ras)[:3]] # create VTK line that will be used for sampling line = vtk.vtkLineSource() line.SetResolution(100) line.SetPoint1(p0ijk[0], p0ijk[1], p0ijk[2]) line.SetPoint2(p1ijk[0], p1ijk[1], p1ijk[2]) # create VTK probe filter and sample the image probe = vtk.vtkProbeFilter() probe.SetInputConnection(line.GetOutputPort()) probe.SetSourceData(volumeNode.GetImageData()) probe.Update() # return VTK array return probe.GetOutput().GetPointData().GetArray('ImageScalars')
def probeVolume(self, volumeNode, rulerNode): # initialize vtkLineSource from Slicer ruler # (ruler endpoints are in RAS) p0ras4 = rulerNode.GetPolyData().GetPoint(0)+(1,) p1ras4 = rulerNode.GetPolyData().GetPoint(1)+(1,) # convert to vtkImageData IJK coordinates ras2ijk = vtk.vtkMatrix4x4() volumeNode.GetRASToIJKMatrix(ras2ijk) p0ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p0ras4)[:3]] p1ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p1ras4)[:3]] # initialize vtkLineSource line = vtk.vtkLineSource() line.SetResolution(100) # TODO: make this a parameter! line.SetPoint1(p0ijk[0], p0ijk[1], p0ijk[2]) line.SetPoint2(p1ijk[0], p1ijk[1], p1ijk[2]) # ready to probe the volume probe = vtk.vtkProbeFilter() probe.SetInputConnection(line.GetOutputPort()) probe.SetSourceData(volumeNode.GetImageData()) probe.Update() data = probe.GetOutput().GetPointData().GetArray('ImageScalars') for p in range(data.GetNumberOfTuples()): print data.GetTuple1(p), print return data
def ConvertTextureToPointAttribute(modelNode, textureImageNode): polyData=modelNode.GetPolyData() textureImageFlipVert=vtk.vtkImageFlip() textureImageFlipVert.SetFilteredAxis(1) textureImageFlipVert.SetInputConnection(textureImageNode.GetImageDataConnection()) textureImageFlipVert.Update() textureImageData=textureImageFlipVert.GetOutput() pointData=polyData.GetPointData() tcoords=pointData.GetTCoords() numOfPoints=pointData.GetNumberOfTuples() assert numOfPoints==tcoords.GetNumberOfTuples(), "Number of texture coordinates does not equal number of points" textureSamplingPointsUv=vtk.vtkPoints() textureSamplingPointsUv.SetNumberOfPoints(numOfPoints) for pointIndex in xrange(numOfPoints): uv=tcoords.GetTuple2(pointIndex) textureSamplingPointsUv.SetPoint(pointIndex, uv[0], uv[1], 0) textureSamplingPointDataUv=vtk.vtkPolyData() uvToXyz=vtk.vtkTransform() textureImageDataSpacingSpacing=textureImageData.GetSpacing() textureImageDataSpacingOrigin=textureImageData.GetOrigin() textureImageDataSpacingDimensions=textureImageData.GetDimensions() uvToXyz.Scale(textureImageDataSpacingDimensions[0]/textureImageDataSpacingSpacing[0], textureImageDataSpacingDimensions[1]/textureImageDataSpacingSpacing[1], 1) uvToXyz.Translate(textureImageDataSpacingOrigin) textureSamplingPointDataUv.SetPoints(textureSamplingPointsUv) transformPolyDataToXyz=vtk.vtkTransformPolyDataFilter() transformPolyDataToXyz.SetInputData(textureSamplingPointDataUv) transformPolyDataToXyz.SetTransform(uvToXyz) probeFilter=vtk.vtkProbeFilter() probeFilter.SetInputConnection(transformPolyDataToXyz.GetOutputPort()) probeFilter.SetSourceData(textureImageData) probeFilter.Update() rgbPoints=probeFilter.GetOutput().GetPointData().GetArray('ImageScalars') colorArrayRed=vtk.vtkDoubleArray() colorArrayRed.SetName('ColorRed') colorArrayRed.SetNumberOfTuples(numOfPoints) colorArrayGreen=vtk.vtkDoubleArray() colorArrayGreen.SetName('ColorGreen') colorArrayGreen.SetNumberOfTuples(numOfPoints) colorArrayBlue=vtk.vtkDoubleArray() colorArrayBlue.SetName('ColorBlue') colorArrayBlue.SetNumberOfTuples(numOfPoints) for pointIndex in xrange(numOfPoints): rgb=rgbPoints.GetTuple3(pointIndex) colorArrayRed.SetValue(pointIndex,rgb[0]) colorArrayGreen.SetValue(pointIndex,rgb[1]) colorArrayBlue.SetValue(pointIndex,rgb[2]) colorArrayRed.Modified() colorArrayGreen.Modified() colorArrayBlue.Modified() pointData.AddArray(colorArrayRed) pointData.AddArray(colorArrayGreen) pointData.AddArray(colorArrayBlue) pointData.Modified() polyData.Modified()
def probeVolume(self, volumeNode, rulerNode): p0ras=rulerNode.GetPolyData().GetPoint(0)+(1,) p1ras=rulerNode.GetPolyData().GetPoint(1)+(1,) ras2ijk=vtk.vtkMatrix4x4() volumeNode.GetRASToIJKMatrix(ras2ijk) p0ijk= [int(round(c)) for c in ras2ijk.MultiplyPoint(p0ras)[:3]] p1ijk= [int(round(c)) for c in ras2ijk.MultiplyPoint(p1ras)[:3]] line=vtk.vtkLineSource() line.SetPoint1(p0ijk[0],p0ijk[1],p0ijk[2]) line.SetPoint2(p1ijk[0],p1ijk[1],p1ijk[2]) line.SetResolution(100) probe=vtk.vtkProbeFilter() probe.SetInputConnection(line.GetOutputPort()) probe.SetSourceData(volumeNode.GetImageData()) probe.Update() return probe.GetOutput().GetPointData().GetArray('ImageScalars')
def updateOutputArray(self,inputVolume,inputRuler,outputArray,numberOfLineSamples): rulerStartPoint_Ruler = [0,0,0] rulerEndPoint_Ruler = [0,0,0] inputRuler.GetPosition1(rulerStartPoint_Ruler) inputRuler.GetPosition2(rulerEndPoint_Ruler) rulerStartPoint_Ruler1 = [rulerStartPoint_Ruler[0], rulerStartPoint_Ruler[1], rulerStartPoint_Ruler[2], 1.0] rulerEndPoint_Ruler1 = [rulerEndPoint_Ruler[0], rulerEndPoint_Ruler[1], rulerEndPoint_Ruler[2], 1.0] rulerToRAS = vtk.vtkMatrix4x4() rulerTransformNode = inputRuler.GetParentTransformNode() if rulerTransformNode: if rulerTransformNode.IsTransformToWorldLinear(): rulerToRAS.DeepCopy(rulerTransformNode.GetMatrixTransformToParent()) else: print("Cannot handle non-linear transforms - ignoring transform of the input ruler") rulerStartPoint_RAS1 = [0,0,0,1] rulerEndPoint_RAS1 = [0,0,0,1] rulerToRAS.MultiplyPoint(rulerStartPoint_Ruler1,rulerStartPoint_RAS1) rulerToRAS.MultiplyPoint(rulerEndPoint_Ruler1,rulerEndPoint_RAS1) rulerLengthMm = math.sqrt(vtk.vtkMath.Distance2BetweenPoints(rulerStartPoint_RAS1[0:3],rulerEndPoint_RAS1[0:3])) # Need to get the start/end point of the line in the IJK coordinate system # as VTK filters cannot take into account direction cosines rasToIJK = vtk.vtkMatrix4x4() parentToIJK = vtk.vtkMatrix4x4() rasToParent = vtk.vtkMatrix4x4() inputVolume.GetRASToIJKMatrix(parentToIJK) transformNode = inputVolume.GetParentTransformNode() if transformNode: if transformNode.IsTransformToWorldLinear(): rasToParent.DeepCopy(transformNode.GetMatrixTransformToParent()) rasToParent.Invert() else: print ("Cannot handle non-linear transforms - ignoring transform of the input volume") vtk.vtkMatrix4x4.Multiply4x4(parentToIJK, rasToParent, rasToIJK) rulerStartPoint_IJK1 = [0,0,0,1] rulerEndPoint_IJK1 = [0,0,0,1] rasToIJK.MultiplyPoint(rulerStartPoint_RAS1,rulerStartPoint_IJK1) rasToIJK.MultiplyPoint(rulerEndPoint_RAS1,rulerEndPoint_IJK1) lineSource = vtk.vtkLineSource() lineSource.SetPoint1(rulerStartPoint_IJK1[0],rulerStartPoint_IJK1[1],rulerStartPoint_IJK1[2]) lineSource.SetPoint2(rulerEndPoint_IJK1[0], rulerEndPoint_IJK1[1], rulerEndPoint_IJK1[2]) lineSource.SetResolution(numberOfLineSamples-1) probeFilter=vtk.vtkProbeFilter() probeFilter.SetInputConnection(lineSource.GetOutputPort()) if vtk.VTK_MAJOR_VERSION <= 5: probeFilter.SetSource(inputVolume.GetImageData()) else: probeFilter.SetSourceData(inputVolume.GetImageData()) probeFilter.Update() probedPoints=probeFilter.GetOutput() # Create arrays of data a = outputArray.GetArray() a.SetNumberOfTuples(probedPoints.GetNumberOfPoints()) x = xrange(0, probedPoints.GetNumberOfPoints()) xStep=rulerLengthMm/(probedPoints.GetNumberOfPoints()-1) probedPointScalars=probedPoints.GetPointData().GetScalars() for i in range(len(x)): a.SetComponent(i, 0, x[i]*xStep) a.SetComponent(i, 1, probedPointScalars.GetTuple(i)[0]) a.SetComponent(i, 2, 0) probedPoints.GetPointData().GetScalars().Modified()
def updateOutputTable(self, inputVolumes, inputRuler, outputTable, lineResolution): rulerLengthMm = self.computeRulerLength(inputRuler) distanceArray = self.getArrayFromTable(outputTable, DISTANCE_ARRAY_NAME) probedPointsList = [] intensityArrayList = [] for inputVolume in inputVolumes: # Need to get the start/end point of the line in the IJK coordinate system # as VTK filters cannot take into account direction cosines rasToIJK = vtk.vtkMatrix4x4() parentToIJK = vtk.vtkMatrix4x4() rasToParent = vtk.vtkMatrix4x4() inputVolume.GetRASToIJKMatrix(parentToIJK) transformNode = inputVolume.GetParentTransformNode() if transformNode: if transformNode.IsTransformToWorldLinear(): rasToParent.DeepCopy( transformNode.GetMatrixTransformToParent()) rasToParent.Invert() else: print( "Cannot handle non-linear transforms - ignoring transform of the input volume" ) vtk.vtkMatrix4x4.Multiply4x4(parentToIJK, rasToParent, rasToIJK) rulerStartPoint_IJK1 = [0, 0, 0, 1] rulerEndPoint_IJK1 = [0, 0, 0, 1] rasToIJK.MultiplyPoint(self.rulerStartPoint_RAS1, rulerStartPoint_IJK1) rasToIJK.MultiplyPoint(self.rulerEndPoint_RAS1, rulerEndPoint_IJK1) lineSource = vtk.vtkLineSource() lineSource.SetPoint1(rulerStartPoint_IJK1[0], rulerStartPoint_IJK1[1], rulerStartPoint_IJK1[2]) lineSource.SetPoint2(rulerEndPoint_IJK1[0], rulerEndPoint_IJK1[1], rulerEndPoint_IJK1[2]) lineSource.SetResolution(lineResolution - 1) probeFilter = vtk.vtkProbeFilter() probeFilter.SetInputConnection(lineSource.GetOutputPort()) probeFilter.SetSourceData(inputVolume.GetImageData()) probeFilter.Update() probedPoints = probeFilter.GetOutput() probedPointsList.append(probedPoints) intensityArrayName = INTENSITY_ARRAY_NAME + '_' + inputVolume.GetName( ) intensityArrayList.append( self.getArrayFromTable(outputTable, intensityArrayName)) # Fill tables for probeIndex in range(len(probedPointsList)): probedPoints = probedPointsList[probeIndex] intensityArray = intensityArrayList[probeIndex] # Create arrays of data outputTable.GetTable().SetNumberOfRows( probedPoints.GetNumberOfPoints()) x = range(0, probedPoints.GetNumberOfPoints()) xStep = rulerLengthMm / (probedPoints.GetNumberOfPoints() - 1) if probeIndex == 0: for i in range(len(x)): distanceArray.SetValue(i, x[i] * xStep) probedPointScalars = probedPoints.GetPointData().GetScalars() for i in range(len(x)): intensityArray.SetValue(i, probedPointScalars.GetTuple(i)[0])
def convertTextureToPointAttribute(self, modelNode, textureImageNode, colorAsVector): polyData = modelNode.GetPolyData() textureImageFlipVert = vtk.vtkImageFlip() textureImageFlipVert.SetFilteredAxis(1) textureImageFlipVert.SetInputConnection( textureImageNode.GetImageDataConnection()) textureImageFlipVert.Update() textureImageData = textureImageFlipVert.GetOutput() pointData = polyData.GetPointData() tcoords = pointData.GetTCoords() numOfPoints = pointData.GetNumberOfTuples() assert numOfPoints == tcoords.GetNumberOfTuples( ), "Number of texture coordinates does not equal number of points" textureSamplingPointsUv = vtk.vtkPoints() textureSamplingPointsUv.SetNumberOfPoints(numOfPoints) for pointIndex in range(numOfPoints): uv = tcoords.GetTuple2(pointIndex) textureSamplingPointsUv.SetPoint(pointIndex, uv[0], uv[1], 0) textureSamplingPointDataUv = vtk.vtkPolyData() uvToXyz = vtk.vtkTransform() textureImageDataSpacingSpacing = textureImageData.GetSpacing() textureImageDataSpacingOrigin = textureImageData.GetOrigin() textureImageDataSpacingDimensions = textureImageData.GetDimensions() uvToXyz.Scale( textureImageDataSpacingDimensions[0] / textureImageDataSpacingSpacing[0], textureImageDataSpacingDimensions[1] / textureImageDataSpacingSpacing[1], 1) uvToXyz.Translate(textureImageDataSpacingOrigin) textureSamplingPointDataUv.SetPoints(textureSamplingPointsUv) transformPolyDataToXyz = vtk.vtkTransformPolyDataFilter() transformPolyDataToXyz.SetInputData(textureSamplingPointDataUv) transformPolyDataToXyz.SetTransform(uvToXyz) probeFilter = vtk.vtkProbeFilter() probeFilter.SetInputConnection(transformPolyDataToXyz.GetOutputPort()) probeFilter.SetSourceData(textureImageData) probeFilter.Update() rgbPoints = probeFilter.GetOutput().GetPointData().GetArray( 'ImageScalars') if colorAsVector: colorArray = vtk.vtkDoubleArray() colorArray.SetName('Color') colorArray.SetNumberOfComponents(3) colorArray.SetNumberOfTuples(numOfPoints) for pointIndex in range(numOfPoints): rgb = rgbPoints.GetTuple3(pointIndex) colorArray.SetTuple3(pointIndex, rgb[0] / 255., rgb[1] / 255., rgb[2] / 255.) colorArray.Modified() pointData.AddArray(colorArray) else: colorArrayRed = vtk.vtkDoubleArray() colorArrayRed.SetName('ColorRed') colorArrayRed.SetNumberOfTuples(numOfPoints) colorArrayGreen = vtk.vtkDoubleArray() colorArrayGreen.SetName('ColorGreen') colorArrayGreen.SetNumberOfTuples(numOfPoints) colorArrayBlue = vtk.vtkDoubleArray() colorArrayBlue.SetName('ColorBlue') colorArrayBlue.SetNumberOfTuples(numOfPoints) for pointIndex in range(numOfPoints): rgb = rgbPoints.GetTuple3(pointIndex) colorArrayRed.SetValue(pointIndex, rgb[0]) colorArrayGreen.SetValue(pointIndex, rgb[1]) colorArrayBlue.SetValue(pointIndex, rgb[2]) colorArrayRed.Modified() colorArrayGreen.Modified() colorArrayBlue.Modified() pointData.AddArray(colorArrayRed) pointData.AddArray(colorArrayGreen) pointData.AddArray(colorArrayBlue) pointData.Modified() polyData.Modified()
def updateOutputArray(self,inputVolume,inputRuler,outputArray,lineResolution): rulerStartPoint_Ruler = [0,0,0] rulerEndPoint_Ruler = [0,0,0] inputRuler.GetPosition1(rulerStartPoint_Ruler) inputRuler.GetPosition2(rulerEndPoint_Ruler) rulerStartPoint_Ruler1 = [rulerStartPoint_Ruler[0], rulerStartPoint_Ruler[1], rulerStartPoint_Ruler[2], 1.0] rulerEndPoint_Ruler1 = [rulerEndPoint_Ruler[0], rulerEndPoint_Ruler[1], rulerEndPoint_Ruler[2], 1.0] rulerToRAS = vtk.vtkMatrix4x4() rulerTransformNode = inputRuler.GetParentTransformNode() if rulerTransformNode: if rulerTransformNode.IsTransformToWorldLinear(): rulerToRAS.DeepCopy(rulerTransformNode.GetMatrixTransformToParent()) else: print ("Cannot handle non-linear transforms - ignoring transform of the input ruler") rulerStartPoint_RAS1 = [0,0,0,1] rulerEndPoint_RAS1 = [0,0,0,1] rulerToRAS.MultiplyPoint(rulerStartPoint_Ruler1,rulerStartPoint_RAS1) rulerToRAS.MultiplyPoint(rulerEndPoint_Ruler1,rulerEndPoint_RAS1) rulerLengthMm=math.sqrt(vtk.vtkMath.Distance2BetweenPoints(rulerStartPoint_RAS1[0:3],rulerEndPoint_RAS1[0:3])) # Need to get the start/end point of the line in the IJK coordinate system # as VTK filters cannot take into account direction cosines rasToIJK = vtk.vtkMatrix4x4() parentToIJK = vtk.vtkMatrix4x4() rasToParent = vtk.vtkMatrix4x4() inputVolume.GetRASToIJKMatrix(parentToIJK) transformNode = inputVolume.GetParentTransformNode() if transformNode: if transformNode.IsTransformToWorldLinear(): rasToParent.DeepCopy(transformNode.GetMatrixTransformToParent()) rasToParent.Invert() else: print ("Cannot handle non-linear transforms - ignoring transform of the input volume") vtk.vtkMatrix4x4.Multiply4x4(parentToIJK, rasToParent, rasToIJK) rulerStartPoint_IJK1 = [0,0,0,1] rulerEndPoint_IJK1 = [0,0,0,1] rasToIJK.MultiplyPoint(rulerStartPoint_RAS1,rulerStartPoint_IJK1) rasToIJK.MultiplyPoint(rulerEndPoint_RAS1,rulerEndPoint_IJK1) lineSource=vtk.vtkLineSource() lineSource.SetPoint1(rulerStartPoint_IJK1[0],rulerStartPoint_IJK1[1],rulerStartPoint_IJK1[2]) lineSource.SetPoint2(rulerEndPoint_IJK1[0], rulerEndPoint_IJK1[1], rulerEndPoint_IJK1[2]) lineSource.SetResolution(lineResolution-1) probeFilter=vtk.vtkProbeFilter() probeFilter.SetInputConnection(lineSource.GetOutputPort()) if vtk.VTK_MAJOR_VERSION <= 5: probeFilter.SetSource(inputVolume.GetImageData()) else: probeFilter.SetSourceData(inputVolume.GetImageData()) probeFilter.Update() probedPoints=probeFilter.GetOutput() # Create arrays of data a = outputArray.GetArray() a.SetNumberOfTuples(probedPoints.GetNumberOfPoints()) x = xrange(0, probedPoints.GetNumberOfPoints()) xStep=rulerLengthMm/(probedPoints.GetNumberOfPoints()-1) probedPointScalars=probedPoints.GetPointData().GetScalars() for i in range(len(x)): a.SetComponent(i, 0, x[i]*xStep) a.SetComponent(i, 1, probedPointScalars.GetTuple(i)[0]) a.SetComponent(i, 2, 0) probedPoints.GetPointData().GetScalars().Modified()