def volume_renderer(img): #Create the renderers, render window, and interactor renWin=vtk.vtkRenderWindow() iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) ren=vtk.vtkRenderer() ren.SetBackground(0.0, 0.0, 0.0) renWin.AddRenderer(ren) #Create a transfer function mapping scalar value to opacity oTFun=vtk.vtkPiecewiseFunction() # oTFun.AddSegment(0,1.0,256,0.1) oTFun.AddPoint(0, 0.0) oTFun.AddPoint(low, 0.1) oTFun.AddPoint(high, 0.8) oTFun.AddPoint(100, 0.05) cTFun=vtk.vtkColorTransferFunction() cTFun.AddRGBPoint(0, 0.0, 0.0, 0.0) # background cTFun.AddRGBPoint(low, 0.7, 0.0, 0.3) cTFun.AddRGBPoint(high, 0.0, 0.0, 0.5) cTFun.AddRGBPoint(200, 1.0, 0.8, 0.5) gradient = vtk.vtkPiecewiseFunction() gradient.AddPoint(0, 0.0) gradient.AddPoint(high, 0.5) gradient.AddPoint(150, 1.0) property =vtk.vtkVolumeProperty() property.SetScalarOpacity(oTFun) property.SetColor(cTFun) property.SetInterpolationTypeToLinear() mapper =vtk.vtkFixedPointVolumeRayCastMapper() #mapper.SetBlendModeToMinimumIntensity() mapper.SetInputData(img ) volume =vtk.vtkVolume() volume.SetMapper(mapper) volume.SetProperty(property) volume.Update() ren.AddViewProp(volume) iren.Initialize() iren.Start()
def vtkKWVolumePropertyWidgetEntryPoint(parent, win): app = parent.GetApplication() # ----------------------------------------------------------------------- # This is a faily big widget, so create a scrolled frame vpw_frame = vtkKWFrameWithScrollbar() vpw_frame.SetParent(parent) vpw_frame.Create() app.Script("pack %s -side top -fill both -expand y", vpw_frame.GetWidgetName()) # ----------------------------------------------------------------------- # Create a volume property widget vpw = vtkKWVolumePropertyWidget() vpw.SetParent(vpw_frame.GetFrame()) vpw.Create() app.Script("pack %s -side top -anchor nw -expand y -padx 2 -pady 2", vpw.GetWidgetName()) # Create a volume property and assign it # We need color tfuncs, opacity, and gradient vpw_vp = vtkVolumeProperty() vpw_vp.SetIndependentComponents(1) vpw_cfun = vtkColorTransferFunction() vpw_cfun.SetColorSpaceToHSV() vpw_cfun.AddHSVSegment(0.0, 0.2, 1.0, 1.0, 255.0, 0.8, 1.0, 1.0) vpw_cfun.AddHSVSegment(80, 0.8, 1.0, 1.0, 130.0, 0.1, 1.0, 1.0) vpw_ofun = vtkPiecewiseFunction() vpw_ofun.AddSegment(0.0, 0.2, 255.0, 0.8) vpw_ofun.AddSegment(40, 0.9, 120.0, 0.1) vpw_gfun = vtkPiecewiseFunction() vpw_gfun.AddSegment(0.0, 0.2, 60.0, 0.4) vpw_vp.SetColor(0, vpw_cfun) vpw_vp.SetScalarOpacity(0, vpw_ofun) vpw_vp.SetGradientOpacity(0, vpw_gfun) vpw.SetVolumeProperty(vpw_vp) vpw.SetWindowLevel(128, 128) #vpw.MergeScalarOpacityAndColorEditors() return "TypeVTK"
def save_vtk_image(images, dst, i): image_import = vtk.vtkImageImport() image_import.CopyImportVoidPointer(images.tostring(), len(images.tostring())) image_import.SetDataScalarTypeToUnsignedChar() image_import.SetNumberOfScalarComponents(1) image_import.SetDataExtent(0, images.shape[2] - 1, 0, images.shape[1] - 1, 0, images.shape[0] - 1) image_import.SetWholeExtent(0, images.shape[2] - 1, 0, images.shape[1] - 1, 0, images.shape[0] - 1) volume = vtk.vtkVolume() volume_mapper = vtk.vtkVolumeRayCastMapper() alpha_channel_func = vtk.vtkPiecewiseFunction() alpha_channel_func.AddPoint(0, 0.0) # alpha_channel_func.AddPoint(64, 0.3) # alpha_channel_func.AddPoint(128, 0.5) alpha_channel_func.AddPoint(100, 1.0) alpha_channel_func.ClampingOn() color_func = vtk.vtkPiecewiseFunction() color_func.AddPoint(5, 0.3) color_func.AddPoint(25, 0.5) color_func.AddPoint(125, 0.7) color_func.AddPoint(255, 1.0) volume_property = vtk.vtkVolumeProperty() volume_property.SetColor(color_func) volume_property.SetInterpolationTypeToLinear() volume_property.SetScalarOpacity(alpha_channel_func) volume.SetProperty(volume_property) volume_ray_cast_func = vtk.vtkVolumeRayCastMIPFunction() volume_mapper.SetInputConnection(image_import.GetOutputPort()) volume_mapper.SetVolumeRayCastFunction(volume_ray_cast_func) # volume_mapper.SetSampleDistance(1) # volume_mapper.SetAutoAdjustSampleDistances(0) # volume_mapper.SetImageSampleDistance(1) volume.SetMapper(volume_mapper) ren = vtk.vtkRenderer() ren.AddVolume(volume) ren.SetBackground(0, 0, 0) renWin = vtk.vtkRenderWindow() renWin.SetSize(1024, 1024) renWin.AddRenderer(ren) renWin.Render() window_2_image = vtk.vtkWindowToImageFilter() window_2_image.SetInput(renWin) window_2_image.Update() png_writer = vtk.vtkPNGWriter() png_writer.SetFileName(dst + '%05d' % (i) + '.png') png_writer.SetInput(window_2_image.GetOutput()) png_writer.Write()
def vtkKWVolumePropertyWidgetEntryPoint(parent, win): app = parent.GetApplication() # ----------------------------------------------------------------------- # This is a faily big widget, so create a scrolled frame vpw_frame = vtkKWFrameWithScrollbar() vpw_frame.SetParent(parent) vpw_frame.Create() app.Script("pack %s -side top -fill both -expand y", vpw_frame.GetWidgetName()) # ----------------------------------------------------------------------- # Create a volume property widget vpw = vtkKWVolumePropertyWidget() vpw.SetParent(vpw_frame.GetFrame()) vpw.Create() app.Script("pack %s -side top -anchor nw -expand y -padx 2 -pady 2", vpw.GetWidgetName()) # Create a volume property and assign it # We need color tfuncs, opacity, and gradient vpw_vp = vtkVolumeProperty() vpw_vp.SetIndependentComponents(1) vpw_cfun = vtkColorTransferFunction() vpw_cfun.SetColorSpaceToHSV() vpw_cfun.AddHSVSegment(0.0, 0.2, 1.0, 1.0, 255.0, 0.8, 1.0, 1.0) vpw_cfun.AddHSVSegment(80, 0.8, 1.0, 1.0, 130.0, 0.1, 1.0, 1.0) vpw_ofun = vtkPiecewiseFunction() vpw_ofun.AddSegment(0.0, 0.2, 255.0, 0.8) vpw_ofun.AddSegment(40, 0.9, 120.0, 0.1) vpw_gfun = vtkPiecewiseFunction() vpw_gfun.AddSegment(0.0, 0.2, 60.0, 0.4) vpw_vp.SetColor(0, vpw_cfun) vpw_vp.SetScalarOpacity(0, vpw_ofun) vpw_vp.SetGradientOpacity(0, vpw_gfun) vpw.SetVolumeProperty(vpw_vp) vpw.SetWindowLevel(128, 128) # vpw.MergeScalarOpacityAndColorEditors() return "TypeVTK"
def volumeRendering(image, image2, lowerThreshold, upperThreshold): color = vtk.vtkColorTransferFunction() color.AddRGBPoint(0, 0.0, 0.0, 0.0) # background color.AddRGBPoint(lowerThreshold, 1.0, 0.0, 0.0) color.AddRGBPoint(upperThreshold, 0.0, 1.0, 0.0) opacity = vtk.vtkPiecewiseFunction() opacity.AddPoint(0, 0.0) opacity.AddPoint(lowerThreshold, 0.15) opacity.AddPoint(upperThreshold, 0.85) gradient = vtk.vtkPiecewiseFunction() gradient.AddPoint(0, 0.0) gradient.AddPoint(90, 0.5) gradient.AddPoint(100, 1.0) volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(color) volumeProperty.SetScalarOpacity(opacity) volumeProperty.SetGradientOpacity(gradient) volumeProperty.SetInterpolationTypeToLinear() volumeProperty.ShadeOn() volumeProperty.SetAmbient(0.2) volumeProperty.SetDiffuse(0.7) volumeProperty.SetSpecular(0.3) volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper() volumeMapper.SetInputData(image) volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) volume.Update() ren = vtk.vtkRenderer() ren.AddViewProp(volume) ren.SetBackground(0, 0, 0) renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) renWin.SetSize(400, 400) # create a renderwindowinteractor iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) iren.Initialize() renWin.Render() iren.Start()
def save_vtk_image(images, dst, i): image_import = vtk.vtkImageImport() image_import.CopyImportVoidPointer(images.tostring(), len(images.tostring())) image_import.SetDataScalarTypeToUnsignedChar() image_import.SetNumberOfScalarComponents(1) image_import.SetDataExtent(0, images.shape[2] - 1, 0, images.shape[1] - 1, 0, images.shape[0] - 1) image_import.SetWholeExtent(0, images.shape[2] - 1, 0, images.shape[1] - 1, 0, images.shape[0] - 1) volume = vtk.vtkVolume() volume_mapper = vtk.vtkVolumeRayCastMapper() alpha_channel_func = vtk.vtkPiecewiseFunction() alpha_channel_func.AddPoint(0, 0.0) # alpha_channel_func.AddPoint(64, 0.3) # alpha_channel_func.AddPoint(128, 0.5) alpha_channel_func.AddPoint(100, 1.0) alpha_channel_func.ClampingOn() color_func = vtk.vtkPiecewiseFunction() color_func.AddPoint(5, 0.3) color_func.AddPoint(25, 0.5) color_func.AddPoint(125, 0.7) color_func.AddPoint(255, 1.0) volume_property = vtk.vtkVolumeProperty() volume_property.SetColor(color_func) volume_property.SetInterpolationTypeToLinear() volume_property.SetScalarOpacity(alpha_channel_func) volume.SetProperty(volume_property) volume_ray_cast_func = vtk.vtkVolumeRayCastMIPFunction() volume_mapper.SetInputConnection(image_import.GetOutputPort()) volume_mapper.SetVolumeRayCastFunction(volume_ray_cast_func) # volume_mapper.SetSampleDistance(1) # volume_mapper.SetAutoAdjustSampleDistances(0) # volume_mapper.SetImageSampleDistance(1) volume.SetMapper(volume_mapper) ren = vtk.vtkRenderer() ren.AddVolume(volume) ren.SetBackground(0, 0, 0) renWin = vtk.vtkRenderWindow() renWin.SetSize(1024, 1024) renWin.AddRenderer(ren) renWin.Render() window_2_image = vtk.vtkWindowToImageFilter() window_2_image.SetInput(renWin) window_2_image.Update() png_writer = vtk.vtkPNGWriter() png_writer.SetFileName(dst + '%05d'%(i) + '.png') png_writer.SetInput(window_2_image.GetOutput()) png_writer.Write()
def volume_render(self, scalar_range): i1 = scalar_range[0] i2 = scalar_range[1] i3 = scalar_range[2] volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper() volumeMapper.SetInputData(self.volume) # The color transfer function maps voxel intensities to colors. # It is modality-specific, and often anatomy-specific as well. # The goal is to one color for flesh (between 500 and 1000) # and another color for bone (1150 and over). volumeColor = vtk.vtkColorTransferFunction() volumeColor.AddRGBPoint(0, 0.0, 0.0, 0.0) volumeColor.AddRGBPoint(i1, 1.0, 0.5, 0.3) volumeColor.AddRGBPoint(i2, 1.0, 0.5, 0.3) volumeColor.AddRGBPoint(i3, 1.0, 1.0, 0.9) # The opacity transfer function is used to control the opacity # of different tissue types. volumeScalarOpacity = vtk.vtkPiecewiseFunction() volumeScalarOpacity.AddPoint(0, 0.00) volumeScalarOpacity.AddPoint(i1, 0.15) volumeScalarOpacity.AddPoint(i2, 0.15) volumeScalarOpacity.AddPoint(i3, 0.85) # The gradient opacity function is used to decrease the opacity # in the "flat" regions of the volume while maintaining the opacity # at the boundaries between tissue types. The gradient is measured # as the amount by which the intensity changes over unit distance. # For most medical data, the unit distance is 1mm. volumeGradientOpacity = vtk.vtkPiecewiseFunction() volumeGradientOpacity.AddPoint(0, 0.0) volumeGradientOpacity.AddPoint(90, 0.5) volumeGradientOpacity.AddPoint(100, 1.0) volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(volumeColor) volumeProperty.SetScalarOpacity(volumeScalarOpacity) volumeProperty.SetGradientOpacity(volumeGradientOpacity) volumeProperty.SetInterpolationTypeToLinear() volumeProperty.ShadeOn() volumeProperty.SetAmbient(0.4) volumeProperty.SetDiffuse(0.6) volumeProperty.SetSpecular(0.2) volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) self.graphics.renderer.AddViewProp(volume)
def createVolumeRender(File, ScalarList, ColorList, OpacList, PieceList): #Create the mappers volumeMapper = vtk.vtkGPUVolumeRayCastMapper() volumeMapper.SetInputConnection(File.GetOutputPort()) volumeMapper.SetBlendModeToComposite() #Create the color transfer function volumeCTF = vtk.vtkColorTransferFunction() volumeCTF.AddRGBPoint(ScalarList[0], ColorList[0][0], ColorList[0][1], ColorList[0][2]) volumeCTF.AddRGBPoint(ScalarList[1], ColorList[1][0], ColorList[1][1], ColorList[1][2]) volumeCTF.AddRGBPoint(ScalarList[2], ColorList[2][0], ColorList[2][1], ColorList[2][2]) volumeCTF.AddRGBPoint(ScalarList[3], ColorList[3][0], ColorList[3][1], ColorList[3][2]) #Create the piecewise function for opacity function volumeOTF = vtk.vtkPiecewiseFunction() volumeOTF.AddPoint(ScalarList[0], OpacList[0]) volumeOTF.AddPoint(ScalarList[1], OpacList[1]) volumeOTF.AddPoint(ScalarList[2], OpacList[2]) volumeOTF.AddPoint(ScalarList[3], OpacList[3]) #Create the piecewise function for internal opacity function volumeGOTF = vtk.vtkPiecewiseFunction() volumeGOTF.AddPoint(0, PieceList[0]) volumeGOTF.AddPoint(90, PieceList[1]) volumeGOTF.AddPoint(100, PieceList[2]) #Add all the function and ambient, diffuse and specular to the volume property volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(volumeCTF) volumeProperty.SetScalarOpacity(volumeOTF) volumeProperty.SetGradientOpacity(volumeGOTF) volumeProperty.SetInterpolationTypeToLinear() volumeProperty.ShadeOn() volumeProperty.SetAmbient(0.5) volumeProperty.SetDiffuse(0.5) volumeProperty.SetSpecular(0.5) #Create volume and set mapper and property to volume volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) #Return volume return volume
def __init__(self, module_manager): ModuleBase.__init__(self, module_manager) self._volume_input = None self._opacity_tf = vtk.vtkPiecewiseFunction() self._colour_tf = vtk.vtkColorTransferFunction() self._lut = vtk.vtkLookupTable() # list of tuples, where each tuple (scalar_value, (r,g,b,a)) self._config.transfer_function = [(0, (0, 0, 0), 0), (255, (255, 255, 255), 1)] self._view_frame = None self._create_view_frame() self._bind_events() self.view() # all modules should toggle this once they have shown their # stuff. self.view_initialised = True self.config_to_logic() self.logic_to_config() self.config_to_view()
def get_transform_function(mid_point, delta, central_value, margin_value): transform_function = vtk.vtkPiecewiseFunction() transform_function.AddPoint(mid_point - delta, margin_value) transform_function.AddPoint(mid_point, central_value) transform_function.AddPoint(mid_point + delta, margin_value) transform_function.AddPoint(0, 0) return transform_function
def volumeRender(reader,ren,renWin): #Create transfer mapping scalar value to opacity opacityTransferFunction = vtk.vtkPiecewiseFunction() opacityTransferFunction.AddPoint(1, 0.0) opacityTransferFunction.AddPoint(100, 0.1) opacityTransferFunction.AddPoint(255,1.0) colorTransferFunction = vtk.vtkColorTransferFunction() colorTransferFunction.AddRGBPoint(0.0,0.0,0.0,0.0) colorTransferFunction.AddRGBPoint(64.0,1.0,0.0,0.0) colorTransferFunction.AddRGBPoint(128.0,0.0,0.0,1.0) colorTransferFunction.AddRGBPoint(192.0,0.0,1.0,0.0) colorTransferFunction.AddRGBPoint(255.0,0.0,0.2,0.0) volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(colorTransferFunction) volumeProperty.SetScalarOpacity(opacityTransferFunction) volumeProperty.ShadeOn() volumeProperty.SetInterpolationTypeToLinear() compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper() volumeMapper.SetInputConnection(reader.GetOutputPort()) volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) ren.RemoveAllViewProps() ren.AddVolume(volume) ren.SetBackground(1,1,1) renWin.Render()
def generateLUT(data): """Generates a random color lookup table Params: data -- List | set | array which contains a unique list of values Returns: returns a dictionary containing the value and associated color """ alphaFunc = vtk.vtkPiecewiseFunction() colorFunc = vtk.vtkColorTransferFunction() lut = dict() for k in data: if k < 50: lut[k] = [0.0, 0.0, 0.0] else: lut[k] = [rd.random(), rd.random(), rd.random()] # histo(lut.keys()) for k, v in lut.items(): colorFunc.AddRGBPoint(k, v[0], v[1], v[2]) if k < 50: alphaFunc.AddPoint(k, 0.0) else: alphaFunc.AddPoint(k, rd.random()) return alphaFunc, colorFunc
def __init__(self, module_manager): ModuleBase.__init__(self, module_manager) self._volume_input = None self._opacity_tf = vtk.vtkPiecewiseFunction() self._colour_tf = vtk.vtkColorTransferFunction() self._lut = vtk.vtkLookupTable() # list of tuples, where each tuple (scalar_value, (r,g,b,a)) self._config.transfer_function = [ (0, (0,0,0), 0), (255, (255,255,255), 1) ] self._view_frame = None self._create_view_frame() self._bind_events() self.view() # all modules should toggle this once they have shown their # stuff. self.view_initialised = True self.config_to_logic() self.logic_to_config() self.config_to_view()
def volumeRender(img, tf=[],spacing=[1.0,1.0,1.0]): importer = numpy2VTK(img,spacing) # Transfer Functions opacity_tf = vtk.vtkPiecewiseFunction() color_tf = vtk.vtkColorTransferFunction() if len(tf) == 0: tf.append([img.min(),0,0,0,0]) tf.append([img.max(),1,1,1,1]) for p in tf: color_tf.AddRGBPoint(p[0], p[1], p[2], p[3]) opacity_tf.AddPoint(p[0], p[4]) volMapper = vtk.vtkGPUVolumeRayCastMapper() volMapper.SetInputConnection(importer.GetOutputPort()) # The property describes how the data will look volProperty = vtk.vtkVolumeProperty() volProperty.SetColor(color_tf) volProperty.SetScalarOpacity(opacity_tf) volProperty.ShadeOn() volProperty.SetInterpolationTypeToLinear() vol = vtk.vtkVolume() vol.SetMapper(volMapper) vol.SetProperty(volProperty) return [vol]
def alpha_fn(deactivated): fn = vtk.vtkPiecewiseFunction() fn.AddPoint(0, 0) for i in range(1, 16): fn.AddPoint(i, 0.0 if i - 1 in deactivated else 0.3) fn.AddPoint(16, 0) return fn
def _createPipeline(self): # setup our pipeline self._splatMapper = vtkdevide.vtkOpenGLVolumeShellSplatMapper() self._splatMapper.SetOmegaL(0.9) self._splatMapper.SetOmegaH(0.9) # high-quality rendermode self._splatMapper.SetRenderMode(0) self._otf = vtk.vtkPiecewiseFunction() self._otf.AddPoint(0.0, 0.0) self._otf.AddPoint(0.9, 0.0) self._otf.AddPoint(1.0, 1.0) self._ctf = vtk.vtkColorTransferFunction() self._ctf.AddRGBPoint(0.0, 0.0, 0.0, 0.0) self._ctf.AddRGBPoint(0.9, 0.0, 0.0, 0.0) self._ctf.AddRGBPoint(1.0, 1.0, 0.937, 0.859) self._volumeProperty = vtk.vtkVolumeProperty() self._volumeProperty.SetScalarOpacity(self._otf) self._volumeProperty.SetColor(self._ctf) self._volumeProperty.ShadeOn() self._volumeProperty.SetAmbient(0.1) self._volumeProperty.SetDiffuse(0.7) self._volumeProperty.SetSpecular(0.2) self._volumeProperty.SetSpecularPower(10) self._volume = vtk.vtkVolume() self._volume.SetProperty(self._volumeProperty) self._volume.SetMapper(self._splatMapper)
def create_volume_rendering(self): opacityfunction=vtk.vtkPiecewiseFunction() opacityfunction.AddPoint(0,0.0) opacityfunction.AddPoint(0.1,0.01) opacityfunction.AddPoint(1,0.02) opacityfunction.AddPoint(1.5,0.03) volproperty=vtk.vtkVolumeProperty() volproperty.SetColor(self.arrowColor) volproperty.SetScalarOpacity(opacityfunction) volproperty.ShadeOn() volproperty.SetInterpolationTypeToLinear() volumeMapper = vtk.vtkGPUVolumeRayCastMapper() volumeMapper.SetInputConnection(self.vol_reader.GetOutputPort()) volumeMapper.SetSampleDistance(0.01) volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volproperty) return volume
def __init__(self, source): # 3 set up the volume mapper volume_mapper = vtk.vtkGPUVolumeRayCastMapper() volume_mapper.SetInputConnection(source.GetOutputPort()) # 4 transfer functions for color and opacity self.color_transfer = vtk.vtkColorTransferFunction() self.alpha_transfer = vtk.vtkPiecewiseFunction() self.c_map_name = 'copper' self.c_map = cm.get_cmap(self.c_map_name) self.init_opacity_points() self.set_color_transfer() # Fill color transfer function # 6 set up the volume properties self.volume_properties = vtk.vtkVolumeProperty() self.volume_properties.SetColor(0, self.color_transfer) self.volume_properties.SetScalarOpacity(0, self.alpha_transfer) self.volume_properties.SetInterpolationTypeToLinear() self.volume_properties.ShadeOn() self.volume_properties.SetAmbient(1.0) self.volume_properties.SetDiffuse(0.7) self.volume_properties.SetSpecular(0.5) # 7 set up the actor self.actor = vtk.vtkVolume() self.actor.SetMapper(volume_mapper) self.actor.SetProperty(self.volume_properties)
def MakeVTKColorScheme(ZeroPoint, MaxScaledVal, poscolor=(0.95, 0.5, 0.14), negcolor=(0, 0.23, 0.48)): #Opacity transfer function opacitytf = vtk.vtkPiecewiseFunction() opacitytf.AddPoint(0, 1.0) opacitytf.AddPoint(ZeroPoint - 1, 0.0) opacitytf.AddPoint(ZeroPoint + 1, 0.0) opacitytf.AddPoint(MaxScaledVal, 1.0) #Color transfer function colortf = vtk.vtkColorTransferFunction() colortf.AddRGBPoint(0, *negcolor) colortf.AddRGBPoint(ZeroPoint - 1, *negcolor) colortf.AddRGBPoint(ZeroPoint + 1, *poscolor) colortf.AddRGBPoint(MaxScaledVal, *poscolor) volprp = vtk.vtkVolumeProperty() volprp.SetColor(colortf) volprp.SetScalarOpacity(opacitytf) volprp.ShadeOn() volprp.SetInterpolationTypeToLinear() return volprp
def createSkinOTF(tissues): skinOTF = vtk.vtkPiecewiseFunction() skinOTF.AddPoint(70, 0) skinOTF.AddPoint(80, tissues[15].opacity *.5) skinOTF.AddPoint(90, tissues[15].opacity *.5) skinOTF.AddPoint(100.0, 0) return skinOTF
def viz_VisualizeDICOM(self, path): volume = vtk.vtkVolume() dc_img_reader = vtk.vtkDICOMImageReader() dc_img_reader.SetDirectoryName(path) dc_img_reader.Update() dc_color_func = vtk.vtkColorTransferFunction() dc_color_func.AddRGBPoint(-3024, 0.0, 0.0, 0.0) dc_color_func.AddRGBPoint(-77, 0.55, 0.25, 0.15) dc_color_func.AddRGBPoint(94, 0.88, 0.60, 0.29) dc_color_func.AddRGBPoint(179, 1.0, 0.94, 0.95) dc_color_func.AddRGBPoint(260, 0.62, 0.0, 0.0) dc_color_func.AddRGBPoint(3071, 0.82, 0.66, 1.0) dc_alpha_func = vtk.vtkPiecewiseFunction() dc_alpha_func.AddPoint(-3024, 0.0) dc_alpha_func.AddPoint(-77, 0.0) dc_alpha_func.AddPoint(94, 0.29) dc_alpha_func.AddPoint(179, 0.55) dc_alpha_func.AddPoint(260, 0.84) dc_alpha_func.AddPoint(3071, 0.875) volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(dc_color_func) volumeProperty.SetScalarOpacity(dc_alpha_func) volumeProperty.ShadeOn() volumeProperty.SetInterpolationTypeToLinear() volumeMapper = vtk.vtkGPUVolumeRayCastMapper() volumeMapper.SetInputConnection(dc_img_reader.GetOutputPort()) volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) self.__ren.AddVolume(volume)
def updateTransferFunction(self): r, g, b = self.color # Transfer functions and properties if not self.colorFunction: self.colorFunction = vtkColorTransferFunction() else: self.colorFunction.RemoveAllPoints() self.colorFunction.AddRGBPoint(self.minimum, r*0.7, g*0.7, b*0.7) self.colorFunction.AddRGBPoint(self.maximum, r, g, b) if not self.opacityFunction: self.opacityFunction = vtkPiecewiseFunction() else: self.opacityFunction.RemoveAllPoints() self.opacityFunction.AddPoint(self.minimum, 0) self.opacityFunction.AddPoint(self.lowerBound, 0) self.opacityFunction.AddPoint(self.lowerBound+0.0001, self.opacity) self.opacityFunction.AddPoint(self.upperBound-0.0001, self.opacity) self.opacityFunction.AddPoint(self.upperBound, 0) self.opacityFunction.AddPoint(self.maximum+0.0001, 0) self.volProp.SetColor(self.colorFunction) self.volProp.SetScalarOpacity(self.opacityFunction) self.updatedTransferFunction.emit()
def updateTransferFunction(self): if not self.colorFunction or not self.opacityFunction: self.colorFunction = vtkColorTransferFunction() self.opacityFunction = vtkPiecewiseFunction() points = sorted(self.points, key=lambda x: x.value) nrOfPoints = self.colorFunction.GetSize() index = 0 for point in points: if index >= nrOfPoints: self.colorFunction.AddRGBPoint(point.value, point.color[0], point.color[1], point.color[2]) self.opacityFunction.AddPoint(point.value, pow(point.opacity, 5)) else: self.colorFunction.SetNodeValue(index, [point.value] + point.color + [0.5, 0.0]) self.opacityFunction.SetNodeValue( index, [point.value, pow(point.opacity, 5), 0.5, 0.0]) index += 1 if nrOfPoints > len(points): self.colorFunction.AdjustRange([points[0].value, points[-1].value]) assert self.colorFunction.GetSize() == len(points)
def __init__(self, img, color): self.volume = vtk.vtkVolume() self.__color = color dataImporter = vtk.vtkImageImport() simg = np.ascontiguousarray(img, np.uint8) dataImporter.CopyImportVoidPointer(simg.data, len(simg.data)) dataImporter.SetDataScalarTypeToUnsignedChar() dataImporter.SetNumberOfScalarComponents(1) dataImporter.SetDataExtent(0, simg.shape[2]-1, 0, simg.shape[1]-1, 0, simg.shape[0]-1) dataImporter.SetWholeExtent(0, simg.shape[2]-1, 0, simg.shape[1]-1, 0, simg.shape[0]-1) self.__smoother = vtk.vtkImageGaussianSmooth() self.__smoother.SetStandardDeviation(1, 1, 1) self.__smoother.SetInputConnection(dataImporter.GetOutputPort()) volumeMapper = vtk.vtkSmartVolumeMapper() volumeMapper.SetInputConnection(self.__smoother.GetOutputPort()) self.__volumeProperty = vtk.vtkVolumeProperty() self.__colorFunc = vtk.vtkColorTransferFunction() self.__alpha = vtk.vtkPiecewiseFunction() for i in range(256): self.__colorFunc.AddRGBPoint(i, i * color[0], i * color[1], i * color[2]) self.__alpha.AddPoint(5, .01) self.__alpha.AddPoint(10, .03) self.__alpha.AddPoint(50, .1) self.__alpha.AddPoint(150, .2) self.__volumeProperty.SetColor(self.__colorFunc) self.__volumeProperty.SetScalarOpacity(self.__alpha) self.volume.SetMapper(volumeMapper) self.volume.SetProperty(self.__volumeProperty)
def updateTransferFunction(self): r, g, b = self.color # Transfer functions and properties if not self.colorFunction: self.colorFunction = vtkColorTransferFunction() else: self.colorFunction.RemoveAllPoints() self.colorFunction.AddRGBPoint(self.minimum, r * 0.7, g * 0.7, b * 0.7) self.colorFunction.AddRGBPoint(self.maximum, r, g, b) if not self.opacityFunction: self.opacityFunction = vtkPiecewiseFunction() else: self.opacityFunction.RemoveAllPoints() self.opacityFunction.AddPoint(self.minimum, 0) self.opacityFunction.AddPoint(self.lowerBound, 0) self.opacityFunction.AddPoint(self.lowerBound + 0.0001, self.opacity) self.opacityFunction.AddPoint(self.upperBound - 0.0001, self.opacity) self.opacityFunction.AddPoint(self.upperBound, 0) self.opacityFunction.AddPoint(self.maximum + 0.0001, 0) self.volProp.SetColor(self.colorFunction) self.volProp.SetScalarOpacity(self.opacityFunction) self.updatedTransferFunction.emit()
def _create_pipeline(self): # setup our pipeline self._otf = vtk.vtkPiecewiseFunction() self._ctf = vtk.vtkColorTransferFunction() self._volume_property = vtk.vtkVolumeProperty() self._volume_property.SetScalarOpacity(self._otf) self._volume_property.SetColor(self._ctf) self._volume_property.ShadeOn() self._volume_property.SetAmbient(0.1) self._volume_property.SetDiffuse(0.7) self._volume_property.SetSpecular(0.2) self._volume_property.SetSpecularPower(10) self._volume_raycast_function = vtk.vtkVolumeRayCastMIPFunction() self._volume_mapper = vtk.vtkVolumeRayCastMapper() # can also used FixedPoint, but then we have to use: # SetBlendModeToMaximumIntensity() and not SetVolumeRayCastFunction #self._volume_mapper = vtk.vtkFixedPointVolumeRayCastMapper() self._volume_mapper.SetVolumeRayCastFunction( self._volume_raycast_function) module_utils.setup_vtk_object_progress(self, self._volume_mapper, 'Preparing render.') self._volume = vtk.vtkVolume() self._volume.SetProperty(self._volume_property) self._volume.SetMapper(self._volume_mapper)
def showVolumeRendering(volumeNode): #slicer.util.loadNodeFromFile("C:/Users/FUS/Desktop/Simulation/Colormap.vp", "TransferFunctionFile", returnNode=False) #preset = slicer.mrmlScene.GetNodeByID('vtkMRMLVolumePropertyNode1') volRenLogic = slicer.modules.volumerendering.logic() displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(volumeNode) propertyNode = displayNode.GetVolumePropertyNode() opacityTransfer = vtk.vtkPiecewiseFunction() opacityTransfer.AddPoint(0, 0) opacityTransfer.AddPoint(0.15, 0.03) opacityTransfer.AddPoint(0.3, 0.15) opacityTransfer.AddPoint(0.5, 0.3) opacityTransfer.AddPoint(0.8, 0.8) opacityTransfer.AddPoint(0.9, 0.9) ctf = vtk.vtkColorTransferFunction() ctf.AddRGBPoint(0.17, 0.1, 0.1, 1.0) ctf.AddRGBPoint(0.3, 0.2, 1.0, 0.2) ctf.AddRGBPoint(0.5, 1.0, 0.5, 0.0) ctf.AddRGBPoint(0.8, 1.0, 0.0, 0.0) propertyNode.SetColor(ctf) propertyNode.SetScalarOpacity(opacityTransfer) slicer.mrmlScene.AddNode(propertyNode) displayNode.UnRegister(volRenLogic)
def default_gradient_opacity(): '''Sharpens boundaries.''' opacityGradientFunction = vtk.vtkPiecewiseFunction() opacityGradientFunction.AddPoint(1, 0.0) opacityGradientFunction.AddPoint(5, 0.1) opacityGradientFunction.AddPoint(100, 1.0) return opacityGradientFunction
def volumeRender(self, img, tf=[], spacing=[1.0, 1.0, 1.0]): importer = dicomShow.numpy2VTK(img, spacing) # Transfer Functions opacity_tf = vtk.vtkPiecewiseFunction() color_tf = vtk.vtkColorTransferFunction() if len(tf) == 0: tf.append([img.min(), 0, 0, 0, 0]) tf.append([img.max(), 1, 1, 1, 1]) for p in tf: color_tf.AddRGBPoint(p[0], p[1], p[2], p[3]) opacity_tf.AddPoint(p[0], p[4]) # working on the GPU volMapper = vtk.vtkGPUVolumeRayCastMapper() volMapper.SetInputConnection(importer.GetOutputPort()) # The property describes how the data will look volProperty = vtk.vtkVolumeProperty() volProperty.SetColor(color_tf) volProperty.SetScalarOpacity(opacity_tf) volProperty.ShadeOn() volProperty.SetInterpolationTypeToLinear() vol = vtk.vtkVolume() vol.SetMapper(volMapper) vol.SetProperty(volProperty) return [vol]
def default_opacity_transfer_function(x1, x2): maxs = max(x1, x2) mins = min(x1, x2) opacityTransferFunction = vtk.vtkPiecewiseFunction() opacityTransferFunction.AddPoint(mins, 0.0) opacityTransferFunction.AddPoint(maxs, 0.2) return opacityTransferFunction
def alphaGradient(self, alphaGrad): """ Assign a set of tranparencies to a volume's gradient along the range of the scalar value. A single constant value can also be assigned. The gradient function is used to decrease the opacity in the "flat" regions of the volume while maintaining the opacity at the boundaries between material types. The gradient is measured as the amount by which the intensity changes over unit distance. |read_vti| |read_vti.py|_ """ self._alphaGrad = alphaGrad volumeProperty = self.GetProperty() if alphaGrad is None: volumeProperty.DisableGradientOpacityOn() return self else: volumeProperty.DisableGradientOpacityOff() #smin, smax = self._imagedata.GetScalarRange() smin, smax = 0, 255 gotf = vtk.vtkPiecewiseFunction() if utils.isSequence(alphaGrad): for i, al in enumerate(alphaGrad): xalpha = smin + (smax - smin) * i / (len(alphaGrad) - 1) # Create transfer mapping scalar value to gradient opacity gotf.AddPoint(xalpha, al) #colors.printc("alphaGrad at", round(xalpha, 1), "\tset to", al, c="b", bold=0) else: gotf.AddPoint(smin, alphaGrad) # constant alphaGrad gotf.AddPoint(smax, alphaGrad) volumeProperty.SetGradientOpacity(gotf) return self
def Create8bOpacityTable(self, scale): if self.opacity_transfer_func: opacity_transfer_func = self.opacity_transfer_func else: opacity_transfer_func = vtk.vtkPiecewiseFunction() opacity_transfer_func.RemoveAllPoints() opacities = [] ww = self.config['ww'] wl = self.TranslateScale(scale, self.config['wl']) l1 = wl - ww / 2.0 l2 = wl + ww / 2.0 self.ww = ww self.wl = self.config['wl'] opacity_transfer_func.RemoveAllPoints() opacity_transfer_func.AddSegment(0, 0, 2**16 - 1, 0) k1 = 0.0 k2 = 1.0 opacity_transfer_func.AddPoint(l1, 0) opacity_transfer_func.AddPoint(l2, 1) self.opacity_transfer_func = opacity_transfer_func return opacity_transfer_func
def originalObject(self): pieceWiseFunction = vtkPiecewiseFunction() for index in range(len(self.nodes)): value = self.nodes[index] pieceWiseFunction.AddPoint(value[0], value[1], value[2], value[3]) return pieceWiseFunction
def createLookupTableSlider(self, name): """ This function creates a lookup table based on the visualization parameters that can be dynamically changed using the sliders. """ opTransferFunction = vtk.vtkPiecewiseFunction() if (name == 'bl'): x_increment = self.ri_bl / 10.0 x_start = self.th_bl x_end = self.th_bl + self.ri_bl + x_increment x_plateau = self.th_bl + 2.0 *self.ri_bl for x in np.arange(x_start, x_end, x_increment): opTransferFunction.AddPoint(x, self.linearRampFunction('bl',x)) # Add plateau after ramp opTransferFunction.AddPoint(x_plateau, self.op_max_bl) if (name == 'rm'): x_increment = self.ri_rm / 10.0 x_start = self.th_rm x_end = self.th_rm + self.ri_rm + x_increment x_plateau = self.th_rm + 2.0 *self.ri_rm for x in np.arange(x_start, x_end, x_increment): opTransferFunction.AddPoint(x, self.linearRampFunction('rm',x)) # Add plateau after ramp opTransferFunction.AddPoint(x_plateau, self.op_max_rm) return opTransferFunction
def __init__(self, name, image_data): if not isinstance(image_data, vtk.vtkImageData): raise TypeError("input has to be vtkImageData") self.name = name # Create transfer mapping scalar value to opacity. opacity_function = vtk.vtkPiecewiseFunction() opacity_function.AddPoint(0, 0.0) opacity_function.AddPoint(127, 0.0) opacity_function.AddPoint(128, 0.2) opacity_function.AddPoint(255, 0.2) # Create transfer mapping scalar value to color. color_function = vtk.vtkColorTransferFunction() color_function.SetColorSpaceToHSV() color_function.AddHSVPoint(0, 0.0, 0.0, 0.0) color_function.AddHSVPoint(127, 0.0, 0.0, 0.0) color_function.AddHSVPoint(128, 0.0, 0.0, 1.0) color_function.AddHSVPoint(255, 0.0, 0.0, 1.0) volume_property = vtk.vtkVolumeProperty() volume_property.SetColor(color_function) volume_property.SetScalarOpacity(opacity_function) volume_property.ShadeOn() volume_property.SetInterpolationTypeToLinear() volume_mapper = vtk.vtkSmartVolumeMapper() volume_mapper.SetInputData(image_data) self.volume = vtk.vtkVolume() self.volume.SetMapper(volume_mapper) self.volume.SetProperty(volume_property)
def test(): reader = vtk.vtkXMLImageDataReader() reader.SetFileName("C:/Users/Asus/Dropbox/2017-18/ScientificCompAndVisualization/Semana2/SS_2017-master/SS_2017-master/data/wind_image.vti") volume = vtk.vtkVolume() #mapper = vtk.vtkFixedPointVolumeRayCastMapper() mapper = vtk.vtkGPUVolumeRayCastMapper() mapper.SetBlendModeToMinimumIntensity(); mapper.SetSampleDistance(0.1) mapper.SetAutoAdjustSampleDistances(0) ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() iRen = vtk.vtkRenderWindowInteractor() # Set connections mapper.SetInputConnection(reader.GetOutputPort()); volume.SetMapper(mapper) ren.AddViewProp(volume) renWin.AddRenderer(ren) iRen.SetRenderWindow(renWin) # Define opacity transfer function and color functions opacityTransferFunction = vtk.vtkPiecewiseFunction() opacityTransferFunction.AddPoint(0.1, 1.0) opacityTransferFunction.AddPoint(14, 0.5) # Create transfer mapping scalar value to color colorTransferFunction = vtk.vtkColorTransferFunction() colorTransferFunction.AddRGBPoint(1.5, 1.0, 1.0, 0.0) colorTransferFunction.AddRGBPoint(0.5, 1.0, 0.0, 0.0) colorTransferFunction.AddRGBPoint(3.0, 0.0, 1.0, 0.0) colorTransferFunction.AddRGBPoint(6.0, 0.0, 1.0, 1.0) colorTransferFunction.AddRGBPoint(14.0, 0.0, 0.0, 1.0) # Now set the opacity and the color volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(colorTransferFunction) volumeProperty.SetScalarOpacity(opacityTransferFunction) volumeProperty.ShadeOff() volumeProperty.SetInterpolationTypeToLinear() volume.SetProperty(volumeProperty) renderer = vtk.vtkRenderer() renderer.SetBackground(0.5, 0.5, 0.5) renderer.AddVolume(volume) renderer.ResetCamera() renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindow.SetSize(500, 500) renderWindow.Render() iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renderWindow) iren.Start()
def alpha(self, alpha): """Assign a set of tranparencies to a volume along the range of the scalar value. A single constant value can also be assigned. E.g.: say alpha=(0.0, 0.3, 0.9, 1) and the scalar range goes from -10 to 150. Then all voxels with a value close to -10 will be completely transparent, voxels at 1/4 of the range will get an alpha equal to 0.3 and voxels with value close to 150 will be completely opaque. """ volumeProperty = self.GetProperty() smin, smax = self._imagedata.GetScalarRange() opacityTransferFunction = vtk.vtkPiecewiseFunction() self._alpha = alpha if utils.isSequence(alpha): for i, al in enumerate(alpha): xalpha = smin + (smax - smin) * i / (len(alpha) - 1) # Create transfer mapping scalar value to opacity opacityTransferFunction.AddPoint(xalpha, al) #colors.printc("alpha at", round(xalpha, 1), "\tset to", al, c="b", bold=0) else: opacityTransferFunction.AddPoint(smin, alpha) # constant alpha opacityTransferFunction.AddPoint(smax, alpha) volumeProperty.SetScalarOpacity(opacityTransferFunction) return self
def Create8bOpacityTable(self, scale): if self.opacity_transfer_func: opacity_transfer_func = self.opacity_transfer_func else: opacity_transfer_func = vtk.vtkPiecewiseFunction() opacity_transfer_func.RemoveAllPoints() opacities = [] ww = self.config['ww'] wl = self.TranslateScale(scale, self.config['wl']) l1 = wl - ww/2.0 l2 = wl + ww/2.0 self.ww = ww self.wl = self.config['wl'] opacity_transfer_func.RemoveAllPoints() opacity_transfer_func.AddSegment(0, 0, 2**16-1, 0) k1 = 0.0 k2 = 1.0 opacity_transfer_func.AddPoint(l1, 0) opacity_transfer_func.AddPoint(l2, 1) self.opacity_transfer_func = opacity_transfer_func return opacity_transfer_func
def init_all_VolumeRendering_component(self, flagMesh): self.flagMesh = flagMesh self.ren= vtk.vtkRenderer() self.renWin.AddRenderer(self.ren) self.iren = vtk.vtkXRenderWindowInteractor() self.iren.SetRenderWindow(self.renWin) self.ren.GetVolumes() self.alpha_channel_function = vtk.vtkPiecewiseFunction() self.alpha_channel_function.AddPoint(0, 0.0, 0.5, 0.0) self.alpha_channel_function.AddPoint(255, 1, 0.5, 0.0) self.color_function = vtk.vtkColorTransferFunction() self.color_function.AddRGBPoint(0, 0, 0.0, 0.0, 0.5, 0.0) self.color_function.AddRGBPoint(255, 1, 1, 1, 0.5, 0.0) self.volume_property = vtk.vtkVolumeProperty() self.volume_property.SetColor(self.color_function) self.volume_property.SetScalarOpacity(self.alpha_channel_function) self.data_importer = vtk.vtkImageImport() if self.flagMesh : self.volume_mapper = vtk.vtkPolyDataMapper() else: self.volume_mapper = vtk.vtkFixedPointVolumeRayCastMapper() self.volume = vtk.vtkVolume()
def initRenderWindow(self): pix_diag = 5.0 self.nVolumes = 0 self.vtkWidget = QVTKRenderWindowInteractor(self.frame) self.ren = vtk.vtkRenderer() self.ren.SetBackground(1.0, 1.0, 1.0) self.vtkWidget.GetRenderWindow().AddRenderer(self.ren) self.window = self.vtkWidget.GetRenderWindow() self.iren = self.vtkWidget.GetRenderWindow().GetInteractor() drange = [0,400] self.vtkWidget.opacity_tf = vtk.vtkPiecewiseFunction() self.vtkWidget.opacity_tf.AddPoint(drange[0],0.0) self.vtkWidget.opacity_tf.AddPoint(drange[1],0.0) self.vtkWidget.color_tf = vtk.vtkColorTransferFunction() self.vtkWidget.color_tf.AddRGBPoint(drange[0], 0.0, 0.0, 0.0) self.vtkWidget.color_tf.AddRGBPoint(drange[1], 1.0, 1.0, 1.0) self.vtkWidget.volProperty = vtk.vtkVolumeProperty() self.vtkWidget.volProperty.SetColor(self.vtkWidget.color_tf); self.vtkWidget.volProperty.SetScalarOpacity(self.vtkWidget.opacity_tf) self.vtkWidget.volProperty.ShadeOn() self.vtkWidget.volProperty.SetInterpolationTypeToLinear() self.vtkWidget.volProperty.SetScalarOpacityUnitDistance(pix_diag)
def CreateOpacityTable(self, scale): if self.opacity_transfer_func: opacity_transfer_func = self.opacity_transfer_func else: opacity_transfer_func = vtk.vtkPiecewiseFunction() opacity_transfer_func.RemoveAllPoints() curve_table = self.config['16bitClutCurves'] opacities = [] ww = self.config['ww'] wl = self.config['wl'] self.ww = ww self.wl = wl l1 = wl - ww/2.0 l2 = wl + ww/2.0 k1 = 0.0 k2 = 1.0 opacity_transfer_func.AddSegment(0, 0, 2**16-1, 0) for i, l in enumerate(curve_table): for j, lopacity in enumerate(l): gray_level = lopacity['x'] #if gray_level <= l1: # opacity = k1 #elif gray_level > l2: # opacity = k2 #else: opacity = lopacity['y'] opacities.append((gray_level, opacity)) opacity_transfer_func.AddPoint( self.TranslateScale(scale, gray_level), opacity) self.opacity_transfer_func = opacity_transfer_func
def __init__(self, source, orientation, opacity=1.0): self.reslice = vtk.vtkImageReslice() self.reslice.SetInputData(source) self.reslice.SetOutputDimensionality(2) self.reslice.SetResliceAxes(orientation) self.reslice.SetInterpolationModeToNearestNeighbor() # Set lookup table color_transfer = vtk.vtkDiscretizableColorTransferFunction() alpha_transfer = vtk.vtkPiecewiseFunction() color_transfer.AddRGBPoint(0, 0., 0., 0.) # Background alpha_transfer.AddPoint(0, 0) # Background for i, organ in enumerate(Settings.labels): color_transfer.AddRGBPoint(Settings.labels[organ]['value'], *Settings.labels[organ]['rgb']) if organ in Settings.organs: alpha_transfer.AddPoint(Settings.labels[organ]['value'], opacity) else: alpha_transfer.AddPoint(Settings.labels[organ]['value'], 0.) color_transfer.SetScalarOpacityFunction(alpha_transfer) color_transfer.EnableOpacityMappingOn() # Map the image through the lookup table self.color = vtk.vtkImageMapToColors() self.color.SetLookupTable(color_transfer) self.color.SetInputConnection(self.reslice.GetOutputPort()) # Display the image self.actor = vtk.vtkImageActor() self.actor.GetMapper().SetInputConnection(self.color.GetOutputPort())
def CreateOpacityTable(self, scale): if self.opacity_transfer_func: opacity_transfer_func = self.opacity_transfer_func else: opacity_transfer_func = vtk.vtkPiecewiseFunction() opacity_transfer_func.RemoveAllPoints() curve_table = self.config['16bitClutCurves'] opacities = [] ww = self.config['ww'] wl = self.config['wl'] self.ww = ww self.wl = wl l1 = wl - ww / 2.0 l2 = wl + ww / 2.0 k1 = 0.0 k2 = 1.0 opacity_transfer_func.AddSegment(0, 0, 2**16 - 1, 0) for i, l in enumerate(curve_table): for j, lopacity in enumerate(l): gray_level = lopacity['x'] #if gray_level <= l1: # opacity = k1 #elif gray_level > l2: # opacity = k2 #else: opacity = lopacity['y'] opacities.append((gray_level, opacity)) opacity_transfer_func.AddPoint( self.TranslateScale(scale, gray_level), opacity) self.opacity_transfer_func = opacity_transfer_func
def createOTF(tissues): OTF = vtk.vtkPiecewiseFunction() OTF.AddPoint(0, 0) for t in tissues: OTF.AddPoint(t.val - 0.5, 0) OTF.AddPoint(t.val, t.opacity) OTF.AddPoint(t.val + 0.5, 0) OTF.AddPoint(17.0, 0) return OTF
def createOpacityTransferFunction(values): opacityTransferFunction = vtk.vtkPiecewiseFunction() opacityTransferFunction.AddPoint(1.0, 0) for v in values: opacityTransferFunction.AddPoint(v - 0.01, 0) opacityTransferFunction.AddPoint(v, 0.2) opacityTransferFunction.AddPoint(v + 0.01, 0) opacityTransferFunction.AddPoint(17.0, 0) return opacityTransferFunction
def getTFxn(self): a = self.alphaFxn.get_function () f = vtk.vtkPiecewiseFunction () npnt = self.alphaFxn.npoints dt = 1.0/(npnt - 1.0) ds = (self.max_x - self.min_x)/(npnt - 1.0) for i in range(npnt): f.AddPoint(self.min_x + i*ds, a.GetValue(i*dt)) return f
def get_vtk_transfer_functions(self): of = vtk.vtkPiecewiseFunction() cf = vtk.vtkColorTransferFunction() for pt in self._pts: (scalar, opacity, color) = pt # Map scalar to tf range s = self._min_range + (self._max_range - self._min_range) * scalar of.AddPoint(s, opacity) cf.AddRGBPoint(s, color[0], color[1], color[2]) return (of,cf)
def setDataUnit(self, dataunit): """ Sets the dataunit this module uses for visualization """ Logging.info("Dataunit for Volume Rendering:", dataunit, kw = "rendering") VisualizationModule.setDataUnit(self, dataunit) otf, otf2 = vtk.vtkPiecewiseFunction(), vtk.vtkPiecewiseFunction() d = dataunit.getSingleComponentBitDepth() maxv = 2 ** d otf2.AddPoint(0, 0.0) otf2.AddPoint(maxv, 1.0) otf.AddPoint(0, 0.0) otf.AddPoint(maxv, 0.2) self.otfs = [otf, otf, otf, otf2, otf] self.setInputChannel(1, 0) self.parameters["Palette"] = self.colorTransferFunction
def updateTransferFunction(self): # Transfer function and property self.colorFunction = vtkColorTransferFunction() self.colorFunction.AddRGBSegment(self.minimum, 0.0, 0.0, 0.0, self.maximum, 1.0, 1.0, 1.0) self.opacityFunction = vtkPiecewiseFunction() self.opacityFunction.AddSegment(self.minimum, 0.0, self.maximum, 1.0) self.volProp.SetColor(self.colorFunction) self.volProp.SetScalarOpacity(self.opacityFunction) self.updatedTransferFunction.emit()
def AdaptOpacity(self, minVal=None, maxVal=None) : import vtk if minVal == None or maxVal == None : m, M = self.GetRange() if minVal == None : minVal = m if maxVal == None : maxVal = M opacityTransferFunction = vtk.vtkPiecewiseFunction() opacityTransferFunction.AddPoint(minVal, self.__MinOpacity__) opacityTransferFunction.AddPoint(maxVal, self.__MaxOpacity__) self.__volumeProperty__.SetScalarOpacity(opacityTransferFunction)
def updateColorOpacity(): opacityFunction = vtk.vtkPiecewiseFunction() for op in opacity: opacityFunction.AddPoint(op[0], op[1]) colorFunction = vtk.vtkColorTransferFunction() for c in rgb: colorFunction.AddRGBPoint(c[0], c[1], c[2], c[3]) global volumeProperty volumeProperty.SetColor(colorFunction) volumeProperty.SetScalarOpacity(opacityFunction) global volume volume.Update()
def make_color_function(self,scalar_range): max_scalar=scalar_range[1] min_scalar=scalar_range[0] # Create transfer mapping scalar value to opacity self.opacityTransferFunction=vtk.vtkPiecewiseFunction() self.opacityTransferFunction.AddPoint(max_scalar/5.0,0.03) self.opacityTransferFunction.AddPoint(max_scalar*0.7,1) self.opacityTransferFunction.AddPoint(max_scalar,1) # Create transfer mapping scalar value to color self.colorTransferFunction=vtk.vtkColorTransferFunction() self.colorTransferFunction.AdjustRange(scalar_range) self.colorTransferFunction.SetScale(100) self.colorTransferFunction.AddRGBPoint(min_scalar,0.0,0.0,1.0) self.colorTransferFunction.AddRGBPoint(max_scalar,1.0,0.0,0.0) self.colorTransferFunction.SetColorSpaceToRGB() self.colorTransferFunction.SetScaleToLinear() # Create transfer mapping scalar value to opacity based on gradient magnitude self.gradientTransferFunction=vtk.vtkPiecewiseFunction() self.gradientTransferFunction.AddPoint(min_scalar,0.01) self.gradientTransferFunction.AddPoint(max_scalar*0.7,1.0)
def transferfunction(self, imagedata, color): scalarRange = imagedata.GetScalarRange() opacity = vtkPiecewiseFunction() opacity.AddPoint(scalarRange[0], 0) opacity.AddPoint(scalarRange[1], 1) transfer = vtkDiscretizableColorTransferFunction() transfer.AddRGBPoint(scalarRange[0], 0, 0, 0) transfer.AddRGBPoint(scalarRange[1], color[0], color[1], color[2]) transfer.SetScalarOpacityFunction(opacity) return transfer
def main(argv): if len(argv) < 2: print "usage:",argv[0]," data.nrrd data.cmap" exit(1) data_fn = argv[1] cmap_fn = argv[2] reader = vtk.vtkPNrrdReader() reader.SetFileName(data_fn) reader.Update() data = reader.GetOutput() # opacity function opacityFunction = vtk.vtkPiecewiseFunction() # color function colorFunction = vtk.vtkColorTransferFunction() cmap = open(cmap_fn, 'r') for line in cmap.readlines(): parts = line.split() value = float(parts[0]) r = float(parts[1]) g = float(parts[2]) b = float(parts[3]) a = float(parts[4]) opacityFunction.AddPoint(value, a) colorFunction.AddRGBPoint(value, r, g, b) # volume setup: #volumeProperty = vtk.vtkVolumeProperty() global volumeProperty volumeProperty.SetColor(colorFunction) volumeProperty.SetScalarOpacity(opacityFunction) # composite function (using ray tracing) compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() volumeMapper = vtk.vtkVolumeRayCastMapper() volumeMapper.SetVolumeRayCastFunction(compositeFunction) volumeMapper.SetInput(data) # make the volume #volume = vtk.vtkVolume() global volume volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) # renderer renderer = vtk.vtkRenderer() renderWin = vtk.vtkRenderWindow() renderWin.AddRenderer(renderer) renderInteractor = vtk.vtkRenderWindowInteractor() renderInteractor.SetRenderWindow(renderWin) renderInteractor.AddObserver( vtk.vtkCommand.KeyPressEvent, keyPressed ) renderer.AddVolume(volume) renderer.SetBackground(0,0,0) renderWin.SetSize(400, 400) renderInteractor.Initialize() renderWin.Render() renderInteractor.Start()
def set_on_vtk_volume_property(self, vtk_volume_property): # Builds the opacity and color functions of = vtk.vtkPiecewiseFunction() cf = vtk.vtkColorTransferFunction() vp = vtk_volume_property for pt in self._pts: (scalar, opacity, color) = pt # Map scalar to tf range s = self._min_range + (self._max_range - self._min_range) * scalar of.AddPoint(s, opacity) cf.AddRGBPoint(s, color[0], color[1], color[2]) vp.SetScalarOpacity(of) vp.SetColor(cf)
def volumeRender(img, tf=[],spacing=[1.0,1.0,1.0]): importer = numpy2VTK(img,spacing) # Transfer Functions opacity_tf = vtk.vtkPiecewiseFunction() color_tf = vtk.vtkColorTransferFunction() if len(tf) == 0: tf.append([img.min(),0,0,0,0]) tf.append([img.max(),1,1,1,1]) for p in tf: color_tf.AddRGBPoint(p[0], p[1], p[2], p[3]) opacity_tf.AddPoint(p[0], p[4]) # working on the GPU # volMapper = vtk.vtkGPUVolumeRayCastMapper() # volMapper.SetInputConnection(importer.GetOutputPort()) # # The property describes how the data will look # volProperty = vtk.vtkVolumeProperty() # volProperty.SetColor(color_tf) # volProperty.SetScalarOpacity(opacity_tf) # volProperty.ShadeOn() # volProperty.SetInterpolationTypeToLinear() # working on the CPU volMapper = vtk.vtkVolumeRayCastMapper() compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() compositeFunction.SetCompositeMethodToInterpolateFirst() volMapper.SetVolumeRayCastFunction(compositeFunction) volMapper.SetInputConnection(importer.GetOutputPort()) # The property describes how the data will look volProperty = vtk.vtkVolumeProperty() volProperty.SetColor(color_tf) volProperty.SetScalarOpacity(opacity_tf) volProperty.ShadeOn() volProperty.SetInterpolationTypeToLinear() # Do the lines below speed things up? # pix_diag = 5.0 # volMapper.SetSampleDistance(pix_diag / 5.0) # volProperty.SetScalarOpacityUnitDistance(pix_diag) vol = vtk.vtkVolume() vol.SetMapper(volMapper) vol.SetProperty(volProperty) return [vol]