def run(self, referenceVolumeNode, secondaryVolumeNode): referenceImageData = referenceVolumeNode.GetImageData() secondaryImageData = secondaryVolumeNode.GetImageData() # Apply image convolution to input imagedata imageMath = vtk.vtkImageMathematics() imageMath.SetInput1(referenceImageData) imageMath.SetInput2(secondaryImageData) imageMath.SetOperationToSubtract() imageMath.Update() imageAccumulate = vtk.vtkImageAccumulate() imageAccumulate.SetInput(referenceImageData) imageAccumulate.IgnoreZeroOn() imageAccumulate.Update() referenceVolumeVoxelCount = imageAccumulate.GetVoxelCount() # Update the volume with convolutionl operation result imageAccumulate2 = vtk.vtkImageAccumulate() imageAccumulate2.SetInput(imageMath.GetOutput()) imageAccumulate2.IgnoreZeroOn() imageAccumulate2.Update() differenceVolumeVoxelCount = imageAccumulate2.GetVoxelCount() self._volumeSimilarity = ( referenceVolumeVoxelCount - differenceVolumeVoxelCount) / referenceVolumeVoxelCount
def __init__(self, grayscaleNode, labelNode, fileName=None): volumeName = grayscaleNode.GetName() self.keys = ("Volume", "Curve Type", "Voxel Count", "Volume mm^3", "Volume cc", "Minimum Intensity", "Maximum Intensity", "Mean Intensity", "Standard Deviation") cubicMMPerVoxel = reduce(lambda x,y: x*y, labelNode.GetSpacing()) ccPerCubicMM = 0.001 self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) for i in xrange(lo,hi+1): thresholder = vtk.vtkImageThreshold() thresholder.SetInput(labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i,i) thresholder.SetOutputScalarType(grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) stat1 = vtk.vtkImageAccumulate() stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() curveType = 'Curve Type' if i == 32: curveType = 'Washout Curve' elif i == 306: curveType = 'Persistent Curve' elif i == 291: curveType = 'Plateau Curve' elif i == 0: curveType = 'Unsegmented Region' if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i,"Volume"] = volumeName self.labelStats[i,"Curve Type"] = curveType self.labelStats[i,"Voxel Count"] = stat1.GetVoxelCount() self.labelStats[i,"Volume mm^3"] = self.labelStats[i,"Voxel Count"] * cubicMMPerVoxel self.labelStats[i,"Volume cc"] = self.labelStats[i,"Volume mm^3"] * ccPerCubicMM self.labelStats[i,"Minimum Intensity"] = stat1.GetMin()[0] self.labelStats[i,"Maximum Intensity"] = stat1.GetMax()[0] self.labelStats[i,"Mean Intensity"] = stat1.GetMean()[0] self.labelStats[i,"Standard Deviation"] = stat1.GetStandardDeviation()[0]
def getSliceAreaIm(self,im,sliceNum,minValue=1): nslices=im.GetDimensions()[2] if sliceNum>(nslices-1) or sliceNum<0: return None else: eV=vtk.vtkExtractVOI() VOI=[0,im.GetDimensions()[0],0,im.GetDimensions()[1],sliceNum,sliceNum] eV.SetInputData(im) eV.SetVOI(VOI) eV.Update() slice=eV.GetOutput() maxValue = slice.GetScalarRange()[1] # thresh = vtk.vtkImageThreshold() # thresh.SetInputData(slice) # thresh.ThresholdBetween(minValue, max) # thresh.SetInValue(1) # thresh.SetOutValue(0) # thresh.SetOutputScalarType(slice.GetScalarType()) # thresh.Update() stencil = vtk.vtkImageToImageStencil() stencil.SetInputConnection(eV.GetOutputPort()) stencil.ThresholdBetween(minValue, maxValue) stencil.Update() stat1 = vtk.vtkImageAccumulate() stat1.SetInputConnection(eV.GetOutputPort()) stat1.SetStencilData(stencil.GetOutput()) stat1.Update() return stat1.GetVoxelCount()
def split(self): """split the merge volume into individual structures""" self.statusText("Splitting...") merge = self.mergeVolume() if not merge: return colorNode = merge.GetDisplayNode().GetColorNode() accum = vtk.vtkImageAccumulate() if vtk.VTK_MAJOR_VERSION <= 5: accum.SetInput(merge.GetImageData()) else: accum.SetInputConnection(merge.GetImageDataConnection()) accum.Update() lo = int(accum.GetMin()[0]) hi = int(accum.GetMax()[0]) # TODO: pending resolution of bug 1822, run the thresholding # in single threaded mode to avoid data corruption observed on mac release # builds thresholder = vtk.vtkImageThreshold() thresholder.SetNumberOfThreads(1) for i in xrange(lo, hi + 1): self.statusText("Splitting label %d..." % i) if vtk.VTK_MAJOR_VERSION <= 5: thresholder.SetInput(merge.GetImageData()) else: thresholder.SetInputConnection(merge.GetImageDataConnection()) thresholder.SetInValue(i) thresholder.SetOutValue(0) thresholder.ReplaceInOn() thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i, i) thresholder.SetOutputScalarType( merge.GetImageData().GetScalarType()) thresholder.Update() if thresholder.GetOutput().GetScalarRange() != (0.0, 0.0): labelName = colorNode.GetColorName(i) self.statusText("Creating structure volume %s..." % labelName) structureVolume = self.structureVolume(labelName) if not structureVolume: self.addStructure(i, "noEdit") structureVolume = self.structureVolume(labelName) structureVolume.GetImageData().DeepCopy( thresholder.GetOutput()) self.editUtil.markVolumeNodeAsModified(structureVolume) self.statusText("Finished splitting.")
def getLabelsFromLabelMap(self, labelMapNode): if not labelMapNode: return accum = vtk.vtkImageAccumulate() accum.SetInput(labelMapNode.GetImageData()) accum.UpdateWholeExtent() data = accum.GetOutput() data.Update() numBins = accum.GetComponentExtent()[1] nonZeroLabels = [] for i in range(0, numBins + 1): numVoxels = data.GetScalarComponentAsDouble(i,0,0,0) if (numVoxels != 0): nonZeroLabels.append(i) return nonZeroLabels
def getLabelsFromLabelMap(self, labelMapNode): if not labelMapNode: return accum = vtk.vtkImageAccumulate() accum.SetInput(labelMapNode.GetImageData()) accum.UpdateWholeExtent() data = accum.GetOutput() data.Update() numBins = accum.GetComponentExtent()[1] nonZeroLabels = [] for i in range(0, numBins + 1): numVoxels = data.GetScalarComponentAsDouble(i, 0, 0, 0) if (numVoxels != 0): nonZeroLabels.append(i) return nonZeroLabels
def split(self): """split the merge volume into individual structures""" self.statusText( "Splitting..." ) merge = self.mergeVolume() if not merge: return colorNode = merge.GetDisplayNode().GetColorNode() accum = vtk.vtkImageAccumulate() if vtk.VTK_MAJOR_VERSION <= 5: accum.SetInput(merge.GetImageData()) else: accum.SetInputConnection(merge.GetImageDataConnection()) accum.Update() lo = int(accum.GetMin()[0]) hi = int(accum.GetMax()[0]) # TODO: pending resolution of bug 1822, run the thresholding # in single threaded mode to avoid data corruption observed on mac release # builds thresholder = vtk.vtkImageThreshold() thresholder.SetNumberOfThreads(1) for i in xrange(lo,hi+1): self.statusText( "Splitting label %d..."%i ) if vtk.VTK_MAJOR_VERSION <= 5: thresholder.SetInput( merge.GetImageData() ) else: thresholder.SetInputConnection( merge.GetImageDataConnection() ) thresholder.SetInValue( i ) thresholder.SetOutValue( 0 ) thresholder.ReplaceInOn() thresholder.ReplaceOutOn() thresholder.ThresholdBetween( i, i ) thresholder.SetOutputScalarType( merge.GetImageData().GetScalarType() ) thresholder.Update() if thresholder.GetOutput().GetScalarRange() != (0.0, 0.0): labelName = colorNode.GetColorName(i) self.statusText( "Creating structure volume %s..."%labelName ) structureVolume = self.structureVolume( labelName ) if not structureVolume: self.addStructure( i, "noEdit" ) structureVolume = self.structureVolume( labelName ) structureVolume.GetImageData().DeepCopy( thresholder.GetOutput() ) self.editUtil.markVolumeNodeAsModified(structureVolume) self.statusText( "Finished splitting." )
def onLabelSelect(self, node): """ Set the label volume node. Read the image and determine the number of label values. Check if other buttons can be enabled """ self.labelNode = node if bool(self.labelNode): #find the correct number of label values stataccum = vtk.vtkImageAccumulate() stataccum.SetInputData(self.labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) self.labelValueSelector.setRange(lo,hi) self.labelValueSelector.setEnabled(True) self.totalLabels = (hi-lo)+1 else: self.labelValueSelector.setEnabled(False) self.parameterSetButton.enabled = bool(self.grayscaleNode) and bool(self.labelNode) self.calculateButton.enabled = bool(self.grayscaleNode) and bool(self.labelNode) and bool(self.cliNodes)
def onLabelSelect(self, node): """ Set the label volume node. Read the image and determine the number of label values. Check if other buttons can be enabled """ self.labelNode = node if bool(self.labelNode): #find the correct number of label values stataccum = vtk.vtkImageAccumulate() stataccum.SetInputData(self.labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) self.labelValueSelector.setRange(lo, hi) self.labelValueSelector.setEnabled(True) self.totalLabels = (hi - lo) + 1 else: self.labelValueSelector.setEnabled(False) self.parameterSetButton.enabled = bool(self.grayscaleNode) and bool( self.labelNode) self.calculateButton.enabled = bool(self.grayscaleNode) and bool( self.labelNode) and bool(self.cliNodes)
def split(self): """split the merge volume into individual structures""" self.statusText("Splitting...") merge = self.mergeVolume() if not merge: return colorNode = merge.GetDisplayNode().GetColorNode() accum = vtk.vtkImageAccumulate() accum.SetInput(merge.GetImageData()) accum.Update() lo = int(accum.GetMin()[0]) hi = int(accum.GetMax()[0]) thresholder = vtk.vtkImageThreshold() for i in xrange(lo, hi + 1): self.statusText("Splitting label %d..." % i) thresholder.SetInput(merge.GetImageData()) thresholder.SetInValue(i) thresholder.SetOutValue(0) thresholder.ReplaceInOn() thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i, i) thresholder.SetOutputScalarType( merge.GetImageData().GetScalarType()) thresholder.Update() if thresholder.GetOutput().GetScalarRange() != (0.0, 0.0): labelName = colorNode.GetColorName(i) self.statusText("Creating structure volume %s..." % labelName) structureVolume = self.structureVolume(labelName) if not structureVolume: self.addStructure(i, "noEdit") structureVolume = self.structureVolume(labelName) structureVolume.GetImageData().DeepCopy( thresholder.GetOutput()) structureVolume.Modified() self.statusText("Finished splitting.")
def split(self): """split the merge volume into individual structures""" self.statusText( "Splitting..." ) merge = self.mergeVolume() if not merge: return colorNode = merge.GetDisplayNode().GetColorNode() accum = vtk.vtkImageAccumulate() accum.SetInput(merge.GetImageData()) accum.Update() lo = int(accum.GetMin()[0]) hi = int(accum.GetMax()[0]) thresholder = vtk.vtkImageThreshold() for i in xrange(lo,hi+1): self.statusText( "Splitting label %d..."%i ) thresholder.SetInput( merge.GetImageData() ) thresholder.SetInValue( i ) thresholder.SetOutValue( 0 ) thresholder.ReplaceInOn() thresholder.ReplaceOutOn() thresholder.ThresholdBetween( i, i ) thresholder.SetOutputScalarType( merge.GetImageData().GetScalarType() ) thresholder.Update() if thresholder.GetOutput().GetScalarRange() != (0.0, 0.0): labelName = colorNode.GetColorName(i) self.statusText( "Creating structure volume %s..."%labelName ) structureVolume = self.structureVolume( labelName ) if not structureVolume: self.addStructure( i, "noEdit" ) structureVolume = self.structureVolume( labelName ) structureVolume.GetImageData().DeepCopy( thresholder.GetOutput() ) structureVolume.Modified() self.statusText( "Finished splitting." )
def getVolumeIm(self,im,minValue=1): thresh = vtk.vtkImageThreshold() thresh.SetInputData(im) max = im.GetScalarRange()[1] thresh.ThresholdBetween(minValue, max) thresh.SetInValue(1) thresh.SetOutValue(0) thresh.SetOutputScalarType(im.GetScalarType()) thresh.Modified() thresh.Update() stencil = vtk.vtkImageToImageStencil() stencil.SetInputData(thresh.GetOutput()) stencil.ThresholdBetween(1, 1) stencil.Update() stat1 = vtk.vtkImageAccumulate() stat1.SetInputData(im) stat1.SetStencilData(stencil.GetOutput()) stat1.Update() return stat1.GetVoxelCount()
def __init__(self, grayscaleNode, labelNode, fileName=None): #import numpy self.keys = ("Index", "Count", "Volume mm^3", "Volume cc", "Min", "Max", "Mean", "StdDev") cubicMMPerVoxel = reduce(lambda x, y: x * y, labelNode.GetSpacing()) ccPerCubicMM = 0.001 # TODO: progress and status updates # this->InvokeEvent(vtkLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) for i in xrange(lo, hi + 1): # this->SetProgress((float)i/hi); # std::string event_message = "Label "; std::stringstream s; s << i; event_message.append(s.str()); # this->InvokeEvent(vtkLabelStatisticsLogic::LabelStatsOuterLoop, (void*)event_message.c_str()); # logic copied from slicer3 LabelStatistics # to create the binary volume of the label # //logic copied from slicer2 LabelStatistics MaskStat # // create the binary volume of the label thresholder = vtk.vtkImageThreshold() thresholder.SetInput(labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i, i) thresholder.SetOutputScalarType( grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i, "Index"] = i self.labelStats[i, "Count"] = stat1.GetVoxelCount() self.labelStats[i, "Volume mm^3"] = self.labelStats[ i, "Count"] * cubicMMPerVoxel self.labelStats[i, "Volume cc"] = self.labelStats[ i, "Volume mm^3"] * ccPerCubicMM self.labelStats[i, "Min"] = stat1.GetMin()[0] self.labelStats[i, "Max"] = stat1.GetMax()[0] self.labelStats[i, "Mean"] = stat1.GetMean()[0] self.labelStats[i, "StdDev"] = stat1.GetStandardDeviation()[0]
def __init__(self, grayscaleNode, labelNode, KEV120, KEV80, fileName=None): #import numpy self.keys = ("Index", "Label Name", "Agatston Score", "Count", "Volume mm^3", "Volume cc", "Min", "Max", "Mean", "StdDev") cubicMMPerVoxel = reduce(lambda x, y: x * y, labelNode.GetSpacing()) ccPerCubicMM = 0.001 # TODO: progress and status updates # this->InvokeEvent(vtkLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() if vtk.VTK_MAJOR_VERSION <= 5: stataccum.SetInput(labelNode.GetImageData()) else: stataccum.SetInputConnection(labelNode.GetImageDataConnection()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) displayNode = labelNode.GetDisplayNode() colorNode = displayNode.GetColorNode() self.labelNode = labelNode self.grayscaleNode = grayscaleNode self.KEV80 = KEV80 self.KEV120 = KEV120 self.calculateAgatstonScores() for i in xrange(lo, 7): # skip indices 0 (background) and 1 (default threshold pixels) # because these are not calcium and do not have an Agatston score if i == 0 or i == 1: continue # this->SetProgress((float)i/hi); # std::string event_message = "Label "; std::stringstream s; s << i; event_message.append(s.str()); # this->InvokeEvent(vtkLabelStatisticsLogic::LabelStatsOuterLoop, (void*)event_message.c_str()); # logic copied from slicer3 LabelStatistics # to create the binary volume of the label # //logic copied from slicer2 LabelStatistics MaskStat # // create the binary volume of the label thresholder = vtk.vtkImageThreshold() if vtk.VTK_MAJOR_VERSION <= 5: thresholder.SetInput(labelNode.GetImageData()) else: thresholder.SetInputConnection( labelNode.GetImageDataConnection()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() if i != 6: thresholder.ThresholdBetween(i, i) else: # label 6 is the total calcium pixels in labels 2, 3, 4 and 5 thresholder.ThresholdBetween(2, 5) thresholder.SetOutputScalarType( grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() if vtk.VTK_MAJOR_VERSION <= 5: stencil.SetInput(thresholder.GetOutput()) else: stencil.SetInputConnection(thresholder.GetOutputPort()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() if vtk.VTK_MAJOR_VERSION <= 5: stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) else: stat1.SetInputConnection( grayscaleNode.GetImageDataConnection()) stencil.Update() stat1.SetStencilData(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i, "Index"] = i self.labelStats[i, "Label Name"] = colorNode.GetColorName(i) self.labelStats[ i, "Agatston Score"] = self.AgatstonScoresPerLabel[i] self.labelStats[i, "Count"] = stat1.GetVoxelCount() self.labelStats[i, "Volume mm^3"] = self.labelStats[ i, "Count"] * cubicMMPerVoxel self.labelStats[i, "Volume cc"] = self.labelStats[ i, "Volume mm^3"] * ccPerCubicMM self.labelStats[i, "Min"] = stat1.GetMin()[0] self.labelStats[i, "Max"] = stat1.GetMax()[0] self.labelStats[i, "Mean"] = stat1.GetMean()[0] self.labelStats[i, "StdDev"] = stat1.GetStandardDeviation()[0]
def __init__(self, grayscaleNode, labelNode, fileName=None): import numpy self.keys = ("Index", "Count", "Volume", "Min", "Max", "Mean", "StdDev") cubicMMPerVoxel = reduce(lambda x,y: x*y, labelNode.GetSpacing()) # TODO: progress and status updates # this->InvokeEvent(vtkLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) for i in xrange(lo,hi+1): # this->SetProgress((float)i/hi); # std::string event_message = "Label "; std::stringstream s; s << i; event_message.append(s.str()); # this->InvokeEvent(vtkLabelStatisticsLogic::LabelStatsOuterLoop, (void*)event_message.c_str()); # logic copied from slicer3 LabelStatistics # to create the binary volume of the label # //logic copied from slicer2 LabelStatistics MaskStat # // create the binary volume of the label thresholder = vtk.vtkImageThreshold() thresholder.SetInput(labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i,i) thresholder.SetOutputScalarType(grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i,"Index"] = i self.labelStats[i,"Count"] = stat1.GetVoxelCount() self.labelStats[i,"Volume"] = self.labelStats[i,"Count"] * cubicMMPerVoxel self.labelStats[i,"Min"] = stat1.GetMin()[0] self.labelStats[i,"Max"] = stat1.GetMax()[0] self.labelStats[i,"Mean"] = stat1.GetMean()[0] self.labelStats[i,"StdDev"] = stat1.GetStandardDeviation()[0]
def __init__(self, grayscaleNode, labelNode, fileName=None): #import numpy self.keys = ( "Type", "Percentage", "Volume liters", "Mean", "StdDev", "Min", "Max", ) self.types = ("Normal", "PS", "PL", "CL1", "CL2", "CL3") cubicMMPerVoxel = reduce(lambda x, y: x * y, labelNode.GetSpacing()) litersPerCubicMM = 0.000001 # TODO: progress and status updates # this->InvokeEvent(vtkEmphysemaSubtypesLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(labelNode.GetImageData()) stataccum.Update() lo = 1 hi = 6 for i in xrange(lo, hi + 1): # this->SetProgress((float)i/hi); # std::string event_message = "Label "; std::stringstream s; s << i; event_message.append(s.str()); # this->InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsOuterLoop, (void*)event_message.c_str()); # logic copied from slicer3 EmphysemaSubtypes # to create the binary volume of the label # //logic copied from slicer2 EmphysemaSubtypes MaskStat # // create the binary volume of the label thresholder = vtk.vtkImageThreshold() thresholder.SetInput(labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i, i) thresholder.SetOutputScalarType( grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i, "Type"] = self.types[i - 1] self.labelStats[i, "Percentage"] = float(stat1.GetVoxelCount()) self.labelStats[i, "Volume liters"] = float( stat1.GetVoxelCount() * cubicMMPerVoxel * litersPerCubicMM) self.labelStats[i, "Min"] = float(stat1.GetMin()[0]) self.labelStats[i, "Max"] = float(stat1.GetMax()[0]) self.labelStats[i, "Mean"] = float(stat1.GetMean()[0]) self.labelStats[i, "StdDev"] = float( stat1.GetStandardDeviation()[0]) # this.InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsInnerLoop, (void*)"1") # this.InvokeEvent(vtkEmphysemaSubtypesLogic::EndLabelStats, (void*)"end label stats") total = 0 for i in xrange(lo, hi + 1): total = total + self.labelStats[i, "Percentage"] for i in xrange(lo, hi + 1): self.labelStats[i, "Percentage"] = float( 100.0 * self.labelStats[i, "Percentage"] / total)
def __init__(self, grayscaleNode, labelNode, KEV120, KEV80, fileName=None): #import numpy self.keys = ("Index", "Label Name", "Agatston Score", "Count", "Volume mm^3", "Volume cc", "Min", "Max", "Mean", "StdDev") cubicMMPerVoxel = reduce(lambda x,y: x*y, labelNode.GetSpacing()) ccPerCubicMM = 0.001 # TODO: progress and status updates # this->InvokeEvent(vtkLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() if vtk.VTK_MAJOR_VERSION <= 5: stataccum.SetInput(labelNode.GetImageData()) else: stataccum.SetInputConnection(labelNode.GetImageDataConnection()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) displayNode = labelNode.GetDisplayNode() colorNode = displayNode.GetColorNode() self.labelNode = labelNode self.grayscaleNode = grayscaleNode self.KEV80 = KEV80 self.KEV120 = KEV120 self.calculateAgatstonScores() for i in xrange(lo,7): # skip indices 0 (background) and 1 (default threshold pixels) # because these are not calcium and do not have an Agatston score if i == 0 or i == 1: continue # this->SetProgress((float)i/hi); # std::string event_message = "Label "; std::stringstream s; s << i; event_message.append(s.str()); # this->InvokeEvent(vtkLabelStatisticsLogic::LabelStatsOuterLoop, (void*)event_message.c_str()); # logic copied from slicer3 LabelStatistics # to create the binary volume of the label # //logic copied from slicer2 LabelStatistics MaskStat # // create the binary volume of the label thresholder = vtk.vtkImageThreshold() if vtk.VTK_MAJOR_VERSION <= 5: thresholder.SetInput(labelNode.GetImageData()) else: thresholder.SetInputConnection(labelNode.GetImageDataConnection()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() if i != 6: thresholder.ThresholdBetween(i,i) else: # label 6 is the total calcium pixels in labels 2, 3, 4 and 5 thresholder.ThresholdBetween(2,5) thresholder.SetOutputScalarType(grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() if vtk.VTK_MAJOR_VERSION <= 5: stencil.SetInput(thresholder.GetOutput()) else: stencil.SetInputConnection(thresholder.GetOutputPort()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() if vtk.VTK_MAJOR_VERSION <= 5: stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) else: stat1.SetInputConnection(grayscaleNode.GetImageDataConnection()) stencil.Update() stat1.SetStencilData(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i,"Index"] = i self.labelStats[i,"Label Name"] = colorNode.GetColorName(i) self.labelStats[i,"Agatston Score"] = self.AgatstonScoresPerLabel[i] self.labelStats[i,"Count"] = stat1.GetVoxelCount() self.labelStats[i,"Volume mm^3"] = self.labelStats[i,"Count"] * cubicMMPerVoxel self.labelStats[i,"Volume cc"] = self.labelStats[i,"Volume mm^3"] * ccPerCubicMM self.labelStats[i,"Min"] = stat1.GetMin()[0] self.labelStats[i,"Max"] = stat1.GetMax()[0] self.labelStats[i,"Mean"] = stat1.GetMean()[0] self.labelStats[i,"StdDev"] = stat1.GetStandardDeviation()[0]
def __init__(self, grayscaleNode, labelNode, fileName=None): #import numpy numOfDataNodes = grayscaleNode.GetNumberOfDataNodes() if labelNode.IsA("vtkMRMLSequenceNode"): numOfDataNodes2 = labelNode.GetNumberOfDataNodes() if numOfDataNodes != numOfDataNodes2: sys.stderr.write( 'Number of nodes in the grayscale volume sequence does not match the number of nodes in the labelmap volume sequence' ) return self.keys = ("Index", "Count", "Volume mm^3", "Volume cc", "Min", "Max", "Mean", "StdDev") if labelNode.IsA("vtkMRMLSequenceNode"): cubicMMPerVoxel = reduce(lambda x, y: x * y, labelNode.GetNthDataNode(0).GetSpacing()) elif labelNode.IsA("vtkMRMLScalarVolumeNode"): cubicMMPerVoxel = reduce(lambda x, y: x * y, labelNode.GetSpacing()) ccPerCubicMM = 0.001 # TODO: progress and status updates # this->InvokeEvent(vtkSequenceLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() isLabelmapSequence = labelNode.IsA("vtkMRMLSequenceNode") if isLabelmapSequence: stataccumInput = labelNode.GetNthDataNode(0).GetImageData() else: stataccumInput = labelNode.GetImageData() if vtk.VTK_MAJOR_VERSION <= 5: stataccum.SetInput(stataccumInput) else: stataccum.SetInputData(stataccumInput) stataccum.Update() # lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) # We select the highest label index for analysis selectedLabelIndex = hi for i in xrange(numOfDataNodes): # this->SetProgress((float)i/hi); # std::string event_message = "Label "; std::stringstream s; s << i; event_message.append(s.str()); # this->InvokeEvent(vtkSequenceLabelStatisticsLogic::LabelStatsOuterLoop, (void*)event_message.c_str()); # logic copied from slicer3 SequenceLabelStatistics # to create the binary volume of the label # //logic copied from slicer2 SequenceLabelStatistics MaskStat # // create the binary volume of the label if isLabelmapSequence: stataccumInput = labelNode.GetNthDataNode(0).GetImageData() if vtk.VTK_MAJOR_VERSION <= 5: stataccum.SetInput(stataccumInput) else: stataccum.SetInputData(stataccumInput) thresholder = vtk.vtkImageThreshold() if vtk.VTK_MAJOR_VERSION <= 5: thresholder.SetInput(stataccumInput) else: thresholder.SetInputData(stataccumInput) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(selectedLabelIndex, selectedLabelIndex) thresholder.SetOutputScalarType( grayscaleNode.GetNthDataNode(i).GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkSequenceLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() if vtk.VTK_MAJOR_VERSION <= 5: stencil.SetInput(thresholder.GetOutput()) else: stencil.SetInputData(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkSequenceLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() if vtk.VTK_MAJOR_VERSION <= 5: stat1.SetInput(grayscaleNode.GetNthDataNode(i).GetImageData()) stat1.SetStencil(stencil.GetOutput()) else: stat1.SetInputData( grayscaleNode.GetNthDataNode(i).GetImageData()) stat1.SetStencilData(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkSequenceLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i, "Index"] = i self.labelStats[i, "Count"] = stat1.GetVoxelCount() self.labelStats[i, "Volume mm^3"] = self.labelStats[ i, "Count"] * cubicMMPerVoxel self.labelStats[i, "Volume cc"] = self.labelStats[ i, "Volume mm^3"] * ccPerCubicMM self.labelStats[i, "Min"] = stat1.GetMin()[0] self.labelStats[i, "Max"] = stat1.GetMax()[0] self.labelStats[i, "Mean"] = stat1.GetMean()[0] self.labelStats[i, "StdDev"] = stat1.GetStandardDeviation()[0]
def __init__(self, grayscaleNode, labelNode, fileName=None): #import numpy self.keys = ("Type", "Percentage", "Volume liters", "Mean", "StdDev", "Min", "Max", ) self.types = ("Normal", "PS", "PL", "CL1", "CL2", "CL3") cubicMMPerVoxel = reduce(lambda x,y: x*y, labelNode.GetSpacing()) litersPerCubicMM = 0.000001 # TODO: progress and status updates # this->InvokeEvent(vtkEmphysemaSubtypesLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(labelNode.GetImageData()) stataccum.Update() lo = 1 hi = 6 for i in xrange(lo,hi+1): # this->SetProgress((float)i/hi); # std::string event_message = "Label "; std::stringstream s; s << i; event_message.append(s.str()); # this->InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsOuterLoop, (void*)event_message.c_str()); # logic copied from slicer3 EmphysemaSubtypes # to create the binary volume of the label # //logic copied from slicer2 EmphysemaSubtypes MaskStat # // create the binary volume of the label thresholder = vtk.vtkImageThreshold() thresholder.SetInput(labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i,i) thresholder.SetOutputScalarType(grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i,"Type"] = self.types[i-1] self.labelStats[i,"Percentage"] = float(stat1.GetVoxelCount()) self.labelStats[i,"Volume liters"] = float(stat1.GetVoxelCount() * cubicMMPerVoxel * litersPerCubicMM) self.labelStats[i,"Min"] = float(stat1.GetMin()[0]) self.labelStats[i,"Max"] = float(stat1.GetMax()[0]) self.labelStats[i,"Mean"] = float(stat1.GetMean()[0]) self.labelStats[i,"StdDev"] = float(stat1.GetStandardDeviation()[0]) # this.InvokeEvent(vtkEmphysemaSubtypesLogic::LabelStatsInnerLoop, (void*)"1") # this.InvokeEvent(vtkEmphysemaSubtypesLogic::EndLabelStats, (void*)"end label stats") total=0 for i in xrange(lo,hi+1): total = total + self.labelStats[i,"Percentage"] for i in xrange(lo,hi+1): self.labelStats[i,"Percentage"] = float(100.0 * self.labelStats[i,"Percentage"]/total)
def ComputeLabelStatistics(self, grayscaleNode, inputlabelNode): """ Computes label statistics for input label node on grayscale volume (mean, std, volume, etc) """ # Print to Slicer CLI print ("Computing Label Statistics..."), start_time = time.time() # resample the label to the space of the grayscale if needed volumesLogic = slicer.modules.volumes.logic() warnings = volumesLogic.CheckForLabelVolumeValidity(grayscaleNode, inputlabelNode) if warnings != "": if "mismatch" in warnings: inputlabelNode = volumesLogic.ResampleVolumeToReferenceVolume(inputlabelNode, grayscaleNode) # Set up Stat accumulator stataccum = vtk.vtkImageAccumulate() stataccum.SetInputConnection(inputlabelNode.GetImageDataConnection()) stataccum.Update() InputLabelValue = int(stataccum.GetMax()[0]) # logic copied from slicer3 LabelStatistics thresholder = {} thresholder = vtk.vtkImageThreshold() thresholder.SetInputConnection(inputlabelNode.GetImageDataConnection()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(InputLabelValue, InputLabelValue) thresholder.SetOutputScalarType(grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInputConnection(thresholder.GetOutputPort()) stencil.ThresholdBetween(1, 1) stencil.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() stat1.SetInputConnection(grayscaleNode.GetImageDataConnection()) stat1.SetStencilData(stencil.GetOutput()) stat1.Update() ### List of possible label stats available ### # cubicMMPerVoxel = reduce(lambda x,y: x*y, inputlabelNode.GetSpacing()) # ccPerCubicMM = 0.001 # labelStats["Labels"].append(i) # labelStats[i,"Index"] = i # labelStats[i,"Count"] = stat1.GetVoxelCount() # labelStats[i,"Volume mm^3"] = labelStats[i,"Count"] * cubicMMPerVoxel # labelStats[i,"Volume cc"] = labelStats[i,"Volume mm^3"] * ccPerCubicMM # labelStats[i,"Min"] = stat1.GetMin()[0] # labelStats[i,"Max"] = stat1.GetMax()[0] # labelStats[i,"Mean"] = stat1.GetMean()[0] # labelStats[i,"StdDev"] = stat1.GetStandardDeviation()[0] # print to Slicer CLI end_time = time.time() print ("done (%0.2f s)") % float(end_time - start_time) # Return desired stats to variables return stat1.GetMean()[0], stat1.GetStandardDeviation()[0]
def getROIstatsIM(self,volumeIm,ROIIm): im=volumeIm roiim=ROIIm ROIStats={} if im and roiim: dim3=im.GetDimensions()[2] stencil = vtk.vtkImageToImageStencil() stencil.SetInputData(roiim) stencilsl = vtk.vtkImageToImageStencil() numrois=int(roiim.GetScalarRange()[1]) stat1 = vtk.vtkImageAccumulate() stat1.SetInputData(im) for roi in range(1,numrois+1): #print("roi " + str(roi)) fg=roi stencil.ThresholdBetween(fg, fg) stencil.Update() stat1.SetStencilData(stencil.GetOutput()) stat1.Update() Sstats={} stats={} stats['count']=stat1.GetVoxelCount() stats['min']=stat1.GetMin()[0] stats['max']=stat1.GetMax()[0] stats['mean']=stat1.GetMean()[0] stats['sd']=stat1.GetStandardDeviation()[0] Sstats['volume']=stats for d3 in range(dim3): #print("slice " + str(d3)) eV=vtk.vtkExtractVOI() VOI=[0,im.GetDimensions()[0],0,im.GetDimensions()[1],d3,d3] eV.SetVOI(VOI) eV.SetInputData(im) eV.Update() slice=eV.GetOutput() eVroi=vtk.vtkExtractVOI() eVroi.SetVOI(VOI) eVroi.SetInputData(roiim) eVroi.Update() roislice=eVroi.GetOutput() stencilsl.SetInputData(roislice) stencilsl.ThresholdBetween(fg, fg) stencilsl.Update() statsl = vtk.vtkImageAccumulate() statsl.SetInputData(slice) statsl.SetStencilData(stencilsl.GetOutput()) statsl.Update() stats={} stats['count']=statsl.GetVoxelCount() stats['min']=statsl.GetMin()[0] stats['max']=statsl.GetMax()[0] stats['mean']=statsl.GetMean()[0] stats['sd']=statsl.GetStandardDeviation()[0] Sstats[d3]=stats ROIStats[roi]=Sstats return ROIStats return None
def __init__(self, grayscaleNode, labelNode, fileName=None): #import numpy self.keys = ("Index", "Count", "Volume mm^3", "Volume cc", "Min", "Max", "Mean", "StdDev") cubicMMPerVoxel = reduce(lambda x,y: x*y, labelNode.GetSpacing()) ccPerCubicMM = 0.001 #geting a set of labels from a label map labels = self.getLabelsFromLabelMap(labelNode) print "NON - ZERO LABELS-----------------------------" print labels # TODO: progress and status updates # this->InvokeEvent(vtkLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) aux1=999999999999999999999999999999999 aux2=999999999999999999999999999999999 for i in xrange(lo,hi+1): print i thresholder = vtk.vtkImageThreshold() thresholder.SetInput(labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i,i) thresholder.SetOutputScalarType(grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i,"Index"] = i self.labelStats[i,"Count"] = stat1.GetVoxelCount() print stat1.GetVoxelCount() self.labelStats[i,"Volume mm^3"] = self.labelStats[i,"Count"] * cubicMMPerVoxel self.labelStats[i,"Volume cc"] = self.labelStats[i,"Volume mm^3"] * ccPerCubicMM self.labelStats[i,"Min"] = stat1.GetMin()[0] self.labelStats[i,"Max"] = stat1.GetMax()[0] self.labelStats[i,"Mean"] = stat1.GetMean()[0] self.labelStats[i,"StdDev"] = stat1.GetStandardDeviation()[0] aux1= stat1.GetVoxelCount() if aux1 < aux2 : aux2 = aux1 print "el menor" print aux2
def getSmallestSegment(self): import numpy self.keys = ("Index", "Count", "Volume mm^3", "Volume cc", "Min", "Max", "Mean", "StdDev") cubicMMPerVoxel = reduce(lambda x, y: x * y, self.labelNode.GetSpacing()) ccPerCubicMM = 0.001 #geting a set of labels from a label map labels = self.getLabelsFromLabelMap(self.labelNode) print "NON - ZERO LABELS-----------------------------" print labels # TODO: progress and status updates # this->InvokeEvent(vtkLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(self.labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) aux1 = 999999999999999999999999999999999 aux2 = 999999999999999999999999999999999 smallestLabel = 0 segmentCount = 0 #This loop finds the most pathological segment(the smallest one) for i in xrange(lo, hi + 1): print i thresholder = vtk.vtkImageThreshold() thresholder.SetInput(self.labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i, i) thresholder.SetOutputScalarType( self.grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") self.stat = vtk.vtkImageAccumulate() stat1 = vtk.vtkImageAccumulate() stat1.SetInput(self.grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i, "Index"] = i self.labelStats[i, "Count"] = stat1.GetVoxelCount() print stat1.GetVoxelCount() self.labelStats[i, "Volume mm^3"] = self.labelStats[ i, "Count"] * cubicMMPerVoxel self.labelStats[i, "Volume cc"] = self.labelStats[ i, "Volume mm^3"] * ccPerCubicMM self.labelStats[i, "Min"] = stat1.GetMin()[0] self.labelStats[i, "Max"] = stat1.GetMax()[0] self.labelStats[i, "Mean"] = stat1.GetMean()[0] self.labelStats[i, "StdDev"] = stat1.GetStandardDeviation()[0] aux1 = stat1.GetVoxelCount() segmentCount = i self.segmentCount = i if aux1 < aux2: aux2 = aux1 smallestLabel = i print "el menor" print aux2 print smallestLabel print "numero total de segmentos" print segmentCount # Looking for the segment next to the most pathological one ar = slicer.util.array('*label') w = numpy.transpose(numpy.where(ar == smallestLabel)) self.smallestSegmentMatrix = w lenght = len(w) - 1 self.sSegmentIJK = w[0] sSegmentTopSlice = w[lenght][0] sSegmentBottomSlice = w[0][0] self.sSegmentTopSlice = w[lenght][0] self.sSegmentBottomSlice = w[0][0] print sSegmentTopSlice print sSegmentBottomSlice closeSegmentBot = 0 csbMatrix = 0 closeSegmentTop = 0 cstMatrix = 0 for c in xrange(2, segmentCount): w = numpy.transpose(numpy.where(ar == c)) m1 = sSegmentBottomSlice - 2 m2 = sSegmentTopSlice + 2 laux = len(w) - 1 if (w[0][0] == m2): print "segmento cercano inferior" print c print w closeSegmentBot = c csbMatrix = w if (w[laux][0] == m1): print "segmento cercano superior" print c closeSegmentTop = c cstMatrix = w self.closeSegmentBottomMatrix = csbMatrix self.closeSegmentTopMatrix = cstMatrix
def getSmallestSegment(self): import numpy self.keys = ("Index", "Count", "Volume mm^3", "Volume cc", "Min", "Max", "Mean", "StdDev") cubicMMPerVoxel = reduce(lambda x,y: x*y, self.labelNode.GetSpacing()) ccPerCubicMM = 0.001 #geting a set of labels from a label map labels = self.getLabelsFromLabelMap(self.labelNode) print "NON - ZERO LABELS-----------------------------" print labels # TODO: progress and status updates # this->InvokeEvent(vtkLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(self.labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) aux1=999999999999999999999999999999999 aux2=999999999999999999999999999999999 smallestLabel=0 segmentCount=0 #This loop finds the most pathological segment(the smallest one) for i in xrange(lo,hi+1): print i thresholder = vtk.vtkImageThreshold() thresholder.SetInput(self.labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i,i) thresholder.SetOutputScalarType(self.grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") self.stat= vtk.vtkImageAccumulate() stat1 = vtk.vtkImageAccumulate() stat1.SetInput(self.grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i,"Index"] = i self.labelStats[i,"Count"] = stat1.GetVoxelCount() print stat1.GetVoxelCount() self.labelStats[i,"Volume mm^3"] = self.labelStats[i,"Count"] * cubicMMPerVoxel self.labelStats[i,"Volume cc"] = self.labelStats[i,"Volume mm^3"] * ccPerCubicMM self.labelStats[i,"Min"] = stat1.GetMin()[0] self.labelStats[i,"Max"] = stat1.GetMax()[0] self.labelStats[i,"Mean"] = stat1.GetMean()[0] self.labelStats[i,"StdDev"] = stat1.GetStandardDeviation()[0] aux1= stat1.GetVoxelCount() segmentCount=i self.segmentCount=i if aux1 < aux2 : aux2 = aux1 smallestLabel= i print "el menor" print aux2 print smallestLabel print "numero total de segmentos" print segmentCount # Looking for the segment next to the most pathological one ar = slicer.util.array('*label') w = numpy.transpose(numpy.where(ar==smallestLabel)) self.smallestSegmentMatrix = w lenght= len(w)-1 self.sSegmentIJK=w[0] sSegmentTopSlice= w[lenght][0] sSegmentBottomSlice= w[0][0] self.sSegmentTopSlice= w[lenght][0] self.sSegmentBottomSlice= w[0][0] print sSegmentTopSlice print sSegmentBottomSlice closeSegmentBot=0 csbMatrix = 0 closeSegmentTop=0 cstMatrix = 0 for c in xrange(2,segmentCount): w = numpy.transpose(numpy.where(ar==c)) m1=sSegmentBottomSlice - 2 m2=sSegmentTopSlice + 2 laux = len(w)-1 if (w[0][0]== m2): print "segmento cercano inferior" print c print w closeSegmentBot = c csbMatrix = w if (w[laux][0] == m1): print "segmento cercano superior" print c closeSegmentTop = c cstMatrix = w self.closeSegmentBottomMatrix=csbMatrix self.closeSegmentTopMatrix = cstMatrix
def __init__(self, grayscaleNode, labelNode, fileName=None): #import numpy self.keys = ("Index", "Count", "Volume mm^3", "Volume cc", "Min", "Max", "Mean", "StdDev") cubicMMPerVoxel = reduce(lambda x, y: x * y, labelNode.GetSpacing()) ccPerCubicMM = 0.001 #geting a set of labels from a label map labels = self.getLabelsFromLabelMap(labelNode) print "NON - ZERO LABELS-----------------------------" print labels # TODO: progress and status updates # this->InvokeEvent(vtkLabelStatisticsLogic::StartLabelStats, (void*)"start label stats") self.labelStats = {} self.labelStats['Labels'] = [] stataccum = vtk.vtkImageAccumulate() stataccum.SetInput(labelNode.GetImageData()) stataccum.Update() lo = int(stataccum.GetMin()[0]) hi = int(stataccum.GetMax()[0]) aux1 = 999999999999999999999999999999999 aux2 = 999999999999999999999999999999999 for i in xrange(lo, hi + 1): print i thresholder = vtk.vtkImageThreshold() thresholder.SetInput(labelNode.GetImageData()) thresholder.SetInValue(1) thresholder.SetOutValue(0) thresholder.ReplaceOutOn() thresholder.ThresholdBetween(i, i) thresholder.SetOutputScalarType( grayscaleNode.GetImageData().GetScalarType()) thresholder.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25"); # use vtk's statistics class with the binary labelmap as a stencil stencil = vtk.vtkImageToImageStencil() stencil.SetInput(thresholder.GetOutput()) stencil.ThresholdBetween(1, 1) # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.5") stat1 = vtk.vtkImageAccumulate() stat1.SetInput(grayscaleNode.GetImageData()) stat1.SetStencil(stencil.GetOutput()) stat1.Update() # this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.75") if stat1.GetVoxelCount() > 0: # add an entry to the LabelStats list self.labelStats["Labels"].append(i) self.labelStats[i, "Index"] = i self.labelStats[i, "Count"] = stat1.GetVoxelCount() print stat1.GetVoxelCount() self.labelStats[i, "Volume mm^3"] = self.labelStats[ i, "Count"] * cubicMMPerVoxel self.labelStats[i, "Volume cc"] = self.labelStats[ i, "Volume mm^3"] * ccPerCubicMM self.labelStats[i, "Min"] = stat1.GetMin()[0] self.labelStats[i, "Max"] = stat1.GetMax()[0] self.labelStats[i, "Mean"] = stat1.GetMean()[0] self.labelStats[i, "StdDev"] = stat1.GetStandardDeviation()[0] aux1 = stat1.GetVoxelCount() if aux1 < aux2: aux2 = aux1 print "el menor" print aux2