def transform_scalars(self, dataset, amount=0.5, threshold=0.0, sigma=1.0): """This filter performs anisotropic diffusion on an image using the classic Perona-Malik, gradient magnitude-based equation. """ # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform step_pct = iter([10, 20, 90, 100]) try: import itk from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc try: self.progress.message = "Converting data to ITK image" self.progress.value = 0 # Get the ITK image. The itk.GradientAnisotropicDiffusionImageFilter # is templated over float pixel types only, so explicitly request a # float ITK image type. itk_image = itkutils.convert_vtk_to_itk_image(dataset) self.progress.value = next(step_pct) self.progress.message = "Running filter" self.progress.value = next(step_pct) unsharp_mask = \ itk.UnsharpMaskImageFilter.New(Input=itk_image) unsharp_mask.SetAmount(amount) unsharp_mask.SetThreshold(threshold) unsharp_mask.SetSigma(sigma) itkutils.observe_filter_progress(self, unsharp_mask, self.progress.value, next(step_pct)) try: unsharp_mask.Update() except RuntimeError: return self.progress.message = "Saving results" enhanced = unsharp_mask.GetOutput() itkutils.set_array_from_itk_image(dataset, enhanced) self.progress.value = next(step_pct) except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc
def transform_scalars(self, dataset, amount=0.5, threshold=0.0, sigma=1.0): """This filter performs anisotropic diffusion on an image using the classic Perona-Malik, gradient magnitude-based equation. """ # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform step_pct = iter([10, 20, 90, 100]) try: import itk from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc try: self.progress.message = "Converting data to ITK image" self.progress.value = 0 # Get the ITK image. The itk.GradientAnisotropicDiffusionImageFilter # is templated over float pixel types only, so explicitly request a # float ITK image type. itk_image = itkutils.convert_vtk_to_itk_image(dataset) self.progress.value = next(step_pct) self.progress.message = "Running filter" self.progress.value = next(step_pct) unsharp_mask = \ itk.UnsharpMaskImageFilter.New(Input=itk_image) unsharp_mask.SetAmount(amount) unsharp_mask.SetThreshold(threshold) unsharp_mask.SetSigma(sigma) itkutils.observe_filter_progress(self, unsharp_mask, self.progress.value, next(step_pct)) try: unsharp_mask.Update() except RuntimeError: return self.progress.message = "Saving results" enhanced = unsharp_mask.GetOutput() itkutils.set_array_from_itk_image(dataset, enhanced) self.progress.value = next(step_pct) except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc
def transform_scalars(self, dataset): """Define this method for Python operators that transform the input array. This example uses an ITK filter to add 10 to each voxel value.""" # Try imports to make sure we have everything that is needed try: from tomviz import itkutils import itk except Exception as exc: print("Could not import necessary module(s)") raise exc self.progress.value = 0 self.progress.maximum = 100 # Add a try/except around the ITK portion. ITK exceptions are # passed up to the Python layer, so we can at least report what # went wrong with the script, e.g., unsupported image type. try: self.progress.value = 0 self.progress.message = "Converting data to ITK image" # Get the ITK image itk_image = itkutils.convert_vtk_to_itk_image(dataset) itk_input_image_type = type(itk_image) self.progress.value = 30 self.progress.message = "Running filter" # ITK filter filter = itk.AddImageFilter[itk_input_image_type, # Input 1 itk_input_image_type, # Input 2 itk_input_image_type].New() # Output filter.SetInput1(itk_image) filter.SetConstant2(10) itkutils.observe_filter_progress(self, filter, 30, 70) try: filter.Update() except RuntimeError: # Exception thrown when ITK filter is aborted return self.progress.message = "Saving results" itkutils.set_array_from_itk_image(dataset, filter.GetOutput()) self.progress.value = 100 except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc
def transform_scalars(self, dataset): """Define this method for Python operators that transform the input array. This example uses an ITK filter to add 10 to each voxel value.""" # Try imports to make sure we have everything that is needed try: from tomviz import itkutils import itk except Exception as exc: print("Could not import necessary module(s)") raise exc self.progress.value = 0 self.progress.maximum = 100 # Add a try/except around the ITK portion. ITK exceptions are # passed up to the Python layer, so we can at least report what # went wrong with the script, e.g., unsupported image type. try: self.progress.value = 0 self.progress.message = "Converting data to ITK image" # Get the ITK image itk_image = itkutils.convert_vtk_to_itk_image(dataset) itk_input_image_type = type(itk_image) self.progress.value = 30 self.progress.message = "Running filter" # ITK filter filter = itk.AddImageFilter[itk_input_image_type, # Input 1 itk_input_image_type, # Input 2 itk_input_image_type].New() # Output filter.SetInput1(itk_image) filter.SetConstant2(10) itkutils.observe_filter_progress(self, filter, 30, 70) try: filter.Update() except RuntimeError: # Exception thrown when ITK filter is aborted return self.progress.message = "Saving results" itkutils.set_array_from_itk_image(dataset, filter.GetOutput()) self.progress.value = 100 except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc
def transform_scalars(self, dataset, conductance=1.0, iterations=100, timestep=0.0625): """This filter performs anisotropic diffusion on an image using the classic Perona-Malik, gradient magnitude-based equation. """ # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform STEP_PCT = [10, 20, 90, 100] try: import itk import itkTypes from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc try: self.progress.value = STEP_PCT[0] self.progress.message = "Converting data to ITK image" # Get the ITK image. The itk.GradientAnisotropicDiffusionImageFilter # is templated over float pixel types only, so explicitly request a # float ITK image type. itk_image = itkutils.convert_vtk_to_itk_image(dataset, itkTypes.F) itk_image_type = type(itk_image) self.progress.value = STEP_PCT[1] self.progress.message = "Running filter" DiffusionFilterType = \ itk.GradientAnisotropicDiffusionImageFilter[itk_image_type, itk_image_type] diffusion_filter = DiffusionFilterType.New() diffusion_filter.SetConductanceParameter(conductance) diffusion_filter.SetNumberOfIterations(iterations) diffusion_filter.SetTimeStep(timestep) diffusion_filter.SetInput(itk_image) itkutils.observe_filter_progress(self, diffusion_filter, STEP_PCT[1], STEP_PCT[2]) try: diffusion_filter.Update() except RuntimeError: return self.progress.message = "Saving results" itkutils.set_array_from_itk_image(dataset, diffusion_filter.GetOutput()) self.progress.value = STEP_PCT[3] except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc
def transform_scalars(self, dataset, stencil_radius=2, iterations=10, threshold=50.0): """This filter smooths a binary image by evolving a level set with a curvature-based speed function. The Stencil Radius determines the scale of the noise to remove. The Threshold determines the iso-contour brightness to discriminate between two pixel classes. """ # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform STEP_PCT = [10, 20, 30, 70, 80, 90, 100] try: import itk import itkTypes from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc # Return values returnValue = None # Add a try/except around the ITK portion. ITK exceptions are # passed up to the Python layer, so we can at least report what # went wrong with the script, e.g,, unsupported image type. try: self.progress.value = STEP_PCT[0] self.progress.message = "Converting data to ITK image" # Get the ITK image itk_image = itkutils.convert_vtk_to_itk_image(dataset) itk_input_image_type = type(itk_image) self.progress.message = "Casting input to float type" itk_filter_image_type = itk.Image[itkTypes.F, itk_image.GetImageDimension()] caster = itk.CastImageFilter[itk_input_image_type, itk_filter_image_type].New() caster.SetInput(itk_image) itkutils.observe_filter_progress(self, caster, STEP_PCT[1], STEP_PCT[2]) try: caster.Update() except RuntimeError: return self.progress.message = "Running filter" smoothing_filter = itk.BinaryMinMaxCurvatureFlowImageFilter[ itk_filter_image_type, itk_filter_image_type].New() smoothing_filter.SetThreshold(threshold) smoothing_filter.SetStencilRadius(stencil_radius) smoothing_filter.SetNumberOfIterations(iterations) smoothing_filter.SetTimeStep(0.0625) smoothing_filter.SetInput(caster) itkutils.observe_filter_progress(self, smoothing_filter, STEP_PCT[2], STEP_PCT[3]) try: smoothing_filter.Update() except RuntimeError: return itk_image_data = smoothing_filter.GetOutput() # Cast output to the input type self.progress.message = "Casting output to input type" caster = itk.CastImageFilter[itk_filter_image_type, itk_input_image_type].New() caster.SetInput(itk_image_data) itkutils.observe_filter_progress(self, caster, STEP_PCT[3], STEP_PCT[4]) try: caster.Update() except RuntimeError: return itk_image_data = caster.GetOutput() self.progress.value = STEP_PCT[5] self.progress.message = "Saving results" itkutils.set_array_from_itk_image(dataset, itk_image_data) self.progress.value = STEP_PCT[6] except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc return returnValue
def transform_scalars(self, dataset, conductance=1.0, iterations=100, timestep=0.0625): """This filter performs anisotropic diffusion on an image using the classic Perona-Malik, gradient magnitude-based equation. """ # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform STEP_PCT = [10, 20, 90, 100] try: import itk import itkTypes from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc try: self.progress.value = STEP_PCT[0] self.progress.message = "Converting data to ITK image" # Get the ITK image. The itk.GradientAnisotropicDiffusionImageFilter # is templated over float pixel types only, so explicitly request a # float ITK image type. itk_image = itkutils.convert_vtk_to_itk_image(dataset, itkTypes.F) itk_image_type = type(itk_image) self.progress.value = STEP_PCT[1] self.progress.message = "Running filter" DiffusionFilterType = \ itk.GradientAnisotropicDiffusionImageFilter[itk_image_type, itk_image_type] diffusion_filter = DiffusionFilterType.New() diffusion_filter.SetConductanceParameter(conductance) diffusion_filter.SetNumberOfIterations(iterations) diffusion_filter.SetTimeStep(timestep) diffusion_filter.SetInput(itk_image) itkutils.observe_filter_progress(self, diffusion_filter, STEP_PCT[1], STEP_PCT[2]) try: diffusion_filter.Update() except RuntimeError: return self.progress.message = "Saving results" itkutils.set_array_from_itk_image(dataset, diffusion_filter.GetOutput()) self.progress.value = STEP_PCT[3] except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc
def transform_scalars(self, dataset, lower_threshold=40.0, upper_threshold=255.0): """This filter computes a binary threshold on the data set and stores the result in a child data set. It does not modify the dataset passed in.""" # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform STEP_PCT = [20, 40, 75, 90, 100] # Set up return value returnValue = None # Try imports to make sure we have everything that is needed try: self.progress.message = "Loading modules" import itk import vtk from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc # Add a try/except around the ITK portion. ITK exceptions are # passed up to the Python layer, so we can at least report what # went wrong with the script, e.g,, unsupported image type. try: self.progress.value = STEP_PCT[0] self.progress.message = "Converting data to ITK image" # Get the ITK image itk_image = itkutils.convert_vtk_to_itk_image(dataset) itk_input_image_type = type(itk_image) self.progress.value = STEP_PCT[1] self.progress.message = "Running filter" # We change the output type to unsigned char 3D # (itk.Image.UC3D) to save memory in the output label map # representation. itk_output_image_type = itk.Image.UC3 # ITK's BinaryThresholdImageFilter does the hard work threshold_filter = itk.BinaryThresholdImageFilter[ itk_input_image_type, itk_output_image_type].New() python_cast = itkutils.get_python_voxel_type(itk_image) threshold_filter.SetLowerThreshold(python_cast(lower_threshold)) threshold_filter.SetUpperThreshold(python_cast(upper_threshold)) threshold_filter.SetInsideValue(1) threshold_filter.SetOutsideValue(0) threshold_filter.SetInput(itk_image) itkutils.observe_filter_progress(self, threshold_filter, STEP_PCT[2], STEP_PCT[3]) try: threshold_filter.Update() except RuntimeError: return returnValue self.progress.message = "Creating child data set" # Set the output as a new child data object of the current data set label_map_dataset = vtk.vtkImageData() label_map_dataset.CopyStructure(dataset) itkutils.set_array_from_itk_image(label_map_dataset, threshold_filter.GetOutput()) self.progress.value = STEP_PCT[4] returnValue = { "thresholded_segmentation": label_map_dataset } except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc return returnValue
def transform_scalars(self, dataset, lower_threshold=40.0, upper_threshold=255.0): """This filter computes a binary threshold on the data set and stores the result in a child data set. It does not modify the dataset passed in.""" # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform STEP_PCT = [20, 40, 75, 90, 100] # Set up return value returnValue = None # Try imports to make sure we have everything that is needed try: self.progress.message = "Loading modules" import itk from vtkmodules.vtkCommonDataModel import vtkImageData from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc # Add a try/except around the ITK portion. ITK exceptions are # passed up to the Python layer, so we can at least report what # went wrong with the script, e.g,, unsupported image type. try: self.progress.value = STEP_PCT[0] self.progress.message = "Converting data to ITK image" # Get the ITK image itk_image = itkutils.convert_vtk_to_itk_image(dataset) itk_input_image_type = type(itk_image) self.progress.value = STEP_PCT[1] self.progress.message = "Running filter" # We change the output type to unsigned char 3D # (itk.Image.UC3D) to save memory in the output label map # representation. itk_output_image_type = itk.Image.UC3 # ITK's BinaryThresholdImageFilter does the hard work threshold_filter = itk.BinaryThresholdImageFilter[ itk_input_image_type, itk_output_image_type].New() python_cast = itkutils.get_python_voxel_type(itk_image) threshold_filter.SetLowerThreshold(python_cast(lower_threshold)) threshold_filter.SetUpperThreshold(python_cast(upper_threshold)) threshold_filter.SetInsideValue(1) threshold_filter.SetOutsideValue(0) threshold_filter.SetInput(itk_image) itkutils.observe_filter_progress(self, threshold_filter, STEP_PCT[2], STEP_PCT[3]) try: threshold_filter.Update() except RuntimeError: return returnValue self.progress.message = "Creating child data set" # Set the output as a new child data object of the current data set label_map_dataset = vtkImageData() label_map_dataset.CopyStructure(dataset) itkutils.set_array_from_itk_image(label_map_dataset, threshold_filter.GetOutput()) self.progress.value = STEP_PCT[4] returnValue = {"thresholded_segmentation": label_map_dataset} except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc return returnValue
def transform_scalars(self, dataset, structuring_element_id=0, radius=1, object_label=1, background_label=0): """Perform morphological opening on segmented objects with a given label by a spherically symmetric structuring element with a given radius. """ # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform STEP_PCT = [10, 30, 60, 90, 100] try: import itk from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc # Add a try/except around the ITK portion. ITK exceptions are # passed up to the Python layer, so we can at least report what # went wrong with the script, e.g,, unsupported image type. try: self.progress.value = STEP_PCT[0] self.progress.message = "Converting data to ITK image" # Get the ITK image itk_image = itkutils.convert_vtk_to_itk_image(dataset) itk_input_image_type = type(itk_image) itk_kernel_type = itk.FlatStructuringElement[3] if (structuring_element_id == 0): itk_kernel = itk_kernel_type.Box(radius) elif (structuring_element_id == 1): itk_kernel = itk_kernel_type.Ball(radius) elif (structuring_element_id == 2): itk_kernel = itk_kernel_type.Cross(radius) else: raise Exception('Invalid kernel shape id %d' % structuring_element_id) self.progress.value = STEP_PCT[1] self.progress.message = "Running filter" erode_filter = itk.BinaryErodeImageFilter[itk_input_image_type, itk_input_image_type, itk_kernel_type].New() erode_filter.SetErodeValue(object_label) erode_filter.SetBackgroundValue(background_label) erode_filter.SetKernel(itk_kernel) erode_filter.SetInput(itk_image) itkutils.observe_filter_progress(self, erode_filter, STEP_PCT[1], STEP_PCT[2]) dilate_filter = itk.BinaryDilateImageFilter[itk_input_image_type, itk_input_image_type, itk_kernel_type].New() dilate_filter.SetDilateValue(object_label) dilate_filter.SetBackgroundValue(background_label) dilate_filter.SetKernel(itk_kernel) dilate_filter.SetInput(erode_filter.GetOutput()) itkutils.observe_filter_progress(self, dilate_filter, STEP_PCT[2], STEP_PCT[3]) try: dilate_filter.Update() except RuntimeError: return self.progress.message = "Saving results" itkutils.set_array_from_itk_image(dataset, dilate_filter.GetOutput()) self.progress.value = STEP_PCT[4] except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc
def transform_scalars(self, dataset, stencil_radius=2, iterations=10, threshold=50.0): """This filter smooths a binary image by evolving a level set with a curvature-based speed function. The Stencil Radius determines the scale of the noise to remove. The Threshold determines the iso-contour brightness to discriminate between two pixel classes. """ # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform STEP_PCT = [10, 20, 30, 70, 80, 90, 100] try: import itk import itkTypes from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc # Return values returnValue = None # Add a try/except around the ITK portion. ITK exceptions are # passed up to the Python layer, so we can at least report what # went wrong with the script, e.g,, unsupported image type. try: self.progress.value = STEP_PCT[0] self.progress.message = "Converting data to ITK image" # Get the ITK image itk_image = itkutils.convert_vtk_to_itk_image(dataset) itk_input_image_type = type(itk_image) self.progress.message = "Casting input to float type" itk_filter_image_type = itk.Image[itkTypes.F, itk_image.GetImageDimension()] caster = itk.CastImageFilter[itk_input_image_type, itk_filter_image_type].New() caster.SetInput(itk_image) itkutils.observe_filter_progress(self, caster, STEP_PCT[1], STEP_PCT[2]) try: caster.Update() except RuntimeError: return self.progress.message = "Running filter" smoothing_filter = itk.BinaryMinMaxCurvatureFlowImageFilter[ itk_filter_image_type, itk_filter_image_type].New() smoothing_filter.SetThreshold(threshold) smoothing_filter.SetStencilRadius(stencil_radius) smoothing_filter.SetNumberOfIterations(iterations) smoothing_filter.SetTimeStep(0.0625) smoothing_filter.SetInput(caster) itkutils.observe_filter_progress(self, smoothing_filter, STEP_PCT[2], STEP_PCT[3]) try: smoothing_filter.Update() except RuntimeError: return itk_image_data = smoothing_filter.GetOutput() # Cast output to the input type self.progress.message = "Casting output to input type" caster = itk.CastImageFilter[itk_filter_image_type, itk_input_image_type].New() caster.SetInput(itk_image_data) itkutils.observe_filter_progress(self, caster, STEP_PCT[3], STEP_PCT[4]) try: caster.Update() except RuntimeError: return itk_image_data = caster.GetOutput() self.progress.value = STEP_PCT[5] self.progress.message = "Saving results" itkutils.set_array_from_itk_image(dataset, itk_image_data) self.progress.value = STEP_PCT[6] except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc return returnValue
def transform_scalars(self, dataset, structuring_element_id=0, radius=1, object_label=1, background_label=0): """Dilate segmented objects with a given label by a spherically symmetric structuring element with a given radius. """ # Initial progress self.progress.value = 0 self.progress.maximum = 100 # Approximate percentage of work completed after each step in the # transform STEP_PCT = [10, 20, 90, 100] try: import itk from tomviz import itkutils except Exception as exc: print("Could not import necessary module(s)") raise exc # Add a try/except around the ITK portion. ITK exceptions are # passed up to the Python layer, so we can at least report what # went wrong with the script, e.g,, unsupported image type. try: self.progress.value = STEP_PCT[0] self.progress.message = "Converting data to ITK image" # Get the ITK image itk_image = itkutils.convert_vtk_to_itk_image(dataset) itk_input_image_type = type(itk_image) itk_kernel_type = itk.FlatStructuringElement[3] if (structuring_element_id == 0): itk_kernel = itk_kernel_type.Box(radius) elif (structuring_element_id == 1): itk_kernel = itk_kernel_type.Ball(radius) elif (structuring_element_id == 2): itk_kernel = itk_kernel_type.Cross(radius) else: raise Exception('Invalid kernel shape id %d' % structuring_element_id) self.progress.value = STEP_PCT[1] self.progress.message = "Running filter" dilate_filter = itk.BinaryDilateImageFilter[itk_input_image_type, itk_input_image_type, itk_kernel_type].New() dilate_filter.SetDilateValue(object_label) dilate_filter.SetBackgroundValue(background_label) dilate_filter.SetKernel(itk_kernel) dilate_filter.SetInput(itk_image) itkutils.observe_filter_progress(self, dilate_filter, STEP_PCT[1], STEP_PCT[2]) try: dilate_filter.Update() except RuntimeError: return self.progress.message = "Saving results" itkutils.set_array_from_itk_image(dataset, dilate_filter.GetOutput()) self.progress.value = STEP_PCT[3] except Exception as exc: print("Problem encountered while running %s" % self.__class__.__name__) raise exc