def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) self._image_logic = vtk.vtkImageLogic() module_utils.setup_vtk_object_progress(self, self._image_logic, 'Performing image logic') self._image_cast = vtk.vtkImageCast() module_utils.setup_vtk_object_progress( self, self._image_cast, 'Casting scalar type before image logic') self._config.operation = 0 self._config.output_true_value = 1.0 # 'choice' widget with 'base:int' type will automatically get cast # to index of selection that user makes. config_list = [ ('Operation:', 'operation', 'base:int', 'choice', 'The operation that should be performed.', tuple(self._operations)), ('Output true value:', 'output_true_value', 'base:float', 'text', 'Output voxels that are TRUE will get this value.') ] ScriptedConfigModuleMixin.__init__(self, config_list, { 'Module (self)': self, 'vtkImageLogic': self._image_logic }) self.sync_module_logic_with_config()
def __init__(self, inputs = (2, 2)): """ Initialization """ lib.ProcessingFilter.ProcessingFilter.__init__(self, inputs) self.vtkfilter = vtk.vtkImageLogic() self.vtkfilter.SetOutputTrueValue(255)
def __init__(self, inputs=(2, 2)): """ Initialization """ lib.ProcessingFilter.ProcessingFilter.__init__(self, inputs) self.vtkfilter = vtk.vtkImageLogic() self.vtkfilter.SetOutputTrueValue(255)
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkImageLogic(), 'Processing.', ('vtkImageData', 'vtkImageData'), ('vtkImageData',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def VTKImageLogic(x1,x2=None,op="or",numret=False): if type(x1) == np.ndarray: i1 = NumToVTKImage(x1) else: i1 = x1 if type(x2) == np.ndarray: i2 = NumToVTKImage(x2) elif x2: i2 = x2 m = vtk.vtkImageLogic() numops = [ "and","or","xor","nand","nor","not"] vtkops = [ m.SetOperationToAnd, m.SetOperationToOr, m.SetOperationToXor, m.SetOperationToNand, m.SetOperationToNor, m.SetOperationToNot] m.SetOutputTrueValue(1.0) vtkops[numops.index(op)]() m.SetInput1Data(i1) if x2 is not None: m.SetInput2Data(i2) o = m.GetOutput() m.Update() if numret: return VTKImageToNum(o) else: return o
def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) self._image_logic = vtk.vtkImageLogic() module_utils.setup_vtk_object_progress(self, self._image_logic, "Performing image logic") self._image_cast = vtk.vtkImageCast() module_utils.setup_vtk_object_progress(self, self._image_cast, "Casting scalar type before image logic") self._config.operation = 0 self._config.output_true_value = 1.0 # 'choice' widget with 'base:int' type will automatically get cast # to index of selection that user makes. config_list = [ ( "Operation:", "operation", "base:int", "choice", "The operation that should be performed.", tuple(self._operations), ), ( "Output true value:", "output_true_value", "base:float", "text", "Output voxels that are TRUE will get this value.", ), ] ScriptedConfigModuleMixin.__init__( self, config_list, {"Module (self)": self, "vtkImageLogic": self._image_logic} ) self.sync_module_logic_with_config()
def removeIslandsMorphologyDecruft(self, image, foregroundLabel, backgroundLabel, iterations=1): # # make binary mask foregroundLabel->1, backgroundLabel->0 # binThresh = vtk.vtkImageThreshold() 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()) 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() 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() 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() 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() 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()) 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() 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() unbinThresh.SetInputConnection(logic.GetOutputPort()) unbinThresh.ThresholdBetween(1, 1) unbinThresh.SetInValue(foregroundLabel) unbinThresh.SetOutValue(backgroundLabel) unbinThresh.Update() image.DeepCopy(unbinThresh.GetOutput())
def removeIslandsMorphologyDecruft(self,image,foregroundLabel,backgroundLabel,iterations=1): # # make binary mask foregroundLabel->1, backgroundLabel->0 # binThresh = vtk.vtkImageThreshold() 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()) 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() 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() 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() 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() 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()) 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() 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() unbinThresh.SetInputConnection( logic.GetOutputPort() ) unbinThresh.ThresholdBetween( 1,1 ) unbinThresh.SetInValue( foregroundLabel ) unbinThresh.SetOutValue( backgroundLabel ) unbinThresh.Update() image.DeepCopy(unbinThresh.GetOutput())
def _appendSingle(self, volToAppend, uid): """ @type uid: C{int} @param uid: index to be assigned to this volume. Method merges proivded volume with existing, cached volume. Following algorithm is used (e - existing image, n - new image, M - image mask): e = (n AND (M(n) XOR M(e))) + e @return: C{None} """ # If privided volume is the first one, simply take it as output volume. if not self.outVol: self.outVol = volToAppend.GetIndexed(uid) print >>sys.stderr, "No volume found. Creating initial volume." return # Otherwise: # XOR existing volume with the new print "...start:" volCurNewXor = vtk.vtkImageLogic() volCurNewXor.SetInput1(volToAppend.GetMask()) volCurNewXor.SetInput2(self.GetMask()) volCurNewXor.SetOperationToXor() # Intersect xored image with the new volume getting the part of the # volume to be updated print "...start volXorAndNew:" volXorAndNew = vtk.vtkImageLogic() volXorAndNew.SetInput1(volToAppend.GetMask()) volXorAndNew.SetInput2(volCurNewXor.GetOutput()) volXorAndNew.SetOperationToAnd() # Change the image type print "...start imgCast:" cast = vtk.vtkImageCast() cast.SetInput(volXorAndNew.GetOutput()) cast.SetOutputScalarTypeToUnsignedChar() # Assign index to the 'new' volume print "...start volToReplace" volToReplace = vtk.vtkImageMask() volToReplace.SetImageInput(volToAppend.GetIndexed(uid)) volToReplace.SetMaskInput(cast.GetOutput()) # Add both volumes print "...start newSumVol" newSumVol = vtk.vtkImageMathematics() newSumVol.SetOperationToAdd() newSumVol.SetNumberOfThreads(1) newSumVol.SetInput1(volToReplace.GetOutput()) newSumVol.SetInput2(self.outVol) # This is funny :) # In order to not to store mass of the references we create deep copy # of the result - that gives much speedup. newSumVol.GetOutput().Update() self.outVol.DeepCopy(newSumVol.GetOutput()) print "...stop:"
def testAllLogic(self): # append multiple displaced spheres into an RGB image. # Image pipeline renWin = vtk.vtkRenderWindow() logics = ["And", "Or", "Xor", "Nand", "Nor", "Not"] types = [ "Float", "Double", "UnsignedInt", "UnsignedLong", "UnsignedShort", "UnsignedChar" ] sphere1 = list() sphere2 = list() logic = list() mapper = list() actor = list() imager = list() for idx, operator in enumerate(logics): ScalarType = types[idx] sphere1.append(vtk.vtkImageEllipsoidSource()) sphere1[idx].SetCenter(95, 100, 0) sphere1[idx].SetRadius(70, 70, 70) eval('sphere1[idx].SetOutputScalarTypeTo' + ScalarType + '()') sphere1[idx].Update() sphere2.append(vtk.vtkImageEllipsoidSource()) sphere2[idx].SetCenter(161, 100, 0) sphere2[idx].SetRadius(70, 70, 70) eval('sphere2[idx].SetOutputScalarTypeTo' + ScalarType + '()') sphere2[idx].Update() logic.append(vtk.vtkImageLogic()) logic[idx].SetInput1Data(sphere1[idx].GetOutput()) if operator != "Not": logic[idx].SetInput2Data(sphere2[idx].GetOutput()) logic[idx].SetOutputTrueValue(150) eval('logic[idx].SetOperationTo' + operator + '()') mapper.append(vtk.vtkImageMapper()) mapper[idx].SetInputConnection(logic[idx].GetOutputPort()) mapper[idx].SetColorWindow(255) mapper[idx].SetColorLevel(127.5) actor.append(vtk.vtkActor2D()) actor[idx].SetMapper(mapper[idx]) imager.append(vtk.vtkRenderer()) imager[idx].AddActor2D(actor[idx]) renWin.AddRenderer(imager[idx]) imager[0].SetViewport(0, .5, .33, 1) imager[1].SetViewport(.33, .5, .66, 1) imager[2].SetViewport(.66, .5, 1, 1) imager[3].SetViewport(0, 0, .33, .5) imager[4].SetViewport(.33, 0, .66, .5) imager[5].SetViewport(.66, 0, 1, .5) renWin.SetSize(768, 512) # render and interact with data iRen = vtk.vtkRenderWindowInteractor() iRen.SetRenderWindow(renWin) renWin.Render() img_file = "TestAllLogic.png" vtk.test.Testing.compareImage( iRen.GetRenderWindow(), vtk.test.Testing.getAbsImagePath(img_file), threshold=25) vtk.test.Testing.interact()
def testAllLogic(self): # append multiple displaced spheres into an RGB image. # Image pipeline renWin = vtk.vtkRenderWindow() logics = ["And", "Or", "Xor", "Nand", "Nor", "Not"] types = ["Float", "Double", "UnsignedInt", "UnsignedLong", "UnsignedShort", "UnsignedChar"] sphere1 = list() sphere2 = list() logic = list() mapper = list() actor = list() imager = list() for idx, operator in enumerate(logics): ScalarType = types[idx] sphere1.append(vtk.vtkImageEllipsoidSource()) sphere1[idx].SetCenter(95, 100, 0) sphere1[idx].SetRadius(70, 70, 70) eval('sphere1[idx].SetOutputScalarTypeTo' + ScalarType + '()') sphere1[idx].Update() sphere2.append(vtk.vtkImageEllipsoidSource()) sphere2[idx].SetCenter(161, 100, 0) sphere2[idx].SetRadius(70, 70, 70) eval('sphere2[idx].SetOutputScalarTypeTo' + ScalarType + '()') sphere2[idx].Update() logic.append(vtk.vtkImageLogic()) logic[idx].SetInput1Data(sphere1[idx].GetOutput()) if operator != "Not": logic[idx].SetInput2Data(sphere2[idx].GetOutput()) logic[idx].SetOutputTrueValue(150) eval('logic[idx].SetOperationTo' + operator + '()') mapper.append(vtk.vtkImageMapper()) mapper[idx].SetInputConnection(logic[idx].GetOutputPort()) mapper[idx].SetColorWindow(255) mapper[idx].SetColorLevel(127.5) actor.append(vtk.vtkActor2D()) actor[idx].SetMapper(mapper[idx]) imager.append(vtk.vtkRenderer()) imager[idx].AddActor2D(actor[idx]) renWin.AddRenderer(imager[idx]) imager[0].SetViewport(0, .5, .33, 1) imager[1].SetViewport(.33, .5, .66, 1) imager[2].SetViewport(.66, .5, 1, 1) imager[3].SetViewport(0, 0, .33, .5) imager[4].SetViewport(.33, 0, .66, .5) imager[5].SetViewport(.66, 0, 1, .5) renWin.SetSize(768, 512) # render and interact with data iRen = vtk.vtkRenderWindowInteractor() iRen.SetRenderWindow(renWin); renWin.Render() img_file = "TestAllLogic.png" vtk.test.Testing.compareImage(iRen.GetRenderWindow(), vtk.test.Testing.getAbsImagePath(img_file), threshold=25) vtk.test.Testing.interact()