def runMasking(self, ijkPoints, seedLabelmap, outputLabelmap): kernelSizePixel = self.getKernelSizePixel() self.floodFillingFilterIsland = vtk.vtkImageThresholdConnectivity() self.floodFillingFilterIsland.SetInputData(seedLabelmap) self.floodFillingFilterIsland.SetInValue(BACKGROUND_VALUE) self.floodFillingFilterIsland.ReplaceInOn() self.floodFillingFilterIsland.ReplaceOutOff() self.floodFillingFilterIsland.ThresholdBetween(LABEL_VALUE, LABEL_VALUE) self.floodFillingFilterIsland.SetSeedPoints(ijkPoints) self.dilate = vtk.vtkImageDilateErode3D() self.dilate.SetInputConnection(self.floodFillingFilterIsland.GetOutputPort()) self.dilate.SetDilateValue(LABEL_VALUE) self.dilate.SetErodeValue(BACKGROUND_VALUE) self.dilate.SetKernelSize( 2*kernelSizePixel[0]-1, 2*kernelSizePixel[1]-1, 2*kernelSizePixel[2]-1) self.dilate.Update() self.imageMask = vtk.vtkImageMask() self.imageMask.SetInputConnection(self.thresh.GetOutputPort()) self.imageMask.SetMaskedOutputValue(BACKGROUND_VALUE) self.imageMask.NotMaskOn() self.imageMask.SetMaskInputData(self.dilate.GetOutput()) self.floodFillingFilter = vtk.vtkImageThresholdConnectivity() self.floodFillingFilter.SetInputConnection(self.imageMask.GetOutputPort()) self.floodFillingFilter.SetInValue(LABEL_VALUE) self.floodFillingFilter.SetOutValue(BACKGROUND_VALUE) self.floodFillingFilter.ThresholdBetween(LABEL_VALUE, LABEL_VALUE) self.floodFillingFilter.SetSeedPoints(ijkPoints) self.floodFillingFilter.Update() outputLabelmap.ShallowCopy(self.floodFillingFilter.GetOutput())
def execute(self, inputs, update=0, last=0): """ Execute the filter with given inputs and return the output """ if not lib.ProcessingFilter.ProcessingFilter.execute(self, inputs): return None if self.parameters["UseImageROI"]: maskImage = self.getInput(2) # If scalar type is not unsigned char, then cast the data cast = vtk.vtkImageCast() cast.SetInput(maskImage) cast.SetOutputScalarType(3) cast.SetClampOverflow(1) maskImage = cast.GetOutput() else: maxx, maxy, maxz = self.dataUnit.getDimensions() roi = self.parameters["ROI"][1] n, maskImage = lib.ImageOperations.getMaskFromROIs([roi], maxx, maxy, maxz) scripting.wantWholeDataset = 1 imagedata = self.getInput(1) imagedata.SetUpdateExtent(imagedata.GetWholeExtent()) imagedata.Update() maskFilter = vtk.vtkImageMask() maskFilter.SetImageInput(imagedata) maskFilter.SetMaskInput(maskImage) maskFilter.SetMaskedOutputValue(self.parameters["OutputValue"]) return maskFilter.GetOutput()
def ipl_maskaimpeel(image_in1, image_in2, peel_iter): ipl_maskaimpeel_settings(peel_iter) extent = image_in1.GetExtent() voi = vtk.vtkExtractVOI() voi.SetInput(image_in1) voi.SetVOI(extent[0], extent[1], extent[2], extent[3], extent[4], extent[5]) voi.Update() shift = vtk.vtkImageShiftScale() shift.SetInputConnection(voi.GetOutputPort()) shift.SetOutputScalarTypeToUnsignedChar() shift.Update() mask = vtk.vtkImageMask() mask.SetImageInput(image_in2) mask.SetMaskInput(shift.GetOutput()) mask.SetMaskedOutputValue(0) mask.Update() if peel_iter != 0: erode = vtk.vtkImageContinuousErode3D() erode.SetInputConnection(mask.GetOutputPort()) erode.SetKernelSize(peel_iter + 1, peel_iter + 1, peel_iter + 1) erode.Update() mask = erode image_out = mask.GetOutput() return image_out
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkImageMask(), 'Processing.', ('vtkImageData', 'vtkImageData'), ('vtkImageData',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def create_overlay(self, emphysemavalue, severeemphysemavalue): """Creates an overlay for the slice-based volume view 0: no emphysema 1: moderate emphysema 2: severe emphysema """ self._view_frame.SetStatusText("Creating Overlay...") mask = vtk.vtkImageMask() mask2 = vtk.vtkImageMask() threshold = vtk.vtkImageThreshold() threshold2 = vtk.vtkImageThreshold() math=vtk.vtkImageMathematics() mask.SetInput(self.image_data) mask.SetMaskInput(self.mask_data) threshold.SetInput(mask.GetOutput()) threshold.ThresholdByLower(emphysemavalue) threshold.SetOutValue(0) threshold.SetInValue(1) threshold2.SetInput(mask.GetOutput()) threshold2.ThresholdByLower(severeemphysemavalue) threshold2.SetOutValue(1) threshold2.SetInValue(2) math.SetOperationToMultiply() math.SetInput1(threshold.GetOutput()) math.SetInput2(threshold2.GetOutput()) math.Update() overlay = math.GetOutput() self.slice_viewer1.set_overlay_input(None) self.slice_viewer1.set_overlay_input(overlay) self.render() self._view_frame.SetStatusText("Created Overlay")
def create_overlay(self, emphysemavalue, severeemphysemavalue): """Creates an overlay for the slice-based volume view 0: no emphysema 1: moderate emphysema 2: severe emphysema """ self._view_frame.SetStatusText("Creating Overlay...") mask = vtk.vtkImageMask() mask2 = vtk.vtkImageMask() threshold = vtk.vtkImageThreshold() threshold2 = vtk.vtkImageThreshold() math = vtk.vtkImageMathematics() mask.SetInput(self.image_data) mask.SetMaskInput(self.mask_data) threshold.SetInput(mask.GetOutput()) threshold.ThresholdByLower(emphysemavalue) threshold.SetOutValue(0) threshold.SetInValue(1) threshold2.SetInput(mask.GetOutput()) threshold2.ThresholdByLower(severeemphysemavalue) threshold2.SetOutValue(1) threshold2.SetInValue(2) math.SetOperationToMultiply() math.SetInput1(threshold.GetOutput()) math.SetInput2(threshold2.GetOutput()) math.Update() overlay = math.GetOutput() self.slice_viewer1.set_overlay_input(None) self.slice_viewer1.set_overlay_input(overlay) self.render() self._view_frame.SetStatusText("Created Overlay")
def getResampledData(self, data, n, resampleToDimensions=None): """ Return the data resampled to given dimensions """ if self.resampling and not scripting.resamplingDisabled: currentResamplingDimensions = self.getResampleDimensions() # If we're given dimensions to resample to, then we use those if resampleToDimensions: currentResamplingDimensions = resampleToDimensions # If the resampling dimensions are not defined (even though resampling is requested) # then don't resample the data if not currentResamplingDimensions: return data if n == self.resampledTimepoint and self.resampleFilter and not currentResamplingDimensions: data = self.resampleFilter.GetOutput() else: Logging.info("Resampling data to ", self.resampleDims, kw="datasource") if not self.resampleFilter: self.resampleFilter = vtk.vtkImageResample() x, y, z = self.originalDimensions rx, ry, rz = currentResamplingDimensions xf = rx / float(x) yf = ry / float(y) zf = rz / float(z) self.resampleFilter.SetAxisMagnificationFactor(0, xf) self.resampleFilter.SetAxisMagnificationFactor(1, yf) self.resampleFilter.SetAxisMagnificationFactor(2, zf) else: self.resampleFilter.RemoveAllInputs() self.resampleFilter.SetInputConnection(data.GetProducerPort()) data = self.resampleFilter.GetOutput() if self.mask: if not self.maskImageFilter: self.maskImageFilter = vtk.vtkImageMask() self.maskImageFilter.SetMaskedOutputValue(0) else: self.maskImageFilter.RemoveAllInputs() self.maskImageFilter.SetImageInput(data) self.maskImageFilter.SetMaskInput(self.mask.getMaskImage()) data = self.maskImageFilter.GetOutput() return data
def createMask(seg,image): ''' combines a mask with an image. Non zero mask implies the output pixel will be the same as the image. If a mask pixel is zero, the output pixel is set to 'MaskedValue'==0. ''' segUchar = castImage(seg, 'uchar') mask = vtk.vtkImageMask(); mask.SetImageInput(image); mask.SetMaskInput(segUchar); mask.SetMaskedOutputValue(0.0); mask.NotMaskOff(); mask.ReleaseDataFlagOff(); mask.Update(); return mask.GetOutput();
def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) # setup VTK pipeline ######################################### # 1. vtkImageMask self._imageMask = vtk.vtkImageMask() self._imageMask.GetOutput().SetUpdateExtentToWholeExtent() #self._imageMask.SetMaskedOutputValue(0) module_utils.setup_vtk_object_progress(self, self._imageMask, 'Masking image') # 2. vtkImageCast self._image_cast = vtk.vtkImageCast() # type required by vtkImageMask self._image_cast.SetOutputScalarTypeToUnsignedChar() # connect output of cast to imagemask input self._imageMask.SetMaskInput(self._image_cast.GetOutput()) module_utils.setup_vtk_object_progress( self, self._image_cast, 'Casting mask image to unsigned char') ############################################################### self._config.not_mask = False self._config.masked_output_value = 0.0 config_list = [ ('Negate (NOT) mask:', 'not_mask', 'base:bool', 'checkbox', 'Should mask be negated (NOT operator applied) before ' 'applying to input?'), ('Masked output value:', 'masked_output_value', 'base:float', 'text', 'Positions outside the mask will be assigned this ' 'value.')] ScriptedConfigModuleMixin.__init__( self, config_list, {'Module (self)' :self, 'vtkImageMask' : self._imageMask}) self.sync_module_logic_with_config()
def getResampledData(self, data, n, resampleToDimensions = None): """ Return the data resampled to given dimensions """ if self.resampling and not scripting.resamplingDisabled: currentResamplingDimensions = self.getResampleDimensions() # If we're given dimensions to resample to, then we use those if resampleToDimensions: currentResamplingDimensions = resampleToDimensions # If the resampling dimensions are not defined (even though resampling is requested) # then don't resample the data if not currentResamplingDimensions: return data if n == self.resampledTimepoint and self.resampleFilter and not currentResamplingDimensions: data = self.resampleFilter.GetOutput() else: Logging.info("Resampling data to ", self.resampleDims, kw = "datasource") if not self.resampleFilter: self.resampleFilter = vtk.vtkImageResample() x, y, z = self.originalDimensions rx, ry, rz = currentResamplingDimensions xf = rx / float(x) yf = ry / float(y) zf = rz / float(z) self.resampleFilter.SetAxisMagnificationFactor(0, xf) self.resampleFilter.SetAxisMagnificationFactor(1, yf) self.resampleFilter.SetAxisMagnificationFactor(2, zf) else: self.resampleFilter.RemoveAllInputs() self.resampleFilter.SetInputConnection(data.GetProducerPort()) data = self.resampleFilter.GetOutput() if self.mask: if not self.maskImageFilter: self.maskImageFilter = vtk.vtkImageMask() self.maskImageFilter.SetMaskedOutputValue(0) else: self.maskImageFilter.RemoveAllInputs() self.maskImageFilter.SetImageInput(data) self.maskImageFilter.SetMaskInput(self.mask.getMaskImage()) data = self.maskImageFilter.GetOutput() return data
def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) # setup VTK pipeline ######################################### # 1. vtkImageMask self._imageMask = vtk.vtkImageMask() self._imageMask.GetOutput().SetUpdateExtentToWholeExtent() #self._imageMask.SetMaskedOutputValue(0) module_utils.setup_vtk_object_progress(self, self._imageMask, 'Masking image') # 2. vtkImageCast self._image_cast = vtk.vtkImageCast() # type required by vtkImageMask self._image_cast.SetOutputScalarTypeToUnsignedChar() # connect output of cast to imagemask input self._imageMask.SetMaskInput(self._image_cast.GetOutput()) module_utils.setup_vtk_object_progress( self, self._image_cast, 'Casting mask image to unsigned char') ############################################################### self._config.not_mask = False self._config.masked_output_value = 0.0 config_list = [ ('Negate (NOT) mask:', 'not_mask', 'base:bool', 'checkbox', 'Should mask be negated (NOT operator applied) before ' 'applying to input?'), ('Masked output value:', 'masked_output_value', 'base:float', 'text', 'Positions outside the mask will be assigned this ' 'value.') ] ScriptedConfigModuleMixin.__init__(self, config_list, { 'Module (self)': self, 'vtkImageMask': self._imageMask }) self.sync_module_logic_with_config()
def create_volumerender(self, contourValueModerate, contourValueSevere): """Creates a volumerender of the masked data using iso-contour surfaces created by the Marching Cubes algorithm at the specified contourvalues. """ self._view_frame.SetStatusText("Creating Volumerender...") self.image_data mask = vtk.vtkImageMask() severeFraction = 0.10 moderateFraction = 0.12 # We only want to contour the lungs, so mask it mask.SetMaskInput(self.mask_data) mask.SetInput(self.image_data) mask.Update() self.lungVolume = mask.GetOutput() if contourValueModerate == 0 and contourValueSevere == 0: # This means we get to calculate the percentual values ourselves! scalars = self.lungVolume.GetScalarRange() range = scalars[1] - scalars[0] contourValueSevere = scalars[0] + range * severeFraction contourValueModerate = scalars[0] + range * moderateFraction self._view_frame.upper_slider.SetValue(contourValueModerate) self._view_frame.lower_slider.SetValue(contourValueSevere) self.create_overlay(contourValueModerate, contourValueSevere) # Create the contours self.adjust_contour(self.lungVolume, contourValueSevere, self.severe_mapper) self.adjust_contour(self.lungVolume, contourValueModerate, self.moderate_mapper) #self.adjust_contour(self.mask_data, 0.5, self.lung_mapper) self.create_lungcontour() # Set the camera to a nice view cam = self.ren.GetActiveCamera() cam.SetPosition(0, -100, 0) cam.SetFocalPoint(0, 0, 0) cam.SetViewUp(0, 0, 1) self.ren.ResetCamera() self.render() self._view_frame.SetStatusText("Created Volumerender")
def getObjectsForROI(self, roi, minsize=1): """ return the intensity values of objects in a given roi """ imagedata = self.getInputFromChannel(0) mx, my, mz = self.dataUnit.getDimensions() n, maskImage = lib.ImageOperations.getMaskFromROIs([roi], mx, my, mz) maskFilter = vtk.vtkImageMask() maskFilter.SetMaskedOutputValue(0) maskFilter.SetMaskInput(maskImage) maskFilter.SetImageInput(imagedata) maskFilter.Update() data = maskFilter.GetOutput() histogram = lib.ImageOperations.get_histogram(data, maxrange=1) ret = [] for i in range(1, len(histogram)): if histogram[i] >= minsize: ret.append(i) return ret
def getObjectsForROI(self, roi, minsize = 1): """ return the intensity values of objects in a given roi """ imagedata = self.getInputFromChannel(0) mx, my, mz = self.dataUnit.getDimensions() n, maskImage = lib.ImageOperations.getMaskFromROIs([roi], mx, my, mz) maskFilter = vtk.vtkImageMask() maskFilter.SetMaskedOutputValue(0) maskFilter.SetMaskInput(maskImage) maskFilter.SetImageInput(imagedata) maskFilter.Update() data = maskFilter.GetOutput() histogram = lib.ImageOperations.get_histogram(data, maxrange = 1) ret = [] for i in range(1, len(histogram)): if histogram[i] >= minsize: ret.append(i) return ret
def create_volumerender(self, contourValueModerate, contourValueSevere): """Creates a volumerender of the masked data using iso-contour surfaces created by the Marching Cubes algorithm at the specified contourvalues. """ self._view_frame.SetStatusText("Creating Volumerender...") self.image_data mask = vtk.vtkImageMask() severeFraction = 0.10 moderateFraction = 0.12 # We only want to contour the lungs, so mask it mask.SetMaskInput(self.mask_data) mask.SetInput(self.image_data) mask.Update() self.lungVolume = mask.GetOutput() if contourValueModerate == 0 and contourValueSevere == 0: # This means we get to calculate the percentual values ourselves! scalars = self.lungVolume.GetScalarRange() range = scalars[1]-scalars[0] contourValueSevere = scalars[0]+range*severeFraction contourValueModerate = scalars[0]+range*moderateFraction self._view_frame.upper_slider.SetValue(contourValueModerate) self._view_frame.lower_slider.SetValue(contourValueSevere) self.create_overlay(contourValueModerate,contourValueSevere) # Create the contours self.adjust_contour(self.lungVolume, contourValueSevere, self.severe_mapper) self.adjust_contour(self.lungVolume, contourValueModerate, self.moderate_mapper) #self.adjust_contour(self.mask_data, 0.5, self.lung_mapper) self.create_lungcontour() # Set the camera to a nice view cam = self.ren.GetActiveCamera() cam.SetPosition(0,-100,0) cam.SetFocalPoint(0,0,0) cam.SetViewUp(0,0,1) self.ren.ResetCamera() self.render() self._view_frame.SetStatusText("Created Volumerender")
reader1.GlobalWarningDisplayOff() #reader1.DataOnCellsOn() reader1.Update() print "Reading: ", input reader2 = vtkn88.vtkn88AIMReader() reader2.SetFileName(input2) reader2.GlobalWarningDisplayOff() #reader2.DataOnCellsOn() reader2.Update() shift = vtk.vtkImageShiftScale() shift.SetInputConnection(reader1.GetOutputPort()) shift.SetOutputScalarTypeToUnsignedChar() mask = vtk.vtkImageMask() mask.SetImageInput(reader2.GetOutput()) mask.SetMaskInput(shift.GetOutput()) mask.SetMaskedOutputValue(0) if peel_iter != 0: erode = vtk.vtkImageResize() erode.SetKernelSize(2 * peel_iter, 2 * peel_iter, 2 * peel_iter) erode.SetInputConnection(mask.GetOutputPort()) erode.Update() writer = vtkn88.vtkn88AIMWriter() writer.SetInputConnection(erode.GetOutputPort()) writer.SetFileName(output) #writer.SetAimOffset( offset[0], offset[1], offset[2] ) writer.Update()
def autocontour_buie(img, args): # these hard-coded constants just make sure that the connectivity filter # actually works, they don't need to be configuable in my opinion CONN_SCALAR_RANGE = [1, args.in_value + 1] CONN_SIZE_RANGE = [0, int(1E10)] # Step 1 was just loading the AIM # Step 2: Threshold s2_threshold = vtk.vtkImageThreshold() s2_threshold.ThresholdByUpper(args.threshold_1) s2_threshold.SetInValue(args.in_value) s2_threshold.SetOutValue(args.out_value) s2_threshold.SetInputData(img) # Step 3: Median s3_median = vtk.vtkImageMedian3D() s3_median.SetKernelSize(*args.step_3_median_kernel) s3_median.SetInputConnection(s2_threshold.GetOutputPort()) # Step 4: Dilate s4_dilate = vtk.vtkImageDilateErode3D() s4_dilate.SetDilateValue(args.in_value) s4_dilate.SetErodeValue(args.out_value) s4_dilate.SetKernelSize(*args.step_4_and_6_dilate_erode_kernel) s4_dilate.SetInputConnection(s3_median.GetOutputPort()) # Step 5: Connectivity (applied to non-bone) # first flip 0 <-> 127 s5a_invert = vtk.vtkImageThreshold() s5a_invert.ThresholdByLower(args.in_value / 2) s5a_invert.SetInValue(args.in_value) s5a_invert.SetOutValue(args.out_value) s5a_invert.SetInputConnection(s4_dilate.GetOutputPort()) # then connectivity s5b_connectivity = vtk.vtkImageConnectivityFilter() s5b_connectivity.SetExtractionModeToLargestRegion() s5b_connectivity.SetScalarRange(*CONN_SCALAR_RANGE) s5b_connectivity.SetSizeRange(*CONN_SIZE_RANGE) s5b_connectivity.SetInputConnection(s5a_invert.GetOutputPort()) # then flip 0 <-> 127 again s5c_invert = vtk.vtkImageThreshold() s5c_invert.ThresholdByLower(0.5) s5c_invert.SetInValue(args.in_value) s5c_invert.SetOutValue(args.out_value) s5c_invert.SetInputConnection(s5b_connectivity.GetOutputPort()) # Step 6: Erode s6_erode = vtk.vtkImageDilateErode3D() s6_erode.SetDilateValue(args.out_value) s6_erode.SetErodeValue(args.in_value) s6_erode.SetKernelSize(*args.step_4_and_6_dilate_erode_kernel) s6_erode.SetInputConnection(s5c_invert.GetOutputPort()) # Step 7: Threshold s7_threshold = vtk.vtkImageThreshold() s7_threshold.ThresholdByLower(args.threshold_2) s7_threshold.SetInValue(args.in_value) s7_threshold.SetOutValue(args.out_value) s7_threshold.SetInputData(img) # Step 8: Mask s8a_mask = vtk.vtkImageMask() s8a_mask.SetInputConnection(0, s7_threshold.GetOutputPort()) s8a_mask.SetInputConnection(1, s6_erode.GetOutputPort()) s8b_invert = vtk.vtkImageThreshold() s8b_invert.ThresholdByLower(args.in_value / 2) s8b_invert.SetInValue(args.in_value) s8b_invert.SetOutValue(args.out_value) s8b_invert.SetInputConnection(s8a_mask.GetOutputPort()) # Step 9: Dilate s9_dilate = vtk.vtkImageDilateErode3D() s9_dilate.SetDilateValue(args.out_value) s9_dilate.SetErodeValue(args.in_value) s9_dilate.SetKernelSize(*args.step_9_and_11_dilate_erode_kernel) s9_dilate.SetInputConnection(s8b_invert.GetOutputPort()) # Step 10: Connectivity # connectivity s10a_connectivity = vtk.vtkImageConnectivityFilter() s10a_connectivity.SetExtractionModeToLargestRegion() s10a_connectivity.SetScalarRange(*CONN_SCALAR_RANGE) s10a_connectivity.SetSizeRange(*CONN_SIZE_RANGE) s10a_connectivity.SetInputConnection(s9_dilate.GetOutputPort()) # then convert to 127 and 0 again s10b_convert = vtk.vtkImageThreshold() s10b_convert.ThresholdByLower(0.5) s10b_convert.SetInValue(args.out_value) s10b_convert.SetOutValue(args.in_value) s10b_convert.SetInputConnection(s10a_connectivity.GetOutputPort()) # Step 11: Erode s11_erode = vtk.vtkImageDilateErode3D() s11_erode.SetDilateValue(args.in_value) s11_erode.SetErodeValue(args.out_value) s11_erode.SetKernelSize(*args.step_9_and_11_dilate_erode_kernel) s11_erode.SetInputConnection(s10b_convert.GetOutputPort()) # Step 12: Gaussian Smooth s12_gauss = vtk.vtkImageGaussianSmooth() s12_gauss.SetStandardDeviation(args.step_12_gaussian_std) s12_gauss.SetRadiusFactors(*args.step_12_gaussian_kernel) s12_gauss.SetInputConnection(s11_erode.GetOutputPort()) # Step 13: Threshold s13_threshold = vtk.vtkImageThreshold() s13_threshold.ThresholdByLower(S13_THRESH) s13_threshold.SetInValue(args.out_value) s13_threshold.SetOutValue(args.in_value) s13_threshold.SetInputConnection(s12_gauss.GetOutputPort()) # Step 14: Mask s14_mask = vtk.vtkImageMask() s14_mask.SetInputConnection(0, s6_erode.GetOutputPort()) s14_mask.SetInputConnection(1, s13_threshold.GetOutputPort()) # Step 15: Invert the Trabecular Mask s15_invert = vtk.vtkImageThreshold() s15_invert.ThresholdByLower(args.in_value / 2) s15_invert.SetInValue(args.in_value) s15_invert.SetOutValue(args.out_value) s15_invert.SetInputConnection(s13_threshold.GetOutputPort()) # update the pipeline s14_mask.Update() s15_invert.Update() # get masks cort_mask = s14_mask.GetOutput() trab_mask = s15_invert.GetOutput() return cort_mask, trab_mask
from vtk.util.misc import vtkGetDataRoot VTK_DATA_ROOT = vtkGetDataRoot() # A script to test the mask filter. # replaces a circle with a color # Image pipeline reader = vtk.vtkPNMReader() reader.ReleaseDataFlagOff() reader.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/earth.ppm") reader.Update() sphere = vtk.vtkImageEllipsoidSource() sphere.SetWholeExtent(0,511,0,255,0,0) sphere.SetCenter(128,128,0) sphere.SetRadius(80,80,1) sphere.Update() mask = vtk.vtkImageMask() mask.SetImageInputData(reader.GetOutput()) mask.SetMaskInputData(sphere.GetOutput()) mask.SetMaskedOutputValue(100,128,200) mask.NotMaskOn() mask.ReleaseDataFlagOff() mask.Update() sphere2 = vtk.vtkImageEllipsoidSource() sphere2.SetWholeExtent(0,511,0,255,0,0) sphere2.SetCenter(328,128,0) sphere2.SetRadius(80,50,1) sphere2.Update() # Test the wrapping of the output masked value mask2 = vtk.vtkImageMask() mask2.SetImageInputData(mask.GetOutput()) mask2.SetMaskInputData(sphere2.GetOutput())
def apply(self, ijkPoints): kernelSizePixel = self.getKernelSizePixel() if kernelSizePixel[0]<=0 and kernelSizePixel[1]<=0 and kernelSizePixel[2]<=0: return qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor) # Get parameter set node parameterSetNode = self.scriptedEffect.parameterSetNode() # Get parameters minimumThreshold = self.scriptedEffect.doubleParameter("MinimumThreshold") maximumThreshold = self.scriptedEffect.doubleParameter("MaximumThreshold") # Get modifier labelmap modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap() # Get master volume image data masterImageData = self.scriptedEffect.masterVolumeImageData() # Set intensity range oldMasterVolumeIntensityMask = parameterSetNode.GetMasterVolumeIntensityMask() parameterSetNode.MasterVolumeIntensityMaskOn() oldIntensityMaskRange = parameterSetNode.GetMasterVolumeIntensityMaskRange() intensityRange = [265.00, 1009.00] if oldMasterVolumeIntensityMask: intensityRange = [max(oldIntensityMaskRange[0], minimumThreshold), min(oldIntensityMaskRange[1], maximumThreshold)] parameterSetNode.SetMasterVolumeIntensityMaskRange(intensityRange) roiNode = lumbarSeed ##self.roiSelector.currentNode() clippedMasterImageData = masterImageData if roiNode is not None: worldToImageMatrix = vtk.vtkMatrix4x4() masterImageData.GetWorldToImageMatrix(worldToImageMatrix) bounds = [0,0,0,0,0,0] roiNode.GetRASBounds(bounds) corner1RAS = [bounds[0], bounds[2], bounds[4], 1] corner1IJK = [0, 0, 0, 0] worldToImageMatrix.MultiplyPoint(corner1RAS, corner1IJK) corner2RAS = [bounds[1], bounds[3], bounds[5], 1] corner2IJK = [0, 0, 0, 0] worldToImageMatrix.MultiplyPoint(corner2RAS, corner2IJK) extent = [0, -1, 0, -1, 0, -1] for i in range(3): lowerPoint = min(corner1IJK[i], corner2IJK[i]) upperPoint = max(corner1IJK[i], corner2IJK[i]) extent[2*i] = int(math.floor(lowerPoint)) extent[2*i+1] = int(math.ceil(upperPoint)) imageToWorldMatrix = vtk.vtkMatrix4x4() masterImageData.GetImageToWorldMatrix(imageToWorldMatrix) clippedMasterImageData = slicer.vtkOrientedImageData() self.padder = vtk.vtkImageConstantPad() self.padder.SetInputData(masterImageData) self.padder.SetOutputWholeExtent(extent) self.padder.Update() clippedMasterImageData.ShallowCopy(self.padder.GetOutput()) clippedMasterImageData.SetImageToWorldMatrix(imageToWorldMatrix) # Pipeline self.thresh = vtk.vtkImageThreshold() self.thresh.SetInValue(LABEL_VALUE) self.thresh.SetOutValue(BACKGROUND_VALUE) self.thresh.SetInputData(clippedMasterImageData) self.thresh.ThresholdBetween(minimumThreshold, maximumThreshold) self.thresh.SetOutputScalarTypeToUnsignedChar() self.thresh.Update() self.erode = vtk.vtkImageDilateErode3D() self.erode.SetInputConnection(self.thresh.GetOutputPort()) self.erode.SetDilateValue(BACKGROUND_VALUE) self.erode.SetErodeValue(LABEL_VALUE) self.erode.SetKernelSize( kernelSizePixel[0], kernelSizePixel[1], kernelSizePixel[2]) self.erodeCast = vtk.vtkImageCast() self.erodeCast.SetInputConnection(self.erode.GetOutputPort()) self.erodeCast.SetOutputScalarTypeToUnsignedInt() self.erodeCast.Update() # Remove small islands self.islandMath = vtkITK.vtkITKIslandMath() self.islandMath.SetInputConnection(self.erodeCast.GetOutputPort()) self.islandMath.SetFullyConnected(False) self.islandMath.SetMinimumSize(125) # remove regions smaller than 5x5x5 voxels self.islandThreshold = vtk.vtkImageThreshold() self.islandThreshold.SetInputConnection(self.islandMath.GetOutputPort()) self.islandThreshold.ThresholdByLower(BACKGROUND_VALUE) self.islandThreshold.SetInValue(BACKGROUND_VALUE) self.islandThreshold.SetOutValue(LABEL_VALUE) self.islandThreshold.SetOutputScalarTypeToUnsignedChar() self.islandThreshold.Update() # Points may be outside the region after it is eroded. # Snap the points to LABEL_VALUE voxels, snappedIJKPoints = self.snapIJKPointsToLabel(ijkPoints, self.islandThreshold.GetOutput()) if snappedIJKPoints.GetNumberOfPoints() == 0: qt.QApplication.restoreOverrideCursor() return # Convert points to real data coordinates. Required for vtkImageThresholdConnectivity. seedPoints = vtk.vtkPoints() origin = masterImageData.GetOrigin() spacing = masterImageData.GetSpacing() for i in range(snappedIJKPoints.GetNumberOfPoints()): ijkPoint = snappedIJKPoints.GetPoint(i) seedPoints.InsertNextPoint( origin[0]+ijkPoint[0]*spacing[0], origin[1]+ijkPoint[1]*spacing[1], origin[2]+ijkPoint[2]*spacing[2]) segmentationAlgorithm = self.scriptedEffect.parameter(SEGMENTATION_ALGORITHM_PARAMETER_NAME) if segmentationAlgorithm == SEGMENTATION_ALGORITHM_MASKING: self.runMasking(seedPoints, self.islandThreshold.GetOutput(), modifierLabelmap) else: self.floodFillingFilterIsland = vtk.vtkImageThresholdConnectivity() self.floodFillingFilterIsland.SetInputConnection(self.islandThreshold.GetOutputPort()) self.floodFillingFilterIsland.SetInValue(SELECTED_ISLAND_VALUE) self.floodFillingFilterIsland.ReplaceInOn() self.floodFillingFilterIsland.ReplaceOutOff() self.floodFillingFilterIsland.ThresholdBetween(265.00, 1009.00) self.floodFillingFilterIsland.SetSeedPoints(seedPoints) self.floodFillingFilterIsland.Update() self.maskCast = vtk.vtkImageCast() self.maskCast.SetInputData(self.thresh.GetOutput()) self.maskCast.SetOutputScalarTypeToUnsignedChar() self.maskCast.Update() self.imageMask = vtk.vtkImageMask() self.imageMask.SetInputConnection(self.floodFillingFilterIsland.GetOutputPort()) self.imageMask.SetMaskedOutputValue(OUTSIDE_THRESHOLD_VALUE) self.imageMask.SetMaskInputData(self.maskCast.GetOutput()) self.imageMask.Update() imageMaskOutput = slicer.vtkOrientedImageData() imageMaskOutput.ShallowCopy(self.imageMask.GetOutput()) imageMaskOutput.CopyDirections(clippedMasterImageData) imageToWorldMatrix = vtk.vtkMatrix4x4() imageMaskOutput.GetImageToWorldMatrix(imageToWorldMatrix) segmentOutputLabelmap = slicer.vtkOrientedImageData() if segmentationAlgorithm == SEGMENTATION_ALGORITHM_GROWCUT: self.runGrowCut(clippedMasterImageData, imageMaskOutput, segmentOutputLabelmap) elif segmentationAlgorithm == SEGMENTATION_ALGORITHM_WATERSHED: self.runWatershed(clippedMasterImageData, imageMaskOutput, segmentOutputLabelmap) else: logging.error("Unknown segmentation algorithm: \"" + segmentationAlgorithm + "\"") segmentOutputLabelmap.SetImageToWorldMatrix(imageToWorldMatrix) self.selectedSegmentThreshold = vtk.vtkImageThreshold() self.selectedSegmentThreshold.SetInputData(segmentOutputLabelmap) self.selectedSegmentThreshold.ThresholdBetween(SELECTED_ISLAND_VALUE, SELECTED_ISLAND_VALUE) self.selectedSegmentThreshold.SetInValue(LABEL_VALUE) self.selectedSegmentThreshold.SetOutValue(BACKGROUND_VALUE) self.selectedSegmentThreshold.SetOutputScalarType(modifierLabelmap.GetScalarType()) self.selectedSegmentThreshold.Update() modifierLabelmap.ShallowCopy(self.selectedSegmentThreshold.GetOutput()) self.scriptedEffect.saveStateForUndo() self.scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd) parameterSetNode.SetMasterVolumeIntensityMask(oldMasterVolumeIntensityMask) parameterSetNode.SetMasterVolumeIntensityMaskRange(oldIntensityMaskRange) qt.QApplication.restoreOverrideCursor()
def execute(self, inputs, update = 0, last = 0): """ Execute the filter with given inputs and return the output """ if not lib.ProcessingFilter.ProcessingFilter.execute(self, inputs): return None maxx, maxy, maxz = self.dataUnit.getDimensions() minx = 0 miny = 0 minz = 0 if self.parameters["UseROI"]: minx, maxx = 99999, 0 miny, maxy = 99999, 0 roi = self.parameters["ROI"][1] pts = roi.getCoveredPoints() for (x, y) in pts: if minx > x: minx = x if x > maxx: maxx = x if miny > y: miny = y if y > maxy: maxy = y minz = self.parameters["FirstSlice"] maxz = self.parameters["LastSlice"] minz -= 1 maxz -= 1 maxx -= 1 maxy -= 1 scripting.wantWholeDataset=1 imagedata = self.getInput(1) imagedata.SetUpdateExtent(imagedata.GetWholeExtent()) imagedata.Update() # Mask original image if self.parameters["UseROI"]: mask = lib.ImageOperations.getMaskFromPoints(pts, maxx+1, maxy+1, maxz+1) maskFilter = vtk.vtkImageMask() maskFilter.SetImageInput(imagedata) maskFilter.SetMaskInput(mask) imagedata = maskFilter.GetOutput() imagedata.Update() # Extract VOI of original data self.vtkfilter.SetInput(imagedata) self.vtkfilter.SetVOI(minx, maxx, miny, maxy, minz, maxz) data = self.vtkfilter.GetOutput() translate = vtk.vtkImageTranslateExtent() translate.SetInput(data) #print "input data=",data translation = [0,0,0] if minz > 0: translation[2] = -minz if minx > 0: translation[0] = -minx if miny > 0: translation[1] = -miny newTranslation = translation[:] #if self.translation: # dx = self.translation[0]-minx # dy = self.translation[1]-miny # dz = self.translation[2]-minz # newTranslation = [dx,dy,dz] self.translation = newTranslation #lib.messenger.send(None, bxdevents.TRANSLATE_DATA, tuple(self.translation)) if translation != [0,0,0]: translate.SetTranslation(tuple(translation)) #translate.SetOutputOrigin(0,0,0) data = translate.GetOutput() data.Update() reslice = vtk.vtkImageChangeInformation() reslice.SetOutputOrigin(0,0,0) reslice.SetInput(data) data = reslice.GetOutput() print "Returning data",data return data
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 execute(self, inputs, update=0, last=0): """ Execute the filter with given inputs and return the output """ if not lib.ProcessingFilter.ProcessingFilter.execute(self, inputs): return None maxx, maxy, maxz = self.dataUnit.getDimensions() minx = 0 miny = 0 minz = 0 if self.parameters["UseROI"]: minx, maxx = 99999, 0 miny, maxy = 99999, 0 roi = self.parameters["ROI"][1] pts = roi.getCoveredPoints() for (x, y) in pts: if minx > x: minx = x if x > maxx: maxx = x if miny > y: miny = y if y > maxy: maxy = y minz = self.parameters["FirstSlice"] maxz = self.parameters["LastSlice"] minz -= 1 maxz -= 1 maxx -= 1 maxy -= 1 scripting.wantWholeDataset = 1 imagedata = self.getInput(1) imagedata.SetUpdateExtent(imagedata.GetWholeExtent()) imagedata.Update() # Mask original image if self.parameters["UseROI"]: mask = lib.ImageOperations.getMaskFromPoints( pts, maxx + 1, maxy + 1, maxz + 1) maskFilter = vtk.vtkImageMask() maskFilter.SetImageInput(imagedata) maskFilter.SetMaskInput(mask) imagedata = maskFilter.GetOutput() imagedata.Update() # Extract VOI of original data self.vtkfilter.SetInput(imagedata) self.vtkfilter.SetVOI(minx, maxx, miny, maxy, minz, maxz) data = self.vtkfilter.GetOutput() translate = vtk.vtkImageTranslateExtent() translate.SetInput(data) #print "input data=",data translation = [0, 0, 0] if minz > 0: translation[2] = -minz if minx > 0: translation[0] = -minx if miny > 0: translation[1] = -miny newTranslation = translation[:] #if self.translation: # dx = self.translation[0]-minx # dy = self.translation[1]-miny # dz = self.translation[2]-minz # newTranslation = [dx,dy,dz] self.translation = newTranslation #lib.messenger.send(None, bxdevents.TRANSLATE_DATA, tuple(self.translation)) if translation != [0, 0, 0]: translate.SetTranslation(tuple(translation)) #translate.SetOutputOrigin(0,0,0) data = translate.GetOutput() data.Update() reslice = vtk.vtkImageChangeInformation() reslice.SetOutputOrigin(0, 0, 0) reslice.SetInput(data) data = reslice.GetOutput() print "Returning data", data return data