Esempio n. 1
0
  def growCut(self):
    growCutFilter = vtkITK.vtkITKGrowCutSegmentationImageFilter()
    background = self.getScopedBackground()
    gestureInput = self.getScopedLabelInput()
    growCutOutput = self.getScopedLabelOutput()

    if not self.areInputsValid():
      logging.warning(self.getInvalidInputsMessage())

    # set the make a zero-valued volume for the output
    # TODO: maybe this should be done in numpy as a one-liner
    thresh = vtk.vtkImageThreshold()
    thresh.ReplaceInOn()
    thresh.ReplaceOutOn()
    thresh.SetInValue(0)
    thresh.SetOutValue(0)
    thresh.SetOutputScalarType( vtk.VTK_SHORT )
    if vtk.VTK_MAJOR_VERSION <= 5:
      thresh.SetInput( gestureInput )
    else:
      thresh.SetInputData( gestureInput )
    thresh.SetOutput( growCutOutput )
    thresh.Update()
    growCutOutput.DeepCopy( gestureInput )

    if vtk.VTK_MAJOR_VERSION <= 5:
      growCutFilter.SetInput( 0, background )
      growCutFilter.SetInput( 1, gestureInput )
      growCutFilter.SetInput( 2, growCutOutput )
    else:
      growCutFilter.SetInputData( 0, background )
      growCutFilter.SetInputData( 1, gestureInput )
      growCutFilter.SetInputConnection( 2, thresh.GetOutputPort() )

    objectSize = 5. # TODO: this is a magic number
    contrastNoiseRatio = 0.8 # TODO: this is a magic number
    priorStrength = 0.003 # TODO: this is a magic number
    segmented = 2 # TODO: this is a magic number
    conversion = 1000 # TODO: this is a magic number

    spacing = gestureInput.GetSpacing()
    voxelVolume = reduce(lambda x,y: x*y, spacing)
    voxelAmount = objectSize / voxelVolume
    voxelNumber = round(voxelAmount) * conversion

    cubeRoot = 1./3.
    oSize = int(round(pow(voxelNumber,cubeRoot)))

    growCutFilter.SetObjectSize( oSize )
    growCutFilter.SetContrastNoiseRatio( contrastNoiseRatio )
    growCutFilter.SetPriorSegmentConfidence( priorStrength )
    growCutFilter.Update()

    growCutOutput.DeepCopy( growCutFilter.GetOutput() )

    self.applyScopedLabel()
def growCut(background, gestureInput, growCutOutput):
    growCutFilter = vtkITK.vtkITKGrowCutSegmentationImageFilter()

    # set the make a zero-valued volume for the output
    # TODO: maybe this should be done in numpy as a one-liner
    thresh = vtk.vtkImageThreshold()
    thresh.ReplaceInOn()
    thresh.ReplaceOutOn()
    thresh.SetInValue(0)
    thresh.SetOutValue(0)
    thresh.SetOutputScalarType(vtk.VTK_SHORT)

    thresh.SetInputData(gestureInput)

    thresh.SetOutput(growCutOutput)
    thresh.Update()
    growCutOutput.DeepCopy(gestureInput)

    growCutFilter.SetInputData(0, background)
    growCutFilter.SetInputData(1, gestureInput)
    growCutFilter.SetInputConnection(2, thresh.GetOutputPort())

    objectSize = 10.  # TODO: this is a magic number
    contrastNoiseRatio = 0.8  # TODO: this is a magic number
    priorStrength = 0.003  # TODO: this is a magic number
    segmented = 2  # TODO: this is a magic number
    conversion = 1000  # TODO: this is a magic number

    spacing = gestureInput.GetSpacing()
    voxelVolume = reduce(lambda x, y: x * y, spacing)
    voxelAmount = objectSize / voxelVolume
    voxelNumber = round(voxelAmount) * conversion

    cubeRoot = 1. / 3.
    oSize = int(round(pow(voxelNumber, cubeRoot)))

    growCutFilter.SetObjectSize(oSize)
    growCutFilter.SetContrastNoiseRatio(contrastNoiseRatio)
    growCutFilter.SetPriorSegmentConfidence(priorStrength)
    growCutFilter.Update()

    growCutOutput.DeepCopy(growCutFilter.GetOutput())

    return growCutOutput
