Example #1
0
  def erode(self,fill,neighborMode,iterations):

    eroder = slicer.vtkImageErode()
    if vtk.VTK_MAJOR_VERSION <= 5:
      eroder.SetInput( self.getScopedLabelInput() )
    else:
      eroder.SetInputData( self.getScopedLabelInput() )
    eroder.SetOutput( self.getScopedLabelOutput() )

    eroder.SetForeground( self.editUtil.getLabel() )
    eroder.SetBackground( fill )

    if neighborMode == '8':
      eroder.SetNeighborTo8()
    elif neighborMode == '4':
      eroder.SetNeighborTo4()
    else:
      # TODO: error feedback from effect logic?
      # bad neighbor mode - silently use default
      print('Bad neighborMode: %s' % neighborMode)

    for i in xrange(iterations):
      # TODO: $this setProgressFilter eroder "Erode ($i)"
      print('updating')
      eroder.Update()

    self.applyScopedLabel()
    eroder.SetOutput( None )
Example #2
0
    def erode(self, fill, neighborMode, iterations):

        eroder = slicer.vtkImageErode()
        if vtk.VTK_MAJOR_VERSION <= 5:
            eroder.SetInput(self.getScopedLabelInput())
        else:
            eroder.SetInputData(self.getScopedLabelInput())
        eroder.SetOutput(self.getScopedLabelOutput())

        eroder.SetForeground(fill)
        eroder.SetBackground(self.editUtil.getLabel())

        if neighborMode == '8':
            eroder.SetNeighborTo8()
        elif neighborMode == '4':
            eroder.SetNeighborTo4()
        else:
            # TODO: error feedback from effect logic?
            # bad neighbor mode - silently use default
            print('Bad neighborMode: %s' % neighborMode)

        for i in xrange(iterations):
            # TODO: $this setProgressFilter eroder "Dilate ($i)"
            eroder.Update()

        self.applyScopedLabel()
        eroder.SetOutput(None)
    def removeIslandsMorphologyDecruft(self, image, foregroundLabel, backgroundLabel, iterations=1):
        #
        # make binary mask foregroundLabel->1, backgroundLabel->0
        #
        binThresh = vtk.vtkImageThreshold()
        if vtk.VTK_MAJOR_VERSION <= 5:
            binThresh.SetInput(image)
        else:
            binThresh.SetInputData(image)
        binThresh.ThresholdBetween(foregroundLabel, foregroundLabel)
        binThresh.SetInValue(1)
        binThresh.SetOutValue(0)
        binThresh.Update()

        #
        # first, erode iterations number of times
        #
        eroder = slicer.vtkImageErode()
        eroderImage = vtk.vtkImageData()
        eroderImage.DeepCopy(binThresh.GetOutput())
        if vtk.VTK_MAJOR_VERSION <= 5:
            eroder.SetInput(eroderImage)
        else:
            eroder.SetInputData(eroderImage)
        for iteration in range(iterations):
            eroder.SetForeground(1)
            eroder.SetBackground(0)
            eroder.SetNeighborTo8()
            eroder.Update()
            eroderImage.DeepCopy(eroder.GetOutput())

        #
        # now save only islands bigger than a specified size
        #

        # note that island operation happens in unsigned long space
        # but the slicer editor works in Short
        castIn = vtk.vtkImageCast()
        if vtk.VTK_MAJOR_VERSION <= 5:
            castIn.SetInput(eroderImage)
        else:
            castIn.SetInputConnection(eroder.GetInputConnection(0, 0))
        castIn.SetOutputScalarTypeToUnsignedLong()

        # now identify the islands in the inverted volume
        # and find the pixel that corresponds to the background
        islandMath = vtkITK.vtkITKIslandMath()
        if vtk.VTK_MAJOR_VERSION <= 5:
            islandMath.SetInput(castIn.GetOutput())
        else:
            islandMath.SetInputConnection(castIn.GetOutputPort())
        islandMath.SetFullyConnected(self.fullyConnected)
        islandMath.SetMinimumSize(self.minimumSize)

        # note that island operation happens in unsigned long space
        # but the slicer editor works in Short
        castOut = vtk.vtkImageCast()
        if vtk.VTK_MAJOR_VERSION <= 5:
            castOut.SetInput(islandMath.GetOutput())
        else:
            castOut.SetInputConnection(islandMath.GetOutputPort())
        castOut.SetOutputScalarTypeToShort()

        castOut.Update()
        islandCount = islandMath.GetNumberOfIslands()
        islandOrigCount = islandMath.GetOriginalNumberOfIslands()
        ignoredIslands = islandOrigCount - islandCount
        print("%d islands created (%d ignored)" % (islandCount, ignoredIslands))

        #
        # now map everything back to 0 and 1
        #

        thresh = vtk.vtkImageThreshold()
        if vtk.VTK_MAJOR_VERSION <= 5:
            thresh.SetInput(castOut.GetOutput())
        else:
            thresh.SetInputConnection(castOut.GetOutputPort())
        thresh.ThresholdByUpper(1)
        thresh.SetInValue(1)
        thresh.SetOutValue(0)
        thresh.Update()

        #
        # now, dilate back (erode background) iterations_plus_one number of times
        #
        dilater = slicer.vtkImageErode()
        dilaterImage = vtk.vtkImageData()
        dilaterImage.DeepCopy(thresh.GetOutput())
        if vtk.VTK_MAJOR_VERSION <= 5:
            dilater.SetInput(dilaterImage)
        else:
            dilater.SetInputData(dilaterImage)
        for iteration in range(1 + iterations):
            dilater.SetForeground(0)
            dilater.SetBackground(1)
            dilater.SetNeighborTo8()
            dilater.Update()
            dilaterImage.DeepCopy(dilater.GetOutput())

        #
        # only keep pixels in both original and dilated result
        #

        logic = vtk.vtkImageLogic()
        if vtk.VTK_MAJOR_VERSION <= 5:
            logic.SetInput1(dilaterImage)
            logic.SetInput2(binThresh.GetOutput())
        else:
            logic.SetInputConnection(0, dilater.GetInputConnection(0, 0))
            logic.SetInputConnection(1, binThresh.GetOutputPort())
        # if foregroundLabel == 0:
        #  logic.SetOperationToNand()
        # else:
        logic.SetOperationToAnd()
        logic.SetOutputTrueValue(1)
        logic.Update()

        #
        # convert from binary mask to 1->foregroundLabel, 0->backgroundLabel
        #
        unbinThresh = vtk.vtkImageThreshold()
        if vtk.VTK_MAJOR_VERSION <= 5:
            unbinThresh.SetInput(logic.GetOutput())
        else:
            unbinThresh.SetInputConnection(logic.GetOutputPort())
        unbinThresh.ThresholdBetween(1, 1)
        unbinThresh.SetInValue(foregroundLabel)
        unbinThresh.SetOutValue(backgroundLabel)
        unbinThresh.Update()

        image.DeepCopy(unbinThresh.GetOutput())
