def __init__(self):
     self.slicerVolumesLogic = slicer.vtkSlicerVolumesLogic()
     self.slicerVolumesLogic.SetMRMLScene(slicer.mrmlScene)
     self.dwiconvert_parameter_node = None
     self.diffusionweightedvolumemasking_parameter_node = None
     self.diffusiontensorestimation_parameter_node = None
     self.seeding_parameter_node = None
Example #2
0
    def mapFromCrosshair(self, targetNode=None):
        """Create a distance map based on the current location of the crosshair node"""

        self.volumeStatistics()

        if targetNode is None:
            targetNode = slicer.vtkSlicerVolumesLogic().CloneVolume(
                slicer.mrmlScene, self.nodes['dtd_covariance_MD'],
                'distanceVolume')

        crosshairNode = slicer.util.getNode('vtkMRMLCrosshairNodedefault')
        crosshairRAS = crosshairNode.GetCrosshairRAS()
        crosshairIndex = self.indexAt(targetNode, crosshairRAS)

        print(f'RAS {crosshairRAS}')
        for nodeName in self.names:
            node = self.nodes[nodeName]
            sample = self.sampleAtRAS(node, crosshairRAS)
            print(f'{nodeName}: {sample}')

        targetArray = slicer.util.arrayFromVolume(targetNode)

        for element in numpy.ndenumerate(targetArray):
            index = element[0]
            distance = self.standardizedDistanceBetweenIndices(
                index, crosshairIndex)
            targetArray[index] = distance

        targetArray = slicer.util.updateVolumeFromArray(
            targetNode, targetArray)
Example #3
0
  def loadVoi(self,filePath,segmentationNode,motionState=0,voi=None):
    
    if not segmentationNode:
       print "No segmentation node"
       return
    
    
    if voi.slicerNodeID[motionState]:
       return voi.slicerNodeID[motionState]
    
    binaryLabelmapReprName = vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName()
    segLogic = slicer.modules.segmentations.logic()
    segLogic.SetMRMLScene(slicer.mrmlScene)
    voi.slicerNodeID[motionState]=None
    
    if not filePath or not os.path.isfile(filePath):
      #Try to execute hed2nrrd.sh
      filePrefix, fileExtension = os.path.splitext(filePath)
      hed2nrrd = "/u/motion/share/bin/hed2nrrd.sh"
      dirName = os.path.dirname(os.path.realpath(filePath) )
      if os.path.isfile(hed2nrrd):
         #If you have write access, nrrd file is created
         if os.access(dirName, os.W_OK):
            os.system(hed2nrrd + " " + dirName)
         else:
            print "No write access to " + dirName +"\n Either create .nrrd files or get write permission."
            
    if not filePath or not os.path.isfile(filePath):        
       print "No file!" + filePath
       return None

    volumesLogic = slicer.vtkSlicerVolumesLogic()
    volumesLogic.SetMRMLScene(slicer.mrmlScene)
    slicerVolumeName = voi.name + "_" + str(motionState)
    slicerVolume = volumesLogic.AddArchetypeVolume(filePath,slicerVolumeName,1) 
    #success, midV = slicer.util.loadVolume(os.path.basename(filePath), 
                                           #properties = {'name' : "Helo", 'labelmap' : True}, returnNode = True)
    slicerVolume.SetOrigin(voi.getSlicerOrigin())
    if not slicerVolume:
      print "Can't load volume " + os.path.basename(filePath)
      return None
    
    colorNode = slicer.util.getNode("vtkMRMLColorTableNodeFileGenericAnatomyColors.txt")
    colorIndex = colorNode.GetColorIndexByName(voi.name.lower())
    
    color = [0,0,0,1]
    if colorIndex < 2:
       colorIndex = colorNode.GetColorIndexByName("region 11")
       
    if not colorNode.GetColor(colorIndex,color):
       print "Can't set color for " + voi.name.lower()
       color = [0,0,0,1]

    displayNode = slicer.vtkMRMLLabelMapVolumeDisplayNode()
    displayNode.SetAndObserveColorNodeID(colorNode.GetID())
    displayNode.SetColor(color[0:3])
    slicer.mrmlScene.AddNode(displayNode)
    slicerVolume.SetAndObserveDisplayNodeID(displayNode.GetID())


    try:
       array = slicer.util.array(slicerVolume.GetID())
    except AttributeError:
      import sys
      sys.stderr.write('Cannot get array.')
      
    array[:] = ( array[:] >> voi.motionStateBit[motionState] ) & 1
    if colorIndex > -1:
       array[:] *= colorIndex
    slicerVolume.GetImageData().Modified()
    if not slicer.vtkSlicerSegmentationsModuleLogic.ImportLabelmapToSegmentationNode(slicerVolume,segmentationNode):
       print "Can't load volume in segmentation"
       return None
    else:
       print "Added " + slicerVolume.GetName() + " to " + segmentationNode.GetName()
    
    return slicerVolume.GetID()
    
    grayMaker = slicer.modules.grayscalemodelmaker
    slicer.cli.run(grayMaker, None, parameters)


# Volume from the sample data that works with our CLI
v = getNode('MRHead')
grayModel(v)



### Numpy, VTK, matplotlib example
import vtk
import slicer
mrml = slicer.vtkMRMLScene()
vl = slicer.vtkSlicerVolumesLogic()
vl.SetAndObserveMRMLScene(mrml)
n = vl.AddArchetypeVolume('../../Testing/Data/Input/MRHeadResampled.nhdr', 'CTC')
i = n.GetImageData()
print (i.GetScalarRange())

import vtk.util.numpy_support
a = vtk.util.numpy_support.vtk_to_numpy(i.GetPointData().GetScalars())
print(a.min(),a.max())

# Matplotlib doesn't come with Slicer, must be installed separately
import matplotlib
import matplotlib.pyplot
n, bins, patches = matplotlib.pyplot.hist(a, 50, facecolor='g', alpha=0.75)
matplotlib.pyplot.show()