def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkImageIslandRemoval2D(), 'Processing.', ('vtkImageData',), ('vtkImageData',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__(self, module_manager, vtk.vtkImageIslandRemoval2D(), 'Processing.', ('vtkImageData', ), ('vtkImageData', ), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def VTKIslandRemoval(x,isval,repval,area,numret=0): if type(x) == np.ndarray: i = NumToVTKImage(x) else: i = x d = vtk.vtkImageIslandRemoval2D() d.SetIslandValue(isval) d.SetReplaceValue(repval) d.SetAreaThreshold(area) d.SquareNeighborhoodOn() d.SetInputData(i) o = d.GetOutput() d.Update() if numret: return VTKImageToNum(o) else: return o
import vtk from vtk.test import Testing from vtk.util.misc import vtkGetDataRoot VTK_DATA_ROOT = vtkGetDataRoot() # A script to test the island removal filter. # first the image is thresholded, then small islands are removed. # Image pipeline reader = vtk.vtkPNGReader() reader.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/fullhead15.png") thresh = vtk.vtkImageThreshold() thresh.SetInputConnection(reader.GetOutputPort()) thresh.ThresholdByUpper(2000.0) thresh.SetInValue(255) thresh.SetOutValue(0) thresh.ReleaseDataFlagOff() island = vtk.vtkImageIslandRemoval2D() island.SetInputConnection(thresh.GetOutputPort()) island.SetIslandValue(255) island.SetReplaceValue(128) island.SetAreaThreshold(100) island.SquareNeighborhoodOn() viewer = vtk.vtkImageViewer() viewer.SetInputConnection(island.GetOutputPort()) viewer.SetColorWindow(255) viewer.SetColorLevel(127.5) #viewer DebugOn viewer.Render() # --- end of script --
def RemoveCoronalIslands(self, ImageData, IslandSizeThreshold): IslandRemovalFilter = vtk.vtkImageIslandRemoval2D() IslandRemovalFilter.SetAreaThreshold(int(IslandSizeThreshold)) IslandRemovalFilter.SetSquareNeighborhood(8) IslandRemovalFilter.SetIslandValue(1) IslandRemovalFilter.SetReplaceValue(0) # Go through each coronal slice of ImageData, storing slices with islands removed CoronalSliceImages = [] Extent = ImageData.GetExtent() #print "DEBUG - Island remover instantiated" for A in range( Extent[2], Extent[3] ): # Take projetion alond A-P line, posterior to anterior direction CurrentSliceImageData = vtk.vtkImageData() CurrentSliceImageData.SetExtent(ImageData.GetExtent()[0:2] + (0, 1) + ImageData.GetExtent()[4:]) CurrentSliceImageData.SetSpacing(ImageData.GetSpacing()) CurrentSliceImageData.AllocateScalars(ImageData.GetScalarType(), 1) for S in range(Extent[4], Extent[5]): # For each row of the labelmap for R in range(Extent[0], Extent[1]): # For each column of the label-map CurrentValue = ImageData.GetScalarComponentAsFloat( R, A, S, 0) CurrentSliceImageData.SetScalarComponentFromFloat( R, 0, S, 0, CurrentValue) #print "DEBUG - Image data instantiated" IslandRemovalFilter.SetInputData(CurrentSliceImageData) IslandRemovalFilter.Update() #print "DEBUG - Filter ouput updated" IslandsRemovedSliceImage = IslandRemovalFilter.GetOutput() CoronalSliceImages.append(IslandsRemovedSliceImage) #print "DEBUG - All slices' islands removed" # Merge all slices into one FilteredImageData ProcessedImageData = vtk.vtkImageData() ProcessedImageData.SetExtent(ImageData.GetExtent()) ProcessedImageData.SetSpacing(ImageData.GetSpacing()) ProcessedImageData.AllocateScalars(ImageData.GetScalarType(), 1) for i, A in enumerate(range(Extent[2], Extent[3])): for S in range(Extent[4], Extent[5]): # For each row of the labelmap for R in range(Extent[0], Extent[1]): # For each column of the label-map CurrentVoxelLabelValue = CoronalSliceImages[ i].GetScalarComponentAsFloat(R, 0, S, 0) ProcessedImageData.SetScalarComponentFromFloat( R, A, S, 0, CurrentVoxelLabelValue) #print "DEBUG - Image data with islands removed created" LabelMapIslandsRemoved = slicer.vtkMRMLLabelMapVolumeNode() LabelMapIslandsRemoved.SetSpacing(ImageData.GetSpacing()) LabelMapIslandsRemoved.SetOrigin(ImageData.GetOrigin()) LabelMapIslandsRemoved.SetAndObserveImageData(ProcessedImageData) #print "DEBUG - Image data copied to labelmap" return LabelMapIslandsRemoved
#!/usr/bin/env python import vtk from vtk.test import Testing from vtk.util.misc import vtkGetDataRoot VTK_DATA_ROOT = vtkGetDataRoot() # A script to test the island removal filter. # first the image is thresholded, then small islands are removed. # Image pipeline reader = vtk.vtkPNGReader() reader.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/fullhead15.png") thresh = vtk.vtkImageThreshold() thresh.SetInputConnection(reader.GetOutputPort()) thresh.ThresholdByUpper(2000.0) thresh.SetInValue(255) thresh.SetOutValue(0) thresh.ReleaseDataFlagOff() island = vtk.vtkImageIslandRemoval2D() island.SetInputConnection(thresh.GetOutputPort()) island.SetIslandValue(255) island.SetReplaceValue(128) island.SetAreaThreshold(100) island.SquareNeighborhoodOn() viewer = vtk.vtkImageViewer() viewer.SetInputConnection(island.GetOutputPort()) viewer.SetColorWindow(255) viewer.SetColorLevel(127.5) #viewer DebugOn viewer.Render() # --- end of script --
def create_frog_actor(frog_fn, frog_tissue_fn, tissue, flying_edges, decimate, lut): # Get the tissue parameters pixel_size = tissue['PIXEL_SIZE'] columns = tissue['COLUMNS'] rows = tissue['ROWS'] voi = tissue['VOI'] spacing = float(tissue['SPACING']) start_slice = float(tissue['START_SLICE']) data_spacing = [pixel_size, pixel_size, spacing] data_origin = [-(columns / 2.0) * pixel_size, -(rows / 2.0) * pixel_size, start_slice * spacing] # # adjust y bounds for PNM coordinate system # tmp = voi[2] voi[2] = rows - voi[3] - 1 voi[3] = rows - tmp - 1 if tissue['NAME'] == 'skin': fn = frog_fn else: fn = frog_tissue_fn reader = vtk.vtkMetaImageReader() reader.SetFileName(str(fn)) reader.SetDataSpacing(data_spacing) reader.SetDataOrigin(data_origin) reader.SetDataExtent(voi) reader.Update() last_connection = reader if not tissue['NAME'] == 'skin': if tissue['ISLAND_REPLACE'] >= 0: island_remover = vtk.vtkImageIslandRemoval2D() island_remover.SetAreaThreshold(tissue['ISLAND_AREA']) island_remover.SetIslandValue(tissue['ISLAND_REPLACE']) island_remover.SetReplaceValue(tissue['TISSUE']) island_remover.SetInput(last_connection.GetOutput()) island_remover.Update() last_connection = island_remover select_tissue = vtk.vtkImageThreshold() select_tissue.ThresholdBetween(tissue['TISSUE'], tissue['TISSUE']) select_tissue.SetInValue(255) select_tissue.SetOutValue(0) select_tissue.SetInputConnection(last_connection.GetOutputPort()) last_connection = select_tissue shrinker = vtk.vtkImageShrink3D() shrinker.SetInputConnection(last_connection.GetOutputPort()) shrinker.SetShrinkFactors(tissue['SAMPLE_RATE']) shrinker.AveragingOn() last_connection = shrinker if not all(v == 0 for v in tissue['GAUSSIAN_STANDARD_DEVIATION']): gaussian = vtk.vtkImageGaussianSmooth() gaussian.SetStandardDeviation(*tissue['GAUSSIAN_STANDARD_DEVIATION']) gaussian.SetRadiusFactors(*tissue['GAUSSIAN_RADIUS_FACTORS']) gaussian.SetInputConnection(shrinker.GetOutputPort()) last_connection = gaussian # Time the isocontouring. ict = collections.defaultdict() iso_value = tissue['VALUE'] if flying_edges: iso_surface = vtk.vtkFlyingEdges3D() iso_surface.SetInputConnection(last_connection.GetOutputPort()) iso_surface.ComputeScalarsOff() iso_surface.ComputeGradientsOff() iso_surface.ComputeNormalsOff() iso_surface.SetValue(0, iso_value) timer = vtk.vtkExecutionTimer() timer.SetFilter(iso_surface) iso_surface.Update() ict['Flying Edges'] = timer.GetElapsedWallClockTime() else: iso_surface = vtk.vtkMarchingCubes() iso_surface.SetInputConnection(last_connection.GetOutputPort()) iso_surface.ComputeScalarsOff() iso_surface.ComputeGradientsOff() iso_surface.ComputeNormalsOff() iso_surface.SetValue(0, iso_value) timer = vtk.vtkExecutionTimer() timer.SetFilter(iso_surface) iso_surface.Update() ict['Marching Cubes'] = timer.GetElapsedWallClockTime() so = SliceOrder() # transform = so.get(tissue['SLICE_ORDER']) # Match Frog.py transform = so.get('hfap') transform.Scale(1, -1, 1) tf = vtk.vtkTransformPolyDataFilter() tf.SetTransform(transform) tf.SetInputConnection(iso_surface.GetOutputPort()) last_connection = tf if decimate: decimator = vtk.vtkDecimatePro() decimator.SetInputConnection(last_connection.GetOutputPort()) decimator.SetFeatureAngle(tissue['DECIMATE_ANGLE']) decimator.MaximumIterations = tissue['DECIMATE_ITERATIONS'] decimator.PreserveTopologyOn() decimator.SetErrorIsAbsolute(1) decimator.SetAbsoluteError(tissue['DECIMATE_ERROR']) decimator.SetTargetReduction(tissue['DECIMATE_REDUCTION']) last_connection = decimator smoother = vtk.vtkWindowedSincPolyDataFilter() smoother.SetInputConnection(last_connection.GetOutputPort()) smoother.SetNumberOfIterations(tissue['SMOOTH_ITERATIONS']) smoother.BoundarySmoothingOff() smoother.FeatureEdgeSmoothingOff() smoother.SetFeatureAngle(tissue['SMOOTH_ANGLE']) smoother.SetPassBand(tissue['SMOOTH_FACTOR']) smoother.NonManifoldSmoothingOn() smoother.NormalizeCoordinatesOff() smoother.Update() normals = vtk.vtkPolyDataNormals() normals.SetInputConnection(smoother.GetOutputPort()) normals.SetFeatureAngle(tissue['FEATURE_ANGLE']) stripper = vtk.vtkStripper() stripper.SetInputConnection(normals.GetOutputPort()) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(stripper.GetOutputPort()) # Create iso-surface contour = vtk.vtkContourFilter() contour.SetInputConnection(reader.GetOutputPort()) contour.SetValue(0, iso_value) actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetOpacity(tissue['OPACITY']) actor.GetProperty().SetDiffuseColor(lut.GetTableValue(tissue['TISSUE'])[:3]) actor.GetProperty().SetSpecular(0.5) actor.GetProperty().SetSpecularPower(10) return ict, actor