예제 #1
0
    def computeLabelStatistics(self, inputScan, inputLabelMapNode):
        """ Use slicer core module to get the min/max intensity value inside the mask.
        Returns tuple (min, max) with intensity values inside the mask. """
        # Export lapel map node into a segmentation node
        segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
        slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(inputLabelMapNode, segmentationNode)

        # Compute statistics (may take time)
        segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
        segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
        segStatLogic.getParameterNode().SetParameter("ScalarVolume", inputScan.GetID())

        # Disable all plugins
        for plugin in segStatLogic.plugins:
          pluginName = plugin.__class__.__name__
          segStatLogic.getParameterNode().SetParameter(f"{pluginName}.enabled", str(False))

        # Explicitly enable ScalarVolumeSegmentStatistics
        segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.enabled", str(True))
        segStatLogic.computeStatistics()
        stats = segStatLogic.getStatistics()

        # Remove temporary segmentation node
        slicer.mrmlScene.RemoveNode(segmentationNode)

        segmentId = stats["SegmentIDs"][0]
        minIntensityValue = stats[segmentId, "ScalarVolumeSegmentStatisticsPlugin.min"]
        maxIntensityValue = stats[segmentId, "ScalarVolumeSegmentStatisticsPlugin.max"]

        return minIntensityValue, maxIntensityValue
예제 #2
0
    def getCochleaSize(self):
        import SegmentStatistics
        # Update time label
        lstm = time.time()

        print(
            "================= Begin Size Calculation ... ====================="
        )
        volInput = self.inputSelectorCoBx.currentNode()
        volSeg = slicer.util.getNode((volInput.GetName() + "S"))

        segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
        segStatLogic.getParameterNode().SetParameter("Segmentation",
                                                     volSeg.GetID())
        segStatLogic.getParameterNode().SetParameter("ScalarVolume",
                                                     volInput.GetID())
        segStatLogic.getParameterNode().SetParameter(
            "LabelmapSegmentStatisticsPlugin.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled", "False")
        segStatLogic.computeStatistics()

        # Remove old table
        nodes = slicer.util.getNodesByClass('vtkMRMLTableNode')
        for f in nodes:
            if ((f.GetName() == "CochleaStatistics")):
                slicer.mrmlScene.RemoveNode(f)

        #make a new table
        self.resultsTableNode = slicer.vtkMRMLTableNode()
        self.resultsTableNode.SetName(
            self.inputFnm + "Tbl")  #TODO: changet he name to the image+"_t"
        segStatLogic.exportToTable(self.resultsTableNode)

        for i in range(0, 7):
            self.resultsTableNode.RemoveColumn(3)

        self.resultsTableNode.GetTable().GetColumn(1).SetName("Size (mm^3)")
        self.resultsTableNode.GetTable().GetColumn(2).SetName("Length (mm)")
        self.resultsTableNode.SetCellText(0, 0, "Scala Tympani")
        self.resultsTableNode.SetCellText(1, 0, "Scala Vestibuli")
        self.resultsTableNode.SetCellText(0, 2, str(self.StLength))
        self.resultsTableNode.SetCellText(1, 2, "0")
        slicer.mrmlScene.AddNode(self.resultsTableNode)
        segStatLogic.showTable(self.resultsTableNode)

        # generate and show 3D model
        volSeg.CreateClosedSurfaceRepresentation()
        print("Calculation is done !!! ")
