Пример #1
0
    def ExecuteSeeds(self, inVolumeNode, sourceSeedsNode):

        self._helper.debug("Starting execution of seeds...")

        if not inVolumeNode or not sourceSeedsNode:
            slicer.Application.ErrorMessage(
                "Not enough information. Aborting Seeds..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            seeds = sourceSeedsNode

            initialLevelSets = slicer.vtkImageData()
            initialLevelSets.DeepCopy(image)
            initialLevelSets.Update()

            levelSetsInputScalars = initialLevelSets.GetPointData().GetScalars(
            )
            levelSetsInputScalars.FillComponent(0, 1.0)

            for i in range(seeds.GetNumberOfFiducials()):
                rasPt = seeds.GetNthFiducialXYZ(i)
                ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                id = image.ComputePointId(int(ijkPt[0]), int(ijkPt[1]),
                                          int(ijkPt[2]))
                levelSetsInputScalars.SetComponent(id, 0, -1.0)

            dilateErode = slicer.vtkImageDilateErode3D()
            dilateErode.SetInput(initialLevelSets)
            dilateErode.SetDilateValue(-1.0)
            dilateErode.SetErodeValue(1.0)
            dilateErode.SetKernelSize(3, 3, 3)
            dilateErode.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(dilateErode.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode, 0.0)

            self._helper.debug("Seeds done...")

            return outputContainer
    def ExecuteSeeds(self,inVolumeNode,sourceSeedsNode):


        self._helper.debug("Starting execution of seeds...")

        if not inVolumeNode or not sourceSeedsNode:
            slicer.Application.ErrorMessage("Not enough information. Aborting Seeds..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            seeds = sourceSeedsNode
    
            initialLevelSets = slicer.vtkImageData()
            initialLevelSets.DeepCopy(image)
            initialLevelSets.Update()

            levelSetsInputScalars = initialLevelSets.GetPointData().GetScalars()
            levelSetsInputScalars.FillComponent(0,1.0)

            for i in range(seeds.GetNumberOfFiducials()):
                rasPt = seeds.GetNthFiducialXYZ(i)
                ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                id = image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2]))
                levelSetsInputScalars.SetComponent(id,0,-1.0)

            dilateErode = slicer.vtkImageDilateErode3D()
            dilateErode.SetInput(initialLevelSets)
            dilateErode.SetDilateValue(-1.0)
            dilateErode.SetErodeValue(1.0)
            dilateErode.SetKernelSize(3,3,3)
            dilateErode.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(dilateErode.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0)

            self._helper.debug("Seeds done...")

            return outputContainer
    def SetAndMergeEvolVolume(self,resultContainer):

        scene = self._mainGUIClass.GetLogic().GetMRMLScene()

        volumeNode = resultContainer.GetNode()
        threshold = resultContainer.GetThreshold()

        if self._mainGUIClass._outEvolVolume == None:

            # no node so far

            self._mainGUIClass._outEvolVolume = slicer.vtkMRMLScalarVolumeNode()
            self._mainGUIClass._outEvolVolume.SetName("VMTK Level-Set Evolution Output Volume")
            self._mainGUIClass._outEvolVolume.SetScene(scene)
            self._mainGUIClass._outEvolVolume.SetAndObserveImageData(slicer.vtkImageData())
            scene.AddNode(self._mainGUIClass._outEvolVolume)

            self._mainGUIClass._outEvolVolumeLast = slicer.vtkMRMLScalarVolumeNode()
            self._mainGUIClass._outEvolVolumeLast.SetName("VMTK Level-Set Evolution Output Volume (Last Step)")
            self._mainGUIClass._outEvolVolumeLast.SetScene(scene)
            self._mainGUIClass._outEvolVolumeLast.SetAndObserveImageData(slicer.vtkImageData())
            scene.AddNode(self._mainGUIClass._outEvolVolumeLast)

        matrix = slicer.vtkMatrix4x4()

        # copy current outEvolVolume to outEvolVolumeLast
        self._mainGUIClass._outEvolVolumeLast.SetAndObserveImageData(self._mainGUIClass._outEvolVolume.GetImageData())
        self._mainGUIClass._outEvolVolume.GetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolumeLast.SetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolumeLast.SetModifiedSinceRead(1)

        volumeNodeData = volumeNode.GetImageData()

        # merge the oldVolume with volumeNode if oldVolume has already content
        if self._mainGUIClass._outEvolVolume.GetImageData().GetPointData().GetScalars():
            # evolVolumeLast has already content
            minFilter = slicer.vtkImageMathematics()
            minFilter.SetOperationToMin()
            minFilter.SetInput1(self._mainGUIClass._outEvolVolume.GetImageData()) #the old one
            minFilter.SetInput2(volumeNode.GetImageData()) #the new one
            minFilter.Update()
            volumeNodeData.DeepCopy(minFilter.GetOutput())

        # copy new volume to outEvolVolume
        volumeNode.GetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolume.SetAndObserveImageData(volumeNodeData)
        self._mainGUIClass._outEvolVolume.SetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolume.SetModifiedSinceRead(1)

        outputContainer = SlicerVMTKLevelSetContainer(self._mainGUIClass._outEvolVolume,threshold)

        return outputContainer
    def Apply(self):

        if not self.GetScriptedModuleNode():
          slicer.Application.ErrorMessage("No input ScriptedModuleNode found")
          return

        scriptedModuleNode = self.GetScriptedModuleNode()
    
        inVolume = scriptedModuleNode.GetParameter('InputVolumeRef')
        if not inVolume:
          slicer.Application.ErrorMessage("No input volume found")
          return
      
        outVolume = scriptedModuleNode.GetParameter('OutputVolumeRef')
        if not outVolume:
          slicer.Application.ErrorMessage("No output volume found")
          return
    
        outVolume.CopyOrientation(inVolume)
        outVolume.SetAndObserveTransformNodeID(inVolume.GetTransformNodeID())
    
        gradientAnisotropicDiffusionImageFilter = slicer.vtkITKGradientAnisotropicDiffusionImageFilter()
        gradientAnisotropicDiffusionImageFilter.SetInput(inVolume.GetImageData())
        gradientAnisotropicDiffusionImageFilter.SetConductanceParameter(scriptedModuleNode.GetParameter('Conductance'))
        gradientAnisotropicDiffusionImageFilter.SetNumberOfIterations(scriptedModuleNode.GetParameter('NumberOfIterations'))
        gradientAnisotropicDiffusionImageFilter.SetTimeStep(scriptedModuleNode.GetParameter('TimeStep'))
        gradientAnisotropicDiffusionImageFilter.Update()
    
        image = slicer.vtkImageData()
        image.DeepCopy(gradientAnisotropicDiffusionImageFilter.GetOutput())
        outVolume.SetAndObserveImageData(image)
        outVolume.ModifiedSinceReadOn()
       
        slicer.Application.InformationMessage("Done applying GradientAnisotropicDiffusion.")
