Example #1
0
    def testIgnoreBTX(self):
        """Try to call a method that is BTX'd, to ensure VTK_IGNORE_BTX=ON
        """

        stringArray = vtk.vtkStringArray()
        information = vtk.vtkInformation()
        stringArray.CopyInformation(information, 0)
Example #2
0
def VtkCreaStrArraySetData(setToDraw, entTypeName, attr):
  '''Creates an array of strings with information associated 
  to the points and cells.
  Parameters:
    setToDraw:   set of entities to be displayed
    entTypeName: type of entities to be displayed 
                 ("pnts", "lines", "nodes", "elementos")
    attr:        attribute to be stored in the array
  '''
  # Define matrix
  arr= vtk.vtkStringArray()
  arr.SetName(attr)
  container= None
  if(entTypeName=="pnts"):
    container= setToDraw.getPoints
  elif(entTypeName=="lines"):
    container= setToDraw.getLines
  elif(entTypeName=="nodes"):
    container= setToDraw.getNodes
  elif(entTypeName=="elementos"):
    container= setToDraw.getElements
  else:
    lmsg.error("error: "+entTypeName+" not implemented.")
  for e in container:
    tmp= str(getattr(e,attr))
    arr.InsertValue(e.getIdx,tmp)
  return arr
Example #3
0
    def testMultiTreeOutputs(self):
        outputs = vtk.vtkStringArray()
        outputs.SetNumberOfComponents(1)
        outputs.SetNumberOfTuples(2)
        outputs.SetValue(0, "tree1")
        outputs.SetValue(1, "tree2")
        rcal = vtk.vtkRCalculatorFilter()


        rcal.SetRscript("library(ape)\n\
                         tree1 = read.tree(text=\"" + tree_data + "\")\n\
                         tree2 = read.tree(text=\"" + tree_data + "\")\n")

        rcal.GetTrees(outputs)

        input = vtk.vtkTable()
        rcal.SetInputData(input)

        rcal.Update()

        compo = rcal.GetOutput()
        tree1 = compo.GetPieceAsDataObject(0)
        self.assertTrue(tree1.IsA('vtkTree'))
        tree2 = compo.GetPieceAsDataObject(1)
        self.assertTrue(tree2.IsA('vtkTree'))
Example #4
0
 def testReturnByReference(self):
     """Return a string by reference."""
     a = vtk.vtkStringArray()
     s = "hello"
     a.InsertNextValue(s)
     t = a.GetValue(0)
     self.assertEqual(t, s)
def GenerateVTKGraph(graph):
    '''
    Take the vertices and edge list in the graph parameter
    and return a VTK graph.
    '''
    g = vtk.vtkMutableDirectedGraph()
    # Label the vertices
    labels = vtk.vtkStringArray()
    labels.SetNumberOfComponents(1)
    labels.SetName("Labels")

    index = dict()
    l = list(graph[0])
    # Make the vertex labels and create a dictionary with the
    # keys as labels and the vertex ids as the values.
    for i in range(0, len(l)):
        # Set the vertex labels
        labels.InsertNextValue(l[i])
        index[l[i]] = g.AddVertex()
    g.GetVertexData().AddArray(labels)
    # Add edges
    l = list(graph[1])
    for i in range(0, len(l)):
        ll = list(l[i])
        g.AddGraphEdge(index[ll[0]], index[ll[1]])
#    g.Dump()
    return g
  def examineForImport(self,fileLists):
    """ Returns a list of qSlicerDICOMLoadable
    instances corresponding to ways of interpreting the 
    fileLists parameter.
    """
    # Create loadables for each file list
    loadables = []
    for fileList in fileLists: # Each file list corresponds to one series, so do loadables
      # Convert file list to VTK object to be able to pass it for examining
      # (VTK class cannot have Qt object as argument, otherwise it is not python wrapped)
      vtkFileList = vtk.vtkStringArray()
      for file in fileList:
        vtkFileList.InsertNextValue(slicer.util.toVTKString(file))

      # Examine files
      loadablesCollection = vtk.vtkCollection()
      slicer.modules.dicomrtimportexport.logic().ExamineForLoad(vtkFileList, loadablesCollection)

      for loadableIndex in xrange(0,loadablesCollection.GetNumberOfItems()):
        vtkLoadable = loadablesCollection.GetItemAsObject(loadableIndex)
        # Create Qt loadable if confidence is greater than 0
        if vtkLoadable.GetConfidence() > 0:
          # Convert to Qt loadable to pass it back
          qtLoadable = slicer.qSlicerDICOMLoadable()
          qtLoadable.copyFromVtkLoadable(vtkLoadable)
          qtLoadable.tooltip = 'Valid RT object in selection'
          qtLoadable.selected = True
          loadables.append(qtLoadable)

    return loadables
Example #7
0
    def loadFolder(self):
        loader = self.folderLoader
        loader.loadFile()

        if loader.accepted:
            loader.setDir(os.path.dirname(str(loader.selectedFolder)))
            self.dicomReader = vtkgdcm.vtkGDCMImageReader()
            regex = re.compile(r'.+\.dcm')
            files = [x for x in os.listdir(loader.selectedFolder) if re.match(regex, x)]
            self.seriesSize = len(files)
            temp = vtk.vtkStringArray()
            temp.SetNumberOfValues(len(files))
            i = 0
            for file in sorted(files):
                temp.SetValue(i, os.path.join(str(loader.selectedFolder), file))
                i = i + 1
            self.dicomReader.SetFileNames(temp)
            self.dicomReader.Update()

            imageData = self.dicomReader.GetOutput()
            size = imageData.GetDimensions()
            width = size[0]
            height = size[1]
            self.vtkWidget.setMaximumSize(QtCore.QSize(width, height))
            self.vtkWidget.setMinimumSize(QtCore.QSize(width, height))
            self.viewer.SetInputConnection(self.dicomReader.GetOutputPort())
            self.iren.ReInitialize()

            self.getMedicalData()

            self.enableSlider(self.seriesSize-1)
            self.ui.dicomSlider.setFocus()
Example #8
0
 def _init(self):
   g = vtk.vtkMutableDirectedGraph()
   v1 = g.AddVertex()
   v2 = g.AddVertex()
   g.AddGraphEdge(v1,v2)
   G_labels = vtk.vtkStringArray()
   G_labels.SetName("VLabels")
   G_labels.InsertNextValue("One")
   G_labels.InsertNextValue("Two")
   g.GetVertexData().AddArray(G_labels)
   G_labels = vtk.vtkStringArray()
   G_labels.SetName("VLabels")
   G_labels.InsertNextValue("Three")
   G_labels.InsertNextValue("Four")
   g.GetVertexData().AddArray(G_labels)
   return g
Example #9
0
    def __init__(self, filename, max_num_of_vertices=-1, edge_color_filename=None):
        super(VTKVisualizer, self).__init__()
        self.vertex_id_idx_map = {}
        self.next_vertex_id = 0
        self.edge_counter = 0
        self.lookup_table = vtk.vtkLookupTable()
        self.lookup_table.SetNumberOfColors(int(1e8))
        self.edge_color_tuples = {}
        self.edge_color_filename = edge_color_filename
        self.label_vertex_id_map = {}
        self.starting_vertex = None
        self.starting_vertex_index = -1
        self.filename = filename
        self.max_num_of_vertices = max_num_of_vertices
        self.g = vtk.vtkMutableDirectedGraph()

        self.vertex_ids = vtk.vtkIntArray()
        self.vertex_ids.SetNumberOfComponents(1)
        self.vertex_ids.SetName(VERTEX_ID)

        self.labels = vtk.vtkStringArray()
        self.labels.SetNumberOfComponents(1)
        self.labels.SetName(LABELS)

        self.glyph_scales = vtk.vtkFloatArray()
        self.glyph_scales.SetNumberOfComponents(1)
        self.glyph_scales.SetName(SCALES)

        self.edge_weights = vtk.vtkDoubleArray()
        self.edge_weights.SetNumberOfComponents(1)
        self.edge_weights.SetName(WEIGHTS)

        self.edge_colors = vtk.vtkIntArray()
        self.edge_colors.SetNumberOfComponents(1)
        self.edge_colors.SetName(EDGE_COLORS)
def DICOMReaderToNumpy(directory):
    file_list = glob.glob(directory + os.sep + "*")
    file_list = sorted(file_list)

    ipp = gdcm.IPPSorter()
    ipp.SetComputeZSpacing(True)
    ipp.Sort(file_list)

    file_list = ipp.GetFilenames()

    array = vtk.vtkStringArray()

    for x in xrange(len(file_list)):
        array.InsertValue(x, file_list[x])

    read = vtkgdcm.vtkGDCMImageReader()
    read.SetFileNames(array)
    read.Update()

    img = vtk.vtkImageData()
    img.DeepCopy(read.GetOutput())
    img.SetSpacing(1, 1, 1)
    img.Update()

    ex = img.GetExtent()
    image = vtk.util.numpy_support.vtk_to_numpy(img.GetPointData().GetScalars())
    image = image.reshape((ex[5] + 1, ex[1] + 1, ex[3] + 1))

    return ApplyWindowLevel(image, 2000, 300)
Example #11
0
    def add_line(self, start, end, color=(0.5, 0.5, 0.5), width=1):
        """
        Adds a line.

        Args:
            start:
                Starting coordinates for line.
            end:
                Ending coordinates for line.
            color:
                Color for text as RGB. Defaults to grey.
            width:
                Width of line. Defaults to 1.
        """
        source = vtk.vtkLineSource()
        source.SetPoint1(start)
        source.SetPoint2(end)

        vertexIDs = vtk.vtkStringArray()
        vertexIDs.SetNumberOfComponents(1)
        vertexIDs.SetName("VertexIDs")
        # Set the vertex labels
        vertexIDs.InsertNextValue("a")
        vertexIDs.InsertNextValue("b")
        source.GetOutput().GetPointData().AddArray(vertexIDs)

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(source.GetOutputPort())
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetColor(color)
        actor.GetProperty().SetLineWidth(width)
        self.ren.AddActor(actor)
  def computeStatistics(self, segmentationNode, grayscaleNode, visibleSegmentsOnly = True):
    import vtkSegmentationCorePython as vtkSegmentationCore

    self.reset()

    self.segmentationNode = segmentationNode
    self.grayscaleNode = grayscaleNode

    # Get segment ID list
    visibleSegmentIds = vtk.vtkStringArray()
    if visibleSegmentsOnly:
      self.segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(visibleSegmentIds)
    else:
      self.segmentationNode.GetSegmentation().GetSegmentIDs(visibleSegmentIds)
    if visibleSegmentIds.GetNumberOfValues() == 0:
      logging.debug("computeStatistics will not return any results: there are no visible segments")
    # Initialize self.statistics with segment IDs and names
    for segmentIndex in range(visibleSegmentIds.GetNumberOfValues()):
      segmentID = visibleSegmentIds.GetValue(segmentIndex)
      segment = self.segmentationNode.GetSegmentation().GetSegment(segmentID)
      self.statistics["SegmentIDs"].append(segmentID)
      self.statistics[segmentID,"Segment"] = segment.GetName()

    self.addSegmentLabelmapStatistics()
    self.addGrayscaleVolumeStatistics()
    self.addSegmentClosedSurfaceStatistics()
	def getRAWReader(self, Series_Info, Channel):
		"""
		create a tiff reader that reads then given channel of the given series
		"""
		XYDim = Series_Info['Resolution_X'] - 1
		NumSect = Series_Info['Number_Sections'] - 1
		XSpace = Series_Info['Voxel_Width_X']
		YSpace = Series_Info['Voxel_Height_Y']
		ZSpace = Series_Info['Voxel_Depth_Z']
		
		RAWReader = vtk.vtkImageReader2()
		if self.progressCallback:
			RAWReader.AddObserver("ProgressEvent", lib.messenger.send)
			lib.messenger.connect(RAWReader, 'ProgressEvent', self.progressCallback)
		arr = vtk.vtkStringArray()
		for i in Channel:
			arr.InsertNextValue(os.path.join(self.path, i))
		RAWReader.SetFileNames(arr)
	
		if Series_Info['Bit_Depth'] == 8:
			RAWReader.SetDataScalarTypeToUnsignedChar()
		elif Series_Info['Bit_Depth'] == 12:
			RAWReader.SetDataScalarTypeToUnsignedShort()
		RAWReader.FileLowerLeftOff()

		spacingX,spacingY,spacingZ = 1.0,1.0,1.0
		if XSpace != 0.0 and YSpace != 0.0:
			spacingY = YSpace / XSpace
		if XSpace != 0.0 and ZSpace != 0.0:
			spacingZ = ZSpace / XSpace
		
		RAWReader.SetDataExtent(0, XYDim, 0, XYDim, 0, NumSect)
		RAWReader.SetDataSpacing(spacingX, spacingY, spacingZ)
		RAWReader.Update()
		return RAWReader
  def onApply(self):
    self.delayedAutoUpdateTimer.stop()
    self.observeSegmentation(False)

    import vtkSegmentationCorePython as vtkSegmentationCore
    segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()
    segmentationDisplayNode = segmentationNode.GetDisplayNode()
    previewNode = self.getPreviewNode()

    self.scriptedEffect.saveStateForUndo()

    # Move segments from preview into current segmentation
    segmentIDs = vtk.vtkStringArray()
    previewNode.GetSegmentation().GetSegmentIDs(segmentIDs)
    for index in xrange(segmentIDs.GetNumberOfValues()):
      segmentID = segmentIDs.GetValue(index)
      previewSegment = previewNode.GetSegmentation().GetSegment(segmentID)
      previewSegmentLabelmap = previewSegment.GetRepresentation(vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName())
      slicer.vtkSlicerSegmentationsModuleLogic.SetBinaryLabelmapToSegment(previewSegmentLabelmap, segmentationNode, segmentID)
      if segmentationDisplayNode is not None and self.isBackgroundLabelmap(previewSegmentLabelmap):
        # Automatically hide result segments that are background (all eight corners are non-zero)
        segmentationDisplayNode.SetSegmentVisibility(segmentID, False)
      previewNode.GetSegmentation().RemoveSegment(segmentID) # delete now to limit memory usage

    self.reset()
 def getSegmentIDs(segmentationNode, visibleOnly=False):
   if not segmentationNode:
     raise AttributeError("SegmentationNode must not be None!")
   segmentIDs = vtk.vtkStringArray()
   segmentation = segmentationNode.GetSegmentation()
   command = segmentationNode.GetDisplayNode().GetVisibleSegmentIDs if visibleOnly else segmentation.GetSegmentIDs
   command(segmentIDs)
   return [segmentIDs.GetValue(idx) for idx in range(segmentIDs.GetNumberOfValues())]
Example #16
0
 def getSegmentIDs(segmentationNode, visibleOnly):
   if not segmentationNode:
     return []
   segmentIDs = vtk.vtkStringArray()
   segmentation = segmentationNode.GetSegmentation()
   command = segmentationNode.GetDisplayNode().GetVisibleSegmentIDs if visibleOnly else segmentation.GetSegmentIDs
   command(segmentIDs)
   return [segmentIDs.GetValue(idx) for idx in range(segmentIDs.GetNumberOfValues())]
Example #17
0
    def testPassUnicodeAsString(self):
        """Pass a unicode where a string is expected.  Should succeed."""
        if not unicode_support:
            return

        a = vtk.vtkStringArray()
        a.InsertNextValue(u'Francois')
        s = a.GetValue(0)
        self.assertEqual(s, 'Francois')
