def ipl_open(image_in, open): ipl_open_settings(open) extent = image_in.GetExtent() clip = vtk.vtkImageReslice() clip.SetInput(image_in) clip.SetOutputExtent(extent[0] - open - 2, extent[1] + open + 2, extent[2] - open - 2, extent[3] + open + 2, extent[4] - open - 2, extent[5] + open + 2) clip.MirrorOn() clip.Update() erode = vtk.vtkImageContinuousErode3D() erode.SetInputConnection(clip.GetOutputPort()) erode.SetKernelSize(2 * open + 1, 2 * open + 1, 2 * open + 1) erode.Update() #Kernel size is twice the dilation distance plus one for the center voxel dilate = vtk.vtkImageContinuousDilate3D() dilate.SetInputConnection(erode.GetOutputPort()) dilate.SetKernelSize(2 * open + 1, 2 * open + 1, 2 * open + 1) dilate.Update() voi = vtk.vtkExtractVOI() voi.SetInput(dilate.GetOutput()) voi.SetVOI(extent[0], extent[1], extent[2], extent[3], extent[4], extent[5]) voi.Update() image_out = voi.GetOutput() return image_out
def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) self._imageDilate = vtk.vtkImageContinuousDilate3D() self._imageErode = vtk.vtkImageContinuousErode3D() self._imageErode.SetInput(self._imageDilate.GetOutput()) module_utils.setup_vtk_object_progress(self, self._imageDilate, 'Performing greyscale 3D dilation') module_utils.setup_vtk_object_progress(self, self._imageErode, 'Performing greyscale 3D erosion') self._config.kernelSize = (3, 3, 3) configList = [ ('Kernel size:', 'kernelSize', 'tuple:int,3', 'text', 'Size of the kernel in x,y,z dimensions.')] ScriptedConfigModuleMixin.__init__( self, configList, {'Module (self)' : self, 'vtkImageContinuousDilate3D' : self._imageDilate, 'vtkImageContinuousErode3D' : self._imageErode}) self.sync_module_logic_with_config()
def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) self._imageDilate = vtk.vtkImageContinuousDilate3D() module_utils.setup_vtk_object_progress(self, self._imageDilate, 'Performing greyscale 3D dilation') self._config.kernelSize = (3, 3, 3) configList = [ ('Kernel size:', 'kernelSize', 'tuple:int,3', 'text', 'Size of the kernel in x,y,z dimensions.')] ScriptedConfigModuleMixin.__init__(self, configList) self._viewFrame = self._createWindow( {'Module (self)' : self, 'vtkImageContinuousDilate3D' : self._imageDilate}) # pass the data down to the underlying logic self.config_to_logic() # and all the way up from logic -> config -> view to make sure self.logic_to_config() self.config_to_view()
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkImageContinuousDilate3D(), 'Processing.', ('vtkImageData',), ('vtkImageData',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__(self, module_manager, vtk.vtkImageContinuousDilate3D(), 'Processing.', ('vtkImageData', ), ('vtkImageData', ), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def __init__(self): """ Initialization """ MorphologicalFilter.__init__(self) self.vtkfilter = vtk.vtkImageContinuousDilate3D() self.vtkfilter.AddObserver('ProgressEvent', lib.messenger.send) lib.messenger.connect(self.vtkfilter, "ProgressEvent", self.updateProgress) self.filterDesc = "Replaces each pixel/voxel with maximum value inside kernel\nInput: Grayscale/Binary image\nOutput: Grayscale/Binary image"
def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) # main morph gradient self._imageDilate = vtk.vtkImageContinuousDilate3D() self._imageErode = vtk.vtkImageContinuousErode3D() self._imageMath = vtk.vtkImageMathematics() self._imageMath.SetOperationToSubtract() self._imageMath.SetInput1(self._imageDilate.GetOutput()) self._imageMath.SetInput2(self._imageErode.GetOutput()) # inner gradient self._innerImageMath = vtk.vtkImageMathematics() self._innerImageMath.SetOperationToSubtract() self._innerImageMath.SetInput1(None) # has to take image self._innerImageMath.SetInput2(self._imageErode.GetOutput()) # outer gradient self._outerImageMath = vtk.vtkImageMathematics() self._outerImageMath.SetOperationToSubtract() self._outerImageMath.SetInput1(self._imageDilate.GetOutput()) self._outerImageMath.SetInput2(None) # has to take image module_utils.setup_vtk_object_progress( self, self._imageDilate, 'Performing greyscale 3D dilation') module_utils.setup_vtk_object_progress( self, self._imageErode, 'Performing greyscale 3D erosion') module_utils.setup_vtk_object_progress( self, self._imageMath, 'Subtracting erosion from ' 'dilation') module_utils.setup_vtk_object_progress( self, self._innerImageMath, 'Subtracting erosion from ' 'image (inner)') module_utils.setup_vtk_object_progress( self, self._outerImageMath, 'Subtracting image from ' 'dilation (outer)') self._config.kernelSize = (3, 3, 3) configList = [('Kernel size:', 'kernelSize', 'tuple:int,3', 'text', 'Size of the kernel in x,y,z dimensions.')] ScriptedConfigModuleMixin.__init__( self, configList, { 'Module (self)': self, 'vtkImageContinuousDilate3D': self._imageDilate, 'vtkImageContinuousErode3D': self._imageErode, 'vtkImageMathematics': self._imageMath }) self.sync_module_logic_with_config()
def dilate(self, neighbours=(2, 2, 2)): """Replace a voxel with the maximum over an ellipsoidal neighborhood of voxels. If `neighbours` of an axis is 1, no processing is done on that axis. See example script: |erode_dilate.py|_ """ ver = vtk.vtkImageContinuousDilate3D() ver.SetInputData(self._data) ver.SetKernelSize(neighbours[0], neighbours[1], neighbours[2]) ver.Update() return self._update(ver.GetOutput())
def VTKDilate(x,k=5,l=None,numret=0): if type(x) == np.ndarray: i = NumToVTKImage(x) else: i = x if l is None: l = k d = vtk.vtkImageContinuousDilate3D() d.SetKernelSize(k,l,1) d.SetInputData(i) o = d.GetOutput() d.Update() if numret: return VTKImageToNum(o) else: return o
def ipl_dilation(image_in, dilate_distance, boundary): ipl_dilation_settings(dilate_distance, boundary) extent = image_in.GetExtent() #Actual distance of pixel dilation distance = dilate_distance + 1 pad = vtk.vtkImageReslice() pad.SetInput(image_in) pad.SetOutputExtent(extent[0] - distance - 1, extent[1] + distance + 1, extent[2] - distance - 1, extent[3] + distance + 1, extent[4] - distance - 1, extent[5] + distance + 1) pad.Update() #Kernel size is twice the dilation distance plus one for the center voxel dilate = vtk.vtkImageContinuousDilate3D() dilate.SetInputConnection(pad.GetOutputPort()) dilate.SetKernelSize(2 * distance + 1, 2 * distance + 1, 2 * distance + 1) dilate.Update() ext = dilate.GetOutput().GetExtent() border = vtk.vtkExtractVOI() border.SetInput(dilate.GetOutput()) border.SetVOI(ext[0] + boundary[0] * distance, ext[1] - boundary[0] * distance, ext[2] + boundary[1] * distance, ext[3] - boundary[1] * distance, ext[4] + boundary[2] * distance, ext[5] - boundary[2] * distance) border.Update() clip = vtk.vtkImageReslice() clip.SetInput(border.GetOutput()) clip.SetOutputExtent(ext[0] + 1, ext[1] - 1, ext[2] + 1, ext[3] - 1, ext[4] + 1, ext[5] - 1) clip.MirrorOn() clip.Update() image_out = clip.GetOutput() return image_out
def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) self._imageDilate = vtk.vtkImageContinuousDilate3D() module_utils.setup_vtk_object_progress(self, self._imageDilate, "Performing greyscale 3D dilation") self._config.kernelSize = (3, 3, 3) configList = [("Kernel size:", "kernelSize", "tuple:int,3", "text", "Size of the kernel in x,y,z dimensions.")] ScriptedConfigModuleMixin.__init__(self, configList) self._viewFrame = self._createWindow({"Module (self)": self, "vtkImageContinuousDilate3D": self._imageDilate}) # pass the data down to the underlying logic self.config_to_logic() # and all the way up from logic -> config -> view to make sure self.logic_to_config() self.config_to_view()
def dilate(image,kernel): dilate = vtk.vtkImageContinuousDilate3D() dilate.SetInput(image) dilate.SetKernelSize(kernel[0],kernel[1],kernel[2]) dilate.Update() return dilate.GetOutput()
#!/usr/bin/env python import vtk from vtk.test import Testing from vtk.util.misc import vtkGetDataRoot VTK_DATA_ROOT = vtkGetDataRoot() # Image pipeline reader = vtk.vtkImageReader() reader.SetDataByteOrderToLittleEndian() reader.SetDataExtent(0,63,0,63,1,93) reader.SetFilePrefix("" + str(VTK_DATA_ROOT) + "/Data/headsq/quarter") reader.SetDataMask(0x7fff) dilate = vtk.vtkImageContinuousDilate3D() dilate.SetInputConnection(reader.GetOutputPort()) dilate.SetKernelSize(11,11,1) erode = vtk.vtkImageContinuousErode3D() erode.SetInputConnection(dilate.GetOutputPort()) erode.SetKernelSize(11,11,1) viewer = vtk.vtkImageViewer() viewer.SetInputConnection(erode.GetOutputPort()) viewer.SetColorWindow(2000) viewer.SetColorLevel(1000) viewer.Render() # --- end of script --
def __init__(self, module_manager): # initialise our base class ModuleBase.__init__(self, module_manager) # main morph gradient self._imageDilate = vtk.vtkImageContinuousDilate3D() self._imageErode = vtk.vtkImageContinuousErode3D() self._imageMath = vtk.vtkImageMathematics() self._imageMath.SetOperationToSubtract() self._imageMath.SetInput1(self._imageDilate.GetOutput()) self._imageMath.SetInput2(self._imageErode.GetOutput()) # inner gradient self._innerImageMath = vtk.vtkImageMathematics() self._innerImageMath.SetOperationToSubtract() self._innerImageMath.SetInput1(None) # has to take image self._innerImageMath.SetInput2(self._imageErode.GetOutput()) # outer gradient self._outerImageMath = vtk.vtkImageMathematics() self._outerImageMath.SetOperationToSubtract() self._outerImageMath.SetInput1(self._imageDilate.GetOutput()) self._outerImageMath.SetInput2(None) # has to take image module_utils.setup_vtk_object_progress(self, self._imageDilate, 'Performing greyscale 3D dilation') module_utils.setup_vtk_object_progress(self, self._imageErode, 'Performing greyscale 3D erosion') module_utils.setup_vtk_object_progress(self, self._imageMath, 'Subtracting erosion from ' 'dilation') module_utils.setup_vtk_object_progress(self, self._innerImageMath, 'Subtracting erosion from ' 'image (inner)') module_utils.setup_vtk_object_progress(self, self._outerImageMath, 'Subtracting image from ' 'dilation (outer)') self._config.kernelSize = (3, 3, 3) configList = [ ('Kernel size:', 'kernelSize', 'tuple:int,3', 'text', 'Size of the kernel in x,y,z dimensions.')] ScriptedConfigModuleMixin.__init__( self, configList, {'Module (self)' : self, 'vtkImageContinuousDilate3D' : self._imageDilate, 'vtkImageContinuousErode3D' : self._imageErode, 'vtkImageMathematics' : self._imageMath}) self.sync_module_logic_with_config()
def __init__(self, ren, renWin, iren): self.ren = ren self.renWin = renWin self.iren = iren # Create a gaussian gs = vtk.vtkImageGaussianSource() gs.SetWholeExtent(0, 30, 0, 30, 0, 30) gs.SetMaximum(255.0) gs.SetStandardDeviation(5) gs.SetCenter(15, 15, 15) # threshold to leave a gap that should show up for # gradient opacity t = vtk.vtkImageThreshold() t.SetInputConnection(gs.GetOutputPort()) t.ReplaceInOn() t.SetInValue(0) t.ThresholdBetween(150, 200) # Use a shift scale to convert to unsigned char ss = vtk.vtkImageShiftScale() ss.SetInputConnection(t.GetOutputPort()) ss.SetOutputScalarTypeToUnsignedChar() # grid will be used for two component dependent grid0 = vtk.vtkImageGridSource() grid0.SetDataScalarTypeToUnsignedChar() grid0.SetGridSpacing(10, 10, 10) grid0.SetLineValue(200) grid0.SetFillValue(10) grid0.SetDataExtent(0, 30, 0, 30, 0, 30) # use dilation to thicken the grid d = vtk.vtkImageContinuousDilate3D() d.SetInputConnection(grid0.GetOutputPort()) d.SetKernelSize(3, 3, 3) # Now make a two component dependent iac = vtk.vtkImageAppendComponents() iac.AddInputConnection(d.GetOutputPort()) iac.AddInputConnection(ss.GetOutputPort()) # Some more gaussians for the four component indepent case gs1 = vtk.vtkImageGaussianSource() gs1.SetWholeExtent(0, 30, 0, 30, 0, 30) gs1.SetMaximum(255.0) gs1.SetStandardDeviation(4) gs1.SetCenter(5, 5, 5) t1 = vtk.vtkImageThreshold() t1.SetInputConnection(gs1.GetOutputPort()) t1.ReplaceInOn() t1.SetInValue(0) t1.ThresholdBetween(150, 256) gs2 = vtk.vtkImageGaussianSource() gs2.SetWholeExtent(0, 30, 0, 30, 0, 30) gs2.SetMaximum(255.0) gs2.SetStandardDeviation(4) gs2.SetCenter(12, 12, 12) gs3 = vtk.vtkImageGaussianSource() gs3.SetWholeExtent(0, 30, 0, 30, 0, 30) gs3.SetMaximum(255.0) gs3.SetStandardDeviation(4) gs3.SetCenter(19, 19, 19) t3 = vtk.vtkImageThreshold() t3.SetInputConnection(gs3.GetOutputPort()) t3.ReplaceInOn() t3.SetInValue(0) t3.ThresholdBetween(150, 256) gs4 = vtk.vtkImageGaussianSource() gs4.SetWholeExtent(0, 30, 0, 30, 0, 30) gs4.SetMaximum(255.0) gs4.SetStandardDeviation(4) gs4.SetCenter(26, 26, 26) # we need a few append filters ... iac1 = vtk.vtkImageAppendComponents() iac1.AddInputConnection(t1.GetOutputPort()) iac1.AddInputConnection(gs2.GetOutputPort()) iac2 = vtk.vtkImageAppendComponents() iac2.AddInputConnection(iac1.GetOutputPort()) iac2.AddInputConnection(t3.GetOutputPort()) iac3 = vtk.vtkImageAppendComponents() iac3.AddInputConnection(iac2.GetOutputPort()) iac3.AddInputConnection(gs4.GetOutputPort()) # create the four component dependend - # use lines in x, y, z for colors gridR = vtk.vtkImageGridSource() gridR.SetDataScalarTypeToUnsignedChar() gridR.SetGridSpacing(10, 100, 100) gridR.SetLineValue(250) gridR.SetFillValue(100) gridR.SetDataExtent(0, 30, 0, 30, 0, 30) dR = vtk.vtkImageContinuousDilate3D() dR.SetInputConnection(gridR.GetOutputPort()) dR.SetKernelSize(2, 2, 2) gridG = vtk.vtkImageGridSource() gridG.SetDataScalarTypeToUnsignedChar() gridG.SetGridSpacing(100, 10, 100) gridG.SetLineValue(250) gridG.SetFillValue(100) gridG.SetDataExtent(0, 30, 0, 30, 0, 30) dG = vtk.vtkImageContinuousDilate3D() dG.SetInputConnection(gridG.GetOutputPort()) dG.SetKernelSize(2, 2, 2) gridB = vtk.vtkImageGridSource() gridB.SetDataScalarTypeToUnsignedChar() gridB.SetGridSpacing(100, 100, 10) gridB.SetLineValue(0) gridB.SetFillValue(250) gridB.SetDataExtent(0, 30, 0, 30, 0, 30) dB = vtk.vtkImageContinuousDilate3D() dB.SetInputConnection(gridB.GetOutputPort()) dB.SetKernelSize(2, 2, 2) # need some appending iacRG = vtk.vtkImageAppendComponents() iacRG.AddInputConnection(dR.GetOutputPort()) iacRG.AddInputConnection(dG.GetOutputPort()) iacRGB = vtk.vtkImageAppendComponents() iacRGB.AddInputConnection(iacRG.GetOutputPort()) iacRGB.AddInputConnection(dB.GetOutputPort()) iacRGBA = vtk.vtkImageAppendComponents() iacRGBA.AddInputConnection(iacRGB.GetOutputPort()) iacRGBA.AddInputConnection(ss.GetOutputPort()) # We need a bunch of opacity functions # this one is a simple ramp to .2 rampPoint2 = vtk.vtkPiecewiseFunction() rampPoint2.AddPoint(0, 0.0) rampPoint2.AddPoint(255, 0.2) # this one is a simple ramp to 1 ramp1 = vtk.vtkPiecewiseFunction() ramp1.AddPoint(0, 0.0) ramp1.AddPoint(255, 1.0) # this one shows a sharp surface surface = vtk.vtkPiecewiseFunction() surface.AddPoint(0, 0.0) surface.AddPoint(10, 0.0) surface.AddPoint(50, 1.0) surface.AddPoint(255, 1.0) # this one is constant 1 constant1 = vtk.vtkPiecewiseFunction() constant1.AddPoint(0, 1.0) constant1.AddPoint(255, 1.0) # this one is used for gradient opacity gop = vtk.vtkPiecewiseFunction() gop.AddPoint(0, 0.0) gop.AddPoint(20, 0.0) gop.AddPoint(60, 1.0) gop.AddPoint(255, 1.0) # We need a bunch of color functions # This one is a simple rainbow rainbow = vtk.vtkColorTransferFunction() rainbow.SetColorSpaceToHSV() rainbow.HSVWrapOff() rainbow.AddHSVPoint(0, 0.1, 1.0, 1.0) rainbow.AddHSVPoint(255, 0.9, 1.0, 1.0) # this is constant red red = vtk.vtkColorTransferFunction() red.AddRGBPoint(0, 1, 0, 0) red.AddRGBPoint(255, 1, 0, 0) # this is constant green green = vtk.vtkColorTransferFunction() green.AddRGBPoint(0, 0, 1, 0) green.AddRGBPoint(255, 0, 1, 0) # this is constant blue blue = vtk.vtkColorTransferFunction() blue.AddRGBPoint(0, 0, 0, 1) blue.AddRGBPoint(255, 0, 0, 1) # this is constant yellow yellow = vtk.vtkColorTransferFunction() yellow.AddRGBPoint(0, 1, 1, 0) yellow.AddRGBPoint(255, 1, 1, 0) #ren = vtk.vtkRenderer() #renWin = vtk.vtkRenderWindow() self.renWin.AddRenderer(self.ren) self.renWin.SetSize(500, 500) #iren = vtk.vtkRenderWindowInteractor() self.iren.SetRenderWindow(self.renWin) self.ren.GetCullers().InitTraversal() culler = self.ren.GetCullers().GetNextItem() culler.SetSortingStyleToBackToFront() # We need 25 mapper / actor pairs which we will render # in a grid. Going down we will vary the input data # with the top row unsigned char, then float, then # two dependent components, then four dependent components # then four independent components. Going across we # will vary the rendering method with MIP, Composite, # Composite Shade, Composite GO, and Composite GO Shade. # Create the 5 by 5 grids self.volumeProperty = [[0 for col in range(0, 5)] for row in range(0, 5)] self.volumeMapper = [[0 for col in range(0, 5)] for row in range(0, 5)] volume = [[0 for col in range(0, 5)] for row in range(0, 5)] for i in range(0, 5): for j in range(0, 5): self.volumeProperty[i][j] = vtk.vtkVolumeProperty() self.volumeMapper[i][j] = vtk.vtkFixedPointVolumeRayCastMapper() self.volumeMapper[i][j].SetSampleDistance(0.25) volume[i][j] = vtk.vtkVolume() volume[i][j].SetMapper(self.volumeMapper[i][j]) volume[i][j].SetProperty(self.volumeProperty[i][j]) volume[i][j].AddPosition(i * 30, j * 30, 0) self.ren.AddVolume(volume[i][j]) for i in range(0, 5): self.volumeMapper[0][i].SetInputConnection(t.GetOutputPort()) self.volumeMapper[1][i].SetInputConnection(ss.GetOutputPort()) self.volumeMapper[2][i].SetInputConnection(iac.GetOutputPort()) self.volumeMapper[3][i].SetInputConnection(iac3.GetOutputPort()) self.volumeMapper[4][i].SetInputConnection(iacRGBA.GetOutputPort()) self.volumeMapper[i][0].SetBlendModeToMaximumIntensity() self.volumeMapper[i][1].SetBlendModeToComposite() self.volumeMapper[i][2].SetBlendModeToComposite() self.volumeMapper[i][3].SetBlendModeToComposite() self.volumeMapper[i][4].SetBlendModeToComposite() self.volumeProperty[0][i].IndependentComponentsOn() self.volumeProperty[1][i].IndependentComponentsOn() self.volumeProperty[2][i].IndependentComponentsOff() self.volumeProperty[3][i].IndependentComponentsOn() self.volumeProperty[4][i].IndependentComponentsOff() self.volumeProperty[0][i].SetColor(rainbow) self.volumeProperty[0][i].SetScalarOpacity(rampPoint2) self.volumeProperty[0][i].SetGradientOpacity(constant1) self.volumeProperty[1][i].SetColor(rainbow) self.volumeProperty[1][i].SetScalarOpacity(rampPoint2) self.volumeProperty[1][i].SetGradientOpacity(constant1) self.volumeProperty[2][i].SetColor(rainbow) self.volumeProperty[2][i].SetScalarOpacity(rampPoint2) self.volumeProperty[2][i].SetGradientOpacity(constant1) self.volumeProperty[3][i].SetColor(0, red) self.volumeProperty[3][i].SetColor(1, green) self.volumeProperty[3][i].SetColor(2, blue) self.volumeProperty[3][i].SetColor(3, yellow) self.volumeProperty[3][i].SetScalarOpacity(0, rampPoint2) self.volumeProperty[3][i].SetScalarOpacity(1, rampPoint2) self.volumeProperty[3][i].SetScalarOpacity(2, rampPoint2) self.volumeProperty[3][i].SetScalarOpacity(3, rampPoint2) self.volumeProperty[3][i].SetGradientOpacity(0, constant1) self.volumeProperty[3][i].SetGradientOpacity(1, constant1) self.volumeProperty[3][i].SetGradientOpacity(2, constant1) self.volumeProperty[3][i].SetGradientOpacity(3, constant1) self.volumeProperty[3][i].SetComponentWeight(0, 1) self.volumeProperty[3][i].SetComponentWeight(1, 1) self.volumeProperty[3][i].SetComponentWeight(2, 1) self.volumeProperty[3][i].SetComponentWeight(3, 1) self.volumeProperty[4][i].SetColor(rainbow) self.volumeProperty[4][i].SetScalarOpacity(rampPoint2) self.volumeProperty[4][i].SetGradientOpacity(constant1) self.volumeProperty[i][2].ShadeOn() self.volumeProperty[i][4].ShadeOn(0) self.volumeProperty[i][4].ShadeOn(1) self.volumeProperty[i][4].ShadeOn(2) self.volumeProperty[i][4].ShadeOn(3) self.volumeProperty[0][0].SetScalarOpacity(ramp1) self.volumeProperty[1][0].SetScalarOpacity(ramp1) self.volumeProperty[2][0].SetScalarOpacity(ramp1) self.volumeProperty[3][0].SetScalarOpacity(0, surface) self.volumeProperty[3][0].SetScalarOpacity(1, surface) self.volumeProperty[3][0].SetScalarOpacity(2, surface) self.volumeProperty[3][0].SetScalarOpacity(3, surface) self.volumeProperty[4][0].SetScalarOpacity(ramp1) self.volumeProperty[0][2].SetScalarOpacity(surface) self.volumeProperty[1][2].SetScalarOpacity(surface) self.volumeProperty[2][2].SetScalarOpacity(surface) self.volumeProperty[3][2].SetScalarOpacity(0, surface) self.volumeProperty[3][2].SetScalarOpacity(1, surface) self.volumeProperty[3][2].SetScalarOpacity(2, surface) self.volumeProperty[3][2].SetScalarOpacity(3, surface) self.volumeProperty[4][2].SetScalarOpacity(surface) self.volumeProperty[0][4].SetScalarOpacity(surface) self.volumeProperty[1][4].SetScalarOpacity(surface) self.volumeProperty[2][4].SetScalarOpacity(surface) self.volumeProperty[3][4].SetScalarOpacity(0, surface) self.volumeProperty[3][4].SetScalarOpacity(1, surface) self.volumeProperty[3][4].SetScalarOpacity(2, surface) self.volumeProperty[3][4].SetScalarOpacity(3, surface) self.volumeProperty[4][4].SetScalarOpacity(surface) self.volumeProperty[0][3].SetGradientOpacity(gop) self.volumeProperty[1][3].SetGradientOpacity(gop) self.volumeProperty[2][3].SetGradientOpacity(gop) self.volumeProperty[3][3].SetGradientOpacity(0, gop) self.volumeProperty[3][3].SetGradientOpacity(2, gop) self.volumeProperty[4][3].SetGradientOpacity(gop) self.volumeProperty[3][3].SetScalarOpacity(0, ramp1) self.volumeProperty[3][3].SetScalarOpacity(2, ramp1) self.volumeProperty[0][4].SetGradientOpacity(gop) self.volumeProperty[1][4].SetGradientOpacity(gop) self.volumeProperty[2][4].SetGradientOpacity(gop) self.volumeProperty[3][4].SetGradientOpacity(0, gop) self.volumeProperty[3][4].SetGradientOpacity(2, gop) self.volumeProperty[4][4].SetGradientOpacity(gop) self.renWin.Render() self.ren.GetActiveCamera().Dolly(1.3) self.ren.GetActiveCamera().Azimuth(15) self.ren.GetActiveCamera().Elevation(5) self.ren.ResetCameraClippingRange()
def __init__(self, ren, renWin, iren): self.ren = ren self.renWin = renWin self.iren = iren # Create a gaussian gs = vtk.vtkImageGaussianSource() gs.SetWholeExtent(0, 30, 0, 30, 0, 30) gs.SetMaximum(255.0) gs.SetStandardDeviation(5) gs.SetCenter(15, 15, 15) # threshold to leave a gap that should show up for # gradient opacity t = vtk.vtkImageThreshold() t.SetInputConnection(gs.GetOutputPort()) t.ReplaceInOn() t.SetInValue(0) t.ThresholdBetween(150, 200) # Use a shift scale to convert to unsigned char ss = vtk.vtkImageShiftScale() ss.SetInputConnection(t.GetOutputPort()) ss.SetOutputScalarTypeToUnsignedChar() # grid will be used for two component dependent grid0 = vtk.vtkImageGridSource() grid0.SetDataScalarTypeToUnsignedChar() grid0.SetGridSpacing(10, 10, 10) grid0.SetLineValue(200) grid0.SetFillValue(10) grid0.SetDataExtent(0, 30, 0, 30, 0, 30) # use dilation to thicken the grid d = vtk.vtkImageContinuousDilate3D() d.SetInputConnection(grid0.GetOutputPort()) d.SetKernelSize(3, 3, 3) # Now make a two component dependent iac = vtk.vtkImageAppendComponents() iac.AddInputConnection(d.GetOutputPort()) iac.AddInputConnection(ss.GetOutputPort()) # Some more gaussians for the four component indepent case gs1 = vtk.vtkImageGaussianSource() gs1.SetWholeExtent(0, 30, 0, 30, 0, 30) gs1.SetMaximum(255.0) gs1.SetStandardDeviation(4) gs1.SetCenter(5, 5, 5) t1 = vtk.vtkImageThreshold() t1.SetInputConnection(gs1.GetOutputPort()) t1.ReplaceInOn() t1.SetInValue(0) t1.ThresholdBetween(150, 256) gs2 = vtk.vtkImageGaussianSource() gs2.SetWholeExtent(0, 30, 0, 30, 0, 30) gs2.SetMaximum(255.0) gs2.SetStandardDeviation(4) gs2.SetCenter(12, 12, 12) gs3 = vtk.vtkImageGaussianSource() gs3.SetWholeExtent(0, 30, 0, 30, 0, 30) gs3.SetMaximum(255.0) gs3.SetStandardDeviation(4) gs3.SetCenter(19, 19, 19) t3 = vtk.vtkImageThreshold() t3.SetInputConnection(gs3.GetOutputPort()) t3.ReplaceInOn() t3.SetInValue(0) t3.ThresholdBetween(150, 256) gs4 = vtk.vtkImageGaussianSource() gs4.SetWholeExtent(0, 30, 0, 30, 0, 30) gs4.SetMaximum(255.0) gs4.SetStandardDeviation(4) gs4.SetCenter(26, 26, 26) # we need a few append filters ... iac1 = vtk.vtkImageAppendComponents() iac1.AddInputConnection(t1.GetOutputPort()) iac1.AddInputConnection(gs2.GetOutputPort()) iac2 = vtk.vtkImageAppendComponents() iac2.AddInputConnection(iac1.GetOutputPort()) iac2.AddInputConnection(t3.GetOutputPort()) iac3 = vtk.vtkImageAppendComponents() iac3.AddInputConnection(iac2.GetOutputPort()) iac3.AddInputConnection(gs4.GetOutputPort()) # create the four component dependent - # use lines in x, y, z for colors gridR = vtk.vtkImageGridSource() gridR.SetDataScalarTypeToUnsignedChar() gridR.SetGridSpacing(10, 100, 100) gridR.SetLineValue(250) gridR.SetFillValue(100) gridR.SetDataExtent(0, 30, 0, 30, 0, 30) dR = vtk.vtkImageContinuousDilate3D() dR.SetInputConnection(gridR.GetOutputPort()) dR.SetKernelSize(2, 2, 2) gridG = vtk.vtkImageGridSource() gridG.SetDataScalarTypeToUnsignedChar() gridG.SetGridSpacing(100, 10, 100) gridG.SetLineValue(250) gridG.SetFillValue(100) gridG.SetDataExtent(0, 30, 0, 30, 0, 30) dG = vtk.vtkImageContinuousDilate3D() dG.SetInputConnection(gridG.GetOutputPort()) dG.SetKernelSize(2, 2, 2) gridB = vtk.vtkImageGridSource() gridB.SetDataScalarTypeToUnsignedChar() gridB.SetGridSpacing(100, 100, 10) gridB.SetLineValue(0) gridB.SetFillValue(250) gridB.SetDataExtent(0, 30, 0, 30, 0, 30) dB = vtk.vtkImageContinuousDilate3D() dB.SetInputConnection(gridB.GetOutputPort()) dB.SetKernelSize(2, 2, 2) # need some appending iacRG = vtk.vtkImageAppendComponents() iacRG.AddInputConnection(dR.GetOutputPort()) iacRG.AddInputConnection(dG.GetOutputPort()) iacRGB = vtk.vtkImageAppendComponents() iacRGB.AddInputConnection(iacRG.GetOutputPort()) iacRGB.AddInputConnection(dB.GetOutputPort()) iacRGBA = vtk.vtkImageAppendComponents() iacRGBA.AddInputConnection(iacRGB.GetOutputPort()) iacRGBA.AddInputConnection(ss.GetOutputPort()) # We need a bunch of opacity functions # this one is a simple ramp to .2 rampPoint2 = vtk.vtkPiecewiseFunction() rampPoint2.AddPoint(0, 0.0) rampPoint2.AddPoint(255, 0.2) # this one is a simple ramp to 1 ramp1 = vtk.vtkPiecewiseFunction() ramp1.AddPoint(0, 0.0) ramp1.AddPoint(255, 1.0) # this one shows a sharp surface surface = vtk.vtkPiecewiseFunction() surface.AddPoint(0, 0.0) surface.AddPoint(10, 0.0) surface.AddPoint(50, 1.0) surface.AddPoint(255, 1.0) # this one is constant 1 constant1 = vtk.vtkPiecewiseFunction() constant1.AddPoint(0, 1.0) constant1.AddPoint(255, 1.0) # this one is used for gradient opacity gop = vtk.vtkPiecewiseFunction() gop.AddPoint(0, 0.0) gop.AddPoint(20, 0.0) gop.AddPoint(60, 1.0) gop.AddPoint(255, 1.0) # We need a bunch of color functions # This one is a simple rainbow rainbow = vtk.vtkColorTransferFunction() rainbow.SetColorSpaceToHSV() rainbow.HSVWrapOff() rainbow.AddHSVPoint(0, 0.1, 1.0, 1.0) rainbow.AddHSVPoint(255, 0.9, 1.0, 1.0) # this is constant red red = vtk.vtkColorTransferFunction() red.AddRGBPoint(0, 1, 0, 0) red.AddRGBPoint(255, 1, 0, 0) # this is constant green green = vtk.vtkColorTransferFunction() green.AddRGBPoint(0, 0, 1, 0) green.AddRGBPoint(255, 0, 1, 0) # this is constant blue blue = vtk.vtkColorTransferFunction() blue.AddRGBPoint(0, 0, 0, 1) blue.AddRGBPoint(255, 0, 0, 1) # this is constant yellow yellow = vtk.vtkColorTransferFunction() yellow.AddRGBPoint(0, 1, 1, 0) yellow.AddRGBPoint(255, 1, 1, 0) #ren = vtk.vtkRenderer() #renWin = vtk.vtkRenderWindow() self.renWin.AddRenderer(self.ren) self.renWin.SetSize(500, 500) #iren = vtk.vtkRenderWindowInteractor() self.iren.SetRenderWindow(self.renWin) self.ren.GetCullers().InitTraversal() culler = self.ren.GetCullers().GetNextItem() culler.SetSortingStyleToBackToFront() # We need 25 mapper / actor pairs which we will render # in a grid. Going down we will vary the input data # with the top row unsigned char, then float, then # two dependent components, then four dependent components # then four independent components. Going across we # will vary the rendering method with MIP, Composite, # Composite Shade, Composite GO, and Composite GO Shade. # Create the 5 by 5 grids self.volumeProperty = [[0 for col in range(0, 5)] for row in range(0, 5)] self.volumeMapper = [[0 for col in range(0, 5)] for row in range(0, 5)] volume = [[0 for col in range(0, 5)] for row in range(0, 5)] for i in range(0, 5): for j in range(0, 5): self.volumeProperty[i][j] = vtk.vtkVolumeProperty() self.volumeMapper[i][j] = vtk.vtkFixedPointVolumeRayCastMapper( ) self.volumeMapper[i][j].SetSampleDistance(0.25) self.volumeMapper[i][j].SetNumberOfThreads(1) volume[i][j] = vtk.vtkVolume() volume[i][j].SetMapper(self.volumeMapper[i][j]) volume[i][j].SetProperty(self.volumeProperty[i][j]) volume[i][j].AddPosition(i * 30, j * 30, 0) self.ren.AddVolume(volume[i][j]) for i in range(0, 5): self.volumeMapper[0][i].SetInputConnection(t.GetOutputPort()) self.volumeMapper[1][i].SetInputConnection(ss.GetOutputPort()) self.volumeMapper[2][i].SetInputConnection(iac.GetOutputPort()) self.volumeMapper[3][i].SetInputConnection(iac3.GetOutputPort()) self.volumeMapper[4][i].SetInputConnection(iacRGBA.GetOutputPort()) self.volumeMapper[i][0].SetBlendModeToMaximumIntensity() self.volumeMapper[i][1].SetBlendModeToComposite() self.volumeMapper[i][2].SetBlendModeToComposite() self.volumeMapper[i][3].SetBlendModeToComposite() self.volumeMapper[i][4].SetBlendModeToComposite() self.volumeProperty[0][i].IndependentComponentsOn() self.volumeProperty[1][i].IndependentComponentsOn() self.volumeProperty[2][i].IndependentComponentsOff() self.volumeProperty[3][i].IndependentComponentsOn() self.volumeProperty[4][i].IndependentComponentsOff() self.volumeProperty[0][i].SetColor(rainbow) self.volumeProperty[0][i].SetScalarOpacity(rampPoint2) self.volumeProperty[0][i].SetGradientOpacity(constant1) self.volumeProperty[1][i].SetColor(rainbow) self.volumeProperty[1][i].SetScalarOpacity(rampPoint2) self.volumeProperty[1][i].SetGradientOpacity(constant1) self.volumeProperty[2][i].SetColor(rainbow) self.volumeProperty[2][i].SetScalarOpacity(rampPoint2) self.volumeProperty[2][i].SetGradientOpacity(constant1) self.volumeProperty[3][i].SetColor(0, red) self.volumeProperty[3][i].SetColor(1, green) self.volumeProperty[3][i].SetColor(2, blue) self.volumeProperty[3][i].SetColor(3, yellow) self.volumeProperty[3][i].SetScalarOpacity(0, rampPoint2) self.volumeProperty[3][i].SetScalarOpacity(1, rampPoint2) self.volumeProperty[3][i].SetScalarOpacity(2, rampPoint2) self.volumeProperty[3][i].SetScalarOpacity(3, rampPoint2) self.volumeProperty[3][i].SetGradientOpacity(0, constant1) self.volumeProperty[3][i].SetGradientOpacity(1, constant1) self.volumeProperty[3][i].SetGradientOpacity(2, constant1) self.volumeProperty[3][i].SetGradientOpacity(3, constant1) self.volumeProperty[3][i].SetComponentWeight(0, 1) self.volumeProperty[3][i].SetComponentWeight(1, 1) self.volumeProperty[3][i].SetComponentWeight(2, 1) self.volumeProperty[3][i].SetComponentWeight(3, 1) self.volumeProperty[4][i].SetColor(rainbow) self.volumeProperty[4][i].SetScalarOpacity(rampPoint2) self.volumeProperty[4][i].SetGradientOpacity(constant1) self.volumeProperty[i][2].ShadeOn() self.volumeProperty[i][4].ShadeOn(0) self.volumeProperty[i][4].ShadeOn(1) self.volumeProperty[i][4].ShadeOn(2) self.volumeProperty[i][4].ShadeOn(3) self.volumeProperty[0][0].SetScalarOpacity(ramp1) self.volumeProperty[1][0].SetScalarOpacity(ramp1) self.volumeProperty[2][0].SetScalarOpacity(ramp1) self.volumeProperty[3][0].SetScalarOpacity(0, surface) self.volumeProperty[3][0].SetScalarOpacity(1, surface) self.volumeProperty[3][0].SetScalarOpacity(2, surface) self.volumeProperty[3][0].SetScalarOpacity(3, surface) self.volumeProperty[4][0].SetScalarOpacity(ramp1) self.volumeProperty[0][2].SetScalarOpacity(surface) self.volumeProperty[1][2].SetScalarOpacity(surface) self.volumeProperty[2][2].SetScalarOpacity(surface) self.volumeProperty[3][2].SetScalarOpacity(0, surface) self.volumeProperty[3][2].SetScalarOpacity(1, surface) self.volumeProperty[3][2].SetScalarOpacity(2, surface) self.volumeProperty[3][2].SetScalarOpacity(3, surface) self.volumeProperty[4][2].SetScalarOpacity(surface) self.volumeProperty[0][4].SetScalarOpacity(surface) self.volumeProperty[1][4].SetScalarOpacity(surface) self.volumeProperty[2][4].SetScalarOpacity(surface) self.volumeProperty[3][4].SetScalarOpacity(0, surface) self.volumeProperty[3][4].SetScalarOpacity(1, surface) self.volumeProperty[3][4].SetScalarOpacity(2, surface) self.volumeProperty[3][4].SetScalarOpacity(3, surface) self.volumeProperty[4][4].SetScalarOpacity(surface) self.volumeProperty[0][3].SetGradientOpacity(gop) self.volumeProperty[1][3].SetGradientOpacity(gop) self.volumeProperty[2][3].SetGradientOpacity(gop) self.volumeProperty[3][3].SetGradientOpacity(0, gop) self.volumeProperty[3][3].SetGradientOpacity(2, gop) self.volumeProperty[4][3].SetGradientOpacity(gop) self.volumeProperty[3][3].SetScalarOpacity(0, ramp1) self.volumeProperty[3][3].SetScalarOpacity(2, ramp1) self.volumeProperty[0][4].SetGradientOpacity(gop) self.volumeProperty[1][4].SetGradientOpacity(gop) self.volumeProperty[2][4].SetGradientOpacity(gop) self.volumeProperty[3][4].SetGradientOpacity(0, gop) self.volumeProperty[3][4].SetGradientOpacity(2, gop) self.volumeProperty[4][4].SetGradientOpacity(gop) self.renWin.Render() self.ren.GetActiveCamera().Dolly(1.3) self.ren.GetActiveCamera().Azimuth(15) self.ren.GetActiveCamera().Elevation(5) self.ren.ResetCameraClippingRange()
#!/usr/bin/env python import vtk from vtk.util.misc import vtkGetDataRoot VTK_DATA_ROOT = vtkGetDataRoot() # Image pipeline reader = vtk.vtkImageReader() reader.SetDataByteOrderToLittleEndian() reader.SetDataExtent(0, 63, 0, 63, 1, 93) reader.SetFilePrefix("" + str(VTK_DATA_ROOT) + "/Data/headsq/quarter") reader.SetDataMask(0x7fff) dilate = vtk.vtkImageContinuousDilate3D() dilate.SetInputConnection(reader.GetOutputPort()) dilate.SetKernelSize(11, 11, 1) erode = vtk.vtkImageContinuousErode3D() erode.SetInputConnection(dilate.GetOutputPort()) erode.SetKernelSize(11, 11, 1) viewer = vtk.vtkImageViewer() viewer.SetInputConnection(erode.GetOutputPort()) viewer.SetColorWindow(2000) viewer.SetColorLevel(1000) viewer.Render() # --- end of script --