Пример #5
0
def dispV0(dataD, inputVolume, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())


    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToShort()

    org = inputVolume.GetOrigin()
    spa = inputVolume.GetSpacing()

    mat = slicer.vtkMatrix4x4()
    inputVolume.GetIJKToRASMatrix(mat)

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    tmp[:] = dataD[:] 

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
    def ApplyVED(self,image,sigmaMin,sigmaMax,numberOfSigmaSteps,alpha,beta,gamma,c,timestep,epsilon,wstrength,sensitivity,numberOfIterations,numberOfDiffusionSubIterations):

        self._helper.debug("Starting execution of VED...")

        cast = slicer.vtkImageCast()
        cast.SetInput(image)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()
        image = cast.GetOutput()

        v = slicer.vtkvmtkVesselEnhancingDiffusionImageFilter()
        v.SetInput(image)
        v.SetSigmaMin(sigmaMin)
        v.SetSigmaMax(sigmaMax)
        v.SetNumberOfSigmaSteps(numberOfSigmaSteps)
        v.SetAlpha(alpha)
        v.SetBeta(beta)
        v.SetGamma(gamma)
        v.SetC(c)
        v.SetTimeStep(timestep)
        v.SetEpsilon(epsilon)
        v.SetWStrength(wstrength)
        v.SetSensitivity(sensitivity)
        v.SetNumberOfIterations(numberOfIterations)
        v.SetNumberOfDiffusionSubIterations(numberOfDiffusionSubIterations)
        v.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(v.GetOutput())
        outVolumeData.Update()
        outVolumeData.GetPointData().GetScalars().Modified()

        return outVolumeData
    def ApplySatoVesselness(self,image,sigmaMin,sigmaMax,numberOfSigmaSteps,alpha,alpha2):

	self._helper.debug(image.GetSpacing())	

        self._helper.debug("Starting execution of Sato...")

        cast = slicer.vtkImageCast()
        cast.SetInput(image)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()
        image = cast.GetOutput()

        v = slicer.vtkvmtkSatoVesselnessMeasureImageFilter()
        v.SetInput(image)
        v.SetSigmaMin(sigmaMin)
        v.SetSigmaMax(sigmaMax)
        v.SetNumberOfSigmaSteps(numberOfSigmaSteps)
        v.SetAlpha1(alpha)
        v.SetAlpha2(alpha2)
        v.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(v.GetOutput())
        outVolumeData.Update()
        outVolumeData.GetPointData().GetScalars().Modified()

        return outVolumeData
Пример #8
0
def dispUV(dataD, org, spa, mat, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())

    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToUnsignedShort()

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    print 'tmp   : ', tmp.shape
    print 'dataD : ', dataD.shape
    tmp[:] = dataD[:]

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
    def ApplyFrangiVesselness(self,image,sigmaMin,sigmaMax,numberOfSigmaSteps,alpha,beta,gamma):

        self._helper.debug("Starting execution of Frangi...")

        cast = slicer.vtkImageCast()
        cast.SetInput(image)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()
        image = cast.GetOutput()

        v = slicer.vtkvmtkVesselnessMeasureImageFilter()
        v.SetInput(image)
        v.SetSigmaMin(sigmaMin)
        v.SetSigmaMax(sigmaMax)
        v.SetNumberOfSigmaSteps(numberOfSigmaSteps)
        v.SetAlpha(alpha)
        v.SetBeta(beta)
        v.SetGamma(gamma)
        v.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(v.GetOutput())
        outVolumeData.Update()
        outVolumeData.GetPointData().GetScalars().Modified()

        return outVolumeData
Пример #10
0
    def BinarizeImageByLabel(self, imageData, labelValue):
        ''' 
            Binarize an image to 0 and 1, while just keeping the voxels with the labelValue
            
            imageData
                vtkImageData
            labelValue
                the value of the voxels to keep
            
            Returns
                vtkImageData after binarizing
        '''

        threshold = slicer.vtkImageThreshold()
        threshold.SetInput(imageData)
        threshold.ThresholdBetween(labelValue, labelValue)
        threshold.ReplaceInOn()
        threshold.ReplaceOutOn()
        threshold.SetInValue(1)
        threshold.SetOutValue(0)
        threshold.Update()

        output = slicer.vtkImageData()
        output.DeepCopy(threshold.GetOutput())

        return output
Пример #11
0
def dispW1(dataD, G, b, org, spa, mat, mu, shpV, nvol):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLDiffusionWeightedVolumeNode()
    r11 = slicer.vtkMRMLDiffusionWeightedVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())

    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToShort()

    r1.SetAndObserveImageData(imgD)
    r1.SetNumberOfGradients(shpV[3])
    r1.SetIJKToRASMatrix(mat)
    r1.SetMeasurementFrameMatrix(mu)
    r1.SetDiffusionGradients(G)
    r1.SetBValues(b)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    tmp[...] = dataD[..., nvol]

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
    def ApplySatoVesselness(self, image, sigmaMin, sigmaMax,
                            numberOfSigmaSteps, alpha, alpha2):

        self._helper.debug(image.GetSpacing())

        self._helper.debug("Starting execution of Sato...")

        cast = slicer.vtkImageCast()
        cast.SetInput(image)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()
        image = cast.GetOutput()

        v = slicer.vtkvmtkSatoVesselnessMeasureImageFilter()
        v.SetInput(image)
        v.SetSigmaMin(sigmaMin)
        v.SetSigmaMax(sigmaMax)
        v.SetNumberOfSigmaSteps(numberOfSigmaSteps)
        v.SetAlpha1(alpha)
        v.SetAlpha2(alpha2)
        v.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(v.GetOutput())
        outVolumeData.Update()
        outVolumeData.GetPointData().GetScalars().Modified()

        return outVolumeData
Пример #13
0
def dispUV(dataD, org, spa, mat, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())


    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToUnsignedShort()


    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    print 'tmp   : ', tmp.shape
    print 'dataD : ', dataD.shape
    tmp[:] = dataD[:] 

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
    def ApplyVED(self, image, sigmaMin, sigmaMax, numberOfSigmaSteps, alpha,
                 beta, gamma, c, timestep, epsilon, wstrength, sensitivity,
                 numberOfIterations, numberOfDiffusionSubIterations):

        self._helper.debug("Starting execution of VED...")

        cast = slicer.vtkImageCast()
        cast.SetInput(image)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()
        image = cast.GetOutput()

        v = slicer.vtkvmtkVesselEnhancingDiffusionImageFilter()
        v.SetInput(image)
        v.SetSigmaMin(sigmaMin)
        v.SetSigmaMax(sigmaMax)
        v.SetNumberOfSigmaSteps(numberOfSigmaSteps)
        v.SetAlpha(alpha)
        v.SetBeta(beta)
        v.SetGamma(gamma)
        v.SetC(c)
        v.SetTimeStep(timestep)
        v.SetEpsilon(epsilon)
        v.SetWStrength(wstrength)
        v.SetSensitivity(sensitivity)
        v.SetNumberOfIterations(numberOfIterations)
        v.SetNumberOfDiffusionSubIterations(numberOfDiffusionSubIterations)
        v.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(v.GetOutput())
        outVolumeData.Update()
        outVolumeData.GetPointData().GetScalars().Modified()

        return outVolumeData
    def ApplyFrangiVesselness(self, image, sigmaMin, sigmaMax,
                              numberOfSigmaSteps, alpha, beta, gamma):

        self._helper.debug("Starting execution of Frangi...")

        cast = slicer.vtkImageCast()
        cast.SetInput(image)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()
        image = cast.GetOutput()

        v = slicer.vtkvmtkVesselnessMeasureImageFilter()
        v.SetInput(image)
        v.SetSigmaMin(sigmaMin)
        v.SetSigmaMax(sigmaMax)
        v.SetNumberOfSigmaSteps(numberOfSigmaSteps)
        v.SetAlpha(alpha)
        v.SetBeta(beta)
        v.SetGamma(gamma)
        v.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(v.GetOutput())
        outVolumeData.Update()
        outVolumeData.GetPointData().GetScalars().Modified()

        return outVolumeData