def growCut(background,gestureInput,growCutOutput):
  growCutFilter = vtkITK.vtkITKGrowCutSegmentationImageFilter()

  # set the make a zero-valued volume for the output
  # TODO: maybe this should be done in numpy as a one-liner
  thresh = vtk.vtkImageThreshold()
  thresh.ReplaceInOn()
  thresh.ReplaceOutOn()
  thresh.SetInValue(0)
  thresh.SetOutValue(0)
  thresh.SetOutputScalarType( vtk.VTK_SHORT )

  thresh.SetInputData( gestureInput )
  
  thresh.SetOutput( growCutOutput )
  thresh.Update()
  growCutOutput.DeepCopy( gestureInput )

  growCutFilter.SetInputData( 0, background )
  growCutFilter.SetInputData( 1, gestureInput )
  growCutFilter.SetInputConnection( 2, thresh.GetOutputPort() )

  objectSize = 10. # TODO: this is a magic number
  contrastNoiseRatio = 0.8 # TODO: this is a magic number
  priorStrength = 0.003 # TODO: this is a magic number
  segmented = 2 # TODO: this is a magic number
  conversion = 1000 # TODO: this is a magic number

  spacing = gestureInput.GetSpacing()
  voxelVolume = reduce(lambda x,y: x*y, spacing)
  voxelAmount = objectSize / voxelVolume
  voxelNumber = round(voxelAmount) * conversion

  cubeRoot = 1./3.
  oSize = int(round(pow(voxelNumber,cubeRoot)))

  growCutFilter.SetObjectSize( oSize )
  growCutFilter.SetContrastNoiseRatio( contrastNoiseRatio )
  growCutFilter.SetPriorSegmentConfidence( priorStrength )
  growCutFilter.Update()

  growCutOutput.DeepCopy( growCutFilter.GetOutput() )

  return growCutOutput
  def growCut(self):
    # Get master volume image data
    import vtkSegmentationCorePython as vtkSegmentationCore
    masterImageData = self.scriptedEffect.masterVolumeImageData()
    # Get segmentation
    segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()

    # Cast master image if not short
    if masterImageData.GetScalarType() != vtk.VTK_SHORT:
      imageCast = vtk.vtkImageCast()
      imageCast.SetInputData(masterImageData)
      imageCast.SetOutputScalarTypeToShort()
      imageCast.ClampOverflowOn()
      imageCast.Update()
      masterImageDataShort = vtkSegmentationCore.vtkOrientedImageData()
      masterImageDataShort.DeepCopy(imageCast.GetOutput()) # Copy image data
      masterImageDataShort.CopyDirections(masterImageData) # Copy geometry
      masterImageData = masterImageDataShort

    # Generate merged labelmap as input to GrowCut
    mergedImage = vtkSegmentationCore.vtkOrientedImageData()
    segmentationNode.GenerateMergedLabelmapForAllSegments(mergedImage, vtkSegmentationCore.vtkSegmentation.EXTENT_UNION_OF_SEGMENTS, masterImageData)

    # Make a zero-valued volume for the output
    outputLabelmap = vtkSegmentationCore.vtkOrientedImageData()
    thresh = vtk.vtkImageThreshold()
    thresh.ReplaceInOn()
    thresh.ReplaceOutOn()
    thresh.SetInValue(0)
    thresh.SetOutValue(0)
    thresh.SetOutputScalarType( vtk.VTK_SHORT )
    thresh.SetInputData( mergedImage )
    thresh.SetOutput( outputLabelmap )
    thresh.Update()
    outputLabelmap.DeepCopy( mergedImage ) #TODO: It was thresholded just above, why deep copy now?

    # Perform grow cut
    import vtkITK
    growCutFilter = vtkITK.vtkITKGrowCutSegmentationImageFilter()
    growCutFilter.SetInputData( 0, masterImageData )
    growCutFilter.SetInputData( 1, mergedImage )
    #TODO: This call sets an empty image for the optional "previous segmentation", and
    #      is apparently needed for the first segmentation too. Why?
    growCutFilter.SetInputConnection( 2, thresh.GetOutputPort() )

    #TODO: These are magic numbers inherited from EditorLib/GrowCut.py
    objectSize = 5.
    contrastNoiseRatio = 0.8
    priorStrength = 0.003
    segmented = 2
    conversion = 1000

    spacing = mergedImage.GetSpacing()
    voxelVolume = reduce(lambda x,y: x*y, spacing)
    voxelAmount = objectSize / voxelVolume
    voxelNumber = round(voxelAmount) * conversion

    cubeRoot = 1./3.
    oSize = int(round(pow(voxelNumber,cubeRoot)))

    growCutFilter.SetObjectSize( oSize )
    growCutFilter.SetContrastNoiseRatio( contrastNoiseRatio )
    growCutFilter.SetPriorSegmentConfidence( priorStrength )
    growCutFilter.Update()

    outputLabelmap.DeepCopy( growCutFilter.GetOutput() )

    # Write output segmentation results in segments
    segmentIDs = vtk.vtkStringArray()
    segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
    for index in xrange(segmentIDs.GetNumberOfValues()):
      segmentID = segmentIDs.GetValue(index)
      segment = segmentationNode.GetSegmentation().GetSegment(segmentID)

      # Get label corresponding to segment in merged labelmap (and so GrowCut output)
      colorIndexStr = vtk.mutable("")
      tagFound = segment.GetTag(slicer.vtkMRMLSegmentationDisplayNode.GetColorIndexTag(), colorIndexStr);
      if not tagFound:
        logging.error('Failed to apply GrowCut result on segment ' + segmentID)
        continue
      colorIndex = int(colorIndexStr.get())

      # 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)
      thresh.ThresholdBetween(colorIndex, colorIndex);
      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)
      slicer.vtkSlicerSegmentationsModuleLogic.SetBinaryLabelmapToSegment(newSegmentLabelmap, segmentationNode, segmentID, slicer.vtkSlicerSegmentationsModuleLogic.MODE_REPLACE, newSegmentLabelmap.GetExtent())
