def Create8bColorTable(self, scale): if self.color_transfer: color_transfer = self.color_transfer else: color_transfer = vtkColorTransferFunction() color_transfer.RemoveAllPoints() color_preset = self.config['CLUT'] if color_preset != "No CLUT": path = os.path.join(inv_paths.RAYCASTING_PRESETS_DIRECTORY, 'color_list', color_preset + '.plist') with open(path, 'rb') as f: p = plistlib.load(f, fmt=plistlib.FMT_XML) r = p['Red'] g = p['Green'] b = p['Blue'] colors = list(zip(r, g, b)) else: # Grayscale from black to white colors = [(i, i, i) for i in range(256)] ww = self.config['ww'] wl = self.TranslateScale(scale, self.config['wl']) init = wl - ww / 2.0 inc = ww / (len(colors) - 1.0) for n, rgb in enumerate(colors): color_transfer.AddRGBPoint(init + n * inc, *[i / 255.0 for i in rgb]) self.color_transfer = color_transfer
def lookupTableToColorTransferFunction(lookupTable): dataTable = lookupTable.GetTable() table = dataTableToList(dataTable) if table: ctf = vtkColorTransferFunction() tableRange = lookupTable.GetTableRange() points = linspace(*tableRange, num=len(table)) for x, rgba in zip(points, table): ctf.AddRGBPoint(x, *[x / 255 for x in rgba[:3]]) return ctf return None
def initialize_volume_color(self): """ Initialize volume color """ # The colorTransferFunction maps voxel intensities to colors. self.volume_color = vtkColorTransferFunction() self.volume_color.AddRGBPoint(-127, 1.0, 1.0, 1.0) self.volume_color.AddRGBPoint(0, 0, 0, 0) self.volume_color.AddRGBPoint(128, 1.0, 1.0, 1.0) # The opacityTransferFunction is used to control the opacity # of different tissue types. self.volume_scalar_opacity = vtkPiecewiseFunction() self.volume_scalar_opacity.AddPoint(-127, 1) self.volume_scalar_opacity.AddPoint(0, 0) self.volume_scalar_opacity.AddPoint(128, 1) # 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. self.volume_gradient_opacity = vtkPiecewiseFunction() self.volume_gradient_opacity.AddPoint(-127, 1) self.volume_gradient_opacity.AddPoint(0, 0) self.volume_gradient_opacity.AddPoint(128, 1) # The VolumeProperty attaches the color and opacity # functions to the volume, and sets other volume properties. # The interpolation should be set to linear # to do a high-quality rendering. self.volume_property = vtkVolumeProperty() self.volume_property.SetColor(self.volume_color) self.volume_property.SetScalarOpacity(self.volume_scalar_opacity) self.volume_property.SetGradientOpacity(self.volume_gradient_opacity) self.volume_property.SetInterpolationTypeToNearest() # To decrease the impact of shading, increase the Ambient and # decrease the Diffuse and Specular. # To increase the impact of shading, decrease the Ambient and # increase the Diffuse and Specular. self.volume_property.ShadeOn() self.volume_property.SetAmbient(0.3) self.volume_property.SetDiffuse(0.6) self.volume_property.SetSpecular(0.5)
def _set_volume_range(self, volume, ctable, alpha, scalar_bar, rng): color_tf = vtkColorTransferFunction() opacity_tf = vtkPiecewiseFunction() for loc, color in zip(np.linspace(*rng, num=len(ctable)), ctable): color_tf.AddRGBPoint(loc, *(color[:-1] / 255.)) opacity_tf.AddPoint(loc, color[-1] * alpha / 255.) color_tf.ClampingOn() opacity_tf.ClampingOn() prop = volume.GetProperty() prop.SetColor(color_tf) prop.SetScalarOpacity(opacity_tf) prop.ShadeOn() prop.SetInterpolationTypeToLinear() if scalar_bar is not None: lut = vtkLookupTable() lut.SetRange(*rng) lut.SetTable(numpy_to_vtk(ctable)) scalar_bar.SetLookupTable(lut)
def Create16bColorTable(self, scale): if self.color_transfer: color_transfer = self.color_transfer else: color_transfer = vtkColorTransferFunction() color_transfer.RemoveAllPoints() curve_table = self.config['16bitClutCurves'] color_table = self.config['16bitClutColors'] colors = [] for i, l in enumerate(curve_table): for j, lopacity in enumerate(l): gray_level = lopacity['x'] r = color_table[i][j]['red'] g = color_table[i][j]['green'] b = color_table[i][j]['blue'] colors.append((gray_level, r, g, b)) color_transfer.AddRGBPoint( self.TranslateScale(scale, gray_level), r, g, b) self.color_transfer = color_transfer
def create_volume(self): if self._actor is None: if int(ses.Session().rendering) == 0: self._volume_mapper = vtkFixedPointVolumeRayCastMapper() #volume_mapper.AutoAdjustSampleDistancesOff() self._volume_mapper.IntermixIntersectingGeometryOn() pix_diag = 2.0 self._volume_mapper.SetImageSampleDistance(0.25) self._volume_mapper.SetSampleDistance(pix_diag / 5.0) else: self._volume_mapper = vtkGPUVolumeRayCastMapper() self._volume_mapper.UseJitteringOn() if Version(vtkVersion().GetVTKVersion()) > Version('8.0'): self._volume_mapper.SetBlendModeToIsoSurface() # else: # isosurfaceFunc = vtk.vtkVolumeRayCastIsosurfaceFunction() # isosurfaceFunc.SetIsoValue(127) # self._volume_mapper = vtk.vtkVolumeRayCastMapper() # self._volume_mapper.SetVolumeRayCastFunction(isosurfaceFunc) self._flip = vtkImageFlip() self._flip.SetInputData(self.mask.imagedata) self._flip.SetFilteredAxis(1) self._flip.FlipAboutOriginOn() self._volume_mapper.SetInputConnection(self._flip.GetOutputPort()) self._volume_mapper.Update() r, g, b = self.colour self._color_transfer = vtkColorTransferFunction() self._color_transfer.RemoveAllPoints() self._color_transfer.AddRGBPoint(0.0, 0, 0, 0) self._color_transfer.AddRGBPoint(254.0, r, g, b) self._color_transfer.AddRGBPoint(255.0, r, g, b) self._piecewise_function = vtkPiecewiseFunction() self._piecewise_function.RemoveAllPoints() self._piecewise_function.AddPoint(0.0, 0.0) self._piecewise_function.AddPoint(127, 1.0) self._volume_property = vtkVolumeProperty() self._volume_property.SetColor(self._color_transfer) self._volume_property.SetScalarOpacity(self._piecewise_function) self._volume_property.ShadeOn() self._volume_property.SetInterpolationTypeToLinear() self._volume_property.SetSpecular(0.75) self._volume_property.SetSpecularPower(2) if not self._volume_mapper.IsA("vtkGPUVolumeRayCastMapper"): self._volume_property.SetScalarOpacityUnitDistance(pix_diag) else: if Version(vtkVersion().GetVTKVersion()) > Version('8.0'): self._volume_property.GetIsoSurfaceValues().SetValue( 0, 127) self._actor = vtkVolume() self._actor.SetMapper(self._volume_mapper) self._actor.SetProperty(self._volume_property) self._actor.Update()