Пример #16
0
def dispW1(dataD, G, b, org, spa, mat, mu, shpV, nvol):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLDiffusionWeightedVolumeNode()
    r11 = slicer.vtkMRMLDiffusionWeightedVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())


    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToShort()


    r1.SetAndObserveImageData(imgD)
    r1.SetNumberOfGradients(shpV[3])
    r1.SetIJKToRASMatrix(mat)
    r1.SetMeasurementFrameMatrix(mu)
    r1.SetDiffusionGradients(G)
    r1.SetBValues(b)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    tmp[...] = dataD[..., nvol] 

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
Пример #17
0
def dispW0(dataD, inputVolume, shpV, nvol):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLDiffusionWeightedVolumeNode()
    r11 = slicer.vtkMRMLDiffusionWeightedVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())

    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToShort()

    org = inputVolume.GetOrigin()
    spa = inputVolume.GetSpacing()

    mat = slicer.vtkMatrix4x4()
    inputVolume.GetIJKToRASMatrix(mat)

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    print 'tmp   : ', tmp.shape
    print 'dataD : ', dataD.shape
    tmp[...] = dataD[..., nvol]

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
Пример #18
0
def dispV0(dataD, inputVolume, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())

    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToShort()

    org = inputVolume.GetOrigin()
    spa = inputVolume.GetSpacing()

    mat = slicer.vtkMatrix4x4()
    inputVolume.GetIJKToRASMatrix(mat)

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    tmp[:] = dataD[:]

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
Пример #19
0
    def ExecuteCurves(self, origInVolumeNode, inVolumeNode, numberOfIterations,
                      propagationScaling, curvatureScaling, advectionScaling,
                      calculateFeatureImage):

        self._helper.debug("Starting execution of Curves..")

        if not inVolumeNode or not origInVolumeNode:
            slicer.Application.ErrorMessage(
                "Not enough information!!! Aborting Geodesic..\n")
            return
        else:

            cast = slicer.vtkImageCast()
            cast.SetInput(origInVolumeNode.GetImageData())
            cast.SetOutputScalarTypeToFloat()
            cast.Update()

            origImage = cast.GetOutput()
            image = inVolumeNode.GetImageData()

            levelSets = slicer.vtkvmtkCurvesLevelSetImageFilter()
            self._helper.debug("FeatureImageCalc: " +
                               str(calculateFeatureImage))
            if calculateFeatureImage == 1:
                levelSets.SetFeatureImage(
                    self.BuildGradientBasedFeatureImage(origImage))
            else:
                levelSets.SetFeatureImage(origImage)
            levelSets.SetDerivativeSigma(self.FeatureDerivativeSigma)
            levelSets.SetAutoGenerateSpeedAdvection(1)
            levelSets.SetPropagationScaling(propagationScaling)
            levelSets.SetCurvatureScaling(curvatureScaling)
            levelSets.SetAdvectionScaling(advectionScaling)

            levelSets.SetInput(image)
            levelSets.SetNumberOfIterations(numberOfIterations)
            levelSets.SetIsoSurfaceValue(self.IsoSurfaceValue)
            levelSets.SetMaximumRMSError(self.MaximumRMSError)
            levelSets.SetInterpolateSurfaceLocation(1)
            levelSets.SetUseImageSpacing(1)
            levelSets.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(levelSets.GetOutput())
            outVolumeData.Update()

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode, 0.0)

            return outputContainer
    def ExecuteCurves(self,origInVolumeNode,inVolumeNode,numberOfIterations,propagationScaling,curvatureScaling,advectionScaling,calculateFeatureImage):

        self._helper.debug("Starting execution of Curves..")

        if not inVolumeNode or not origInVolumeNode:
            slicer.Application.ErrorMessage("Not enough information!!! Aborting Geodesic..\n")
            return
        else:

            cast = slicer.vtkImageCast()
            cast.SetInput(origInVolumeNode.GetImageData())
            cast.SetOutputScalarTypeToFloat()
            cast.Update()

            origImage = cast.GetOutput()
            image = inVolumeNode.GetImageData()

            levelSets = slicer.vtkvmtkCurvesLevelSetImageFilter()
            self._helper.debug("FeatureImageCalc: " + str(calculateFeatureImage))
            if calculateFeatureImage==1:
                levelSets.SetFeatureImage(self.BuildGradientBasedFeatureImage(origImage))
            else:
                levelSets.SetFeatureImage(origImage)
            levelSets.SetDerivativeSigma(self.FeatureDerivativeSigma)
            levelSets.SetAutoGenerateSpeedAdvection(1)
            levelSets.SetPropagationScaling(propagationScaling)
            levelSets.SetCurvatureScaling(curvatureScaling)
            levelSets.SetAdvectionScaling(advectionScaling)


            levelSets.SetInput(image)
            levelSets.SetNumberOfIterations(numberOfIterations)
            levelSets.SetIsoSurfaceValue(self.IsoSurfaceValue)
            levelSets.SetMaximumRMSError(self.MaximumRMSError)
            levelSets.SetInterpolateSurfaceLocation(1)
            levelSets.SetUseImageSpacing(1)
            levelSets.Update()


            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(levelSets.GetOutput())
            outVolumeData.Update()

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0)

            return outputContainer
    def Execute(self):
        SlicerVMTKAdvancedPageSkeleton.Execute(self)

        inVolume =  self._parentClass._inVolumeSelector.GetSelected()
        outVolume = self._parentClass._outVolumeSelector.GetSelected()

        if not inVolume or not outVolume or not self.CheckForVmtkLibrary():
            slicer.Application.ErrorMessage("No input volume or no output volume found. Aborting Frangi..\n")
            return
        else:

            sigmaMin = float(self._sigmaMin.GetWidget().GetValue())
            sigmaMax = float(self._sigmaMax.GetWidget().GetValue())
            numberOfSigmaSteps =  int(self._numberOfSigmaSteps.GetValue())
            alpha = float(self._alpha.GetWidget().GetValue())
            beta = float(self._beta.GetWidget().GetValue())
            gamma = float(self._gamma.GetWidget().GetValue())

            imageData = slicer.vtkImageData()
            imageData.DeepCopy(inVolume.GetImageData())
            imageData.Update()
            if (self._sigmaUnit.GetWidget().GetWidget(0).GetSelectedState()==1):
                self._parentClass.debug("use mm!")
                imageData.SetSpacing(inVolume.GetSpacing()[0], inVolume.GetSpacing()[1], inVolume.GetSpacing()[2])


            outVolumeImage = self._parentClass._logic.ApplyFrangiVesselness(imageData,sigmaMin,sigmaMax,numberOfSigmaSteps,alpha,beta,gamma)
            outVolumeImage.SetSpacing(1,1,1)

            matrix = slicer.vtkMatrix4x4()
            inVolume.GetIJKToRASMatrix(matrix)
            outVolume.SetAndObserveImageData(outVolumeImage)
            outVolume.SetIJKToRASMatrix(matrix)

            displayNode = inVolume.GetDisplayNode()
            if displayNode != None:
              newDisplayNode = displayNode.NewInstance()
              newDisplayNode.Copy(displayNode)
              slicer.MRMLScene.AddNodeNoNotify(newDisplayNode);
              outVolume.SetAndObserveDisplayNodeID(newDisplayNode.GetID())
              newDisplayNode.AutoWindowLevelOff()
              newDisplayNode.AutoWindowLevelOn() # renew auto windowlevel

            appLogic = slicer.ApplicationLogic
            selectionNode = appLogic.GetSelectionNode()
            if inVolume.GetLabelMap():
              outVolume.SetLabelMap(1)
              selectionNode.SetReferenceActiveLabelVolumeID(outVolume.GetID())
            else:
              selectionNode.SetReferenceActiveVolumeID(outVolume.GetID())
            appLogic.PropagateVolumeSelection()
    def ExecuteThreshold(self,inVolumeNode,lowerThreshold,higherThreshold):


        self._helper.debug("Starting execution of Threshold...")


        if not inVolumeNode:
            slicer.Application.ErrorMessage("No input volume found. Aborting Threshold..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            scalarRange = image.GetScalarRange()


            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)

            threshold.ThresholdBetween(lowerThreshold,higherThreshold)

            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetInValue(-1.0)
            threshold.SetOutValue(1.0)
            threshold.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(threshold.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0)

            self._helper.debug("Threshold done...")
    
            return outputContainer
Пример #23
0
    def ExecuteIsosurface(self, inVolumeNode, value):

        self._helper.debug("Starting execution of Isosurface...")

        if not inVolumeNode:
            slicer.Application.ErrorMessage(
                "No input volume found. Aborting Isosurface..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            imageMathematics = slicer.vtkImageMathematics()
            imageMathematics.SetInput(image)
            imageMathematics.SetConstantK(-1.0)
            imageMathematics.SetOperationToMultiplyByK()
            imageMathematics.Update()

            subtract = slicer.vtkImageMathematics()
            subtract.SetInput(imageMathematics.GetOutput())
            subtract.SetOperationToAddConstant()
            subtract.SetConstantC(value)
            subtract.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())

            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode, 10)

            self._helper.debug("Isosurface done...")

            return outputContainer
