def __init__(self,fixed=None,moving=None,transform=None): self.interval = 20 self.timer = None # parameter defaults self.sampleSpacing = 3 self.gradientWindow = 1 self.stepSize = 1 # the parametricTransform #self.parametricTransform = RegimaticTranslationTransform() self.parametricTransform = RegimaticTranslateSRotatePATransform() #self.parametricTransform = RegimaticRigidTransform() #TODO: fix quaternion # slicer nodes set by the GUI self.fixed = fixed self.moving = moving self.transform = transform # optimizer state variables self.iteration = 0 self.metric = 0 # helper objects self.scratchMatrix = vtk.vtkMatrix4x4() self.ijkToRAS = vtk.vtkMatrix4x4() self.rasToIJK = vtk.vtkMatrix4x4() self.matrixToParent = vtk.vtkMatrix4x4() self.reslice = vtk.vtkImageReslice() self.resliceTransform = vtk.vtkTransform() self.viewer = None
def run(self,referenceNode, inputNode, transformNode, outputNode): """ Run the actual algorithm """ dimensions = [1,1,1] referenceNode.GetImageData().GetDimensions(dimensions) inputIJK2RASMatrix = vtk.vtkMatrix4x4() inputNode.GetIJKToRASMatrix(inputIJK2RASMatrix) referenceRAS2IJKMatrix = vtk.vtkMatrix4x4() referenceNode.GetRASToIJKMatrix(referenceRAS2IJKMatrix) inputRAS2RASMatrix = transformNode.GetMatrixTransformToParent() resampleTransform = vtk.vtkTransform() resampleTransform.Identity() resampleTransform.PostMultiply() resampleTransform.SetMatrix(inputIJK2RASMatrix) resampleTransform.Concatenate(inputRAS2RASMatrix) resampleTransform.Concatenate(referenceRAS2IJKMatrix) resampleTransform.Inverse() resampler = vtk.vtkImageReslice() resampler.SetInput(inputNode.GetImageData()) resampler.SetOutputOrigin(0,0,0) resampler.SetOutputSpacing(1,1,1) resampler.SetOutputExtent(0,dimensions[0],0,dimensions[1],0,dimensions[2]) resampler.SetResliceTransform(resampleTransform) resampler.Update() outputNode.CopyOrientation(referenceNode) outputNode.SetAndObserveImageData(resampler.GetOutput()) return True
def run(self, referenceSequenceNode, inputNode, transformSequenceNode, outputSequenceNode): """ Run the actual algorithm """ if outputSequenceNode: outputSequenceNode.RemoveAllDataNodes() numOfImageNodes = referenceSequenceNode.GetNumberOfDataNodes() for i in xrange(numOfImageNodes): referenceNode = referenceSequenceNode.GetNthDataNode(i) referenceNodeIndexValue = referenceSequenceNode.GetNthIndexValue(i) dimensions = [1, 1, 1] referenceNode.GetImageData().GetDimensions(dimensions) transformNode = transformSequenceNode.GetNthDataNode(i) inputIJK2RASMatrix = vtk.vtkMatrix4x4() inputNode.GetIJKToRASMatrix(inputIJK2RASMatrix) referenceRAS2IJKMatrix = vtk.vtkMatrix4x4() referenceNode.GetRASToIJKMatrix(referenceRAS2IJKMatrix) inputRAS2RASMatrix = transformNode.GetTransformToParent() resampleTransform = vtk.vtkGeneralTransform() resampleTransform.Identity() resampleTransform.PostMultiply() resampleTransform.Concatenate(inputIJK2RASMatrix) resampleTransform.Concatenate(inputRAS2RASMatrix) resampleTransform.Concatenate(referenceRAS2IJKMatrix) resampleTransform.Inverse() resampler = vtk.vtkImageReslice() resampler.SetInput(inputNode.GetImageData()) resampler.SetOutputOrigin(0, 0, 0) resampler.SetOutputSpacing(1, 1, 1) resampler.SetOutputExtent(0, dimensions[0], 0, dimensions[1], 0, dimensions[2]) resampler.SetResliceTransform(resampleTransform) resampler.Update() outputNode = slicer.vtkMRMLScalarVolumeNode() outputNode.CopyOrientation(referenceNode) outputNode.SetAndObserveImageData(resampler.GetOutput()) outputSequenceNode.SetDataNodeAtValue(outputNode, referenceNodeIndexValue) return True
def __init__(self,fixed=None,moving=None,transform=None,fiducial=None,checked = None): self.interval = 2 self.timer = None # parameter defaults self.sampleSpacing = 10 self.gradientWindow = 1 self.stepSize = 1 # slicer nodes set by the GUI self.fixed = fixed self.moving = moving self.transform = transform self.fiducial = fiducial self.checked = checked # optimizer state variables self.iteration = 0 self.position = [0, 0, 0] self.paintCoordinates = [] self.x0, self.y0, self.z0 = 0,0,0 self.tx0, self.ty0,self.tz0 = 0,0,0 self.m = vtk.vtkMatrix4x4() self.r = vtk.vtkTransform() self.transformNode,self.neutral = None, None self.before = 0 self.plan = 'plan' self.actionState = "idle" self.interactorObserverTags = [] self.styleObserverTags = [] self.sliceWidgetsPerStyle = {} self.tac=0 self.WMAX = 0 self.L=[] self.divider = 1 self.step = 1 # helper objects self.scratchMatrix = vtk.vtkMatrix4x4() self.ijkToRAS = vtk.vtkMatrix4x4() self.rasToIJK = vtk.vtkMatrix4x4() self.reslice = vtk.vtkImageReslice() self.resliceTransform = vtk.vtkTransform() self.viewer = None self.render = None
def run(self,referenceSequenceNode, inputNode, transformSequenceNode, outputSequenceNode): """ Run the actual algorithm """ if outputSequenceNode: outputSequenceNode.RemoveAllDataNodes() numOfImageNodes = referenceSequenceNode.GetNumberOfDataNodes() for i in xrange(numOfImageNodes): referenceNode = referenceSequenceNode.GetNthDataNode(i) referenceNodeIndexValue = referenceSequenceNode.GetNthIndexValue(i) dimensions = [1,1,1] referenceNode.GetImageData().GetDimensions(dimensions) transformNode = transformSequenceNode.GetNthDataNode(i) inputIJK2RASMatrix = vtk.vtkMatrix4x4() inputNode.GetIJKToRASMatrix(inputIJK2RASMatrix) referenceRAS2IJKMatrix = vtk.vtkMatrix4x4() referenceNode.GetRASToIJKMatrix(referenceRAS2IJKMatrix) inputRAS2RASMatrix = transformNode.GetTransformToParent() resampleTransform = vtk.vtkGeneralTransform() resampleTransform.Identity() resampleTransform.PostMultiply() resampleTransform.Concatenate(inputIJK2RASMatrix) resampleTransform.Concatenate(inputRAS2RASMatrix) resampleTransform.Concatenate(referenceRAS2IJKMatrix) resampleTransform.Inverse() resampler = vtk.vtkImageReslice() resampler.SetInput(inputNode.GetImageData()) resampler.SetOutputOrigin(0,0,0) resampler.SetOutputSpacing(1,1,1) resampler.SetOutputExtent(0,dimensions[0],0,dimensions[1],0,dimensions[2]) resampler.SetResliceTransform(resampleTransform) resampler.Update() outputNode = slicer.vtkMRMLScalarVolumeNode() outputNode.CopyOrientation(referenceNode) outputNode.SetAndObserveImageData(resampler.GetOutput()) outputSequenceNode.SetDataNodeAtValue(outputNode, referenceNodeIndexValue) return True
def resliceThroughTransform(sourceNode, transform, referenceNode, targetNode): """ Fills the targetNode's vtkImageData with the source after applying the transform. Uses spacing from referenceNode. Ignores any vtkMRMLTransforms. sourceNode, referenceNode, targetNode: vtkMRMLScalarVolumeNodes transform: vtkAbstractTransform """ # # get the transform from RAS back to source pixel space sourceRASToIJK = vtk.vtkMatrix4x4() sourceNode.GetRASToIJKMatrix(sourceRASToIJK) # # get the transform from target image space to RAS referenceIJKToRAS = vtk.vtkMatrix4x4() targetNode.GetIJKToRASMatrix(referenceIJKToRAS) # # this is the ijkToRAS concatenated with the passed in (abstract)transform resliceTransform = vtk.vtkGeneralTransform() resliceTransform.Concatenate(sourceRASToIJK) resliceTransform.Concatenate(transform) resliceTransform.Concatenate(referenceIJKToRAS) # # use the matrix to extract the volume and convert it to an array reslice = vtk.vtkImageReslice() reslice.SetInterpolationModeToLinear() reslice.InterpolateOn() reslice.SetResliceTransform(resliceTransform) if vtk.VTK_MAJOR_VERSION <= 5: reslice.SetInput(sourceNode.GetImageData()) else: reslice.SetInputConnection(sourceNode.GetImageDataConnection()) # dimensions = referenceNode.GetImageData().GetDimensions() reslice.SetOutputExtent(0, dimensions[0] - 1, 0, dimensions[1] - 1, 0, dimensions[2] - 1) reslice.SetOutputOrigin((0, 0, 0)) reslice.SetOutputSpacing((1, 1, 1)) # reslice.UpdateWholeExtent() targetNode.SetAndObserveImageData(reslice.GetOutput())
def resliceThroughTransform(self, sourceNode, transform, referenceNode, targetNode): """ Fills the targetNode's vtkImageData with the source after applying the transform. Uses spacing from referenceNode. Ignores any vtkMRMLTransforms. sourceNode, referenceNode, targetNode: vtkMRMLScalarVolumeNodes transform: vtkAbstractTransform """ # get the transform from RAS back to source pixel space sourceRASToIJK = vtk.vtkMatrix4x4() sourceNode.GetRASToIJKMatrix(sourceRASToIJK) # get the transform from target image space to RAS referenceIJKToRAS = vtk.vtkMatrix4x4() targetNode.GetIJKToRASMatrix(referenceIJKToRAS) # this is the ijkToRAS concatenated with the passed in (abstract)transform self.resliceTransform = vtk.vtkGeneralTransform() self.resliceTransform.Concatenate(sourceRASToIJK) self.resliceTransform.Concatenate(transform) self.resliceTransform.Concatenate(referenceIJKToRAS) # use the matrix to extract the volume and convert it to an array self.reslice = vtk.vtkImageReslice() self.reslice.SetInterpolationModeToLinear() self.reslice.InterpolateOn() self.reslice.SetResliceTransform(self.resliceTransform) self.reslice.SetInput( sourceNode.GetImageData() ) dimensions = referenceNode.GetImageData().GetDimensions() self.reslice.SetOutputExtent(0, dimensions[0]-1, 0, dimensions[1]-1, 0, dimensions[2]-1) self.reslice.SetOutputOrigin((0,0,0)) self.reslice.SetOutputSpacing((1,1,1)) self.reslice.UpdateWholeExtent() targetNode.SetAndObserveImageData(self.reslice.GetOutput())
def resampleInputVolumeNodetoReferenceVolumeNode(self, inputVolumeNode, referenceVolumeNode, outputVolumeNode): if not inputVolumeNode or not referenceVolumeNode or not outputVolumeNode: print "Volumes not initialized." return inputVolumeIJK2RASMatrix = vtk.vtkMatrix4x4() inputVolumeNode.GetIJKToRASMatrix(inputVolumeIJK2RASMatrix) referenceVolumeRAS2IJKMatrix = vtk.vtkMatrix4x4() referenceVolumeNode.GetRASToIJKMatrix(referenceVolumeRAS2IJKMatrix) dimensions = referenceVolumeNode.GetImageData().GetDimensions() outputVolumeResliceTransform = vtk.vtkTransform() outputVolumeResliceTransform.Identity() outputVolumeResliceTransform.PostMultiply() outputVolumeResliceTransform.SetMatrix(inputVolumeIJK2RASMatrix) if inputVolumeNode.GetTransformNodeID() is not None: inputVolumeNodeTransformNode = slicer.util.getNode(inputVolumeNode.GetTransformNodeID()) inputVolumeRAS2RASMatrix = vtk.vtkMatrix4x4() if inputVolumeNodeTransformNode is not None: inputVolumeNodeTransformNode.GetMatrixTransformToWorld(inputVolumeRAS2RASMatrix) outputVolumeResliceTransform.Concatenate(inputVolumeRAS2RASMatrix) outputVolumeResliceTransform.Concatenate(referenceVolumeRAS2IJKMatrix) outputVolumeResliceTransform.Inverse() resliceFilter = vtk.vtkImageReslice() resliceFilter.SetInput(inputVolumeNode.GetImageData()) resliceFilter.SetOutputOrigin(0, 0, 0) resliceFilter.SetOutputSpacing(1, 1, 1) resliceFilter.SetOutputExtent(0, dimensions[0]-1, 0, dimensions[1]-1, 0, dimensions[2]-1) resliceFilter.SetResliceTransform(outputVolumeResliceTransform) resliceFilter.Update() outputVolumeNode.CopyOrientation(referenceVolumeNode) outputVolumeNode.SetAndObserveImageData(resliceFilter.GetOutput()) return
def extractROI(originalVolumeID, newVolumeID, rasCoordinates, diameter): ''' ''' originalVolume = slicer.mrmlScene.GetNodeByID(originalVolumeID) newVolume = slicer.mrmlScene.GetNodeByID(newVolumeID) # code below converted from cropVolume module by A. Fedorov # optimized after that :) inputRASToIJK = vtk.vtkMatrix4x4() inputIJKToRAS = vtk.vtkMatrix4x4() outputIJKToRAS = vtk.vtkMatrix4x4() outputRASToIJK = vtk.vtkMatrix4x4() volumeXform = vtk.vtkMatrix4x4() T = vtk.vtkMatrix4x4() originalVolume.GetRASToIJKMatrix(inputRASToIJK) originalVolume.GetIJKToRASMatrix(inputIJKToRAS) outputIJKToRAS.Identity() outputRASToIJK.Identity() volumeXform.Identity() T.Identity() # if the originalVolume is under a transform volumeTransformNode = originalVolume.GetParentTransformNode() if volumeTransformNode: volumeTransformNode.GetMatrixTransformToWorld(volumeXform) volumeXform.Invert() maxSpacing = max(originalVolume.GetSpacing()) # build our box rX = diameter * 4 * maxSpacing rY = diameter * 4 * maxSpacing rZ = diameter * 4 * maxSpacing cX = rasCoordinates[0] cY = rasCoordinates[1] cZ = rasCoordinates[2] inputSpacingX = originalVolume.GetSpacing()[0] inputSpacingY = originalVolume.GetSpacing()[1] inputSpacingZ = originalVolume.GetSpacing()[2] outputExtentX = int(2.0 * rX / inputSpacingX) outputExtentY = int(2.0 * rY / inputSpacingY) outputExtentZ = int(2.0 * rZ / inputSpacingZ) # configure spacing outputIJKToRAS.SetElement(0, 0, inputSpacingX) outputIJKToRAS.SetElement(1, 1, inputSpacingY) outputIJKToRAS.SetElement(2, 2, inputSpacingZ) # configure origin outputIJKToRAS.SetElement(0, 3, (cX - rX + inputSpacingX * 0.5)) outputIJKToRAS.SetElement(1, 3, (cY - rY + inputSpacingY * 0.5)) outputIJKToRAS.SetElement(2, 3, (cZ - rZ + inputSpacingZ * 0.5)) outputRASToIJK.DeepCopy(outputIJKToRAS) outputRASToIJK.Invert() T.DeepCopy(outputIJKToRAS) T.Multiply4x4(volumeXform, T, T) T.Multiply4x4(inputRASToIJK, T, T) resliceT = vtk.vtkTransform() resliceT.SetMatrix(T) reslicer = vtk.vtkImageReslice() reslicer.SetInterpolationModeToLinear() reslicer.SetInput(originalVolume.GetImageData()) reslicer.SetOutputExtent(0, int(outputExtentX), 0, int(outputExtentY), 0, int(outputExtentZ)) reslicer.SetOutputOrigin(0, 0, 0) reslicer.SetOutputSpacing(1, 1, 1) #reslicer.SetOutputOrigin(image.GetOrigin()) #reslicer.SetOutputSpacing(image.GetSpacing()) reslicer.SetResliceTransform(resliceT) reslicer.UpdateWholeExtent() changer = vtk.vtkImageChangeInformation() changer.SetInput(reslicer.GetOutput()) changer.SetOutputOrigin(0, 0, 0) changer.SetOutputSpacing(1, 1, 1) #changer.SetOutputOrigin(image.GetOrigin()) # changer.SetOutputSpacing(image.GetSpacing()) changer.Update() outImageData = vtk.vtkImageData() outImageData.DeepCopy(changer.GetOutput()) outImageData.Update() newVolume.SetAndObserveImageData(outImageData) newVolume.SetIJKToRASMatrix(outputIJKToRAS) newVolume.SetRASToIJKMatrix(outputRASToIJK) newVolume.Modified() newVolume.SetModifiedSinceRead(1)
def extractROI(originalVolumeID,newVolumeID,rasCoordinates,diameter): ''' ''' originalVolume = slicer.mrmlScene.GetNodeByID(originalVolumeID) newVolume = slicer.mrmlScene.GetNodeByID(newVolumeID) # code below converted from cropVolume module by A. Fedorov # optimized after that :) inputRASToIJK = vtk.vtkMatrix4x4() inputIJKToRAS = vtk.vtkMatrix4x4() outputIJKToRAS = vtk.vtkMatrix4x4() outputRASToIJK = vtk.vtkMatrix4x4() volumeXform = vtk.vtkMatrix4x4() T = vtk.vtkMatrix4x4() originalVolume.GetRASToIJKMatrix(inputRASToIJK) originalVolume.GetIJKToRASMatrix(inputIJKToRAS) outputIJKToRAS.Identity() outputRASToIJK.Identity() volumeXform.Identity() T.Identity() # if the originalVolume is under a transform volumeTransformNode = originalVolume.GetParentTransformNode() if volumeTransformNode: volumeTransformNode.GetMatrixTransformToWorld(volumeXform) volumeXform.Invert() maxSpacing = max(originalVolume.GetSpacing()) # build our box rX = diameter*4*maxSpacing rY = diameter*4*maxSpacing rZ = diameter*4*maxSpacing cX = rasCoordinates[0] cY = rasCoordinates[1] cZ = rasCoordinates[2] inputSpacingX = originalVolume.GetSpacing()[0] inputSpacingY = originalVolume.GetSpacing()[1] inputSpacingZ = originalVolume.GetSpacing()[2] outputExtentX = int(2.0*rX/inputSpacingX) outputExtentY = int(2.0*rY/inputSpacingY) outputExtentZ = int(2.0*rZ/inputSpacingZ) # configure spacing outputIJKToRAS.SetElement(0,0,inputSpacingX) outputIJKToRAS.SetElement(1,1,inputSpacingY) outputIJKToRAS.SetElement(2,2,inputSpacingZ) # configure origin outputIJKToRAS.SetElement(0,3,(cX-rX+inputSpacingX*0.5)) outputIJKToRAS.SetElement(1,3,(cY-rY+inputSpacingY*0.5)) outputIJKToRAS.SetElement(2,3,(cZ-rZ+inputSpacingZ*0.5)) outputRASToIJK.DeepCopy(outputIJKToRAS) outputRASToIJK.Invert() T.DeepCopy(outputIJKToRAS) T.Multiply4x4(volumeXform,T,T) T.Multiply4x4(inputRASToIJK,T,T) resliceT = vtk.vtkTransform() resliceT.SetMatrix(T) reslicer = vtk.vtkImageReslice() reslicer.SetInterpolationModeToLinear() reslicer.SetInput(originalVolume.GetImageData()) reslicer.SetOutputExtent(0,int(outputExtentX),0,int(outputExtentY),0,int(outputExtentZ)) reslicer.SetOutputOrigin(0,0,0) reslicer.SetOutputSpacing(1,1,1) #reslicer.SetOutputOrigin(image.GetOrigin()) #reslicer.SetOutputSpacing(image.GetSpacing()) reslicer.SetResliceTransform(resliceT) reslicer.UpdateWholeExtent() changer = vtk.vtkImageChangeInformation() changer.SetInput(reslicer.GetOutput()) changer.SetOutputOrigin(0,0,0) changer.SetOutputSpacing(1,1,1) #changer.SetOutputOrigin(image.GetOrigin()) # changer.SetOutputSpacing(image.GetSpacing()) changer.Update() outImageData = vtk.vtkImageData() outImageData.DeepCopy(changer.GetOutput()) outImageData.Update() newVolume.SetAndObserveImageData(outImageData) newVolume.SetIJKToRASMatrix(outputIJKToRAS) newVolume.SetRASToIJKMatrix(outputRASToIJK) newVolume.Modified()