Example #18
0
  def onApply(self):

    # Get list of visible segment IDs, as the effect ignores hidden segments.
    segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()
    visibleSegmentIds = vtk.vtkStringArray()
    segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(visibleSegmentIds)
    if visibleSegmentIds.GetNumberOfValues() == 0:
      logging.info("Smoothing operation skipped: there are no visible segments")
      return

    # This can be a long operation - indicate it to the user
    qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)

    # Allow users revert to this state by clicking Undo
    self.scriptedEffect.saveStateForUndo()

    # Export master image data to temporary new volume node.
    # Note: Although the original master volume node is already in the scene, we do not use it here,
    # because the master volume may have been resampled to match segmentation geometry.
    import vtkSegmentationCorePython as vtkSegmentationCore
    masterVolumeNode = slicer.vtkMRMLScalarVolumeNode()
    slicer.mrmlScene.AddNode(masterVolumeNode)
    masterVolumeNode.SetAndObserveTransformNodeID(segmentationNode.GetTransformNodeID())
    slicer.vtkSlicerSegmentationsModuleLogic.CopyOrientedImageDataToVolumeNode(self.scriptedEffect.masterVolumeImageData(), masterVolumeNode)
    # Generate merged labelmap of all visible segments, as the filter expects a single labelmap with all the labels.
    mergedLabelmapNode = slicer.vtkMRMLLabelMapVolumeNode()
    slicer.mrmlScene.AddNode(mergedLabelmapNode)
    slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(segmentationNode, visibleSegmentIds, mergedLabelmapNode, masterVolumeNode)

    # Run segmentation algorithm
    import SimpleITK as sitk
    import sitkUtils
    # Read input data from Slicer into SimpleITK
    labelImage = sitk.ReadImage(sitkUtils.GetSlicerITKReadWriteAddress(mergedLabelmapNode.GetName()))
    backgroundImage = sitk.ReadImage(sitkUtils.GetSlicerITKReadWriteAddress(masterVolumeNode.GetName()))
    # Run watershed filter
    featureImage = sitk.GradientMagnitudeRecursiveGaussian(backgroundImage, float(self.scriptedEffect.doubleParameter("ObjectScaleMm")))
    del backgroundImage
    f = sitk.MorphologicalWatershedFromMarkersImageFilter()
    f.SetMarkWatershedLine(False)
    f.SetFullyConnected(False)
    labelImage = f.Execute(featureImage, labelImage)
    del featureImage
    # Pixel type of watershed output is the same as the input. Convert it to int16 now.
    if labelImage.GetPixelID() != sitk.sitkInt16:
      labelImage = sitk.Cast(labelImage, sitk.sitkInt16)
    # Write result from SimpleITK to Slicer. This currently performs a deep copy of the bulk data.
    sitk.WriteImage(labelImage, sitkUtils.GetSlicerITKReadWriteAddress(mergedLabelmapNode.GetName()))
    mergedLabelmapNode.GetImageData().Modified()
    mergedLabelmapNode.Modified()

    # Update segmentation from labelmap node and remove temporary nodes
    slicer.vtkSlicerSegmentationsModuleLogic.ImportLabelmapToSegmentationNode(mergedLabelmapNode, segmentationNode, visibleSegmentIds)
    slicer.mrmlScene.RemoveNode(masterVolumeNode)
    slicer.mrmlScene.RemoveNode(mergedLabelmapNode)

    qt.QApplication.restoreOverrideCursor()
Example #19
0
def dict_to_vtkarrays(row, fields, attributes):
    import vtk
    for key in fields:
        value = row[key]
        comp = 1
        if isinstance(value, list):
            comp = len(value)
            value = value[0]
        if isinstance(value, (int, long, float)):
            arr = vtk.vtkDoubleArray()
        elif isinstance(value, str):
            arr = vtk.vtkStringArray()
        elif isinstance(value, unicode):
            arr = vtk.vtkUnicodeStringArray()
        else:
            arr = vtk.vtkStringArray()
        arr.SetName(key)
        arr.SetNumberOfComponents(comp)
        attributes.AddArray(arr)
Example #20
0
 def loadFilesWithArchetype(self,files,name):
   """Load files in the traditional Slicer manner
   using the volume logic helper class
   and the vtkITK archetype helper code
   """
   name = slicer.util.toVTKString(name)
   fileList = vtk.vtkStringArray()
   for f in files:
     fileList.InsertNextValue(slicer.util.toVTKString(f))
   volumesLogic = slicer.modules.volumes.logic()
   return(volumesLogic.AddArchetypeScalarVolume(files[0],name,0,fileList))
Example #21
0
 def testPassEncodedString(self):
     """Pass encoded 8-bit strings."""
     a = vtk.vtkStringArray()
     # latin1 encoded string will be returned as "bytes", which is
     # just a normal str object in Python 2
     encoded = cedilla.encode('latin1')
     a.InsertNextValue(encoded)
     result = a.GetValue(0)
     self.assertEqual(type(result), bytes)
     self.assertEqual(result, encoded)
     # utf-8 encoded string will be returned as "str", which is
     # actually unicode in Python 3
     a = vtk.vtkStringArray()
     encoded = cedilla.encode('utf-8')
     a.InsertNextValue(encoded)
     result = a.GetValue(0)
     self.assertEqual(type(result), str)
     if sys.hexversion >= 0x03000000:
         self.assertEqual(result.encode('utf-8'), encoded)
     else:
         self.assertEqual(result, encoded)
Example #22
0
 def switchSegment(self, segmentIndexOffset = 1):
   """Select previous/next visible segment"""
   currentSegmentId = self.editor.currentSegmentID()
   visibleSegmentIds = vtk.vtkStringArray()
   self.editor.segmentationNode().GetDisplayNode().GetVisibleSegmentIDs(visibleSegmentIds)
   for segmentIndex in range(visibleSegmentIds.GetNumberOfValues()):
     segmentId = visibleSegmentIds.GetValue(segmentIndex)
     if segmentId == currentSegmentId:
       newSegmentIndex = segmentIndex + segmentIndexOffset
       if newSegmentIndex>=0 and newSegmentIndex<visibleSegmentIds.GetNumberOfValues():
         newSegmentId = visibleSegmentIds.GetValue(newSegmentIndex)
         self.editor.setCurrentSegmentID(newSegmentId)
       return
 def InitializeMetricsTable( metricsTable, taskNames ):
   if ( metricsTable == None ):
     return
 
   metricsTable.GetTable().Initialize()
   
   # TODO: Make the more robust (e.g. qSlicerMetricsTableWidget::METRIC_TABLE_COLUMNS) 
   metricsTableColumnNames = [ "MetricName", "MetricRoles", "MetricUnit" ]
   metricsTableColumnNames = metricsTableColumnNames + taskNames
   for columnName in metricsTableColumnNames:
     column = vtk.vtkStringArray()
     column.SetName( columnName )
     metricsTable.GetTable().AddColumn( column )
Example #24
0
 def RemoveCells(self,cellIds):
     networkCellArray = vtk.vtkCellArray()
     networkLabelsArray = vtk.vtkStringArray()
     numberOfCells = self.Network.GetNumberOfCells()
     for i in range(numberOfCells):
         if i in cellIds:
             continue
         networkCellArray.InsertNextCell(self.Network.GetCell(i))
         networkLabelsArray.InsertNextValue(self.NetworkLabelsArray.GetValue(i))
     self.Network.SetLines(networkCellArray)
     self.NetworkLabelsArray.DeepCopy(networkLabelsArray)
     self.Network.BuildCells()
     self.Network.Modified()
Example #25
0
def addTextureMetaData(polyData, materialName):

    global materialsLibrary
    material = materialsLibrary[materialName]

    textureFile = material.effect.diffuse.sampler.surface.image.path

    if not os.path.isfile(textureFile):
        print 'warning, cannot find texture file:', os.path.abspath(textureFile)

    s = vtk.vtkStringArray()
    s.InsertNextValue(textureFile)
    s.SetName('texture_filename')
    polyData.GetFieldData().AddArray(s)
Example #26
0
	def GetTree(self):
		"""Returns a full vtkTree based on data loaded in LoadData()."""

		if self.data_loaded:

			vertex_id = vtk.vtkIdTypeArray()
			vertex_id.SetName('vertex_ids')
			for ii in range(len(self.cp)):
				vertex_id.InsertNextValue(ii)

			NINvtk = VN.numpy_to_vtk(self.NumberInNet, deep=True)
			NINvtk.SetName('num_in_vertex')
			SCALESvtk = VN.numpy_to_vtk(self.Scales, deep=True)
			SCALESvtk.SetName('scale')

			# This array will default to empty strings
			BLANKvtk = vtk.vtkStringArray()
			BLANKvtk.SetNumberOfComponents(1)
			BLANKvtk.SetNumberOfTuples(self.NumberInNet.shape[0])
			BLANKvtk.SetName('blank')

			# Build tree out of CP list of "is a child of"
			#	remembering that Matlab indices are 1-based and numpy/VTK 0-based
			print 'Building graph'
			dg = vtk.vtkMutableDirectedGraph()
			edge_id = vtk.vtkIdTypeArray()
			edge_id.SetName('edge_ids')
			for ii in range(self.cp.size):
				dg.AddVertex()
			for ii in range(self.cp.size):
				if self.cp[ii] > 0:		# CP already zero-based
					dg.AddGraphEdge(self.cp[ii],ii)		# Method for use with wrappers -- AddEdge() in C++
					edge_id.InsertNextValue(ii)

			dg.GetVertexData().AddArray(NINvtk)
			dg.GetVertexData().AddArray(SCALESvtk)
			dg.GetVertexData().AddArray(vertex_id)
			dg.GetVertexData().SetActiveScalars('scale')
			dg.GetVertexData().SetActivePedigreeIds('vertex_ids')
			dg.GetEdgeData().AddArray(edge_id)
			dg.GetEdgeData().SetActivePedigreeIds('edge_ids')

			tree = vtk.vtkTree()
			tree.CheckedShallowCopy(dg)

			return tree

		else:
			raise IOError, "Can't get tree until data is loaded successfully"
Example #27
0
 def __init__(self, filepaths):
     """
     @type filenames: list
     @param filenames: The list of image filenames to open as a volume
     """
     self.reader = None
     self.filepaths = filepaths
     xmax, ymax = getImageSize(filepaths[0])
     self.pixelExtents = (0, xmax, 0, ymax)
     self.sliceRange = None
     self.dataSpacing = None
     
     self.imageArray = vtk.vtkStringArray()
     for fname in filepaths:
         self.imageArray.InsertNextValue(fname)
    def ConvertStructureSetToLabelmap(self):
      import vtkSegmentationCorePython as vtkSegmentationCore

      labelmapsToSave = []

      # Get all segmentation nodes from the scene
      segmentationNodes = slicer.util.getNodes('vtkMRMLSegmentationNode*')

      for segmentationNode in segmentationNodes.values():
        logging.info('  Converting structure set ' + segmentationNode.GetName())
        # Set referenced volume as rasterization reference
        referenceVolume = slicer.vtkSlicerDicomRtImportExportModuleLogic.GetReferencedVolumeByDicomForSegmentation(
          segmentationNode)
        if referenceVolume == None:
          logging.error('No reference volume found for segmentation ' + segmentationNode.GetName())
          continue

        # Perform conversion
        binaryLabelmapRepresentationName = vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName()
        segmentation = segmentationNode.GetSegmentation()
        segmentation.CreateRepresentation(binaryLabelmapRepresentationName)

        # Create labelmap volume nodes from binary labelmaps
        segmentIDs = vtk.vtkStringArray()
        segmentation.GetSegmentIDs(segmentIDs)
        for segmentIndex in xrange(0, segmentIDs.GetNumberOfValues()):
          segmentID = segmentIDs.GetValue(segmentIndex)
          segment = segmentation.GetSegment(segmentID)
          binaryLabelmap = segment.GetRepresentation(
            vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName())
          if not binaryLabelmap:
            logging.error(
              'Failed to retrieve binary labelmap from segment ' + segmentID + ' in segmentation ' + segmentationNode.GetName())
            continue
          labelmapNode = slicer.vtkMRMLLabelMapVolumeNode()
          slicer.mrmlScene.AddNode(labelmapNode)
          labelmapName = segmentationNode.GetName() + "_" + segmentID
          labelmapNode.SetName(labelmapName)
          if not slicer.vtkSlicerSegmentationsModuleLogic.CreateLabelmapVolumeFromOrientedImageData(
              binaryLabelmap, labelmapNode):
            logging.error('Failed to create labelmap from segment ' + segmentID + ' in segmentation ' + segmentationNode.GetName())
            continue

          # Append volume to list
          labelmapsToSave.append(labelmapNode)

      return labelmapsToSave
Example #29
0
  def computeStatistics(self):
    """Compute statistical measures for all (visible) segments"""
    self.reset()

    segmentationNode = slicer.mrmlScene.GetNodeByID(self.getParameterNode().GetParameter("Segmentation"))

    # Get segment ID list
    visibleSegmentIds = vtk.vtkStringArray()
    if self.getParameterNode().GetParameter('visibleSegmentsOnly')=='True':
      segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(visibleSegmentIds)
    else:
      segmentationNode.GetSegmentation().GetSegmentIDs(visibleSegmentIds)
    if visibleSegmentIds.GetNumberOfValues() == 0:
      logging.debug("computeStatistics will not return any results: there are no visible segments")

    # update statistics for all segment IDs
    for segmentIndex in range(visibleSegmentIds.GetNumberOfValues()):
      segmentID = visibleSegmentIds.GetValue(segmentIndex)
      self.updateStatisticsForSegment(segmentID)
Example #30
0
def makeVTKWellsUsingModule(fname_base, welltracks_df, xml=False):
    
    numpoints = welltracks_df.shape[0]
    wells = welltracks_df['Well'].unique().tolist()
    numwells = len(wells)

    grid = vtkUnstructuredGrid()
    points = vtkPoints()  
    
    for i in range(numpoints):
        points.InsertNextPoint(welltracks_df.loc[i,'X'], welltracks_df.loc[i,'Y'], welltracks_df.loc[i,'Elev_mASL'])
    
    cells = vtkCellArray()
    wellname = vtkStringArray()
    wellname.SetName('Well')
    
    for well in wells:
        print well
        polyline = vtkPolyLine()
        indices = welltracks_df[welltracks_df['Well']==well].index.tolist()
        for i, j in enumerate(indices):
            polyline.GetPointIds().SetNumberOfIds(len(indices))
            polyline.GetPointIds().SetId(i,j)
            
        cells.InsertNextCell(polyline)
        wellname.InsertNextValue(well)
        
    grid.SetPoints(points)
    grid.SetCells(VTK_POLY_LINE, cells)
    grid.GetCellData().AddArray(wellname)
    
    if xml:
        writer = vtkXMLUnstructuredGridWriter()
        writer.SetFileName('{}.vtu'.format(fname_base))
        writer.SetDataModeToAscii()
        writer.SetInputData(grid)
        writer.Write()
        
    else:
        writer = vtkUnstructuredGridWriter()
        writer.SetFileName('{}.vtk'.format(fname_base))
        writer.SetInputData(grid)
        writer.Write()
Example #31
0
def test():
    import vtk, qt, ctk, slicer
    import os

    slicer.modules.DeerSegmentorWidget.onBtnInitializeStudy()
    for sid, deer in slicer.modules.DeerSegmentorWidget.logic.deers.items():
        if not deer.db_info["done"] == '1':
            continue
        #open deer
        slicer.modules.DeerSegmentorWidget.logic.load_deer(sid)

        segmentation_node = slicer.mrmlScene.GetNodesByClass(
            "vtkMRMLSegmentationNode").GetItemAsObject(0)
        accepted = ["mask", "non-liver", "worm"]

        volume = deer.node_dict[deer.t1_path]
        labelmap_node = slicer.vtkMRMLLabelMapVolumeNode()

        for seg_name in accepted:
            print(f"exporting segement {seg_name}...")
            segment = segmentation_node.GetSegmentation().GetSegment(seg_name)
            segments = vtk.vtkStringArray()
            segments.SetNumberOfValues(1)
            segments.SetValue(0, segment.GetName())

            slicer.mrmlScene.AddNode(labelmap_node)
            slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(
                segmentation_node, segments, labelmap_node, volume)

            myStorageNode = labelmap_node.CreateDefaultStorageNode()
            myStorageNode.SetFileName(
                os.path.join(deer.slicer_out_dir, f"{seg_name}.nii.gz"))
            myStorageNode.WriteData(labelmap_node)
            slicer.mrmlScene.RemoveNode(myStorageNode)

        slicer.mrmlScene.RemoveNode(labelmap_node)

        #close deer
        slicer.modules.DeerSegmentorWidget.logic.close_active_deer(True)
Example #32
0
def getMaskVolume(referenceVolume):
    from scipy import ndimage
    shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
    # create segmentation node
    segmentationNode = slicer.mrmlScene.AddNewNodeByClass(
        "vtkMRMLSegmentationNode")
    segmentationNode.CreateDefaultDisplayNodes()
    segmentationNode.GetDisplayNode().SetVisibility(False)
    # add masks
    nLabelMaps = slicer.mrmlScene.GetNumberOfNodesByClass(
        'vtkMRMLLabelMapVolumeNode')
    for i in range(nLabelMaps):
        labelMapNode = slicer.mrmlScene.GetNthNodeByClass(
            i, 'vtkMRMLLabelMapVolumeNode')
        if 'correction' in shNode.GetItemAttributeNames(
                shNode.GetItemByDataNode(labelMapNode)):
            slicer.modules.segmentations.logic(
            ).ImportLabelmapToSegmentationNode(labelMapNode, segmentationNode)
    # if no segment return zeros
    if not segmentationNode.GetSegmentation().GetNumberOfSegments():
        slicer.mrmlScene.RemoveNode(segmentationNode)
        scalarVolumeNode = GridNodeHelper.emptyVolume(
            referenceVolume.GetImageData().GetDimensions(),
            referenceVolume.GetOrigin(), referenceVolume.GetSpacing())
        return scalarVolumeNode
    # segmentation to label
    labelMapNode = slicer.mrmlScene.AddNewNodeByClass(
        "vtkMRMLLabelMapVolumeNode")
    slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(
        segmentationNode, vtk.vtkStringArray(), labelMapNode, referenceVolume)
    slicer.mrmlScene.RemoveNode(segmentationNode)
    # distance filter
    distanceFilterNode = getDistanceMap(labelMapNode)
    slicer.mrmlScene.RemoveNode(labelMapNode)
    distanceFilterArray = slicer.util.array(distanceFilterNode.GetID())
    distanceFilterArray[:] = ndimage.gaussian_filter(
        1 - (np.tanh(distanceFilterArray) + 1) / 2, 1)
    distanceFilterNode.Modified()
    return distanceFilterNode