Пример #24
0
    def ExecuteThreshold(self, inVolumeNode, lowerThreshold, higherThreshold):

        self._helper.debug("Starting execution of Threshold...")

        if not inVolumeNode:
            slicer.Application.ErrorMessage(
                "No input volume found. Aborting Threshold..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            scalarRange = image.GetScalarRange()

            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)

            threshold.ThresholdBetween(lowerThreshold, higherThreshold)

            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetInValue(-1.0)
            threshold.SetOutValue(1.0)
            threshold.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(threshold.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode, 0.0)

            self._helper.debug("Threshold done...")

            return outputContainer
    def ExecuteIsosurface(self,inVolumeNode,value):


        self._helper.debug("Starting execution of Isosurface...")

        if not inVolumeNode:
            slicer.Application.ErrorMessage("No input volume found. Aborting Isosurface..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            imageMathematics = slicer.vtkImageMathematics()
            imageMathematics.SetInput(image)
            imageMathematics.SetConstantK(-1.0)
            imageMathematics.SetOperationToMultiplyByK()
            imageMathematics.Update()

            subtract = slicer.vtkImageMathematics()
            subtract.SetInput(imageMathematics.GetOutput())
            subtract.SetOperationToAddConstant()
            subtract.SetConstantC(value)
            subtract.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())

            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,10)

            self._helper.debug("Isosurface done...")

            return outputContainer
    def BuildSimpleLabelMap(self, image, inValue, outValue):

        threshold = slicer.vtkImageThreshold()
        threshold.SetInput(image)
        threshold.ThresholdByLower(0)
        threshold.ReplaceInOn()
        threshold.ReplaceOutOn()
        threshold.SetOutValue(outValue)
        threshold.SetInValue(inValue)
        threshold.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(threshold.GetOutput())
        outVolumeData.Update()

        return outVolumeData
    def BuildSimpleLabelMap(self,image,inValue,outValue):

        threshold = slicer.vtkImageThreshold()
        threshold.SetInput(image)
        threshold.ThresholdByLower(0)
        threshold.ReplaceInOn()
        threshold.ReplaceOutOn()
        threshold.SetOutValue(outValue)
        threshold.SetInValue(inValue)
        threshold.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(threshold.GetOutput())
        outVolumeData.Update()

        return outVolumeData
    def BuildGradientBasedFeatureImage(self, imageData):

        cast = slicer.vtkImageCast()
        cast.SetInput(imageData)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()

        if (self._derivativeSigma > 0.0):
            gradientMagnitude = slicer.vtkvmtkGradientMagnitudeRecursiveGaussianImageFilter(
            )
            gradientMagnitude.SetInput(cast.GetOutput())
            gradientMagnitude.SetSigma(self._derivativeSigma)
            gradientMagnitude.SetNormalizeAcrossScale(0)
            gradientMagnitude.Update()
        else:
            gradientMagnitude = slicer.vtkvmtkGradientMagnitudeImageFilter()
            gradientMagnitude.SetInput(cast.GetOutput())
            gradientMagnitude.Update()

        featureImage = None
        if self._sigmoidRemapping == 1:
            scalarRange = gradientMagnitude.GetOutput().GetPointData(
            ).GetScalars().GetRange()
            inputMinimum = scalarRange[0]
            inputMaximum = scalarRange[1]
            alpha = -(inputMaximum - inputMinimum) / 6.0
            beta = (inputMaximum + inputMinimum) / 2.0
            sigmoid = slicer.vtkvmtkSigmoidImageFilter()
            sigmoid.SetInput(gradientMagnitude.GetOutput())
            sigmoid.SetAlpha(alpha)
            sigmoid.SetBeta(beta)
            sigmoid.SetOutputMinimum(0.0)
            sigmoid.SetOutputMaximum(1.0)
            sigmoid.Update()
            featureImage = sigmoid.GetOutput()
        else:
            boundedReciprocal = slicer.vtkvmtkBoundedReciprocalImageFilter()
            boundedReciprocal.SetInput(gradientMagnitude.GetOutput())
            boundedReciprocal.Update()
            featureImage = boundedReciprocal.GetOutput()

        featureImageOutput = slicer.vtkImageData()
        featureImageOutput.DeepCopy(featureImage)
        featureImageOutput.Update()

        return featureImageOutput
    def ExecuteGAC(self, origImage, segmentationImage, numberOfIterations,
                   propagationScaling, curvatureScaling, advectionScaling,
                   method):

        self._parentClass.GetHelper().debug("Starting GAC..")

        calculateFeatureImage = 1

        cast = slicer.vtkImageCast()
        cast.SetInput(origImage)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()

        if method == 'curves':
            levelSets = slicer.vtkvmtkCurvesLevelSetImageFilter()
        else:
            levelSets = slicer.vtkvmtkGeodesicActiveContourLevelSetImageFilter(
            )

        if calculateFeatureImage == 1:
            levelSets.SetFeatureImage(
                self.BuildGradientBasedFeatureImage(origImage))
        else:
            levelSets.SetFeatureImage(origImage)
        levelSets.SetDerivativeSigma(self._featureDerivativeSigma)
        levelSets.SetAutoGenerateSpeedAdvection(1)
        levelSets.SetPropagationScaling(propagationScaling)
        levelSets.SetCurvatureScaling(curvatureScaling)
        levelSets.SetAdvectionScaling(advectionScaling)
        levelSets.SetInput(segmentationImage)
        levelSets.SetNumberOfIterations(numberOfIterations)
        levelSets.SetIsoSurfaceValue(self._isoSurfaceValue)
        levelSets.SetMaximumRMSError(self._maximumRMSError)
        levelSets.SetInterpolateSurfaceLocation(1)
        levelSets.SetUseImageSpacing(1)
        levelSets.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(levelSets.GetOutput())
        outVolumeData.Update()

        self._parentClass.GetHelper().debug("End of GAC..")

        return outVolumeData
    def BuildGradientBasedFeatureImage(self,imageData):

        cast = slicer.vtkImageCast()
        cast.SetInput(imageData)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()

        if (self.DerivativeSigma > 0.0):
            gradientMagnitude = slicer.vtkvmtkGradientMagnitudeRecursiveGaussianImageFilter()
            gradientMagnitude.SetInput(cast.GetOutput())
            gradientMagnitude.SetSigma(self.DerivativeSigma)
            gradientMagnitude.SetNormalizeAcrossScale(0)
            gradientMagnitude.Update()
        else:
            gradientMagnitude = slicer.vtkvmtkGradientMagnitudeImageFilter()
            gradientMagnitude.SetInput(cast.GetOutput())
            gradientMagnitude.Update()

        featureImage = None
        if self.SigmoidRemapping==1:
            scalarRange = gradientMagnitude.GetOutput().GetPointData().GetScalars().GetRange()
            inputMinimum = scalarRange[0]
            inputMaximum = scalarRange[1]
            alpha = - (inputMaximum - inputMinimum) / 6.0
            beta = (inputMaximum + inputMinimum) / 2.0
            sigmoid = slicer.vtkvmtkSigmoidImageFilter()
            sigmoid.SetInput(gradientMagnitude.GetOutput())
            sigmoid.SetAlpha(alpha)
            sigmoid.SetBeta(beta)
            sigmoid.SetOutputMinimum(0.0)
            sigmoid.SetOutputMaximum(1.0)
            sigmoid.Update()
            featureImage = sigmoid.GetOutput()
        else:
            boundedReciprocal = slicer.vtkvmtkBoundedReciprocalImageFilter()
            boundedReciprocal.SetInput(gradientMagnitude.GetOutput())
            boundedReciprocal.Update()
            featureImage = boundedReciprocal.GetOutput()

        featureImageOutput = slicer.vtkImageData()
        featureImageOutput.DeepCopy(featureImage)
        featureImageOutput.Update()

        return featureImageOutput