Example #4
0
  def removeIslandsMorphologyDecruft(self,image,foregroundLabel,backgroundLabel,iterations=1):
    #
    # make binary mask foregroundLabel->1, backgroundLabel->0
    #
    binThresh = vtk.vtkImageThreshold()
    if vtk.VTK_MAJOR_VERSION <= 5:
      binThresh.SetInput( image )
    else:
      binThresh.SetInputData( image )
    binThresh.ThresholdBetween(foregroundLabel,foregroundLabel)
    binThresh.SetInValue( 1 )
    binThresh.SetOutValue( 0 )
    binThresh.Update()

    #
    # first, erode iterations number of times
    #
    eroder = slicer.vtkImageErode()
    eroderImage = vtk.vtkImageData()
    eroderImage.DeepCopy(binThresh.GetOutput())
    if vtk.VTK_MAJOR_VERSION <= 5:
      eroder.SetInput(eroderImage)
    else:
      eroder.SetInputData(eroderImage)
    for iteration in range(iterations):
      eroder.SetForeground( 1 )
      eroder.SetBackground( 0 )
      eroder.SetNeighborTo8()
      eroder.Update()
      eroderImage.DeepCopy(eroder.GetOutput())


    #
    # now save only islands bigger than a specified size
    #

    # note that island operation happens in unsigned long space
    # but the slicer editor works in Short
    castIn = vtk.vtkImageCast()
    if vtk.VTK_MAJOR_VERSION <= 5:
      castIn.SetInput( eroderImage )
    else:
      castIn.SetInputConnection( eroder.GetInputConnection(0,0) )
    castIn.SetOutputScalarTypeToUnsignedLong()

    # now identify the islands in the inverted volume
    # and find the pixel that corresponds to the background
    islandMath = vtkITK.vtkITKIslandMath()
    if vtk.VTK_MAJOR_VERSION <= 5:
      islandMath.SetInput( castIn.GetOutput() )
    else:
      islandMath.SetInputConnection( castIn.GetOutputPort() )
    islandMath.SetFullyConnected( self.fullyConnected )
    islandMath.SetMinimumSize( self.minimumSize )

    # note that island operation happens in unsigned long space
    # but the slicer editor works in Short
    castOut = vtk.vtkImageCast()
    if vtk.VTK_MAJOR_VERSION <= 5:
      castOut.SetInput( islandMath.GetOutput() )
    else:
      castOut.SetInputConnection( islandMath.GetOutputPort() )
    castOut.SetOutputScalarTypeToShort()

    castOut.Update()
    islandCount = islandMath.GetNumberOfIslands()
    islandOrigCount = islandMath.GetOriginalNumberOfIslands()
    ignoredIslands = islandOrigCount - islandCount
    print( "%d islands created (%d ignored)" % (islandCount, ignoredIslands) )

    #
    # now map everything back to 0 and 1
    #

    thresh = vtk.vtkImageThreshold()
    if vtk.VTK_MAJOR_VERSION <= 5:
      thresh.SetInput( castOut.GetOutput() )
    else:
      thresh.SetInputConnection( castOut.GetOutputPort() )
    thresh.ThresholdByUpper(1)
    thresh.SetInValue( 1 )
    thresh.SetOutValue( 0 )
    thresh.Update()

    #
    # now, dilate back (erode background) iterations_plus_one number of times
    #
    dilater = slicer.vtkImageErode()
    dilaterImage = vtk.vtkImageData()
    dilaterImage.DeepCopy(thresh.GetOutput())
    if vtk.VTK_MAJOR_VERSION <= 5:
      dilater.SetInput(dilaterImage)
    else:
      dilater.SetInputData(dilaterImage)
    for iteration in range(1+iterations):
      dilater.SetForeground( 0 )
      dilater.SetBackground( 1 )
      dilater.SetNeighborTo8()
      dilater.Update()
      dilaterImage.DeepCopy(dilater.GetOutput())

    #
    # only keep pixels in both original and dilated result
    #

    logic = vtk.vtkImageLogic()
    if vtk.VTK_MAJOR_VERSION <= 5:
      logic.SetInput1(dilaterImage)
      logic.SetInput2(binThresh.GetOutput())
    else:
      logic.SetInputConnection(0, dilater.GetInputConnection(0,0))
      logic.SetInputConnection(1, binThresh.GetOutputPort())
    #if foregroundLabel == 0:
    #  logic.SetOperationToNand()
    #else:
    logic.SetOperationToAnd()
    logic.SetOutputTrueValue(1)
    logic.Update()

    #
    # convert from binary mask to 1->foregroundLabel, 0->backgroundLabel
    #
    unbinThresh = vtk.vtkImageThreshold()
    if vtk.VTK_MAJOR_VERSION <= 5:
      unbinThresh.SetInput( logic.GetOutput() )
    else:
      unbinThresh.SetInputConnection( logic.GetOutputPort() )
    unbinThresh.ThresholdBetween( 1,1 )
    unbinThresh.SetInValue( foregroundLabel )
    unbinThresh.SetOutValue( backgroundLabel )
    unbinThresh.Update()

    image.DeepCopy(unbinThresh.GetOutput())