Example #33
0
def convert_string_array(arr, name=None):
    """A helper to convert a numpy array of strings to a vtkStringArray
    or vice versa. Note that this is terribly inefficient - inefficient support
    is better than no support :). If you have ideas on how to make this faster,
    please consider opening a pull request.
    """
    if isinstance(arr, np.ndarray):
        vtkarr = vtk.vtkStringArray()
        ########### OPTIMIZE ###########
        for val in arr:
            vtkarr.InsertNextValue(val)
        ################################
        if isinstance(name, str):
            vtkarr.SetName(name)
        return vtkarr
    # Otherwise it is a vtk array and needs to be converted back to numpy
    carr = np.empty(arr.GetNumberOfValues(), dtype='O')
    ############### OPTIMIZE ###############
    for i in range(arr.GetNumberOfValues()):
        carr[i] = arr.GetValue(i)
    ########################################
    return carr.astype('|S')
Example #34
0
    def onApply(self):
        self.delayedAutoUpdateTimer.stop()
        self.observeSegmentation(False)

        import vtkSegmentationCorePython as vtkSegmentationCore
        segmentationNode = self.scriptedEffect.parameterSetNode(
        ).GetSegmentationNode()
        segmentationDisplayNode = segmentationNode.GetDisplayNode()
        previewNode = self.getPreviewNode()

        self.scriptedEffect.saveStateForUndo()

        previewContainsClosedSurfaceRepresentation = previewNode.GetSegmentation(
        ).ContainsRepresentation(
            slicer.vtkSegmentationConverter.
            GetSegmentationClosedSurfaceRepresentationName())

        # Move segments from preview into current segmentation
        segmentIDs = vtk.vtkStringArray()
        previewNode.GetSegmentation().GetSegmentIDs(segmentIDs)
        for index in range(segmentIDs.GetNumberOfValues()):
            segmentID = segmentIDs.GetValue(index)
            previewSegmentLabelmap = slicer.vtkOrientedImageData()
            previewNode.GetBinaryLabelmapRepresentation(
                segmentID, previewSegmentLabelmap)
            self.scriptedEffect.modifySegmentByLabelmap(
                segmentationNode, segmentID, previewSegmentLabelmap,
                slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
            if segmentationDisplayNode is not None and self.isBackgroundLabelmap(
                    previewSegmentLabelmap):
                # Automatically hide result segments that are background (all eight corners are non-zero)
                segmentationDisplayNode.SetSegmentVisibility(segmentID, False)
            previewNode.GetSegmentation().RemoveSegment(
                segmentID)  # delete now to limit memory usage

        if previewContainsClosedSurfaceRepresentation:
            segmentationNode.CreateClosedSurfaceRepresentation()

        self.reset()
    def OnRemoveIslandsButtonClicked(self):
        logic = UsSurfaceToLandmarksLogic()
        WorkingLabelMap = self.WorkingLabelMapStorage.currentNode()
        WorkingImageData = WorkingLabelMap.GetImageData()

        # Clear mrmlScene of old labelmap node before exporting the new one
        OldLabelMapNode = self.WorkingLabelMapStorage.currentNode()
        if OldLabelMapNode != None:
            slicer.mrmlScene.RemoveNode(OldLabelMapNode)

        print "DEBUG - About to remove islands"

        LabelMapIslandsRemoved = logic.RemoveCoronalIslands(
            WorkingImageData, self.IslandSizeThresholdSlider.value)
        LabelMapIslandsRemoved.SetName(WorkingLabelMap.GetName())
        print "DEBUG - Islands removed"

        ConvSeg = slicer.vtkMRMLSegmentationNode()
        ConvSeg.SetScene(slicer.mrmlScene)
        SegLogic = slicer.modules.segmentations.logic()
        SegLogic.ImportLabelmapToSegmentationNode(LabelMapIslandsRemoved,
                                                  ConvSeg)
        ConvSeg.CreateBinaryLabelmapRepresentation()
        ConvSeg.SetMasterRepresentationToBinaryLabelmap()

        LabelMapName = vtk.vtkStringArray()
        LabelMapName.InsertNextValue(WorkingLabelMap.GetName())
        DeislandedLabelMapNode = slicer.vtkMRMLLabelMapVolumeNode()
        DeislandedLabelMapNode.SetName(WorkingLabelMap.GetName())
        slicer.mrmlScene.AddNode(DeislandedLabelMapNode)
        SegLogic.ExportSegmentsToLabelmapNode(ConvSeg, LabelMapName,
                                              DeislandedLabelMapNode)

        # Update mrmlScene and UI
        #slicer.mrmlScene.AddNode(LabelMapIslandsRemoved)
        self.WorkingLabelMapStorage.setCurrentNode(DeislandedLabelMapNode)
        self.UpdateSegmentationNode()

        return True
def createEntropionSegment(segmentation_node, ref_img_path,
                           out_segmentation_path):
    if segmentation_node is None or not ref_img_path.exists():
        print('Could not find: {}'.format(ref_img_path))
        return

    current_segmentation = segmentation_node.GetSegmentation()
    number_of_segments = current_segmentation.GetNumberOfSegments()
    if number_of_segments > 3:
        # Most probably has an entropion already, return
        return

    # Export segment as vtkImageData (via temporary labelmap volume node)
    image_node = slicer.util.loadVolume(str(ref_img_path),
                                        {'singleFile': True})
    segmentIds = vtk.vtkStringArray()
    current_segmentation.GetSegmentIDs(segmentIds)
    segmentIds.InsertNextValue('Entropion')
    segmentation_node.GetSegmentation().AddEmptySegment('Entropion')

    # Save this label to image
    slicer.util.saveNode(segmentation_node, str(out_segmentation_path))
Example #37
0
    def onApply(self):
        # Make sure the user wants to do the operation, even if the segment is not visible
        if not self.scriptedEffect.confirmCurrentSegmentVisible():
            return

        try:
            # This can be a long operation - indicate it to the user
            qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
            self.scriptedEffect.saveStateForUndo()

            applyToAllVisibleSegments = int(self.scriptedEffect.parameter("ApplyToAllVisibleSegments")) != 0 \
                if self.scriptedEffect.parameter("ApplyToAllVisibleSegments") else False

            if applyToAllVisibleSegments:
                # Smooth all visible segments
                inputSegmentIDs = vtk.vtkStringArray()
                segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()
                segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(inputSegmentIDs)
                segmentEditorWidget = slicer.modules.segmenteditor.widgetRepresentation().self().editor
                segmentEditorNode = segmentEditorWidget.mrmlSegmentEditorNode()
                # store which segment was selected before operation
                selectedStartSegmentID = segmentEditorNode.GetSelectedSegmentID()
                if inputSegmentIDs.GetNumberOfValues() == 0:
                    logging.info("Margin operation skipped: there are no visible segments.")
                    return
                # select input segments one by one, process
                for index in range(inputSegmentIDs.GetNumberOfValues()):
                    segmentID = inputSegmentIDs.GetValue(index)
                    self.showStatusMessage(f'Processing {segmentationNode.GetSegmentation().GetSegment(segmentID).GetName()}...')
                    segmentEditorNode.SetSelectedSegmentID(segmentID)
                    self.processMargin()
                # restore segment selection
                segmentEditorNode.SetSelectedSegmentID(selectedStartSegmentID)
            else:
                self.processMargin()

        finally:
            qt.QApplication.restoreOverrideCursor()
    def convertSegmentation(self, segPath, volPath, binLabelOutPath):
        """This function will take in the path to a segmentation file,
    a nrrd volume file, and the desired output path for the binary labelmap.
    It will load the segmentation into slicer, find the segmentation id for
    the colon segment, and convert that segment into a binary labelmap.
    It will save it to the output path parameter. """
        success, segNode = slicer.util.loadSegmentation(segPath,
                                                        returnNode=True)
        segmentation = segNode.GetSegmentation()
        segID = segmentation.GetSegmentIdBySegmentName('colon')
        segment = segmentation.GetSegment(segID)

        labelMapNode = slicer.vtkMRMLLabelMapVolumeNode()
        slicer.mrmlScene.AddNode(labelMapNode)

        success, referenceNode = slicer.util.loadVolume(volPath,
                                                        returnNode=True)
        segToExport = vtk.vtkStringArray()
        segToExport.InsertNextValue(segID)
        slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(
            segNode, segToExport, labelMapNode, referenceNode)
        slicer.util.saveNode(labelMapNode, binLabelOutPath)
        logging.info("Saved: " + binLabelOutPath)
Example #39
0
def make_rect(coords):
    xmin, ymin, xmax, ymax = coords

    pts = vtk.vtkPoints()
    pts.InsertPoint(0, xmin, ymin, 0)
    pts.InsertPoint(1, xmax, ymin, 0)
    pts.InsertPoint(2, xmax, ymax, 0)
    pts.InsertPoint(3, xmin, ymax, 0)
    rect = vtk.vtkCellArray()
    rect.InsertNextCell(5)
    rect.InsertCellPoint(0)
    rect.InsertCellPoint(1)
    rect.InsertCellPoint(2)
    rect.InsertCellPoint(3)
    rect.InsertCellPoint(0)

    output = vtk.vtkPolyData()
    output.SetPoints(pts)
    output.SetLines(rect)

    labels = vtk.vtkStringArray()
    labels.InsertNextValue("one")
    labels.InsertNextValue("two")
    labels.InsertNextValue("three")
    labels.InsertNextValue("four")
    labels.SetName("labels")
    output.GetPointData().AddArray(labels)

    tenths = vtk.vtkTypeInt64Array()
    tenths.InsertNextValue(to_str("10abcdefgh"))
    tenths.InsertNextValue(to_str("20abcdefgh"))
    tenths.InsertNextValue(to_str("30abcdefgh"))
    tenths.InsertNextValue(to_str("40abcdefgh"))
    tenths.SetName("tenths")
    output.GetPointData().AddArray(tenths)

    return output
Example #40
0
    def testMultiTableOutputs(self):
        outputs = vtk.vtkStringArray()
        outputs.SetNumberOfComponents(1)
        outputs.SetNumberOfTuples(3)
        outputs.SetValue(0, "output1")
        outputs.SetValue(1, "output2")
        outputs.SetValue(2, "output3")
        rcal = vtk.vtkRCalculatorFilter()
        rcal.SetRscript("output1 = list(test=c(1,2,3,4))\n\
                         output2 = list(test=c(5,6,7,8))\n\
                         output3 = list(test=c(9,10,11,12))\n");

        rcal.GetTables(outputs)

        input = vtk.vtkTable()
        rcal.SetInputData(input)

        rcal.Update()


        t1 = rcal.GetOutput().GetPieceAsDataObject(0).GetColumnByName('test')
        value = 1

        for i in range(0, t1.GetNumberOfTuples()):
            self.assertEqual(value, t1.GetValue(i))
            value += 1

        t2  = rcal.GetOutput().GetPieceAsDataObject(1).GetColumnByName('test')
        for i in range(0, t2.GetNumberOfTuples()):
            self.assertEqual(value, t2.GetValue(i))
            value += 1

        t3  = rcal.GetOutput().GetPieceAsDataObject(2).GetColumnByName('test')
        for i in range(0, t3.GetNumberOfTuples()):
            self.assertEqual(value, t3.GetValue(i))
            value += 1
    def createMeshFromSegmentationTetGen(self,
                                         inputSegmentation,
                                         outputMeshNode,
                                         additionalParameters=""):

        visibleSegmentIds = vtk.vtkStringArray()
        inputSegmentation.GetDisplayNode().GetVisibleSegmentIDs(
            visibleSegmentIds)
        if visibleSegmentIds.GetNumberOfValues() == 0:
            logging.info(
                "createMeshFromSegmentationTetGen skipped: there are no visible segments"
            )
            return
        inputSegmentation.CreateClosedSurfaceRepresentation()
        appender = vtk.vtkAppendPolyData()
        for i in range(visibleSegmentIds.GetNumberOfValues()):
            segmentId = visibleSegmentIds.GetValue(i)
            polydata = inputSegmentation.GetClosedSurfaceRepresentation(
                segmentId)
            appender.AddInputData(polydata)

        appender.Update()
        self.createMeshFromPolyDataTetGen(appender.GetOutput(), outputMeshNode,
                                          additionalParameters)
    def testMultiTreeOutputs(self):
        outputs = vtk.vtkStringArray()
        outputs.SetNumberOfComponents(1)
        outputs.SetNumberOfTuples(2)
        outputs.SetValue(0, "tree1")
        outputs.SetValue(1, "tree2")
        rcal = vtk.vtkRCalculatorFilter()

        rcal.SetRscript("library(ape)\n\
                         tree1 = read.tree(text=\"" + tree_data + "\")\n\
                         tree2 = read.tree(text=\"" + tree_data + "\")\n")

        rcal.GetTrees(outputs)

        input = vtk.vtkTable()
        rcal.SetInputData(input)

        rcal.Update()

        compo = rcal.GetOutput()
        tree1 = compo.GetPieceAsDataObject(0)
        self.assertTrue(tree1.IsA('vtkTree'))
        tree2 = compo.GetPieceAsDataObject(1)
        self.assertTrue(tree2.IsA('vtkTree'))
    def computeStatistics(self):
        """Compute statistical measures for all (visible) segments"""
        self.reset()

        segmentationNode = slicer.mrmlScene.GetNodeByID(
            self.getParameterNode().GetParameter("Segmentation"))

        # Get segment ID list
        visibleSegmentIds = vtk.vtkStringArray()
        if self.getParameterNode().GetParameter(
                'visibleSegmentsOnly') == 'True':
            segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(
                visibleSegmentIds)
        else:
            segmentationNode.GetSegmentation().GetSegmentIDs(visibleSegmentIds)
        if visibleSegmentIds.GetNumberOfValues() == 0:
            logging.debug(
                "computeStatistics will not return any results: there are no visible segments"
            )

        # update statistics for all segment IDs
        for segmentIndex in range(visibleSegmentIds.GetNumberOfValues()):
            segmentID = visibleSegmentIds.GetValue(segmentIndex)
            self.updateStatisticsForSegment(segmentID)
Example #44
0
    def onApply(self):

        # Get list of visible segment IDs, as the effect ignores hidden segments.
        segmentationNode = self.scriptedEffect.parameterSetNode(
        ).GetSegmentationNode()
        visibleSegmentIds = vtk.vtkStringArray()
        segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(
            visibleSegmentIds)
        if visibleSegmentIds.GetNumberOfValues() == 0:
            logging.info(
                "Smoothing operation skipped: there are no visible segments")
            return

        # This can be a long operation - indicate it to the user
        qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)

        # Allow users revert to this state by clicking Undo
        self.scriptedEffect.saveStateForUndo()

        # Export source image data to temporary new volume node.
        # Note: Although the original source volume node is already in the scene, we do not use it here,
        # because the source volume may have been resampled to match segmentation geometry.
        sourceVolumeNode = slicer.vtkMRMLScalarVolumeNode()
        slicer.mrmlScene.AddNode(sourceVolumeNode)
        sourceVolumeNode.SetAndObserveTransformNodeID(
            segmentationNode.GetTransformNodeID())
        slicer.vtkSlicerSegmentationsModuleLogic.CopyOrientedImageDataToVolumeNode(
            self.scriptedEffect.sourceVolumeImageData(), sourceVolumeNode)
        # Generate merged labelmap of all visible segments, as the filter expects a single labelmap with all the labels.
        mergedLabelmapNode = slicer.vtkMRMLLabelMapVolumeNode()
        slicer.mrmlScene.AddNode(mergedLabelmapNode)
        slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(
            segmentationNode, visibleSegmentIds, mergedLabelmapNode,
            sourceVolumeNode)

        # Run segmentation algorithm
        import SimpleITK as sitk
        import sitkUtils
        # Read input data from Slicer into SimpleITK
        labelImage = sitk.ReadImage(
            sitkUtils.GetSlicerITKReadWriteAddress(
                mergedLabelmapNode.GetName()))
        backgroundImage = sitk.ReadImage(
            sitkUtils.GetSlicerITKReadWriteAddress(sourceVolumeNode.GetName()))
        # Run watershed filter
        featureImage = sitk.GradientMagnitudeRecursiveGaussian(
            backgroundImage,
            float(self.scriptedEffect.doubleParameter("ObjectScaleMm")))
        del backgroundImage
        f = sitk.MorphologicalWatershedFromMarkersImageFilter()
        f.SetMarkWatershedLine(False)
        f.SetFullyConnected(False)
        labelImage = f.Execute(featureImage, labelImage)
        del featureImage
        # Pixel type of watershed output is the same as the input. Convert it to int16 now.
        if labelImage.GetPixelID() != sitk.sitkInt16:
            labelImage = sitk.Cast(labelImage, sitk.sitkInt16)
        # Write result from SimpleITK to Slicer. This currently performs a deep copy of the bulk data.
        sitk.WriteImage(
            labelImage,
            sitkUtils.GetSlicerITKReadWriteAddress(
                mergedLabelmapNode.GetName()))
        mergedLabelmapNode.GetImageData().Modified()
        mergedLabelmapNode.Modified()

        # Update segmentation from labelmap node and remove temporary nodes
        slicer.vtkSlicerSegmentationsModuleLogic.ImportLabelmapToSegmentationNode(
            mergedLabelmapNode, segmentationNode, visibleSegmentIds)
        slicer.mrmlScene.RemoveNode(sourceVolumeNode)
        slicer.mrmlScene.RemoveNode(mergedLabelmapNode)

        qt.QApplication.restoreOverrideCursor()