Пример #31
0
def dispS(dataD, inputVolume, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    r11.ScalarVisibilityOn()
    r11.SetScalarRange(dataD.min(), dataD.max())
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())

    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetNumberOfScalarComponents(1)
    imgD.SetScalarTypeToDouble()
    imgD.AllocateScalars()

    org = inputVolume.GetOrigin()
    spa = inputVolume.GetSpacing()

    mat = slicer.vtkMatrix4x4()
    inputVolume.GetIJKToRASMatrix(mat)

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().GetPointData().GetScalars().ToArray()
    print 'tmp   : ', tmp.shape
    print 'dataD : ', dataD.shape
    dataD = reshape(dataD, (shpV[0]*shpV[1]*shpV[2], 1))
    tmp[:] = dataD[:]

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
    def ExecuteGAC(self,origImage,segmentationImage,numberOfIterations,propagationScaling,curvatureScaling,advectionScaling,method):

        self._parentClass.GetHelper().debug("Starting GAC..")

        calculateFeatureImage = 1

        cast = slicer.vtkImageCast()
        cast.SetInput(origImage)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()

        if method=='curves':
            levelSets = slicer.vtkvmtkCurvesLevelSetImageFilter()
        else:
            levelSets = slicer.vtkvmtkGeodesicActiveContourLevelSetImageFilter()

        if calculateFeatureImage==1:
            levelSets.SetFeatureImage(self.BuildGradientBasedFeatureImage(origImage))
        else:
            levelSets.SetFeatureImage(origImage)
        levelSets.SetDerivativeSigma(self._featureDerivativeSigma)
        levelSets.SetAutoGenerateSpeedAdvection(1)
        levelSets.SetPropagationScaling(propagationScaling)
        levelSets.SetCurvatureScaling(curvatureScaling)
        levelSets.SetAdvectionScaling(advectionScaling)
        levelSets.SetInput(segmentationImage)
        levelSets.SetNumberOfIterations(numberOfIterations)
        levelSets.SetIsoSurfaceValue(self._isoSurfaceValue)
        levelSets.SetMaximumRMSError(self._maximumRMSError)
        levelSets.SetInterpolateSurfaceLocation(1)
        levelSets.SetUseImageSpacing(1)
        levelSets.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(levelSets.GetOutput())
        outVolumeData.Update()

        self._parentClass.GetHelper().debug("End of GAC..")

        return outVolumeData
Пример #33
0
def dispS(dataD, inputVolume, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    r11.ScalarVisibilityOn()
    r11.SetScalarRange(dataD.min(), dataD.max())
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())

    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetNumberOfScalarComponents(1)
    imgD.SetScalarTypeToDouble()
    imgD.AllocateScalars()

    org = inputVolume.GetOrigin()
    spa = inputVolume.GetSpacing()

    mat = slicer.vtkMatrix4x4()
    inputVolume.GetIJKToRASMatrix(mat)

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().GetPointData().GetScalars().ToArray()
    print 'tmp   : ', tmp.shape
    print 'dataD : ', dataD.shape
    dataD = reshape(dataD, (shpV[0] * shpV[1] * shpV[2], 1))
    tmp[:] = dataD[:]

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
Пример #34
0
    def AddImages(self, imageData1, imageData2):
        ''' 
            Adds two images
            
            imageData1
                vtkImageData
            imageData2
                vtkImageData
            
            Returns
                vtkImageData after addition
        '''
        add = slicer.vtkImageMathematics()
        add.SetInput1(imageData1)
        add.SetInput2(imageData2)
        add.SetOperationToAdd()
        add.Update()

        output = slicer.vtkImageData()
        output.DeepCopy(add.GetOutput())

        return output
Пример #35
0
def dispW0(dataD, inputVolume, shpV, nvol):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLDiffusionWeightedVolumeNode()
    r11 = slicer.vtkMRMLDiffusionWeightedVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())


    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToShort()

    org = inputVolume.GetOrigin()
    spa = inputVolume.GetSpacing()

    mat = slicer.vtkMatrix4x4()
    inputVolume.GetIJKToRASMatrix(mat)

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    print 'tmp   : ', tmp.shape
    print 'dataD : ', dataD.shape
    tmp[...] = dataD[..., nvol] 

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
Пример #36
0
    def MultiplyImage(self, imageData, factor):
        ''' 
            Multiply an image
            This includes automatic Re-Cast to Float
            
            imageData
                vtkImageData
            dividend
                the number to divide with, will be casted to float
            
            Returns
                vtkImageData after multiplication
        '''
        imageData.DeepCopy(self.ReCastImage(imageData, "Float"))
        mul = slicer.vtkImageMathematics()
        mul.SetInput(imageData)
        mul.SetOperationToMultiplyByK()
        mul.SetConstantK(factor)
        mul.Update()

        output = slicer.vtkImageData()
        output.DeepCopy(mul.GetOutput())

        return output
    def ExecuteFM(self, image, lowerThreshold, higherThreshold, sourceSeedIds,
                  targetSeedIds, sideBranches):

        self._parentClass.GetHelper().debug("Starting FM..")

        cast = slicer.vtkImageCast()
        cast.SetInput(image)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()
        image = cast.GetOutput()

        scalarRange = image.GetScalarRange()

        imageDimensions = image.GetDimensions()
        maxImageDimensions = max(imageDimensions)

        threshold = slicer.vtkImageThreshold()
        threshold.SetInput(image)
        threshold.ThresholdBetween(lowerThreshold, higherThreshold)
        threshold.ReplaceInOff()
        threshold.ReplaceOutOn()
        threshold.SetOutValue(scalarRange[0] - scalarRange[1])
        threshold.Update()

        thresholdedImage = threshold.GetOutput()

        scalarRange = thresholdedImage.GetScalarRange()

        shiftScale = slicer.vtkImageShiftScale()
        shiftScale.SetInput(thresholdedImage)
        shiftScale.SetShift(-scalarRange[0])
        shiftScale.SetScale(1 / (scalarRange[1] - scalarRange[0]))
        shiftScale.Update()

        speedImage = shiftScale.GetOutput()

        if sideBranches:
            # ignore sidebranches, use colliding fronts
            self._parentClass.GetHelper().debug("COLLIDINGFRONTS")
            fastMarching = slicer.vtkvmtkCollidingFrontsImageFilter()
            fastMarching.SetInput(speedImage)
            fastMarching.SetSeeds1(sourceSeedIds)
            fastMarching.SetSeeds2(targetSeedIds)
            fastMarching.ApplyConnectivityOn()
            fastMarching.StopOnTargetsOn()
            fastMarching.Update()

            subtract = slicer.vtkImageMathematics()
            subtract.SetInput(fastMarching.GetOutput())
            subtract.SetOperationToAddConstant()
            subtract.SetConstantC(-10 * fastMarching.GetNegativeEpsilon())
            subtract.Update()

        else:
            fastMarching = slicer.vtkvmtkFastMarchingUpwindGradientImageFilter(
            )
            fastMarching.SetInput(speedImage)
            fastMarching.SetSeeds(sourceSeedIds)
            fastMarching.GenerateGradientImageOn()
            fastMarching.SetTargetOffset(0.0)
            fastMarching.SetTargets(targetSeedIds)
            if targetSeedIds.GetNumberOfIds() > 0:
                fastMarching.SetTargetReachedModeToOneTarget()
            else:
                fastMarching.SetTargetReachedModeToNoTargets()
            fastMarching.Update()

            if targetSeedIds.GetNumberOfIds() > 0:
                subtract = slicer.vtkImageMathematics()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.SetOperationToAddConstant()
                subtract.SetConstantC(-fastMarching.GetTargetValue())
                subtract.Update()

            else:
                #self._parentClass.GetHelper().debug("No target mode "+str(fastMarching.GetTargetValue()))
                subtract = slicer.vtkImageThreshold()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.ThresholdByLower(2000)  # TODO find robuste value
                subtract.ReplaceInOff()
                subtract.ReplaceOutOn()
                subtract.SetOutValue(-1)
                subtract.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(subtract.GetOutput())
        outVolumeData.Update()

        self._parentClass.GetHelper().debug("End of FM..")

        return outVolumeData
