Example #1
0
 def ImageXOR(self, imageData1, imageData2):
     """ This function XORs two image datas 
 PARAM: imageData1 vtkImageData()
 PARAM: imageData2 vtkImageData()
 RETURN: vtkImageData()
 """
     imageLogic = vtk.vtkImageLogic()
     imageLogic.SetOperationToXor()
     imageLogic.SetInput1Data(imageData1)
     imageLogic.SetInput2Data(imageData2)
     imageLogic.Update()
     return imageLogic.GetOutput()
Example #2
0
 def ImageOR(self,imageData1,imageData2):
   """ This function ORs two image datas 
   PARAM: imageData1 vtkImageData()
   PARAM: imageData2 vtkImageData()
   RETURN: vtkImageData()
   """
   imageLogic= vtk.vtkImageLogic()
   imageLogic.SetOperationToOr()
   imageLogic.SetInput1Data(imageData1) 
   imageLogic.SetInput2Data(imageData2)
   imageLogic.Update()
   return imageLogic.GetOutput()
Example #3
0
 def ImageNegation(self,imageData,referenceVolume):
   """ This function negates the image
   PARAM: imageData1 vtkImageData()
   PARAM: vtkMRMLScalarVolumeNode() referenceVolume 
   RETURN: vtkImageData()
   ASK ANDRAS IS THERE IS A BETTERWAY TO FLIP THE BINARY BITS
   """
   air             =   self.PolyDataToImageData(vtk.vtkPolyData(),referenceVolume,0,255)
   imageLogic= vtk.vtkImageLogic()
   imageLogic.SetOperationToXor()
   imageLogic.SetInput1Data(imageData) 
   imageLogic.SetInput2Data(air)
   imageLogic.Update()
   return imageLogic.GetOutput()
Example #4
0
 def ImageNegation(self, imageData, referenceVolume):
     """ This function negates the image
 PARAM: imageData1 vtkImageData()
 PARAM: vtkMRMLScalarVolumeNode() referenceVolume 
 RETURN: vtkImageData()
 ASK ANDRAS IS THERE IS A BETTERWAY TO FLIP THE BINARY BITS
 """
     air = self.PolyDataToImageData(vtk.vtkPolyData(), referenceVolume, 0,
                                    255)
     imageLogic = vtk.vtkImageLogic()
     imageLogic.SetOperationToXor()
     imageLogic.SetInput1Data(imageData)
     imageLogic.SetInput2Data(air)
     imageLogic.Update()
     return imageLogic.GetOutput()
Example #5
0
 def MergeAllImages(self, arrayOfImageData):
   """ This function will append all images by -or- operations from an 
   array of image data to create a single imaga data will all datas merged
   
   PARAM: Array of image data          Array< ImageData > 
   RETURN : vtkImageData
   """
   if (len(arrayOfImageData)==0):
     print "Warning: Empty array of Image Data"
     return None
   imageLogic= vtk.vtkImageLogic()
   imageLogic.SetOperationToOr()
   accumulator = arrayOfImageData.pop()   # Initialize the addition sequence
   while (0 < len(arrayOfImageData)): #Appends all previous images
     imageLogic.SetInput1Data(accumulator) 
     imageLogic.SetInput2Data(arrayOfImageData.pop()) #Appends Next tube
     imageLogic.Update()
     accumulator=(imageLogic.GetOutput())
   return accumulator
Example #6
0
 def MergeAllImages(self, arrayOfImageData):
     """ This function will append all images by -or- operations from an 
 array of image data to create a single imaga data will all datas merged
 
 PARAM: Array of image data          Array< ImageData > 
 RETURN : vtkImageData
 """
     if (len(arrayOfImageData) == 0):
         print "Warning: Empty array of Image Data"
         return None
     imageLogic = vtk.vtkImageLogic()
     imageLogic.SetOperationToOr()
     accumulator = arrayOfImageData.pop(
     )  # Initialize the addition sequence
     while (0 < len(arrayOfImageData)):  #Appends all previous images
         imageLogic.SetInput1Data(accumulator)
         imageLogic.SetInput2Data(
             arrayOfImageData.pop())  #Appends Next tube
         imageLogic.Update()
         accumulator = (imageLogic.GetOutput())
     return accumulator
    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 #8
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())