Example #45
0
    def smoothMultipleSegments(self, maskImage=None, maskExtent=None):
        import vtkSegmentationCorePython as vtkSegmentationCore

        self.showStatusMessage(f'Joint smoothing ...')
        # Generate merged labelmap of all visible segments
        segmentationNode = self.scriptedEffect.parameterSetNode(
        ).GetSegmentationNode()
        visibleSegmentIds = vtk.vtkStringArray()
        segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(
            visibleSegmentIds)
        if visibleSegmentIds.GetNumberOfValues() == 0:
            logging.info(
                "Smoothing operation skipped: there are no visible segments")
            return

        mergedImage = slicer.vtkOrientedImageData()
        if not segmentationNode.GenerateMergedLabelmapForAllSegments(
                mergedImage, vtkSegmentationCore.vtkSegmentation.
                EXTENT_UNION_OF_SEGMENTS_PADDED, None, visibleSegmentIds):
            logging.error(
                'Failed to apply smoothing: cannot get list of visible segments'
            )
            return

        segmentLabelValues = []  # list of [segmentId, labelValue]
        for i in range(visibleSegmentIds.GetNumberOfValues()):
            segmentId = visibleSegmentIds.GetValue(i)
            segmentLabelValues.append([segmentId, i + 1])

        # Perform smoothing in voxel space
        ici = vtk.vtkImageChangeInformation()
        ici.SetInputData(mergedImage)
        ici.SetOutputSpacing(1, 1, 1)
        ici.SetOutputOrigin(0, 0, 0)

        # Convert labelmap to combined polydata
        # vtkDiscreteFlyingEdges3D cannot be used here, as in the output of that filter,
        # each labeled region is completely disconnected from neighboring regions, and
        # for joint smoothing it is essential for the points to move together.
        convertToPolyData = vtk.vtkDiscreteMarchingCubes()
        convertToPolyData.SetInputConnection(ici.GetOutputPort())
        convertToPolyData.SetNumberOfContours(len(segmentLabelValues))

        contourIndex = 0
        for segmentId, labelValue in segmentLabelValues:
            convertToPolyData.SetValue(contourIndex, labelValue)
            contourIndex += 1

        # Low-pass filtering using Taubin's method
        smoothingFactor = self.scriptedEffect.doubleParameter(
            "JointTaubinSmoothingFactor")
        smoothingIterations = 100  # according to VTK documentation 10-20 iterations could be enough but we use a higher value to reduce chance of shrinking
        passBand = pow(
            10.0, -4.0 * smoothingFactor
        )  # gives a nice range of 1-0.0001 from a user input of 0-1
        smoother = vtk.vtkWindowedSincPolyDataFilter()
        smoother.SetInputConnection(convertToPolyData.GetOutputPort())
        smoother.SetNumberOfIterations(smoothingIterations)
        smoother.BoundarySmoothingOff()
        smoother.FeatureEdgeSmoothingOff()
        smoother.SetFeatureAngle(90.0)
        smoother.SetPassBand(passBand)
        smoother.NonManifoldSmoothingOn()
        smoother.NormalizeCoordinatesOn()

        # Extract a label
        threshold = vtk.vtkThreshold()
        threshold.SetInputConnection(smoother.GetOutputPort())

        # Convert to polydata
        geometryFilter = vtk.vtkGeometryFilter()
        geometryFilter.SetInputConnection(threshold.GetOutputPort())

        # Convert polydata to stencil
        polyDataToImageStencil = vtk.vtkPolyDataToImageStencil()
        polyDataToImageStencil.SetInputConnection(
            geometryFilter.GetOutputPort())
        polyDataToImageStencil.SetOutputSpacing(1, 1, 1)
        polyDataToImageStencil.SetOutputOrigin(0, 0, 0)
        polyDataToImageStencil.SetOutputWholeExtent(mergedImage.GetExtent())

        # Convert stencil to image
        stencil = vtk.vtkImageStencil()
        emptyBinaryLabelMap = vtk.vtkImageData()
        emptyBinaryLabelMap.SetExtent(mergedImage.GetExtent())
        emptyBinaryLabelMap.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
        vtkSegmentationCore.vtkOrientedImageDataResample.FillImage(
            emptyBinaryLabelMap, 0)
        stencil.SetInputData(emptyBinaryLabelMap)
        stencil.SetStencilConnection(polyDataToImageStencil.GetOutputPort())
        stencil.ReverseStencilOn()
        stencil.SetBackgroundValue(
            1
        )  # General foreground value is 1 (background value because of reverse stencil)

        imageToWorldMatrix = vtk.vtkMatrix4x4()
        mergedImage.GetImageToWorldMatrix(imageToWorldMatrix)

        # TODO: Temporarily setting the overwrite mode to OverwriteVisibleSegments is an approach that should be change once additional
        # layer control options have been implemented. Users may wish to keep segments on separate layers, and not allow them to be separated/merged automatically.
        # This effect could leverage those options once they have been implemented.
        oldOverwriteMode = self.scriptedEffect.parameterSetNode(
        ).GetOverwriteMode()
        self.scriptedEffect.parameterSetNode().SetOverwriteMode(
            slicer.vtkMRMLSegmentEditorNode.OverwriteVisibleSegments)
        for segmentId, labelValue in segmentLabelValues:
            threshold.ThresholdBetween(labelValue, labelValue)
            stencil.Update()
            smoothedBinaryLabelMap = slicer.vtkOrientedImageData()
            smoothedBinaryLabelMap.ShallowCopy(stencil.GetOutput())
            smoothedBinaryLabelMap.SetImageToWorldMatrix(imageToWorldMatrix)
            self.scriptedEffect.modifySegmentByLabelmap(
                segmentationNode, segmentId, smoothedBinaryLabelMap,
                slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet,
                False)
        self.scriptedEffect.parameterSetNode().SetOverwriteMode(
            oldOverwriteMode)
Example #46
0
polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
polydata.SetPolys(polygons)

polydata.GetCellData().SetScalars(numbers)

# write an XML file
# writer = vtk.vtkXMLPolyDataWriter()
# writer.SetFileName('sample.vtp')
# writer.SetInputData(polydata)
# writer.Write()

## ------------------- ##
# create a data field
df = vtk.vtkFieldData()
temperature = vtk.vtkStringArray()
temperature.SetName("Colors")
temperature.InsertNextValue("Red")

df.AddArray(temperature)

# add data field to polydata
polydata.SetFieldData(df)
## ------------------- ##

polydata.Modified()