Пример #38
0
    def SetAndMergeEvolVolume(self, resultContainer):

        scene = self._mainGUIClass.GetLogic().GetMRMLScene()

        volumeNode = resultContainer.GetNode()
        threshold = resultContainer.GetThreshold()

        if self._mainGUIClass._outEvolVolume == None:

            # no node so far

            self._mainGUIClass._outEvolVolume = slicer.vtkMRMLScalarVolumeNode(
            )
            self._mainGUIClass._outEvolVolume.SetName(
                "VMTK Level-Set Evolution Output Volume")
            self._mainGUIClass._outEvolVolume.SetScene(scene)
            self._mainGUIClass._outEvolVolume.SetAndObserveImageData(
                slicer.vtkImageData())
            scene.AddNode(self._mainGUIClass._outEvolVolume)

            self._mainGUIClass._outEvolVolumeLast = slicer.vtkMRMLScalarVolumeNode(
            )
            self._mainGUIClass._outEvolVolumeLast.SetName(
                "VMTK Level-Set Evolution Output Volume (Last Step)")
            self._mainGUIClass._outEvolVolumeLast.SetScene(scene)
            self._mainGUIClass._outEvolVolumeLast.SetAndObserveImageData(
                slicer.vtkImageData())
            scene.AddNode(self._mainGUIClass._outEvolVolumeLast)

        matrix = slicer.vtkMatrix4x4()

        # copy current outEvolVolume to outEvolVolumeLast
        self._mainGUIClass._outEvolVolumeLast.SetAndObserveImageData(
            self._mainGUIClass._outEvolVolume.GetImageData())
        self._mainGUIClass._outEvolVolume.GetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolumeLast.SetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolumeLast.SetModifiedSinceRead(1)

        volumeNodeData = volumeNode.GetImageData()

        # merge the oldVolume with volumeNode if oldVolume has already content
        if self._mainGUIClass._outEvolVolume.GetImageData().GetPointData(
        ).GetScalars():
            # evolVolumeLast has already content
            minFilter = slicer.vtkImageMathematics()
            minFilter.SetOperationToMin()
            minFilter.SetInput1(
                self._mainGUIClass._outEvolVolume.GetImageData())  #the old one
            minFilter.SetInput2(volumeNode.GetImageData())  #the new one
            minFilter.Update()
            volumeNodeData.DeepCopy(minFilter.GetOutput())

        # copy new volume to outEvolVolume
        volumeNode.GetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolume.SetAndObserveImageData(
            volumeNodeData)
        self._mainGUIClass._outEvolVolume.SetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolume.SetModifiedSinceRead(1)

        outputContainer = SlicerVMTKLevelSetContainer(
            self._mainGUIClass._outEvolVolume, threshold)

        return outputContainer
    def ExecuteCollidingFronts(self,inVolumeNode,lowerThreshold,higherThreshold,sourceSeedsNode,targetSeedsNode):

        self._helper.debug("Starting execution of Colliding Fronts..")

        if not inVolumeNode or not sourceSeedsNode or not targetSeedsNode:
            self._helper.debug(inVolumeNode)
            self._helper.debug(lowerThreshold)         
            self._helper.debug(higherThreshold)  
            self._helper.debug(sourceSeedsNode)  
            self._helper.debug(targetSeedsNode)         
            slicer.Application.ErrorMessage("Not enough information!!! Aborting Colliding Fronts..\n")
            return
        else:

            sourceSeedIds = slicer.vtkIdList()
            targetSeedIds = slicer.vtkIdList()

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            rasPt = sourceSeedsNode.GetNthFiducialXYZ(0)
            self._helper.debug(rasPt)
            ijkPt = self._helper.ConvertRAS2IJK(rasPt)
            self._helper.debug(ijkPt)
            sourceSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            rasPt = targetSeedsNode.GetNthFiducialXYZ(0)
            self._helper.debug(rasPt)
            ijkPt = self._helper.ConvertRAS2IJK(rasPt)
            self._helper.debug(ijkPt)
            targetSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            scalarRange = image.GetScalarRange()
            self._helper.debug("CF: after converting seeds")

            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)
            threshold.ThresholdBetween(lowerThreshold, higherThreshold)
            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetOutValue(scalarRange[0] - scalarRange[1])
            threshold.Update()

            self._helper.debug("CF: after thresholding")
    
            scalarRange = threshold.GetOutput().GetScalarRange()

            thresholdedImage = threshold.GetOutput()

            shiftScale = slicer.vtkImageShiftScale()
            shiftScale.SetInput(thresholdedImage)
            shiftScale.SetShift(-scalarRange[0])
            shiftScale.SetScale(1/(scalarRange[1]-scalarRange[0]))
            shiftScale.Update()
            
            speedImage = shiftScale.GetOutput()

            self._helper.debug("CF: after shiftScale")            

            collidingFronts = slicer.vtkvmtkCollidingFrontsImageFilter()
            collidingFronts.SetInput(speedImage)
            collidingFronts.SetSeeds1(sourceSeedIds)
            collidingFronts.SetSeeds2(targetSeedIds)
            collidingFronts.ApplyConnectivityOn()
            collidingFronts.StopOnTargetsOn()
            collidingFronts.Update()

            self._helper.debug("CF: after CF")

            subtract = slicer.vtkImageMathematics()
            subtract.SetInput(collidingFronts.GetOutput())
            subtract.SetOperationToAddConstant()
            subtract.SetConstantC(-10.0 * collidingFronts.GetNegativeEpsilon())
            subtract.Update()

            self._helper.debug("CF: after substract")

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,collidingFronts.GetNegativeEpsilon())

            self._helper.debug("Colliding Fronts done...")

            return outputContainer
    def ExecuteFastMarching(self,inVolumeNode,lowerThreshold,higherThreshold,sourceSeedsNode,targetSeedsNode):

        self._helper.debug("Starting execution of Fast Marching...")

        if not inVolumeNode or not sourceSeedsNode:
            self._helper.debug(inVolumeNode)
            self._helper.debug(lowerThreshold)         
            self._helper.debug(higherThreshold)  
            self._helper.debug(sourceSeedsNode)  
            self._helper.debug(targetSeedsNode)         
            slicer.Application.ErrorMessage("Not enough information!!! Aborting Fast Marching..\n")
            return
        else:

            sourceSeedIds = slicer.vtkIdList()
            targetSeedIds = slicer.vtkIdList()

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            for i in range(sourceSeedsNode.GetNumberOfFiducials()):
                rasPt = sourceSeedsNode.GetNthFiducialXYZ(i)
                ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                sourceSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            if targetSeedsNode:
                for i in range(targetSeedsNode.GetNumberOfFiducials()):
                    rasPt = targetSeedsNode.GetNthFiducialXYZ(i)
                    ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                    targetSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            scalarRange = image.GetScalarRange()

            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)
            threshold.ThresholdBetween(lowerThreshold,higherThreshold)
            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetOutValue(scalarRange[0] - scalarRange[1])
            threshold.Update()

            scalarRange = threshold.GetOutput().GetScalarRange()

            thresholdedImage = threshold.GetOutput()

            shiftScale = slicer.vtkImageShiftScale()
            shiftScale.SetInput(thresholdedImage)
            shiftScale.SetShift(-scalarRange[0])
            shiftScale.SetScale(1/(scalarRange[1]-scalarRange[0]))
            shiftScale.Update()

            speedImage = shiftScale.GetOutput()

            fastMarching = slicer.vtkvmtkFastMarchingUpwindGradientImageFilter()
            fastMarching.SetInput(speedImage)
            fastMarching.SetSeeds(sourceSeedIds)
            fastMarching.GenerateGradientImageOff()
            fastMarching.SetTargetOffset(100.0)
            fastMarching.SetTargets(targetSeedIds)
            if targetSeedIds.GetNumberOfIds() > 0:
                fastMarching.SetTargetReachedModeToOneTarget()
            else:
                fastMarching.SetTargetReachedModeToNoTargets()
            fastMarching.Update()

            if targetSeedIds.GetNumberOfIds() > 0:
                subtract = slicer.vtkImageMathematics()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.SetOperationToAddConstant()
                subtract.SetConstantC(-fastMarching.GetTargetValue())
                subtract.Update()
            else:
                subtract = slicer.vtkImageThreshold()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.ThresholdByLower(1000)#better value soon
                subtract.ReplaceInOff()
                subtract.ReplaceOutOn()
                subtract.SetOutValue(-1)
                subtract.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0)

            self._helper.debug("Fast Marching done...")

            return outputContainer