Esempio n. 5
0
    def growCut(self):
        # Get master volume image data
        import vtkSegmentationCorePython as vtkSegmentationCore
        masterImageData = self.scriptedEffect.masterVolumeImageData()
        # Get segmentation
        segmentationNode = self.scriptedEffect.parameterSetNode(
        ).GetSegmentationNode()

        # Cast master image if not short
        if masterImageData.GetScalarType() != vtk.VTK_SHORT:
            imageCast = vtk.vtkImageCast()
            imageCast.SetInputData(masterImageData)
            imageCast.SetOutputScalarTypeToShort()
            imageCast.ClampOverflowOn()
            imageCast.Update()
            masterImageDataShort = vtkSegmentationCore.vtkOrientedImageData()
            masterImageDataShort.DeepCopy(
                imageCast.GetOutput())  # Copy image data
            masterImageDataShort.CopyDirections(
                masterImageData)  # Copy geometry
            masterImageData = masterImageDataShort

        # Generate merged labelmap as input to GrowCut
        mergedImage = vtkSegmentationCore.vtkOrientedImageData()
        segmentationNode.GenerateMergedLabelmapForAllSegments(
            mergedImage,
            vtkSegmentationCore.vtkSegmentation.EXTENT_UNION_OF_SEGMENTS,
            masterImageData)

        # Make a zero-valued volume for the output
        outputLabelmap = vtkSegmentationCore.vtkOrientedImageData()
        thresh = vtk.vtkImageThreshold()
        thresh.ReplaceInOn()
        thresh.ReplaceOutOn()
        thresh.SetInValue(0)
        thresh.SetOutValue(0)
        thresh.SetOutputScalarType(vtk.VTK_SHORT)
        thresh.SetInputData(mergedImage)
        thresh.SetOutput(outputLabelmap)
        thresh.Update()
        outputLabelmap.DeepCopy(
            mergedImage
        )  #TODO: It was thresholded just above, why deep copy now?

        # Perform grow cut
        import vtkITK
        growCutFilter = vtkITK.vtkITKGrowCutSegmentationImageFilter()
        growCutFilter.SetInputData(0, masterImageData)
        growCutFilter.SetInputData(1, mergedImage)
        #TODO: This call sets an empty image for the optional "previous segmentation", and
        #      is apparently needed for the first segmentation too. Why?
        growCutFilter.SetInputConnection(2, thresh.GetOutputPort())

        #TODO: These are magic numbers inherited from EditorLib/GrowCut.py
        objectSize = 5.
        contrastNoiseRatio = 0.8
        priorStrength = 0.003
        segmented = 2
        conversion = 1000

        spacing = mergedImage.GetSpacing()
        voxelVolume = reduce(lambda x, y: x * y, spacing)
        voxelAmount = objectSize / voxelVolume
        voxelNumber = round(voxelAmount) * conversion

        cubeRoot = 1. / 3.
        oSize = int(round(pow(voxelNumber, cubeRoot)))

        growCutFilter.SetObjectSize(oSize)
        growCutFilter.SetContrastNoiseRatio(contrastNoiseRatio)
        growCutFilter.SetPriorSegmentConfidence(priorStrength)
        growCutFilter.Update()

        outputLabelmap.DeepCopy(growCutFilter.GetOutput())

        # Write output segmentation results in segments
        segmentIDs = vtk.vtkStringArray()
        segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
        for index in xrange(segmentIDs.GetNumberOfValues()):
            segmentID = segmentIDs.GetValue(index)
            segment = segmentationNode.GetSegmentation().GetSegment(segmentID)

            # Get label corresponding to segment in merged labelmap (and so GrowCut output)
            colorIndexStr = vtk.mutable("")
            tagFound = segment.GetTag(
                slicer.vtkMRMLSegmentationDisplayNode.GetColorIndexTag(),
                colorIndexStr)
            if not tagFound:
                logging.error('Failed to apply GrowCut result on segment ' +
                              segmentID)
                continue
            colorIndex = int(colorIndexStr.get())

            # 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)
            thresh.ThresholdBetween(colorIndex, colorIndex)
            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)
            slicer.vtkSlicerSegmentationsModuleLogic.SetBinaryLabelmapToSegment(
                newSegmentLabelmap, segmentationNode, segmentID,
                slicer.vtkSlicerSegmentationsModuleLogic.MODE_REPLACE,
                newSegmentLabelmap.GetExtent())