예제 #3
0
  def getCochleaSize(self):
        import SegmentStatistics
        print("================= Begin Size Calculation ... =====================")
        volInput = self.inputVolumeNode 
        volSeg   = slicer.util.getNode((volInput.GetName()+"S"))
        
        segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
        segStatLogic.getParameterNode().SetParameter("Segmentation", volSeg.GetID())
        segStatLogic.getParameterNode().SetParameter("ScalarVolume", volInput.GetID())
        segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled","False")
        segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled","False")
        segStatLogic.computeStatistics()

        # Remove old table
        nodes = slicer.util.getNodesByClass('vtkMRMLTableNode')
        for f in nodes:
            if ((f.GetName() == "CochleaStatistics") ):
                    slicer.mrmlScene.RemoveNode(f)
            #endif
        #endfor
                    
        # compute the fiducial length
        self.StLength = 0       
        self.StLength = self.getFiducilsDistance(self.markupsNode)

        print(" length = " + str(self.StLength))

        #make a new table  with few columns
        self.resultsTableNode = slicer.vtkMRMLTableNode()
        self.resultsTableNode.SetName(self.inputFnm+"Tbl") #TODO: changet he name to the image+"_t"         
        segStatLogic.exportToTable(self.resultsTableNode)      
        for i in range (0,7):
            self.resultsTableNode.RemoveColumn(3)
        #endfor           
        self.resultsTableNode.GetTable().GetColumn(1).SetName("Size (mm^3)")
        self.resultsTableNode.GetTable().GetColumn(2).SetName("Length (mm)")        
        self.resultsTableNode.SetCellText(0,0,"Scala Tympani")
        self.resultsTableNode.SetCellText(1,0,"Scala Vestibuli")
        self.resultsTableNode.SetCellText(0,2,str(self.StLength))           
        self.resultsTableNode.SetCellText(1,2,"0")   
        slicer.mrmlScene.AddNode(self.resultsTableNode)       
        segStatLogic.showTable(self.resultsTableNode)                
        
        # generate and show 3D model
        volSeg.CreateClosedSurfaceRepresentation()
        print("Measurements are completed !!! ")
예제 #4
0
    def findVolume(self, segment):
        # TODO: labelmap volume is more accurate: switch to labelmap based volume calculation

        segStats = SegmentStatistics.SegmentStatisticsLogic()
        #//params = segStats.getParameterNode()
        table = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode")
        segStats.getParameterNode().SetParameter(
            "LabelMapSegmentStatisticsPlugin.enabled", str(False))
        segStats.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.enabled", str(False))
        segStats.getParameterNode().SetParameter("Segmentation", segment)
        segStats.getParameterNode().SetParameter("MeasurementsTable",
                                                 table.GetID())
        segStats.getParameterNode().SetParameter(
            "ClosedSurfaceSegmentStatisticsPlugin.enabled", str(True))
        segStats.getParameterNode().SetParameter("visibleSegmentsOnly",
                                                 str(False))
        segStats.computeStatistics()
        #//segStats.updateStatisticsForSegment(segment)
        segStats.exportToTable(table)
        segStats.showTable(table)
예제 #5
0
    def onApplySButton(self):
        """ This function takes advantage of the functions of the Segment Statistics module to assess the statistics of the segmentation"""
        self.stats = {}
        self.NofV = 1
        self.logic = SegmentStatistics.SegmentStatisticsLogic()
        self.parameterNode = self.logic.getParameterNode()
        self.parameterNode.SetParameter(
            "Segmentation",
            self.segmentationSelector.currentNode().GetID())
        self.parameterNode.SetParameter(
            'ClosedSurfaceSegmentStatisticsPlugin.enabled', 'False')
        self.parameterNode.SetParameter(
            'LabelmapSegmentStatisticsPlugin.enabled', 'True')
        self.parameterNode.SetParameter(
            'LabelmapSegmentStatisticsPlugin.volume_cm3.enabled', 'False')
        self.parameterNode.SetParameter(
            'LabelmapSegmentStatisticsPlugin.volume_mm3.enabled', 'False')
        self.parameterNode.SetParameter(
            'LabelmapSegmentStatisticsPlugin.voxel_count.enabled', 'False')
        self.parameterNode.SetParameter(
            'LabelmapSegmentStatisticsPlugin.surface_area_mm2.enabled', 'True')

        if self.scalarSelector.currentNode():
            self.parameterNode.SetParameter(
                "ScalarVolume",
                self.scalarSelector.currentNode().GetID())
            self.GetStats(self.scalarSelector.currentNode())

        if self.scalarSelector2.currentNode():
            self.parameterNode.SetParameter(
                "ScalarVolume",
                self.scalarSelector2.currentNode().GetID())
            self.GetStats(self.scalarSelector2.currentNode())

        try:
            self.ROImean = np.array(self.stats['Mean'])
        except:
            self.ROImean = [0]

        self.PopulateTableStats()