Пример #41
0
    def ReCastImage(self, imageData, castString):
        ''' 
            Re-Cast an image to a different type
            
            imageData
                vtkImageData
            castString
                New type as a String, possible values are
                "Char"
                "Unsigned Char"
                "Double"
                "Float"
                "Int"
                "Unsigned Int"
                "Long"
                "Unsigned Long"
                "Short"
                "Unsigned Short"
                other values will result in "Short"
                
            Returns
                vtkImageData after Re-Cast
        '''

        # get the current type
        currentType = imageData.GetScalarType()

        cast = slicer.vtkImageCast()
        cast.SetInput(imageData)
        cast.ClampOverflowOn()

        needToCast = False

        if (castString == "Char" and (currentType != 2 or currentType != 15)):
            cast.SetOutputScalarTypeToChar()
            needToCast = True
        elif (castString == "Unsigned Char" and currentType != 3):
            cast.SetOutputScalarTypeToUnsignedChar()
            needToCast = True
        elif (castString == "Double" and currentType != 11):
            cast.SetOutputScalarTypeToUnsignedChar()
            needToCast = True
        elif (castString == "Float" and currentType != 10):
            cast.SetOutputScalarTypeToFloat()
            needToCast = True
        elif (castString == "Int" and currentType != 6):
            cast.SetOutputScalarTypeToInt()
            needToCast = True
        elif (castString == "Unsigned Int" and currentType != 7):
            cast.SetOutputScalarTypeToUnsignedInt()
            needToCast = True
        elif (castString == "Long" and currentType != 8):
            cast.SetOutputScalarTypeToLong()
            needToCast = True
        elif (castString == "Unsigned Long" and currentType != 9):
            cast.SetOutputScalarTypeToUnsignedLong()
            needToCast = True
        elif (castString == "Short" and currentType != 4):
            cast.SetOutputScalarTypeToShort()
            needToCast = True
        elif (castString == "Unsigned Short" and currentType != 5):
            cast.SetOutputScalarTypeToUnsignedShort()
            needToCast = True
        else:
            cast.SetOutputScalarTypeToShort()
            needToCast = True

        # check if we really need to cast,
        # else exit here and return the input imageData
        if not needToCast:
            return imageData

        cast.Update()

        output = slicer.vtkImageData()
        output.DeepCopy(cast.GetOutput())

        return output
Пример #42
0
    def ExecuteCollidingFronts(self, inVolumeNode, lowerThreshold,
                               higherThreshold, sourceSeedsNode,
                               targetSeedsNode):

        self._helper.debug("Starting execution of Colliding Fronts..")

        if not inVolumeNode or not sourceSeedsNode or not targetSeedsNode:
            self._helper.debug(inVolumeNode)
            self._helper.debug(lowerThreshold)
            self._helper.debug(higherThreshold)
            self._helper.debug(sourceSeedsNode)
            self._helper.debug(targetSeedsNode)
            slicer.Application.ErrorMessage(
                "Not enough information!!! Aborting Colliding Fronts..\n")
            return
        else:

            sourceSeedIds = slicer.vtkIdList()
            targetSeedIds = slicer.vtkIdList()

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            rasPt = sourceSeedsNode.GetNthFiducialXYZ(0)
            self._helper.debug(rasPt)
            ijkPt = self._helper.ConvertRAS2IJK(rasPt)
            self._helper.debug(ijkPt)
            sourceSeedIds.InsertNextId(
                image.ComputePointId(int(ijkPt[0]), int(ijkPt[1]),
                                     int(ijkPt[2])))

            rasPt = targetSeedsNode.GetNthFiducialXYZ(0)
            self._helper.debug(rasPt)
            ijkPt = self._helper.ConvertRAS2IJK(rasPt)
            self._helper.debug(ijkPt)
            targetSeedIds.InsertNextId(
                image.ComputePointId(int(ijkPt[0]), int(ijkPt[1]),
                                     int(ijkPt[2])))

            scalarRange = image.GetScalarRange()
            self._helper.debug("CF: after converting seeds")

            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)
            threshold.ThresholdBetween(lowerThreshold, higherThreshold)
            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetOutValue(scalarRange[0] - scalarRange[1])
            threshold.Update()

            self._helper.debug("CF: after thresholding")

            scalarRange = threshold.GetOutput().GetScalarRange()

            thresholdedImage = threshold.GetOutput()

            shiftScale = slicer.vtkImageShiftScale()
            shiftScale.SetInput(thresholdedImage)
            shiftScale.SetShift(-scalarRange[0])
            shiftScale.SetScale(1 / (scalarRange[1] - scalarRange[0]))
            shiftScale.Update()

            speedImage = shiftScale.GetOutput()

            self._helper.debug("CF: after shiftScale")

            collidingFronts = slicer.vtkvmtkCollidingFrontsImageFilter()
            collidingFronts.SetInput(speedImage)
            collidingFronts.SetSeeds1(sourceSeedIds)
            collidingFronts.SetSeeds2(targetSeedIds)
            collidingFronts.ApplyConnectivityOn()
            collidingFronts.StopOnTargetsOn()
            collidingFronts.Update()

            self._helper.debug("CF: after CF")

            subtract = slicer.vtkImageMathematics()
            subtract.SetInput(collidingFronts.GetOutput())
            subtract.SetOperationToAddConstant()
            subtract.SetConstantC(-10.0 * collidingFronts.GetNegativeEpsilon())
            subtract.Update()

            self._helper.debug("CF: after substract")

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(
                outVolumeNode, collidingFronts.GetNegativeEpsilon())

            self._helper.debug("Colliding Fronts done...")

            return outputContainer
    def ExecuteFM(self,image,lowerThreshold,higherThreshold,sourceSeedIds,targetSeedIds,sideBranches):

        self._parentClass.GetHelper().debug("Starting FM..")

        cast = slicer.vtkImageCast()
        cast.SetInput(image)
        cast.SetOutputScalarTypeToFloat()
        cast.Update()
        image = cast.GetOutput()

        scalarRange = image.GetScalarRange()

        imageDimensions = image.GetDimensions()
        maxImageDimensions = max(imageDimensions)

        threshold = slicer.vtkImageThreshold()
        threshold.SetInput(image)
        threshold.ThresholdBetween(lowerThreshold,higherThreshold)
        threshold.ReplaceInOff()
        threshold.ReplaceOutOn()
        threshold.SetOutValue(scalarRange[0] - scalarRange[1])
        threshold.Update()

        thresholdedImage = threshold.GetOutput()

        scalarRange = thresholdedImage.GetScalarRange()

        shiftScale = slicer.vtkImageShiftScale()
        shiftScale.SetInput(thresholdedImage)
        shiftScale.SetShift(-scalarRange[0])
        shiftScale.SetScale(1/(scalarRange[1]-scalarRange[0]))
        shiftScale.Update()

        speedImage = shiftScale.GetOutput()

        if sideBranches:
            # ignore sidebranches, use colliding fronts
            self._parentClass.GetHelper().debug("COLLIDINGFRONTS")
            fastMarching = slicer.vtkvmtkCollidingFrontsImageFilter()
            fastMarching.SetInput(speedImage)
            fastMarching.SetSeeds1(sourceSeedIds)
            fastMarching.SetSeeds2(targetSeedIds)
            fastMarching.ApplyConnectivityOn()
            fastMarching.StopOnTargetsOn()            
            fastMarching.Update()

            subtract = slicer.vtkImageMathematics()
            subtract.SetInput(fastMarching.GetOutput())
            subtract.SetOperationToAddConstant()
            subtract.SetConstantC(-10*fastMarching.GetNegativeEpsilon())
            subtract.Update()

        else:
            fastMarching = slicer.vtkvmtkFastMarchingUpwindGradientImageFilter()
            fastMarching.SetInput(speedImage)
            fastMarching.SetSeeds(sourceSeedIds)
            fastMarching.GenerateGradientImageOn()
            fastMarching.SetTargetOffset(0.0)
            fastMarching.SetTargets(targetSeedIds)
            if targetSeedIds.GetNumberOfIds() > 0:
                fastMarching.SetTargetReachedModeToOneTarget()
            else:
                fastMarching.SetTargetReachedModeToNoTargets()
            fastMarching.Update()

            if targetSeedIds.GetNumberOfIds() > 0:
                subtract = slicer.vtkImageMathematics()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.SetOperationToAddConstant()
                subtract.SetConstantC(-fastMarching.GetTargetValue())
                subtract.Update()

            else:
                #self._parentClass.GetHelper().debug("No target mode "+str(fastMarching.GetTargetValue()))
                subtract = slicer.vtkImageThreshold()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.ThresholdByLower(2000) # TODO find robuste value
                subtract.ReplaceInOff()
                subtract.ReplaceOutOn()
                subtract.SetOutValue(-1)
                subtract.Update()

        outVolumeData = slicer.vtkImageData()
        outVolumeData.DeepCopy(subtract.GetOutput())
        outVolumeData.Update()
        
        self._parentClass.GetHelper().debug("End of FM..")

        return outVolumeData