# Write a vtk file
writer = vtk.vtkPolyDataWriter()
writer.SetFileName('sample.vtk')
writer.SetInputData(polydata)
Example #47
0
    def processInteractionEvents(self, callerInteractor, eventId, viewWidget):
        import vtkSegmentationCorePython as vtkSegmentationCore

        abortEvent = False

        # Only allow in modes where segment selection is needed
        if not self.currentOperationRequiresSegmentSelection():
            return False

        # Only allow for slice views
        if viewWidget.className() != "qMRMLSliceWidget":
            return abortEvent

        if eventId != vtk.vtkCommand.LeftButtonPressEvent:
            return abortEvent

        abortEvent = True

        # Generate merged labelmap of all visible segments
        segmentationNode = self.scriptedEffect.parameterSetNode(
        ).GetSegmentationNode()
        visibleSegmentIds = vtk.vtkStringArray()
        segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(
            visibleSegmentIds)
        if visibleSegmentIds.GetNumberOfValues() == 0:
            logging.info(
                "Smoothing operation skipped: there are no visible segments")
            return abortEvent

        self.scriptedEffect.saveStateForUndo()

        # This can be a long operation - indicate it to the user
        qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)

        operationName = self.scriptedEffect.parameter("Operation")

        if operationName == ADD_SELECTED_ISLAND:
            inputLabelImage = vtkSegmentationCore.vtkOrientedImageData()
            if not segmentationNode.GenerateMergedLabelmapForAllSegments(
                    inputLabelImage, vtkSegmentationCore.vtkSegmentation.
                    EXTENT_UNION_OF_SEGMENTS_PADDED, None, visibleSegmentIds):
                logging.error(
                    'Failed to apply smoothing: cannot get list of visible segments'
                )
                qt.QApplication.restoreOverrideCursor()
                return abortEvent
        else:
            selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap(
            )
            # We need to know exactly the value of the segment voxels, apply threshold to make force the selected label value
            labelValue = 1
            backgroundValue = 0
            thresh = vtk.vtkImageThreshold()
            thresh.SetInputData(selectedSegmentLabelmap)
            thresh.ThresholdByLower(0)
            thresh.SetInValue(backgroundValue)
            thresh.SetOutValue(labelValue)
            thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
            thresh.Update()
            # Create oriented image data from output
            import vtkSegmentationCorePython as vtkSegmentationCore
            inputLabelImage = vtkSegmentationCore.vtkOrientedImageData()
            inputLabelImage.ShallowCopy(thresh.GetOutput())
            selectedSegmentLabelmapImageToWorldMatrix = vtk.vtkMatrix4x4()
            selectedSegmentLabelmap.GetImageToWorldMatrix(
                selectedSegmentLabelmapImageToWorldMatrix)
            inputLabelImage.SetImageToWorldMatrix(
                selectedSegmentLabelmapImageToWorldMatrix)

        xy = callerInteractor.GetEventPosition()
        ijk = self.xyToIjk(xy, viewWidget, inputLabelImage)
        pixelValue = inputLabelImage.GetScalarComponentAsFloat(
            ijk[0], ijk[1], ijk[2], 0)

        try:

            floodFillingFilter = vtk.vtkImageThresholdConnectivity()
            floodFillingFilter.SetInputData(inputLabelImage)
            seedPoints = vtk.vtkPoints()
            origin = inputLabelImage.GetOrigin()
            spacing = inputLabelImage.GetSpacing()
            seedPoints.InsertNextPoint(origin[0] + ijk[0] * spacing[0],
                                       origin[1] + ijk[1] * spacing[1],
                                       origin[2] + ijk[2] * spacing[2])
            floodFillingFilter.SetSeedPoints(seedPoints)
            floodFillingFilter.ThresholdBetween(pixelValue, pixelValue)

            if operationName == ADD_SELECTED_ISLAND:
                floodFillingFilter.SetInValue(1)
                floodFillingFilter.SetOutValue(0)
                floodFillingFilter.Update()
                modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap(
                )
                modifierLabelmap.DeepCopy(floodFillingFilter.GetOutput())
                self.scriptedEffect.modifySelectedSegmentByLabelmap(
                    modifierLabelmap, slicer.
                    qSlicerSegmentEditorAbstractEffect.ModificationModeAdd)

            elif pixelValue != 0:  # if clicked on empty part then there is nothing to remove or keep

                if operationName == KEEP_SELECTED_ISLAND:
                    floodFillingFilter.SetInValue(1)
                    floodFillingFilter.SetOutValue(0)
                else:  # operationName == REMOVE_SELECTED_ISLAND:
                    floodFillingFilter.SetInValue(1)
                    floodFillingFilter.SetOutValue(0)

                floodFillingFilter.Update()
                modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap(
                )
                modifierLabelmap.DeepCopy(floodFillingFilter.GetOutput())

                if operationName == KEEP_SELECTED_ISLAND:
                    self.scriptedEffect.modifySelectedSegmentByLabelmap(
                        modifierLabelmap, slicer.
                        qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
                else:  # operationName == REMOVE_SELECTED_ISLAND:
                    self.scriptedEffect.modifySelectedSegmentByLabelmap(
                        modifierLabelmap,
                        slicer.qSlicerSegmentEditorAbstractEffect.
                        ModificationModeRemove)

        except IndexError:
            logging.error('apply: Failed to threshold master volume!')
        finally:
            qt.QApplication.restoreOverrideCursor()

        return abortEvent
Example #48
0
# write to the temp directory if possible, otherwise use .
#
dir = "."
if (info.commands(globals(), locals(), "rtTester") == "rtTester"):
    dir = rtTester.GetTempDirectory()
    pass
# create some random points in a sphere
#
sphere1 = vtk.vtkPointSource()
sphere1.SetNumberOfPoints(13)
xform = vtk.vtkTransform()
xform.RotateWXYZ(20, 1, 0, 0)
xformFilter = vtk.vtkTransformFilter()
xformFilter.SetTransform(xform)
xformFilter.SetInputConnection(sphere1.GetOutputPort())
labels = vtk.vtkStringArray()
labels.InsertNextValue("0")
labels.InsertNextValue("1")
labels.InsertNextValue("2")
labels.InsertNextValue("3")
labels.InsertNextValue("Halifax")
labels.InsertNextValue("Toronto")
labels.InsertNextValue("Vancouver")
labels.InsertNextValue("Larry")
labels.InsertNextValue("Bob")
labels.InsertNextValue("Jackie")
labels.InsertNextValue("10")
labels.InsertNextValue("11")
labels.InsertNextValue("12")
weights = vtk.vtkDoubleArray()
weights.InsertNextValue(1.0)
Example #49
0
    def onApply(self):
        inputVolume = self.getInputVolume()
        segmentID = self.scriptedEffect.parameterSetNode(
        ).GetSelectedSegmentID()
        segmentationNode = self.scriptedEffect.parameterSetNode(
        ).GetSegmentationNode()
        volumesLogic = slicer.modules.volumes.logic()
        scene = inputVolume.GetScene()
        padExtent = [
            -self.pad.value, self.pad.value, -self.pad.value, self.pad.value,
            -self.pad.value, self.pad.value
        ]
        #iterate over segments
        for segmentIndex in range(
                segmentationNode.GetSegmentation().GetNumberOfSegments()):
            segmentID = segmentationNode.GetSegmentation().GetNthSegmentID(
                segmentIndex)
            segmentIDs = vtk.vtkStringArray()
            segmentIDs.InsertNextValue(segmentID)

            # create volume for output
            outputVolumeName = inputVolume.GetName() + '_' + segmentID
            outputVolume = volumesLogic.CloneVolumeGeneric(
                scene, inputVolume, outputVolumeName, False)

            # crop segment
            slicer.app.setOverrideCursor(qt.Qt.WaitCursor)
            import SegmentEditorMaskVolumeLib
            SegmentEditorMaskVolumeLib.SegmentEditorEffect.maskVolumeWithSegment(
                self, segmentationNode, segmentID, "FILL_OUTSIDE", [0],
                inputVolume, outputVolume)

            #calculate extent of masked image
            pt = [-16.87524999999998, 19.68725000000002, 16.80000000000001, 1]
            rasToIjk = vtk.vtkMatrix4x4()
            outputVolume.GetRASToIJKMatrix(rasToIjk)
            print(rasToIjk.MultiplyPoint(pt))

            ijkToRas = vtk.vtkMatrix4x4()
            outputVolume.GetIJKToRASMatrix(ijkToRas)
            print(ijkToRas.MultiplyPoint(pt))

            cropThreshold = 0
            img = slicer.modules.segmentations.logic(
            ).CreateOrientedImageDataFromVolumeNode(outputVolume)
            img.UnRegister(None)
            extent = [0, 0, 0, 0, 0, 0]
            vtkSegmentationCore.vtkOrientedImageDataResample.CalculateEffectiveExtent(
                img, extent, cropThreshold)

            # pad and crop
            cropFilter = vtk.vtkImageConstantPad()
            cropFilter.SetInputData(outputVolume.GetImageData())
            cropFilter.SetConstant(self.fillValue.value)
            cropFilter.SetOutputWholeExtent(extent)
            cropFilter.Update()

            padFilter = vtk.vtkImageConstantPad()
            padFilter.SetInputData(cropFilter.GetOutput())
            padFilter.SetConstant(self.fillValue.value)
            for i in range(len(extent)):
                extent[i] = extent[i] + padExtent[i]

            padFilter.SetOutputWholeExtent(extent)
            padFilter.Update()
            outputVolume.SetAndObserveImageData(padFilter.GetOutput())

            qt.QApplication.restoreOverrideCursor()
Example #50
0
import os
import numpy as np
import natsort
import vtk

import sys

dataPath = sys.argv[1]
outputfile = sys.argv[2]
for dirName, subDir, fileList in os.walk(dataPath):
    pass
index = natsort.index_natsorted(fileList)
fileList = natsort.order_by_index(fileList, index)

stringArray = vtk.vtkStringArray()
for i, fileName in enumerate(fileList):
    stringArray.InsertNextValue(fileName)

reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName(dataPath)
reader.SetFileNames(stringArray)
writer = vtk.vtkMetaImageWriter()
writer.SetInputConnection(reader.GetOutputPort())
writer.SetFileName(outputfile + '.mhd')
writer.Write()
Example #51
0
def to_vtk(bdf):
    # type: (BDF) -> VTKData
    import vtk
    # TODO: use pynastran numpy_to_vtk
    from vtk.util.numpy_support import numpy_to_vtk

    nid_list = []
    nid_dict = {}

    node_pos = np.empty((len(bdf.nodes), 3), dtype=np.float64)

    i = 0
    for node in itervalues(bdf.nodes):
        node_pos[i] = node.get_position()
        nid = node.nid
        nid_list.append(nid)
        nid_dict[nid] = i
        i += 1

    _points = vtk.vtkPoints()
    _points.SetData(numpy_to_vtk(node_pos))

    points = vtk.vtkPoints()
    points.DeepCopy(_points)

    cells = []
    cell_types = []
    cell_count = 0
    elem_types = []

    eid_list = []
    eid_dict = {}

    _nastran_to_vtk = nastran_to_vtk

    bdf_data_to_plot = chain(itervalues(bdf.nodes), itervalues(bdf.elements),
                             itervalues(bdf.rigid_elements))

    category_list = []

    for elem in bdf_data_to_plot:
        elem_type = elem.type
        cell_type, add_method, category = _nastran_to_vtk.get(
            elem_type, (None, None, None))

        if cell_type is None:
            continue

        cell_types.append(cell_type)
        elem_types.append(elem_type)

        eid = add_method(elem, cells, nid_dict)  # returns element/grid id

        eid_list.append(eid)
        eid_dict[eid] = cell_count

        category_list.append(categories[category])

        cell_count += 1

    cells = np.array(cells, dtype=np.int64)

    id_array = vtk.vtkIdTypeArray()
    id_array.SetVoidArray(cells, len(cells), 1)

    vtk_cells = vtk.vtkCellArray()
    vtk_cells.SetCells(cell_count, id_array)

    cell_types = np.array(cell_types, 'B')
    vtk_cell_types = numpy_to_vtk(cell_types)

    cell_locations = np.array([i for i in range(cell_count)])

    vtk_cell_locations = numpy_to_vtk(cell_locations,
                                      deep=1,
                                      array_type=vtk.VTK_ID_TYPE)

    ugrid = vtk.vtkUnstructuredGrid()
    ugrid.SetPoints(points)

    ugrid.SetCells(vtk_cell_types, vtk_cell_locations, vtk_cells)

    vtk_cell_locations.SetName('index')
    ugrid.GetCellData().AddArray(vtk_cell_locations)

    _elem_types = vtk.vtkStringArray()
    _elem_types.SetName('element_type')
    _elem_types.SetNumberOfValues(len(elem_types))

    for i in range(len(elem_types)):
        _elem_types.SetValue(i, elem_types[i])

    ugrid.GetCellData().AddArray(_elem_types)

    _cat_arr = np.array(category_list, dtype=np.int64)
    _cat = vtk.vtkIdTypeArray()
    _cat.SetNumberOfValues(len(category_list))
    _cat.SetVoidArray(_cat_arr, len(_cat_arr), 1)
    _cat.SetName('category')

    ugrid.GetCellData().AddArray(_cat)

    id_array = numpy_to_vtk(np.array(eid_list),
                            deep=1,
                            array_type=vtk.VTK_ID_TYPE)

    # id_array = vtk.vtkIdTypeArray()
    # id_array.SetVoidArray(eid_list, len(eid_list), 1)
    id_array.SetName('element_id')

    ugrid.GetCellData().AddArray(id_array)

    copy = vtk.vtkUnstructuredGrid()
    copy.DeepCopy(ugrid)

    vtk_data = VTKData(copy, nid_list, nid_dict, eid_list, eid_dict)

    return vtk_data
Example #52
0
    def Execute(self):
 
        if not self.Network:
            self.Network = vtk.vtkPolyData()
            networkPoints = vtk.vtkPoints()
            networkLines = vtk.vtkCellArray()
            radiusArray = vtk.vtkDoubleArray()
            radiusArray.SetName(self.RadiusArrayName)
            self.Network.SetPoints(networkPoints)
            self.Network.SetLines(networkLines)
            self.Network.GetPointData().AddArray(radiusArray)
        
        if not self.vmtkRenderer:
            self.vmtkRenderer = vmtkrenderer.vmtkRenderer()
            self.vmtkRenderer.Initialize()
            self.OwnRenderer = 1

        self.vmtkRenderer.ExitAfterTextInputMode = False
        self.vmtkRenderer.RegisterScript(self) 

        if self.Image and (not self.PlaneWidgetX or not self.PlaneWidgetY or not self.PlaneWidgetZ):
            imageViewer = vmtkimageviewer.vmtkImageViewer()
            imageViewer.Image = self.Image
            imageViewer.vmtkRenderer = self.vmtkRenderer
            imageViewer.Display = 0
            imageViewer.Execute()
            self.PlaneWidgetX = imageViewer.PlaneWidgetX
            self.PlaneWidgetY = imageViewer.PlaneWidgetY
            self.PlaneWidgetZ = imageViewer.PlaneWidgetZ

        if self.Image:
            spacing = self.Image.GetSpacing()
            self.CurrentRadius = min(spacing)

        if self.UseActiveTubes and not self.FeatureImage:
            imageFeatures = vmtkimagefeatures.vmtkImageFeatures()
            imageFeatures.Image = self.Image
            imageFeatures.FeatureImageType = 'vtkgradient'
            imageFeatures.Execute()
            self.FeatureImage = imageFeatures.FeatureImage
 
        self.NetworkRadiusArray = self.Network.GetPointData().GetArray(self.RadiusArrayName)

        self.Network.GetPointData().SetActiveScalars(self.RadiusArrayName)

        networkMapper = vtk.vtkPolyDataMapper()
        networkMapper.SetInputData(self.Network)
        networkMapper.SetScalarModeToUseCellData()

        self.NetworkActor = vtk.vtkActor()
        self.NetworkActor.SetMapper(networkMapper)
        self.vmtkRenderer.Renderer.AddActor(self.NetworkActor)

        self.NetworkTube = vtk.vtkTubeFilter()
        self.NetworkTube.SetInputData(self.Network)
        self.NetworkTube.SetVaryRadiusToVaryRadiusByAbsoluteScalar()
        self.NetworkTube.SetNumberOfSides(20)
        networkTubeMapper = vtk.vtkPolyDataMapper()
        networkTubeMapper.SetInputConnection(self.NetworkTube.GetOutputPort())
        networkTubeMapper.ScalarVisibilityOff()
        networkTubeActor = vtk.vtkActor()
        networkTubeActor.SetMapper(networkTubeMapper)
        networkTubeActor.PickableOff()
        networkTubeActor.GetProperty().SetOpacity(0.2)
        self.vmtkRenderer.Renderer.AddActor(networkTubeActor)

        self.Selection = vtk.vtkPolyData()
        self.SelectionPoints = vtk.vtkPoints()
        self.SelectionRadiusArray = vtk.vtkDoubleArray()
        self.SelectionRadiusArray.SetName(self.RadiusArrayName)
        self.Selection.SetPoints(self.SelectionPoints)
        self.Selection.GetPointData().AddArray(self.SelectionRadiusArray)
        self.Selection.GetPointData().SetActiveScalars(self.RadiusArrayName)

        glyphs = vtk.vtkGlyph3D()
        glyphSource = vtk.vtkSphereSource()
        glyphSource.SetRadius(1.0)
        glyphSource.SetThetaResolution(20)
        glyphSource.SetPhiResolution(20)
        glyphs.SetInputData(self.Selection)
        glyphs.SetSourceConnection(glyphSource.GetOutputPort())
        glyphs.SetScaleModeToScaleByScalar()
        glyphs.SetScaleFactor(1.0)

        selectionMapper = vtk.vtkPolyDataMapper()
        selectionMapper.SetInputConnection(glyphs.GetOutputPort())

        self.SelectionActor = vtk.vtkActor()
        self.SelectionActor.SetMapper(selectionMapper)
        self.SelectionActor.GetProperty().SetColor(1.0,0.0,0.0)
        self.SelectionActor.GetProperty().SetOpacity(0.5)
        self.SelectionActor.PickableOff()
        self.vmtkRenderer.Renderer.AddActor(self.SelectionActor)

        self.ActiveSegmentSeeds = vtk.vtkPolyData()
        self.ActiveSegmentSeedsPoints = vtk.vtkPoints()
        self.ActiveSegmentSeedsRadiusArray = vtk.vtkDoubleArray()
        self.ActiveSegmentSeedsRadiusArray.SetName(self.RadiusArrayName)
        self.ActiveSegmentSeeds.SetPoints(self.ActiveSegmentSeedsPoints)
        self.ActiveSegmentSeeds.GetPointData().AddArray(self.ActiveSegmentSeedsRadiusArray)
        self.ActiveSegmentSeeds.GetPointData().SetActiveScalars(self.RadiusArrayName)

        activeSegmentSeedsGlyphs = vtk.vtkGlyph3D()
        activeSegmentSeedsGlyphSource = vtk.vtkSphereSource()
        activeSegmentSeedsGlyphSource.SetRadius(1.0)
        activeSegmentSeedsGlyphSource.SetThetaResolution(20)
        activeSegmentSeedsGlyphSource.SetPhiResolution(20)
        activeSegmentSeedsGlyphs.SetInputData(self.ActiveSegmentSeeds)
        activeSegmentSeedsGlyphs.SetSourceConnection(activeSegmentSeedsGlyphSource.GetOutputPort())
        activeSegmentSeedsGlyphs.SetScaleModeToScaleByScalar()
        activeSegmentSeedsGlyphs.SetScaleFactor(1.0)

        activeSegmentSeedsMapper = vtk.vtkPolyDataMapper()
        activeSegmentSeedsMapper.SetInputConnection(activeSegmentSeedsGlyphs.GetOutputPort())
        activeSegmentSeedsMapper.ScalarVisibilityOff()

        self.ActiveSegmentSeedsActor = vtk.vtkActor()
        self.ActiveSegmentSeedsActor.SetMapper(activeSegmentSeedsMapper)
        self.ActiveSegmentSeedsActor.GetProperty().SetColor(1.0,0.0,0.0)
        self.ActiveSegmentSeedsActor.GetProperty().SetOpacity(0.5)
        self.ActiveSegmentSeedsActor.PickableOff()
        self.vmtkRenderer.Renderer.AddActor(self.ActiveSegmentSeedsActor)

        self.ActiveSegment = vtk.vtkPolyData()
        self.ActiveSegmentPoints = vtk.vtkPoints()
        self.ActiveSegmentCellArray = vtk.vtkCellArray()
        self.ActiveSegmentRadiusArray = vtk.vtkDoubleArray()
        self.ActiveSegmentRadiusArray.SetName(self.RadiusArrayName)
        self.ActiveSegment.SetPoints(self.ActiveSegmentPoints)
        self.ActiveSegment.SetLines(self.ActiveSegmentCellArray)
        self.ActiveSegment.GetPointData().AddArray(self.ActiveSegmentRadiusArray)
        self.ActiveSegment.GetPointData().SetActiveScalars(self.RadiusArrayName)

        activeSegmentMapper = vtk.vtkPolyDataMapper()
        activeSegmentMapper.ScalarVisibilityOff()
        if self.SplineInterpolation and self.Image != None:
            splineFilter = vtk.vtkSplineFilter()
            splineFilter.SetInputData(self.ActiveSegment)
            splineFilter.SetSubdivideToLength()
            splineFilter.SetLength(2.0*min(self.Image.GetSpacing()))
            activeSegmentMapper.SetInputConnection(splineFilter.GetOutputPort())
        else:
            activeSegmentMapper.SetInputData(self.ActiveSegment)

        self.ActiveSegmentActor = vtk.vtkActor()
        self.ActiveSegmentActor.SetMapper(activeSegmentMapper)
        self.ActiveSegmentActor.GetProperty().SetColor(1.0,1.0,1.0)
        self.ActiveSegmentActor.GetProperty().SetLineWidth(3.0)
        self.ActiveSegmentActor.PickableOff()
        self.vmtkRenderer.Renderer.AddActor(self.ActiveSegmentActor)

        activeTube = vtk.vtkTubeFilter()
        activeTube.SetInputConnection(activeSegmentMapper.GetInputPort())
        activeTube.SetVaryRadiusToVaryRadiusByAbsoluteScalar()
        activeTube.SetNumberOfSides(20)
        activeTubeMapper = vtk.vtkPolyDataMapper()
        activeTubeMapper.SetInputConnection(activeTube.GetOutputPort())
        activeTubeMapper.ScalarVisibilityOff()
        activeTubeActor = vtk.vtkActor()
        activeTubeActor.SetMapper(activeTubeMapper)
        activeTubeActor.PickableOff()
        activeTubeActor.GetProperty().SetOpacity(0.6)
        self.vmtkRenderer.Renderer.AddActor(activeTubeActor)

        self.NetworkLabelsArray = vtk.vtkStringArray.SafeDownCast(self.Network.GetCellData().GetAbstractArray(self.LabelsArrayName))
        if not self.NetworkLabelsArray:
            self.NetworkLabelsArray = vtk.vtkStringArray()
            self.NetworkLabelsArray.SetName(self.LabelsArrayName)
            self.NetworkLabelsArray.SetNumberOfValues(self.Network.GetNumberOfCells())
            for i in range(self.Network.GetNumberOfCells()):
                self.NetworkLabelsArray.SetValue(i,'')
            self.Network.GetCellData().AddArray(self.NetworkLabelsArray)
        self.CellCenters = vtk.vtkCellCenters()
        self.CellCenters.SetInputData(self.Network)
        self.CellCenters.VertexCellsOff()
        self.CellCenters.Update()

        labeledMapper = vtk.vtkLabeledDataMapper()
        labeledMapper.SetInputConnection(self.CellCenters.GetOutputPort())
        labeledMapper.SetLabelModeToLabelFieldData()
        labeledMapper.SetFieldDataName(self.LabelsArrayName)
        labeledMapper.GetLabelTextProperty().SetFontFamilyToArial()
        labeledMapper.GetLabelTextProperty().BoldOff()
        labeledMapper.GetLabelTextProperty().ItalicOff()
        labeledMapper.GetLabelTextProperty().ShadowOff()
        
        self.LabelsActor = vtk.vtkActor2D()
        self.LabelsActor.SetMapper(labeledMapper)
        self.LabelsActor.VisibilityOff()
        self.vmtkRenderer.Renderer.AddActor(self.LabelsActor)

        self.CellPicker = vtk.vtkCellPicker()
        self.CellPicker.SetTolerance(1E-2)
        self.CellPicker.InitializePickList()
        self.CellPicker.AddPickList(self.NetworkActor)
        self.CellPicker.PickFromListOn()

        self.vmtkRenderer.AddKeyBinding('a','Add mode.',self.AddCallback)
        self.vmtkRenderer.AddKeyBinding('d','Delete mode.',self.DeleteCallback)
        self.vmtkRenderer.AddKeyBinding('m','Merge mode.',self.MergeCallback)
        self.vmtkRenderer.AddKeyBinding('s','Split mode.',self.SplitCallback)
        self.vmtkRenderer.AddKeyBinding('l','Label mode.',self.LabelCallback)
        self.vmtkRenderer.AddKeyBinding('Tab','Show labels.',self.ShowLabelCallback)
        self.vmtkRenderer.RenderWindowInteractor.AddObserver("KeyReleaseEvent", self.KeyReleaseCallback)
        self.vmtkRenderer.RenderWindowInteractor.AddObserver("LeftButtonPressEvent", self.LeftButtonPressCallback)
        self.vmtkRenderer.RenderWindowInteractor.AddObserver("MouseMoveEvent", self.MouseMoveCallback)

        if self.PlaneWidgetX:
            self.PlaneWidgetX.UseContinuousCursorOn()
            self.PlaneWidgetX.AddObserver("StartInteractionEvent", self.PlaneStartInteractionCallback)
        if self.PlaneWidgetY:
            self.PlaneWidgetY.UseContinuousCursorOn()
            self.PlaneWidgetY.AddObserver("StartInteractionEvent", self.PlaneStartInteractionCallback)
        if self.PlaneWidgetZ:
            self.PlaneWidgetZ.UseContinuousCursorOn()
            self.PlaneWidgetZ.AddObserver("StartInteractionEvent", self.PlaneStartInteractionCallback)

        self.FirstRender()

        self.Surface = self.NetworkTube.GetOutput()

        if self.OwnRenderer:
            self.vmtkRenderer.Deallocate()
Example #53
0
    def run(self,
            inputVolume,
            directory,
            exportlabelmaps=True,
            exportOBJ=True,
            medianFilter=True,
            exportDICOM=True,
            showResult=True):
        """
    Run the processing algorithm.
    Can be used without GUI widget.
    :param inputVolume: volume to be segmented
    :param exportOBJ: export to OBJ files
    :param exportlabelmaps: export to labelmaps
    :param medianFilter: Whether the medial filter is used for improving the segmentation
    :param exportDICOM: Whether the isotropic volume is exported as a DICOM series
    :param showResult: show output volume in slice viewers
    """

        if not inputVolume:
            raise ValueError("Input volume is invalid")

        logging.info('Processing started')

        # Perform the temporal bone autosegmentation
        InputVolume: inputVolume.GetID()
        spacing = inputVolume.GetSpacing()[2]
        # resample
        if spacing <= 0.25:
            parameters = {
                "outputPixelSpacing": "0.25,0.25,0.25",
                "InputVolume": inputVolume,
                "interpolationType": 'linear',
                "OutputVolume": inputVolume
            }
            slicer.cli.runSync(slicer.modules.resamplescalarvolume, None,
                               parameters)
        else:
            parameters = {
                "outputPixelSpacing": "0.25,0.25,0.25",
                "InputVolume": inputVolume,
                "interpolationType": 'bspline',
                "OutputVolume": inputVolume
            }
            slicer.cli.runSync(slicer.modules.resamplescalarvolume, None,
                               parameters)
        print('\nResampling...\n')
        spacing = inputVolume.GetSpacing()[2]
        volumeName = inputVolume.GetName()
        # Create segmentation
        segmentationNode = slicer.mrmlScene.AddNewNodeByClass(
            "vtkMRMLSegmentationNode")
        segmentationNode.CreateDefaultDisplayNodes()  # only needed for display
        segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(
            inputVolume)
        otic_capsule_segment = segmentationNode.GetSegmentation(
        ).AddEmptySegment("otic_capsule")
        # Create segment editor to get access to effects
        segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
        segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
        segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass(
            "vtkMRMLSegmentEditorNode")
        segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
        segmentEditorWidget.setSegmentationNode(segmentationNode)
        segmentEditorWidget.setMasterVolumeNode(inputVolume)

        segmentationNode1 = slicer.mrmlScene.AddNewNodeByClass(
            "vtkMRMLSegmentationNode")
        segmentationNode1.CreateDefaultDisplayNodes(
        )  # only needed for display
        segmentationNode1.SetReferenceImageGeometryParameterFromVolumeNode(
            inputVolume)
        # Create segment editor to get access to effects
        segmentEditorWidget1 = slicer.qMRMLSegmentEditorWidget()
        segmentEditorWidget1.setMRMLScene(slicer.mrmlScene)
        segmentEditorNode1 = slicer.mrmlScene.AddNewNodeByClass(
            "vtkMRMLSegmentEditorNode")
        segmentEditorWidget1.setMRMLSegmentEditorNode(segmentEditorNode1)
        segmentEditorWidget1.setSegmentationNode(segmentationNode1)
        segmentEditorWidget1.setMasterVolumeNode(inputVolume)

        ## Autosegmentation of the otic capsule
        # NVIDIA auto segmentation
        segmentEditorWidget.setActiveEffectByName("Nvidia AIAA")
        effect = segmentEditorWidget.activeEffect()
        serverUrl = "http://tbone.onthewifi.com:956/v1/models"
        effect.self().ui.serverComboBox.currentText = serverUrl
        effect.self().onClickFetchModels()
        effect.self().ui.segmentationModelSelector.currentText = "inner_ear"
        effect.self().onClickSegmentation()
        inner_ear = segmentationNode.GetSegmentation(
        ).GetSegmentIdBySegmentName("inner_ear")
        segmentEditorWidget.setCurrentSegmentID(inner_ear)
        segmentationNode.GetSegmentation().GetSegment(inner_ear).SetColor(
            1, 0, 0)
        segmentationNode.GetSegmentation().GetSegment(
            otic_capsule_segment).SetColor(0.89, 0.92, 0.65)
        segmentEditorNode.SetMasterVolumeIntensityMaskRange(-300, 550)
        #Turn mask range on
        segmentEditorNode.MasterVolumeIntensityMaskOn()
        #growing the membranous labyrinth
        # Margin effect
        segmentEditorWidget.setActiveEffectByName("Margin")
        effect = segmentEditorWidget.activeEffect()
        effect.setParameter("MarginSizeMm", 0.3)
        effect.self().onApply()
        segmentEditorWidget.setCurrentSegmentID('otic_capsule')
        #Copying the inner ear segment to otic capsule
        # Logical effect
        segmentEditorWidget.setActiveEffectByName("Logical operators")
        effect = segmentEditorWidget.activeEffect()
        effect.setParameter("Operation", 'COPY')
        effect.setParameter("ModifierSegmentID", inner_ear)
        effect.setParameter("BypassMasking", 1)
        effect.self().onApply()
        # Define the Mask range for bony labyrinth
        segmentEditorNode.SetMasterVolumeIntensityMaskRange(650, 2500)
        #Turn mask range on
        segmentEditorNode.MasterVolumeIntensityMaskOn()
        #segmenting the otic capsule from the inner ear
        # Margin effect
        segmentEditorWidget.setActiveEffectByName("Margin")
        effect = segmentEditorWidget.activeEffect()
        effect.setParameter("MarginSizeMm", spacing * 5)
        effect.self().onApply()
        segmentEditorNode.MasterVolumeIntensityMaskOff()
        # Islands effect
        segmentEditorWidget.setActiveEffectByName("Islands")
        effect = segmentEditorWidget.activeEffect()
        effect.setParameter("Operation", 'KEEP_LARGEST_ISLAND')
        effect.self().onApply()
        #Make the inner ear not visible to export only the otic capsule
        #segmentationNode.GetDisplayNode().SetSegmentVisibility(inner_ear, False)
        #copy the inner ear segment to another segmentation Node
        #segmentationNode1.GetSegmentation().CopySegmentFromSegmentation(segmentationNode.GetSegmentation(),inner_ear)
        # Remove the inner ear to export only the otic capsule
        segmentationNode.GetSegmentation().RemoveSegment(inner_ear)

        if exportlabelmaps == True:
            #Create labelmap for otic capsule and export the segment
            oticcapsule_labelmap = slicer.mrmlScene.AddNewNodeByClass(
                'vtkMRMLLabelMapVolumeNode')
            oticcapsule_labelmap.SetName('oticcapsule_labelmap')
            slicer.modules.segmentations.logic(
            ).ExportVisibleSegmentsToLabelmapNode(segmentationNode,
                                                  oticcapsule_labelmap,
                                                  inputVolume)
            slicer.util.saveNode(oticcapsule_labelmap,
                                 directory + '/oticcapsule_labelmap.nrrd')

        if exportOBJ == True:
            shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(
                slicer.mrmlScene)
            segnumber = shNode.GetItemByDataNode(segmentationNode)
            shNode.SetItemName(segnumber, volumeName + '_otic capsule')
            segmentIDs = vtk.vtkStringArray()
            segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
            slicer.modules.segmentations.logic(
            ).ExportSegmentsClosedSurfaceRepresentationToFiles(
                directory, segmentationNode, segmentIDs, "OBJ", True, 1.0,
                False)

#copy the otic capsule segment to another segmentation Node
        segmentationNode1.GetSegmentation().CopySegmentFromSegmentation(
            segmentationNode.GetSegmentation(), otic_capsule_segment)

        # Remove the otic capsule after exporting
        segmentationNode.GetSegmentation().RemoveSegment('otic_capsule')

        # Autosegmentation of the ossicles
        segmentEditorWidget.setActiveEffectByName("Nvidia AIAA")
        effect = segmentEditorWidget.activeEffect()
        effect.self().ui.segmentationModelSelector.currentText = 'ossicles'
        effect.self().onClickSegmentation()
        ossicles = segmentationNode.GetSegmentation(
        ).GetSegmentIdBySegmentName("ossicles")
        segmentationNode.GetSegmentation().GetSegment(ossicles).SetColor(
            1, 1, 0.88)
        segmentEditorWidget.setCurrentSegmentID(ossicles)
        # Islands effect
        segmentEditorWidget.setActiveEffectByName("Islands")
        effect = segmentEditorWidget.activeEffect()
        effect.setParameter("Operation", 'REMOVE_SMALL_ISLANDS')
        effect.setParameter("MinimumSize", 50)
        effect.self().onApply()

        if exportlabelmaps == True:
            #Create labelmap for ossicles and export the segment
            ossicles_labelmap = slicer.mrmlScene.AddNewNodeByClass(
                'vtkMRMLLabelMapVolumeNode')
            ossicles_labelmap.SetName('ossicles_labelmap')
            slicer.modules.segmentations.logic(
            ).ExportVisibleSegmentsToLabelmapNode(segmentationNode,
                                                  ossicles_labelmap,
                                                  inputVolume)
            slicer.util.saveNode(ossicles_labelmap,
                                 directory + '/ossicles_labelmap.nrrd')

        if exportOBJ == True:
            shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(
                slicer.mrmlScene)
            segnumber = shNode.GetItemByDataNode(segmentationNode)
            shNode.SetItemName(segnumber, volumeName + '_ossicles')
            segmentIDs = vtk.vtkStringArray()
            segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
            slicer.modules.segmentations.logic(
            ).ExportSegmentsClosedSurfaceRepresentationToFiles(
                directory, segmentationNode, segmentIDs, "OBJ", True, 1.0,
                False)

#copy the ossicles segment to another segmentation Node
        segmentationNode1.GetSegmentation().CopySegmentFromSegmentation(
            segmentationNode.GetSegmentation(), ossicles)

        # Remove the ossicles after exporting
        segmentationNode.GetSegmentation().RemoveSegment(ossicles)

        # Autosegmentation of Cochlear duct
        segmentEditorWidget.setActiveEffectByName("Nvidia AIAA")
        effect = segmentEditorWidget.activeEffect()
        effect.self(
        ).ui.segmentationModelSelector.currentText = "cochlear_duct"
        effect.self().onClickSegmentation()
        cochlear_duct = segmentationNode.GetSegmentation(
        ).GetSegmentIdBySegmentName("cochlear_duct")
        segmentEditorWidget.setCurrentSegmentID(cochlear_duct)
        segmentationNode.GetSegmentation().GetSegment(cochlear_duct).SetColor(
            1, 0, 0)
        segmentEditorNode.SetMasterVolumeIntensityMaskRange(-410, 750)
        #Turn mask range on
        segmentEditorNode.MasterVolumeIntensityMaskOn()
        #growing the membranous labyrinth
        # Margin effect
        segmentEditorWidget.setActiveEffectByName("Margin")
        effect = segmentEditorWidget.activeEffect()
        effect.setParameter("MarginSizeMm", 0.3)
        effect.self().onApply()

        if exportlabelmaps == True:
            #Create labelmap for cochlear duct and export the segment
            cochlear_duct_labelmap = slicer.mrmlScene.AddNewNodeByClass(
                'vtkMRMLLabelMapVolumeNode')
            cochlear_duct_labelmap.SetName('cochlear_duct_labelmap')
            slicer.modules.segmentations.logic(
            ).ExportVisibleSegmentsToLabelmapNode(segmentationNode,
                                                  cochlear_duct_labelmap,
                                                  inputVolume)
            slicer.util.saveNode(cochlear_duct_labelmap,
                                 directory + '/cochlear_duct_labelmap.nrrd')

        if exportOBJ == True:
            shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(
                slicer.mrmlScene)
            segnumber = shNode.GetItemByDataNode(segmentationNode)
            shNode.SetItemName(segnumber, volumeName + '_cochlear_duct')
            segmentIDs = vtk.vtkStringArray()
            segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
            slicer.modules.segmentations.logic(
            ).ExportSegmentsClosedSurfaceRepresentationToFiles(
                directory, segmentationNode, segmentIDs, "OBJ", True, 1.0,
                False)

#copy the cochlear_duct segment to another segmentation Node
        segmentationNode1.GetSegmentation().CopySegmentFromSegmentation(
            segmentationNode.GetSegmentation(), cochlear_duct)

        # Remove the cochlear_duct after exporting
        segmentationNode.GetSegmentation().RemoveSegment(cochlear_duct)

        if medianFilter == True:
            volumesLogic = slicer.modules.volumes.logic()
            median = volumesLogic.CloneVolume(slicer.mrmlScene, inputVolume,
                                              'Volume_median_filter')

            parameters = {
                "neighborhood": "1,1,1",
                "inputVolume": inputVolume,
                "outputVolume": median
            }
            slicer.cli.runSync(slicer.modules.medianimagefilter, None,
                               parameters)

            segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(
                median)
            segmentEditorWidget.setMasterVolumeNode(median)

        # Autosegmentation of Facial Nerve
        segmentEditorWidget.setActiveEffectByName("Nvidia AIAA")
        effect = segmentEditorWidget.activeEffect()
        effect.self().ui.segmentationModelSelector.currentText = 'facial_nerve'
        effect.self().onClickSegmentation()
        facial_nerve = segmentationNode.GetSegmentation(
        ).GetSegmentIdBySegmentName("facial_nerve")
        segmentationNode.GetSegmentation().GetSegment(facial_nerve).SetColor(
            0.9, 1, 0)

        if exportlabelmaps == True:
            #Create labelmap for facial nerve and export the segment
            facial_labelmap = slicer.mrmlScene.AddNewNodeByClass(
                'vtkMRMLLabelMapVolumeNode')
            slicer.modules.segmentations.logic(
            ).ExportVisibleSegmentsToLabelmapNode(segmentationNode,
                                                  facial_labelmap, inputVolume)
            facial_labelmap.SetName('facial_labelmap')
            slicer.util.saveNode(facial_labelmap,
                                 directory + '/facial_labelmap.nrrd')

        if exportOBJ == True:
            shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(
                slicer.mrmlScene)
            segnumber = shNode.GetItemByDataNode(segmentationNode)
            shNode.SetItemName(segnumber, volumeName + '_facial_nerve')
            segmentIDs = vtk.vtkStringArray()
            segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
            slicer.modules.segmentations.logic(
            ).ExportSegmentsClosedSurfaceRepresentationToFiles(
                directory, segmentationNode, segmentIDs, "OBJ", True, 1.0,
                False)

#copy the facial nerve segment to another segmentation Node
        segmentationNode1.GetSegmentation().CopySegmentFromSegmentation(
            segmentationNode.GetSegmentation(), facial_nerve)

        # Remove the facial_nerve after exporting
        segmentationNode.GetSegmentation().RemoveSegment(facial_nerve)

        # Autosegmentation of Sigmoid sinus
        segmentEditorWidget.setActiveEffectByName("Nvidia AIAA")
        effect = segmentEditorWidget.activeEffect()
        effect.self(
        ).ui.segmentationModelSelector.currentText = 'sigmoid_sinus'
        effect.self().onClickSegmentation()
        sigmoid = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(
            "sigmoid_sinus")
        segmentEditorWidget.setCurrentSegmentID(sigmoid)
        # Islands effect
        segmentEditorWidget.setActiveEffectByName("Islands")
        effect = segmentEditorWidget.activeEffect()
        effect.setParameter("Operation", 'REMOVE_SMALL_ISLANDS')
        effect.setParameter("MinimumSize", 500)
        effect.self().onApply()

        if exportlabelmaps == True:
            # Create labelmap for sigmoid sinus and export the segment
            sigmoid_labelmap = slicer.mrmlScene.AddNewNodeByClass(
                'vtkMRMLLabelMapVolumeNode')
            sigmoid_labelmap.SetName('sigmoid_labelmap')
            slicer.modules.segmentations.logic(
            ).ExportVisibleSegmentsToLabelmapNode(segmentationNode,
                                                  sigmoid_labelmap,
                                                  inputVolume)
            slicer.util.saveNode(sigmoid_labelmap,
                                 directory + '/sigmoid_labelmap.nrrd')

        if exportOBJ == True:
            shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(
                slicer.mrmlScene)
            segnumber = shNode.GetItemByDataNode(segmentationNode)
            shNode.SetItemName(segnumber, volumeName + '_sigmoid')
            segmentIDs = vtk.vtkStringArray()
            segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
            slicer.modules.segmentations.logic(
            ).ExportSegmentsClosedSurfaceRepresentationToFiles(
                directory, segmentationNode, segmentIDs, "OBJ", True, 1.0,
                False)

#copy the sigmoid segment to another segmentation Node
        segmentationNode1.GetSegmentation().CopySegmentFromSegmentation(
            segmentationNode.GetSegmentation(), sigmoid)

        # Remove the sigmoid after exporting
        segmentationNode.GetSegmentation().RemoveSegment(sigmoid)

        # Remove the empty segmentation node
        slicer.mrmlScene.RemoveNode(segmentationNode)

        # Export the isotropic volume as nrrd file
        slicer.util.saveNode(inputVolume, directory + '/Volume.nrrd')

        # Export the isotropic volume as a DICOM series
        if exportDICOM == True:
            from datetime import datetime
            now = datetime.now()
            patient = now.strftime("%b-%d-%Y_%H-%M-%S")

            # Create patient and study and put the volume under the study
            shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(
                slicer.mrmlScene)
            patientItemID = shNode.CreateSubjectItem(shNode.GetSceneItemID(),
                                                     patient)
            studyItemID = shNode.CreateStudyItem(patientItemID,
                                                 "Auto-segmentation Study")
            volumeShItemID = shNode.GetItemByDataNode(inputVolume)
            shNode.SetItemParent(volumeShItemID, studyItemID)

            import DICOMScalarVolumePlugin
            exporter = DICOMScalarVolumePlugin.DICOMScalarVolumePluginClass()
            exportables = exporter.examineForExport(volumeShItemID)
            for exp in exportables:
                exp.directory = directory

            exporter.export(exportables)

        # Make segmentation results visible in 3D
        segmentationNode1.CreateClosedSurfaceRepresentation()

        layoutManager = slicer.app.layoutManager()
        threeDWidget = layoutManager.threeDWidget(0)
        threeDView = threeDWidget.threeDView()
        threeDView.resetFocalPoint()

        # Remove volume
        #slicer.mrmlScene.RemoveNode(median)

        #saveNode(inputVolume, directory+'/inputVolume.nrrd')
        #slicer.mrmlScene.RemoveNode(segmentationNode)
        #print(path)
        logging.info('Processing completed')
Example #54
0
    if opts.verbose: print "Performing Delaunay triangulation"
    delaunay = vtk.vtkDelaunay2D()
    delaunay.SetInput(only_valid_poly)
    delaunay.Update()
    if opts.verbose: print "Getting rid of unused points"
    no_orphaned_points = vtk.vtkCleanPolyData()
    no_orphaned_points.SetInput(delaunay.GetOutput())
    no_orphaned_points.Update()
    to_write = no_orphaned_points.GetOutput()
else:
    to_write = bot_with_z

to_write.GetPointData().RemoveArray("vtkValidPointMask")

if opts.verbose: print "Adding provenance"
command_used = vtk.vtkStringArray()
command_used.SetName("provenance")
command_used.InsertNextValue(" ".join(sys.argv))
to_write.GetFieldData().AddArray(command_used)

if opts.verbose: print "Writing", vtp_out
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName(vtp_out)
if opts.ascii:
    writer.SetDataModeToAscii()
else:
    writer.SetDataModeToBinary()
writer.SetInputConnection(to_write.GetProducerPort())
writer.Write()

sys.exit(0)
Example #55
0
    def CreateImageData(self, filelist, zspacing, size, bits):
        message = _("Generating multiplanar visualization...")

        if not const.VTK_WARNING:
            log_path = os.path.join(const.LOG_FOLDER, 'vtkoutput.txt')
            fow = vtk.vtkFileOutputWindow()
            fow.SetFileName(log_path)
            ow = vtk.vtkOutputWindow()
            ow.SetInstance(fow)

        x, y = size
        px, py = utils.predict_memory(len(filelist), x, y, bits)
        utils.debug("Image Resized to >>> %f x %f" % (px, py))

        if (x == px) and (y == py):
            const.REDUCE_IMAGEDATA_QUALITY = 0
        else:
            const.REDUCE_IMAGEDATA_QUALITY = 1

        if not (const.REDUCE_IMAGEDATA_QUALITY):
            update_progress = vtk_utils.ShowProgress(
                1, dialog_type="ProgressDialog")

            array = vtk.vtkStringArray()
            for x in xrange(len(filelist)):
                if not self.running:
                    return False
                array.InsertValue(x, filelist[x])

            if not self.running:
                return False
            reader = vtkgdcm.vtkGDCMImageReader()
            reader.SetFileNames(array)
            reader.AddObserver(
                "ProgressEvent",
                lambda obj, evt: update_progress(reader, message))
            reader.Update()

            if not self.running:
                reader.AbortExecuteOn()
                return False
            # The zpacing is a DicomGroup property, so we need to set it
            imagedata = vtk.vtkImageData()
            imagedata.DeepCopy(reader.GetOutput())
            spacing = imagedata.GetSpacing()
            imagedata.SetSpacing(spacing[0], spacing[1], zspacing)
        else:

            update_progress = vtk_utils.ShowProgress(
                2 * len(filelist), dialog_type="ProgressDialog")

            # Reformat each slice and future append them
            appender = vtk.vtkImageAppend()
            appender.SetAppendAxis(2)  #Define Stack in Z

            # Reformat each slice
            for x in xrange(len(filelist)):
                # TODO: We need to check this automatically according
                # to each computer's architecture
                # If the resolution of the matrix is too large
                if not self.running:
                    return False
                reader = vtkgdcm.vtkGDCMImageReader()
                reader.SetFileName(filelist[x])
                reader.AddObserver(
                    "ProgressEvent",
                    lambda obj, evt: update_progress(reader, message))
                reader.Update()

                #Resample image in x,y dimension
                slice_imagedata = ResampleImage2D(reader.GetOutput(), px, py,
                                                  update_progress)
                #Stack images in Z axes
                appender.AddInput(slice_imagedata)
                #appender.AddObserver("ProgressEvent", lambda obj,evt:update_progress(appender))
                appender.Update()

            # The zpacing is a DicomGroup property, so we need to set it
            if not self.running:
                return False
            imagedata = vtk.vtkImageData()
            imagedata.DeepCopy(appender.GetOutput())
            spacing = imagedata.GetSpacing()

            imagedata.SetSpacing(spacing[0], spacing[1], zspacing)

        imagedata.AddObserver(
            "ProgressEvent",
            lambda obj, evt: update_progress(imagedata, message))
        imagedata.Update()

        return imagedata
Example #56
0
    def testStackedPlot(self):
        "Test if stacked plots can be built with python"

        # Set up a 2D scene, add an XY chart to it
        view = vtk.vtkContextView()
        view.GetRenderer().SetBackground(1.0,1.0,1.0)
        view.GetRenderWindow().SetSize(400,300)
        chart = vtk.vtkChartXY()
        view.GetScene().AddItem(chart)

        # Create a table with some data in it
        table = vtk.vtkTable()

        arrMonthLabels = vtk.vtkStringArray()
        arrMonthPositions = vtk.vtkDoubleArray()

        arrMonth = vtk.vtkIntArray()
        arrMonth.SetName("Month")

        arrBooks = vtk.vtkIntArray()
        arrBooks.SetName("Books")

        arrNew = vtk.vtkIntArray()
        arrNew.SetName("New / Popular")

        arrPeriodical = vtk.vtkIntArray()
        arrPeriodical.SetName("Periodical")

        arrAudiobook = vtk.vtkIntArray()
        arrAudiobook.SetName("Audiobook")

        arrVideo = vtk.vtkIntArray()
        arrVideo.SetName("Video")

        numMonths = 12

        for i in range(0,numMonths):
            arrMonthLabels.InsertNextValue(month_labels[i])
            arrMonthPositions.InsertNextValue(float(i))

            arrMonth.InsertNextValue(i)
            arrBooks.InsertNextValue(book[i])
            arrNew.InsertNextValue(new_popular[i])
            arrPeriodical.InsertNextValue(periodical[i])
            arrAudiobook.InsertNextValue(audiobook[i])
            arrVideo.InsertNextValue(video[i])

        table.AddColumn(arrMonth)
        table.AddColumn(arrBooks)
        table.AddColumn(arrNew)
        table.AddColumn(arrPeriodical)
        table.AddColumn(arrAudiobook)
        table.AddColumn(arrVideo)

        # Set up the X Labels
        chart.GetAxis(1).SetCustomTickPositions(arrMonthPositions, arrMonthLabels)
        chart.GetAxis(1).SetMaximum(11)
        chart.GetAxis(1).SetBehavior(vtk.vtkAxis.FIXED)

        chart.SetShowLegend(True)

        # Create the stacked plot
        stack = chart.AddPlot(3)
        stack.SetUseIndexForXSeries(True)
        stack.SetInputData(table)
        stack.SetInputArray(1,"Books")
        stack.SetInputArray(2,"New / Popular")
        stack.SetInputArray(3,"Periodical")
        stack.SetInputArray(4,"Audiobook")
        stack.SetInputArray(5,"Video")

        # Set up a nice color series
        colorSeries = vtk.vtkColorSeries()
        colorSeries.SetColorScheme(2)
        stack.SetColorSeries(colorSeries)

        view.GetRenderWindow().SetMultiSamples(0)
        view.GetRenderWindow().Render()

        img_file = "TestStackedPlot.png"
        vtk.test.Testing.compareImage(view.GetRenderWindow(),
                                      vtk.test.Testing.getAbsImagePath(img_file),
                                      threshold=25)
        vtk.test.Testing.interact()
Example #57
0
    def preview(self):
        # Get master volume image data
        import vtkSegmentationCorePython as vtkSegmentationCore
        masterImageData = self.scriptedEffect.masterVolumeImageData()

        # Get segmentation
        segmentationNode = self.scriptedEffect.parameterSetNode(
        ).GetSegmentationNode()

        previewNode = self.getPreviewNode()
        if not previewNode or not self.mergedLabelmapGeometryImage \
          or (self.clippedMasterImageDataRequired and not self.clippedMasterImageData):
            self.reset()
            # Compute merged labelmap extent (effective extent slightly expanded)
            self.selectedSegmentIds = vtk.vtkStringArray()
            segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(
                self.selectedSegmentIds)
            if self.selectedSegmentIds.GetNumberOfValues(
            ) < self.minimumNumberOfSegments:
                logging.error(
                    "Auto-complete operation skipped: at least {0} visible segments are required"
                    .format(self.minimumNumberOfSegments))
                return
            if not self.mergedLabelmapGeometryImage:
                self.mergedLabelmapGeometryImage = vtkSegmentationCore.vtkOrientedImageData(
                )
            commonGeometryString = segmentationNode.GetSegmentation(
            ).DetermineCommonLabelmapGeometry(
                vtkSegmentationCore.vtkSegmentation.
                EXTENT_UNION_OF_EFFECTIVE_SEGMENTS, self.selectedSegmentIds)
            if not commonGeometryString:
                logging.info(
                    "Auto-complete operation skipped: all visible segments are empty"
                )
                return
            vtkSegmentationCore.vtkSegmentationConverter.DeserializeImageGeometry(
                commonGeometryString, self.mergedLabelmapGeometryImage)

            masterImageExtent = masterImageData.GetExtent()
            labelsEffectiveExtent = self.mergedLabelmapGeometryImage.GetExtent(
            )
            margin = [17, 17, 17]
            labelsExpandedExtent = [
                max(masterImageExtent[0],
                    labelsEffectiveExtent[0] - margin[0]),
                min(masterImageExtent[1],
                    labelsEffectiveExtent[1] + margin[0]),
                max(masterImageExtent[2],
                    labelsEffectiveExtent[2] - margin[1]),
                min(masterImageExtent[3],
                    labelsEffectiveExtent[3] + margin[1]),
                max(masterImageExtent[4],
                    labelsEffectiveExtent[4] - margin[2]),
                min(masterImageExtent[5], labelsEffectiveExtent[5] + margin[2])
            ]
            self.mergedLabelmapGeometryImage.SetExtent(labelsExpandedExtent)

            previewNode = slicer.mrmlScene.CreateNodeByClass(
                'vtkMRMLSegmentationNode')
            previewNode.UnRegister(None)
            previewNode = slicer.mrmlScene.AddNode(previewNode)
            previewNode.CreateDefaultDisplayNodes()
            previewNode.GetDisplayNode().SetVisibility2DOutline(False)
            if segmentationNode.GetParentTransformNode():
                previewNode.SetAndObserveTransformNodeID(
                    segmentationNode.GetParentTransformNode().GetID())
            self.scriptedEffect.parameterSetNode().SetNodeReferenceID(
                ResultPreviewNodeReferenceRole, previewNode.GetID())
            self.scriptedEffect.setCommonParameter(
                "SegmentationResultPreviewOwnerEffect",
                self.scriptedEffect.name)
            self.setPreviewOpacity(0.6)

            if self.clippedMasterImageDataRequired:
                self.clippedMasterImageData = vtkSegmentationCore.vtkOrientedImageData(
                )
                masterImageClipper = vtk.vtkImageConstantPad()
                masterImageClipper.SetInputData(masterImageData)
                masterImageClipper.SetOutputWholeExtent(
                    self.mergedLabelmapGeometryImage.GetExtent())
                masterImageClipper.Update()
                self.clippedMasterImageData.ShallowCopy(
                    masterImageClipper.GetOutput())
                self.clippedMasterImageData.CopyDirections(
                    self.mergedLabelmapGeometryImage)

        previewNode.SetName(segmentationNode.GetName() + " preview")

        mergedImage = vtkSegmentationCore.vtkOrientedImageData()
        segmentationNode.GenerateMergedLabelmapForAllSegments(
            mergedImage, vtkSegmentationCore.vtkSegmentation.
            EXTENT_UNION_OF_EFFECTIVE_SEGMENTS,
            self.mergedLabelmapGeometryImage, self.selectedSegmentIds)

        outputLabelmap = vtkSegmentationCore.vtkOrientedImageData()

        self.computePreviewLabelmap(mergedImage, outputLabelmap)

        # Write output segmentation results in segments
        for index in xrange(self.selectedSegmentIds.GetNumberOfValues()):
            segmentID = self.selectedSegmentIds.GetValue(index)
            segment = segmentationNode.GetSegmentation().GetSegment(segmentID)
            # Disable save with scene?

            # Get only the label of the current segment from the output image
            thresh = vtk.vtkImageThreshold()
            thresh.ReplaceInOn()
            thresh.ReplaceOutOn()
            thresh.SetInValue(1)
            thresh.SetOutValue(0)
            labelValue = index + 1  # n-th segment label value = n + 1 (background label value is 0)
            thresh.ThresholdBetween(labelValue, labelValue)
            thresh.SetOutputScalarType(vtk.VTK_UNSIGNED_CHAR)
            thresh.SetInputData(outputLabelmap)
            thresh.Update()

            # Write label to segment
            newSegmentLabelmap = vtkSegmentationCore.vtkOrientedImageData()
            newSegmentLabelmap.ShallowCopy(thresh.GetOutput())
            newSegmentLabelmap.CopyDirections(mergedImage)
            newSegment = previewNode.GetSegmentation().GetSegment(segmentID)
            if not newSegment:
                newSegment = vtkSegmentationCore.vtkSegment()
                newSegment.SetName(segment.GetName())
                color = segmentationNode.GetSegmentation().GetSegment(
                    segmentID).GetColor()
                newSegment.SetColor(color)
                previewNode.GetSegmentation().AddSegment(newSegment, segmentID)
            slicer.vtkSlicerSegmentationsModuleLogic.SetBinaryLabelmapToSegment(
                newSegmentLabelmap, previewNode, segmentID)

        self.updateGUIFromMRML()
Example #58
0
    def show_bounds(self,
                    mesh=None,
                    bounds=None,
                    show_xaxis=True,
                    show_yaxis=True,
                    show_zaxis=True,
                    show_xlabels=True,
                    show_ylabels=True,
                    show_zlabels=True,
                    italic=False,
                    bold=True,
                    shadow=False,
                    font_size=None,
                    font_family=None,
                    color=None,
                    xlabel='X Axis',
                    ylabel='Y Axis',
                    zlabel='Z Axis',
                    use_2d=False,
                    grid=None,
                    location='closest',
                    ticks=None,
                    all_edges=False,
                    corner_factor=0.5,
                    loc=None,
                    fmt=None,
                    minor_ticks=False,
                    padding=0.0):
        """Add bounds axes.

        Shows the bounds of the most recent input mesh unless mesh is specified.

        Parameters
        ----------
        mesh : vtkPolydata or unstructured grid, optional
            Input mesh to draw bounds axes around

        bounds : list or tuple, optional
            Bounds to override mesh bounds.
            [xmin, xmax, ymin, ymax, zmin, zmax]

        show_xaxis : bool, optional
            Makes x axis visible.  Default True.

        show_yaxis : bool, optional
            Makes y axis visible.  Default True.

        show_zaxis : bool, optional
            Makes z axis visible.  Default True.

        show_xlabels : bool, optional
            Shows x labels.  Default True.

        show_ylabels : bool, optional
            Shows y labels.  Default True.

        show_zlabels : bool, optional
            Shows z labels.  Default True.

        italic : bool, optional
            Italicises axis labels and numbers.  Default False.

        bold : bool, optional
            Bolds axis labels and numbers.  Default True.

        shadow : bool, optional
            Adds a black shadow to the text.  Default False.

        font_size : float, optional
            Sets the size of the label font.  Defaults to 16.

        font_family : string, optional
            Font family.  Must be either courier, times, or arial.

        color : string or 3 item list, optional
            Color of all labels and axis titles.  Default white.
            Either a string, rgb list, or hex color string.  For example:

                color='white'
                color='w'
                color=[1, 1, 1]
                color='#FFFFFF'

        xlabel : string, optional
            Title of the x axis.  Default "X Axis"

        ylabel : string, optional
            Title of the y axis.  Default "Y Axis"

        zlabel : string, optional
            Title of the z axis.  Default "Z Axis"

        use_2d : bool, optional
            A bug with vtk 6.3 in Windows seems to cause this function
            to crash this can be enabled for smoother plotting for
            other environments.

        grid : bool or str, optional
            Add grid lines to the backface (``True``, ``'back'``, or
            ``'backface'``) or to the frontface (``'front'``,
            ``'frontface'``) of the axes actor.

        location : str, optional
            Set how the axes are drawn: either static (``'all'``),
            closest triad (``front``), furthest triad (``'back'``),
            static closest to the origin (``'origin'``), or outer
            edges (``'outer'``) in relation to the camera
            position. Options include: ``'all', 'front', 'back',
            'origin', 'outer'``

        ticks : str, optional
            Set how the ticks are drawn on the axes grid. Options include:
            ``'inside', 'outside', 'both'``

        all_edges : bool, optional
            Adds an unlabeled and unticked box at the boundaries of
            plot. Useful for when wanting to plot outer grids while
            still retaining all edges of the boundary.

        corner_factor : float, optional
            If ``all_edges````, this is the factor along each axis to
            draw the default box. Dafuault is 0.5 to show the full box.

        loc : int, tuple, or list
            Index of the renderer to add the actor to.  For example,
            ``loc=2`` or ``loc=(1, 1)``.  If None, selects the last
            active Renderer.

        padding : float, optional
            An optional percent padding along each axial direction to cushion
            the datasets in the scene from the axes annotations. Defaults to
            have no padding

        Return
        ------
        cube_axes_actor : vtk.vtkCubeAxesActor
            Bounds actor

        Examples
        --------
        >>> import pyvista
        >>> from pyvista import examples
        >>> mesh = pyvista.Sphere()
        >>> plotter = pyvista.Plotter()
        >>> _ = plotter.add_mesh(mesh)
        >>> _ = plotter.show_bounds(grid='front', location='outer', all_edges=True)
        >>> plotter.show() # doctest:+SKIP

        """
        self.remove_bounds_axes()

        if font_family is None:
            font_family = rcParams['font']['family']
        if font_size is None:
            font_size = rcParams['font']['size']
        if color is None:
            color = rcParams['font']['color']
        if fmt is None:
            fmt = rcParams['font']['fmt']

        color = parse_color(color)

        # Use the bounds of all data in the rendering window
        if mesh is None and bounds is None:
            bounds = self.bounds

        # create actor
        cube_axes_actor = vtk.vtkCubeAxesActor()
        if use_2d or not np.allclose(self.scale, [1.0, 1.0, 1.0]):
            cube_axes_actor.SetUse2DMode(True)
        else:
            cube_axes_actor.SetUse2DMode(False)

        if grid:
            if isinstance(grid,
                          str) and grid.lower() in ('front', 'frontface'):
                cube_axes_actor.SetGridLineLocation(
                    cube_axes_actor.VTK_GRID_LINES_CLOSEST)
            if isinstance(grid, str) and grid.lower() in ('both', 'all'):
                cube_axes_actor.SetGridLineLocation(
                    cube_axes_actor.VTK_GRID_LINES_ALL)
            else:
                cube_axes_actor.SetGridLineLocation(
                    cube_axes_actor.VTK_GRID_LINES_FURTHEST)
            # Only show user desired grid lines
            cube_axes_actor.SetDrawXGridlines(show_xaxis)
            cube_axes_actor.SetDrawYGridlines(show_yaxis)
            cube_axes_actor.SetDrawZGridlines(show_zaxis)
            # Set the colors
            cube_axes_actor.GetXAxesGridlinesProperty().SetColor(color)
            cube_axes_actor.GetYAxesGridlinesProperty().SetColor(color)
            cube_axes_actor.GetZAxesGridlinesProperty().SetColor(color)

        if isinstance(ticks, str):
            ticks = ticks.lower()
            if ticks in ('inside'):
                cube_axes_actor.SetTickLocationToInside()
            elif ticks in ('outside'):
                cube_axes_actor.SetTickLocationToOutside()
            elif ticks in ('both'):
                cube_axes_actor.SetTickLocationToBoth()
            else:
                raise ValueError(
                    'Value of ticks ({}) not understood.'.format(ticks))

        if isinstance(location, str):
            location = location.lower()
            if location in ('all'):
                cube_axes_actor.SetFlyModeToStaticEdges()
            elif location in ('origin'):
                cube_axes_actor.SetFlyModeToStaticTriad()
            elif location in ('outer'):
                cube_axes_actor.SetFlyModeToOuterEdges()
            elif location in ('default', 'closest', 'front'):
                cube_axes_actor.SetFlyModeToClosestTriad()
            elif location in ('furthest', 'back'):
                cube_axes_actor.SetFlyModeToFurthestTriad()
            else:
                raise ValueError(
                    'Value of location ({}) not understood.'.format(location))

        # set bounds
        if bounds is None:
            bounds = np.array(mesh.GetBounds())
        if isinstance(padding, (int, float)) and 0.0 <= padding < 1.0:
            if not np.any(np.abs(bounds) == np.inf):
                cushion = np.array([
                    np.abs(bounds[1] - bounds[0]),
                    np.abs(bounds[3] - bounds[2]),
                    np.abs(bounds[5] - bounds[4])
                ]) * padding
                bounds[::2] -= cushion
                bounds[1::2] += cushion
        else:
            raise ValueError(
                'padding ({}) not understood. Must be float between 0 and 1'.
                format(padding))
        cube_axes_actor.SetBounds(bounds)

        # show or hide axes
        cube_axes_actor.SetXAxisVisibility(show_xaxis)
        cube_axes_actor.SetYAxisVisibility(show_yaxis)
        cube_axes_actor.SetZAxisVisibility(show_zaxis)

        # disable minor ticks
        if not minor_ticks:
            cube_axes_actor.XAxisMinorTickVisibilityOff()
            cube_axes_actor.YAxisMinorTickVisibilityOff()
            cube_axes_actor.ZAxisMinorTickVisibilityOff()

        cube_axes_actor.SetCamera(self.camera)

        # set color
        cube_axes_actor.GetXAxesLinesProperty().SetColor(color)
        cube_axes_actor.GetYAxesLinesProperty().SetColor(color)
        cube_axes_actor.GetZAxesLinesProperty().SetColor(color)

        # empty arr
        empty_str = vtk.vtkStringArray()
        empty_str.InsertNextValue('')

        # show lines
        if show_xaxis:
            cube_axes_actor.SetXTitle(xlabel)
        else:
            cube_axes_actor.SetXTitle('')
            cube_axes_actor.SetAxisLabels(0, empty_str)

        if show_yaxis:
            cube_axes_actor.SetYTitle(ylabel)
        else:
            cube_axes_actor.SetYTitle('')
            cube_axes_actor.SetAxisLabels(1, empty_str)

        if show_zaxis:
            cube_axes_actor.SetZTitle(zlabel)
        else:
            cube_axes_actor.SetZTitle('')
            cube_axes_actor.SetAxisLabels(2, empty_str)

        # show labels
        if not show_xlabels:
            cube_axes_actor.SetAxisLabels(0, empty_str)

        if not show_ylabels:
            cube_axes_actor.SetAxisLabels(1, empty_str)

        if not show_zlabels:
            cube_axes_actor.SetAxisLabels(2, empty_str)

        # set font
        font_family = parse_font_family(font_family)
        for i in range(3):
            cube_axes_actor.GetTitleTextProperty(i).SetFontSize(font_size)
            cube_axes_actor.GetTitleTextProperty(i).SetColor(color)
            cube_axes_actor.GetTitleTextProperty(i).SetFontFamily(font_family)
            cube_axes_actor.GetTitleTextProperty(i).SetBold(bold)

            cube_axes_actor.GetLabelTextProperty(i).SetFontSize(font_size)
            cube_axes_actor.GetLabelTextProperty(i).SetColor(color)
            cube_axes_actor.GetLabelTextProperty(i).SetFontFamily(font_family)
            cube_axes_actor.GetLabelTextProperty(i).SetBold(bold)

        self.add_actor(cube_axes_actor, reset_camera=False, pickable=False)
        self.cube_axes_actor = cube_axes_actor

        if all_edges:
            self.add_bounding_box(color=color, corner_factor=corner_factor)

        if fmt is not None:
            cube_axes_actor.SetXLabelFormat(fmt)
            cube_axes_actor.SetYLabelFormat(fmt)
            cube_axes_actor.SetZLabelFormat(fmt)

        return cube_axes_actor
Example #59
0
    def AddPointLabels(self, points, labels, bold=True, fontsize=16,
                       textcolor='k', font_family='courier', shadow=False,
                       showpoints=True, pointcolor='k', pointsize=5):
        """
        Creates a point actor with one label from list labels assigned to
        each point.
        
        Parameters
        ----------
        points : np.ndarray
            3 x n numpy array of points.
            
        labels : list
            List of labels.  Must be the same length as points.
            
        italic : bool, optional
            Italicises title and bar labels.  Default False.
            
        bold : bool, optional
            Bolds title and bar labels.  Default True
            
        fontsize : float, optional
            Sets the size of the title font.  Defaults to 16.
            
        textcolor : string or 3 item list, optional, defaults to black
            Color of text.
            Either a string, rgb list, or hex color string.  For example:
                textcolor='white'
                textcolor='w'
                textcolor=[1, 1, 1]
                textcolor='#FFFFFF'
                
        font_family : string, optional
            Font family.  Must be either courier, times, or arial.
            
        shadow : bool, optional
            Adds a black shadow to the text.  Defaults to False
            
        showpoints : bool, optional
            Controls if points are visible.  Default True
            
        pointcolor : string or 3 item list, optional, defaults to black
            Color of points (if visible).
            Either a string, rgb list, or hex color string.  For example:
                textcolor='white'
                textcolor='w'
                textcolor=[1, 1, 1]
                textcolor='#FFFFFF'
                
        pointsize : float, optional
            Size of points (if visible)
        
        Returns
        -------
        labelMapper : vtk.vtkvtkLabeledDataMapper
            VTK label mapper.  Can be used to change properties of the labels.
        
        """
        
        if len(points) != len(labels):
            raise Exception('There must be one label for each point')
        
        vtkpoints = vtkInterface.MakePointMesh(points)

        vtklabels = vtk.vtkStringArray()
        vtklabels.SetName('labels')
        for item in labels:
            vtklabels.InsertNextValue(str(item))
        
        vtkpoints.GetPointData().AddArray(vtklabels)
        
        # create label mapper
        labelMapper = vtk.vtkLabeledDataMapper()
        labelMapper.SetInputData(vtkpoints)
        textprop = labelMapper.GetLabelTextProperty()
        textprop.SetBold(bold)
        textprop.SetFontSize(fontsize)
        textprop.SetFontFamily(ParseFontFamily(font_family))
        textprop.SetColor(ParseColor(textcolor))
        textprop.SetShadow(shadow)
        labelMapper.SetLabelModeToLabelFieldData()
        labelMapper.SetFieldDataName('labels')

        labelActor = vtk.vtkActor2D()
        labelActor.SetMapper(labelMapper)
        
        # add points
        if showpoints:
            self.AddMesh(vtkpoints, style='points', color=pointcolor,
                         psize=pointsize)
        else:
            self.AddMesh(vtkpoints)
            
        self.AddActor(labelActor)
        return labelMapper
Example #60
0
    def AddBoundsAxes(self, mesh=None, bounds=None, show_xaxis=True, 
                      show_yaxis=True, show_zaxis=True, show_xlabels=True,
                      show_ylabels=True, show_zlabels=True, italic=False,
                      bold=True, shadow=False, fontsize=16, 
                      font_family='courier', color='w', 
                      xtitle='X Axis', ytitle='Y Axis', ztitle='Z Axis'):
        """
        Adds bounds axes.  Shows the bounds of the most recent input mesh 
        unless mesh is specified.
        
        Parameters
        ----------
        mesh : vtkPolydata or unstructured grid, optional
            Input mesh to draw bounds axes around
        
        bounds : list or tuple, optional
            Bounds to override mesh bounds.
            [xmin, xmax, ymin, ymax, zmin, zmax]
            
        show_xaxis : bool, optional
            Makes x axis visible.  Default True.
            
        show_yaxis : bool, optional
            Makes y axis visible.  Default True.
        
        show_zaxis : bool, optional
            Makes z axis visible.  Default True.
        
        show_xlabels : bool, optional
            Shows x labels.  Default True.

        show_ylabels : bool, optional
            Shows y labels.  Default True.

        show_zlabels : bool, optional
            Shows z labels.  Default True.
            
        italic : bool, optional
            Italicises axis labels and numbers.  Default False.
            
        bold  : bool, optional
            Bolds axis labels and numbers.  Default True.
        
        shadow : bool, optional
            Adds a black shadow to the text.  Default False.
            
        fontsize : float, optional
            Sets the size of the label font.  Defaults to 16.
            
        font_family : string, optional
            Font family.  Must be either courier, times, or arial.

        color : string or 3 item list, optional
            Color of all labels and axis titles.  Default white.
            Either a string, rgb list, or hex color string.  For example:
                color='white'
                color='w'
                color=[1, 1, 1]
                color='#FFFFFF'

        xtitle : string, optional
            Title of the x axis.  Default "X Axis"

        ytitle : string, optional
            Title of the y axis.  Default "Y Axis"
            
        ztitle : string, optional
            Title of the z axis.  Default "Z Axis"

        Returns
        -------
        cubeAxesActor : vtk.vtkCubeAxesActor
            Bounds actor
            
        """
        
        # Use last input mesh if availble
        if not mesh and not bounds:
            if not hasattr(self, 'mesh'):
                raise Exception('Specify bounds or first input a mesh')
            mesh = self.mesh
        
        # create actor
        cubeAxesActor = vtk.vtkCubeAxesActor()
        cubeAxesActor.SetUse2DMode(True)

        # set bounds
        if not bounds:
            bounds = mesh.GetBounds()
        cubeAxesActor.SetBounds(mesh.GetBounds())
        
        # show or hide axes
        cubeAxesActor.SetXAxisVisibility(show_xaxis)
        cubeAxesActor.SetYAxisVisibility(show_yaxis)
        cubeAxesActor.SetZAxisVisibility(show_zaxis)
        
        # disable minor ticks
        cubeAxesActor.XAxisMinorTickVisibilityOff();
        cubeAxesActor.YAxisMinorTickVisibilityOff();
        cubeAxesActor.ZAxisMinorTickVisibilityOff();
        
        cubeAxesActor.SetCamera(self.ren.GetActiveCamera())
        
        # set color
        color = ParseColor(color)
        cubeAxesActor.GetXAxesLinesProperty().SetColor(color)
        cubeAxesActor.GetYAxesLinesProperty().SetColor(color)
        cubeAxesActor.GetZAxesLinesProperty().SetColor(color)

        # empty arr
        empty_str = vtk.vtkStringArray()
        empty_str.InsertNextValue('')
        
        # show lines
        if show_xaxis:
            cubeAxesActor.SetXTitle(xtitle)
        else:
            cubeAxesActor.SetXTitle('')
            cubeAxesActor.SetAxisLabels(0, empty_str)

        if show_yaxis:
            cubeAxesActor.SetYTitle(ytitle)
        else:
            cubeAxesActor.SetYTitle('')
            cubeAxesActor.SetAxisLabels(1, empty_str)

        if show_zaxis:
            cubeAxesActor.SetZTitle(ztitle)
        else:
            cubeAxesActor.SetZTitle('')
            cubeAxesActor.SetAxisLabels(2, empty_str)

        # show labels
        if not show_xlabels:
            cubeAxesActor.SetAxisLabels(0, empty_str)

        if not show_ylabels:
            cubeAxesActor.SetAxisLabels(1, empty_str)

        if not show_zlabels:
            cubeAxesActor.SetAxisLabels(2, empty_str)

        # set font
        font_family = ParseFontFamily(font_family)
        for i in range(3):
            cubeAxesActor.GetTitleTextProperty(i).SetFontSize(fontsize)
            cubeAxesActor.GetTitleTextProperty(i).SetColor(color)
            cubeAxesActor.GetTitleTextProperty(i).SetFontFamily(font_family)
            cubeAxesActor.GetTitleTextProperty(i).SetBold(bold)

            cubeAxesActor.GetLabelTextProperty(i).SetFontSize(fontsize)
            cubeAxesActor.GetLabelTextProperty(i).SetColor(color)
            cubeAxesActor.GetLabelTextProperty(i).SetFontFamily(font_family)
            cubeAxesActor.GetLabelTextProperty(i).SetBold(bold)


        self.AddActor(cubeAxesActor)
        self.cubeAxesActor = cubeAxesActor
        return cubeAxesActor