예제 #6
0
    def computeImplantVolumeCc(self, inputVolume, segmentationNode,
                               implantSegmentId):
        """
    Run the processing algorithm.
    Can be used without GUI widget.
    :param inputVolume: volume to be thresholded
    :param outputVolume: thresholding result
    :param imageThreshold: values above/below this threshold will be set to 0
    :param invert: if True then values above the threshold will be set to 0, otherwise values below are set to 0
    :param showResult: show output volume in slice viewers
    """
        if not inputVolume:
            raise ValueError("Input volume is invalid")

        masterVolumeNode = inputVolume

        # Compute segment volumes
        import SegmentStatistics
        segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
        segStatLogic.getParameterNode().SetParameter("Segmentation",
                                                     segmentationNode.GetID())
        segStatLogic.getParameterNode().SetParameter("ScalarVolume",
                                                     masterVolumeNode.GetID())
        segStatLogic.getParameterNode().SetParameter(
            "LabelmapSegmentStatisticsPlugin.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.volume_mm3.enabled", "False")
        segStatLogic.computeStatistics()
        # print(segStatLogic.getStatistics())  # prints all computed metrics
        implantVolumeCc = segStatLogic.getStatistics()[
            implantSegmentId, "ScalarVolumeSegmentStatisticsPlugin.volume_cm3"]
        logging.info("Processing result: " + str(implantVolumeCc))

        return implantVolumeCc
        """
    def run(self,
            inputVolume,
            imageThreshold_min,
            imageThreshold_max,
            minimumVoxelsize,
            tls_per_min,
            enableScreenshots=0):
        """
    Run the actual algorithm
    """

        if not self.isValidInputData(inputVolume):
            slicer.util.errorDisplay('Input volume error.')
            return False

        logging.info('Processing started')

        masterVolumeNode = inputVolume

        # Create segmentation
        SegmentationNodeCol = slicer.mrmlScene.GetNodesByClass(
            "vtkMRMLSegmentationNode")
        SegmentationNodeCol.UnRegister(slicer.mrmlScene)
        b = True
        for SegmentationNode in SegmentationNodeCol:
            if SegmentationNode.GetName() == "rc_seg":
                SegmentationNode.GetSegmentation().RemoveAllSegments()
                SegmentationNode.GetDisplayNode(
                ).ClearSegmentDisplayProperties()
                print("segmantation rc_seg cleared ")
                b = False
                break

        if b:
            SegmentationNode = slicer.mrmlScene.AddNewNodeByClass(
                "vtkMRMLSegmentationNode")
            SegmentationNode.UnRegister(slicer.mrmlScene)
            SegmentationNode.SetName("rc_seg")
            print("segmantation created : ", SegmentationNode.GetName())
            SegmentationNode.CreateDefaultDisplayNodes(
            )  # only needed for display
            SegmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(
                masterVolumeNode)

        addedSegmentID = SegmentationNode.GetSegmentation().AddEmptySegment(
            SegmentationNode.GetName() + "_")

        # Create temporary segment editor to get access to effects
        segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass(
            "vtkMRMLSegmentEditorNode")
        segmentEditorNode.SetSelectedSegmentID(addedSegmentID)

        segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
        segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
        segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
        segmentEditorWidget.setSegmentationNode(SegmentationNode)
        segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

        segmentEditorWidget.setActiveEffectByName("Threshold")
        effect = segmentEditorWidget.activeEffect()

        # Fill by thresholding

        effect.setParameter("MinimumThreshold", str(imageThreshold_min))
        effect.setParameter("MaximumThreshold", str(imageThreshold_max))
        effect.self().onApply()

        # Fill by thresholding
        if minimumVoxelsize is not None:
            segmentEditorWidget.setActiveEffectByName("Islands")
            effect = segmentEditorWidget.activeEffect()
            effect.setParameter("Operation", "SPLIT_ISLANDS_TO_SEGMENTS")
            effect.setParameter("MinimumSize", minimumVoxelsize)
            effect.self().onApply()

        # Compute segment volumes
        resultsTableNode = slicer.mrmlScene.AddNewNodeByClass(
            'vtkMRMLTableNode')
        resultsTableNode.UnRegister(slicer.mrmlScene)

        import SegmentStatistics
        segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
        segStatLogic.getParameterNode().SetParameter("Segmentation",
                                                     SegmentationNode.GetID())
        segStatLogic.getParameterNode().SetParameter("ScalarVolume",
                                                     masterVolumeNode.GetID())

        segStatLogic.getParameterNode().SetParameter(
            "ClosedSurfaceSegmentStatisticsPlugin.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ClosedSurfaceSegmentStatisticsPlugin.surface_mm2.enabled",
            "False")
        segStatLogic.getParameterNode().SetParameter(
            "ClosedSurfaceSegmentStatisticsPlugin.volume_cm3.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ClosedSurfaceSegmentStatisticsPlugin.volume_mm3.enabled", "False")

        segStatLogic.getParameterNode().SetParameter(
            "LabelmapSegmentStatisticsPlugin.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "LabelmapSegmentStatisticsPlugin.volume_cm3.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "LabelmapSegmentStatisticsPlugin.volume_mm3.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "LabelmapSegmentStatisticsPlugin.voxel_count.enabled", "False")

        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.enabled", "True")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.max.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.mean.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.median.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.min.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.stdev.enabled", "False")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.volume_mm3.enabled", "True")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.volume_cm3.enabled", "True")
        segStatLogic.getParameterNode().SetParameter(
            "ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled", "False")

        segStatLogic.computeStatistics()
        segStatLogic.exportToTable(resultsTableNode)
        col = resultsTableNode.AddColumn()
        col.SetName("Time of lithotripsy [min:sec:1/100s]")
        id_colone_vmm3 = resultsTableNode.GetColumnIndex("Volume [mm3]")
        id_colone_Tl = resultsTableNode.GetColumnIndex(
            "Time of lithotripsy [min:sec:1/100s]")

        import math
        for i in range(resultsTableNode.GetNumberOfRows()):
            vol_i = float(resultsTableNode.GetCellText(i, id_colone_vmm3))
            tl_i = math.modf(vol_i / tls_per_min)
            tl_i_min = int(tl_i[1])
            tli_1 = math.modf(60. * tl_i[0])
            tli_sec = int(tli_1[1])
            tli_cent = 100 * tli_1[0]
            resultsTableNode.SetCellText(
                i, id_colone_Tl,
                "{:d}:{:d}:{:.0f}".format(tl_i_min, tli_sec, tli_cent))

        segStatLogic.showTable(resultsTableNode)

        logging.info('Processing completed !')

        return True
예제 #8
0
	#Turn mask range on
	segmentEditorNode.MasterVolumeIntensityMaskOn()
	#smoothing the cochlea
	# Smoothing effect
	segmentEditorWidget.setActiveEffectByName("Smoothing")
	effect = segmentEditorWidget.activeEffect()
	effect.setParameter("SmoothingMethod", "MEDIAN")
	effect.setParameter("KernelSizeMm", 0.3)	
	effect.self().onApply()
	#Turn mask range off
	segmentEditorNode.MasterVolumeIntensityMaskOff()
	## Create table	
	resultsTableNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTableNode')
	## Use segmentStatistics to calculate the volume and export	
	import SegmentStatistics
	segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
	segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
	segStatLogic.getParameterNode().SetParameter("ScalarVolume", masterVolumeNode.GetID())
	segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled","False")
	segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled","False")
	segStatLogic.computeStatistics()
	segStatLogic.exportToTable(resultsTableNode)
	segStatLogic.showTable(resultsTableNode)
	#prepare and save the segmentation	
	seg = getNode('Segmentation')
	##Get the segment vtk representation 
	segmentationNode = getNode("vtkMRMLSegmentationNode1")
	cochlea_vtk = segmentationNode.GetSegmentation().GetSegment(cochlea)
	##Create Model
	modelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
	##Export segment to model