Пример #44
0
    def Apply(self):

        inputVolume = self.VolumeSelector.GetSelected()
        if not inputVolume:
            self.ErrorDialog("No input volume found")
            return

        outputVolume = self.OutVolumeSelector.GetSelected()
        if not outputVolume:
            self.ErrorDialog("No output volume found")
            return

        inputSeeds = self.FiducialsNodeSelector.GetSelected()
        if not inputSeeds:
            self.ErrorDialog("No fiducials found")
            return

        if inputSeeds.GetNumberOfFiducials() < 2:
            self.ErrorDialog("Need at least 2 fiducials in the list")
            return

        self.Status("Applying Subvolume...")

        # get the seed points (RAS)
        p0 = inputSeeds.GetNthFiducialXYZ(0)
        p1 = inputSeeds.GetNthFiducialXYZ(1)
        lower = map(min, p0, p1)

        # convert seed points to input volume IJK
        rasToIJK = slicer.vtkMatrix4x4()
        inputVolume.GetRASToIJKMatrix(rasToIJK)
        p0IJK = rasToIJK.MultiplyPoint(p0[0], p0[1], p0[2], 1.0)
        p1IJK = rasToIJK.MultiplyPoint(p1[0], p1[1], p1[2], 1.0)

        # find the bounding box of the seeds
        lowerIJK = map(int, map(min, p0IJK, p1IJK))
        upperIJK = map(int, map(max, p0IJK, p1IJK))

        # get an array of the input volume, extract the sub
        a = inputVolume.GetImageData().ToArray()
        sub = a[lowerIJK[2] : upperIJK[2], lowerIJK[1] : upperIJK[1], lowerIJK[0] : upperIJK[0]]

        # set up output node
        outImage = slicer.vtkImageData()
        outImage.SetDimensions(sub.shape[2], sub.shape[1], sub.shape[0])
        outImage.AllocateScalars()
        outImage.ToArray()[:] = sub[:]
        outputVolume.SetAndObserveImageData(outImage)
        rasToIJK.Invert()
        ijkToRAS = rasToIJK
        outputVolume.SetIJKToRASMatrix(rasToIJK)
        origin = ijkToRAS.MultiplyPoint(lowerIJK[0], lowerIJK[1], lowerIJK[2], 1.0)
        outputVolume.SetOrigin(origin[0], origin[1], origin[2])
        outputVolume.ModifiedSinceReadOn()

        displayNode = inputVolume.GetDisplayNode()
        if displayNode != None:
            newDisplayNode = displayNode.NewInstance()
            newDisplayNode.Copy(displayNode)
            slicer.MRMLScene.AddNodeNoNotify(newDisplayNode)
            outputVolume.SetAndObserveDisplayNodeID(newDisplayNode.GetID())

        appLogic = slicer.ApplicationLogic
        selectionNode = appLogic.GetSelectionNode()
        if inputVolume.GetLabelMap():
            outputVolume.SetLabelMap(1)
            selectionNode.SetReferenceActiveLabelVolumeID(outputVolume.GetID())
        else:
            selectionNode.SetReferenceActiveVolumeID(outputVolume.GetID())
        appLogic.PropagateVolumeSelection()

        self.Status("Done applying Subvolume.")
Пример #45
0
    def ExecuteFastMarching(self, inVolumeNode, lowerThreshold,
                            higherThreshold, sourceSeedsNode, targetSeedsNode):

        self._helper.debug("Starting execution of Fast Marching...")

        if not inVolumeNode or not sourceSeedsNode:
            self._helper.debug(inVolumeNode)
            self._helper.debug(lowerThreshold)
            self._helper.debug(higherThreshold)
            self._helper.debug(sourceSeedsNode)
            self._helper.debug(targetSeedsNode)
            slicer.Application.ErrorMessage(
                "Not enough information!!! Aborting Fast Marching..\n")
            return
        else:

            sourceSeedIds = slicer.vtkIdList()
            targetSeedIds = slicer.vtkIdList()

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            for i in range(sourceSeedsNode.GetNumberOfFiducials()):
                rasPt = sourceSeedsNode.GetNthFiducialXYZ(i)
                ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                sourceSeedIds.InsertNextId(
                    image.ComputePointId(int(ijkPt[0]), int(ijkPt[1]),
                                         int(ijkPt[2])))

            if targetSeedsNode:
                for i in range(targetSeedsNode.GetNumberOfFiducials()):
                    rasPt = targetSeedsNode.GetNthFiducialXYZ(i)
                    ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                    targetSeedIds.InsertNextId(
                        image.ComputePointId(int(ijkPt[0]), int(ijkPt[1]),
                                             int(ijkPt[2])))

            scalarRange = image.GetScalarRange()

            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)
            threshold.ThresholdBetween(lowerThreshold, higherThreshold)
            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetOutValue(scalarRange[0] - scalarRange[1])
            threshold.Update()

            scalarRange = threshold.GetOutput().GetScalarRange()

            thresholdedImage = threshold.GetOutput()

            shiftScale = slicer.vtkImageShiftScale()
            shiftScale.SetInput(thresholdedImage)
            shiftScale.SetShift(-scalarRange[0])
            shiftScale.SetScale(1 / (scalarRange[1] - scalarRange[0]))
            shiftScale.Update()

            speedImage = shiftScale.GetOutput()

            fastMarching = slicer.vtkvmtkFastMarchingUpwindGradientImageFilter(
            )
            fastMarching.SetInput(speedImage)
            fastMarching.SetSeeds(sourceSeedIds)
            fastMarching.GenerateGradientImageOff()
            fastMarching.SetTargetOffset(100.0)
            fastMarching.SetTargets(targetSeedIds)
            if targetSeedIds.GetNumberOfIds() > 0:
                fastMarching.SetTargetReachedModeToOneTarget()
            else:
                fastMarching.SetTargetReachedModeToNoTargets()
            fastMarching.Update()

            if targetSeedIds.GetNumberOfIds() > 0:
                subtract = slicer.vtkImageMathematics()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.SetOperationToAddConstant()
                subtract.SetConstantC(-fastMarching.GetTargetValue())
                subtract.Update()
            else:
                subtract = slicer.vtkImageThreshold()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.ThresholdByLower(1000)  #better value soon
                subtract.ReplaceInOff()
                subtract.ReplaceOutOn()
                subtract.SetOutValue(-1)
                subtract.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode, 0.0)

            self._helper.debug("Fast Marching done...")

            return outputContainer