def GetPeelActor(self, p): lut = vtk.vtkWindowLevelLookupTable() lut.SetWindow(self.window_width) lut.SetLevel(self.window_level) lut.Build() init = self.window_level - self.window_width / 2 end = self.window_level + self.window_width / 2 # Set mapper auto mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(self.peel[p]) mapper.SetScalarRange(init, end) mapper.SetLookupTable(lut) mapper.InterpolateScalarsBeforeMappingOn() # Set actor self.currentPeelActor.SetMapper(mapper) currentPeel = self.TransformPeelPosition(p) self.locator.SetDataSet(currentPeel) self.locator.BuildLocator() self.GetCenters(currentPeel) self.GetNormals(currentPeel) return self.currentPeelActor
def polyShowScalars(ren,obj): #@c Show scalar values on poly in renderer in grayscale #@a ren: renderer #@a obj: object name if isinstance(obj,list): tag = "%s_%s" % (ren[0],obj[0]) elif isinstance(obj,str): tag = "%s_%s" % (ren[0],obj) else: raise ValueError("Argument type unsupported.") lookupGrayscale = [None]*2 lookupGrayscale[0] = "p_grayscaleLUT_"+tag lookupGrayscale[1] = vtk.vtkWindowLevelLookupTable() lookupGrayscale[1].SetWindow(1023) lookupGrayscale[1].SetLevel(512) lookupGrayscale[1].SetWindow(0.5) lookupGrayscale[1].SetLevel(0.5) lookupGrayscale[1].SetHueRange(0.,0.) lookupGrayscale[1].SetSaturationRange(0.,0.) lookupGrayscale[1].SetValueRange(0.,1.) lookupGrayscale[1].SetNumberOfColors(16384) lookupGrayscale[1].Build() act = polyGetActor(ren,obj) act[1].GetMapper().SetLookupTable(lookupGrayscale[1]) act[1].GetMapper().ScalarVisibilityOn() Range = act[1].GetMapper().GetInput().GetPointData().GetScalars().GetRange() act[1].GetMapper().SetScalarRange(Range[0],Range[1]) vis.render(ren) setattr(vis,act[0],act) setattr(vis,lookupGrayscale[0],lookupGrayscale) return
def createGridActor(grid, showScalar=False, range=None, showEdges=False, colorMap='colors'): mapper = vtk.vtkDataSetMapper() mapper.SetInput(grid) actor = vtk.vtkActor() if showScalar: scalars = grid.GetPointData().GetScalars() if scalars==None: scalars = grid.GetCellData().GetScalars() if scalars==None: scalars = grid.GetPointData().GetArray(0) grid.GetPointData().SetScalars(scalars) if scalars==None: scalars = grid.GetCellData().GetArray(0) grid.GetCellData().SetScalars(scalars) if range==None: range = [scalars.GetRange()[0],.8*scalars.GetRange()[1]] mapper.SetScalarRange(*range) if colorMap=='GrayScale':wl = vtk.vtkWindowLevelLookupTable() else: wl = vtk.vtkLookupTable() wl.SetScaleToLinear() wl.SetRange(*range) wl.SetHueRange(0.6667,0.0)#blue to red wl.Build() mapper.SetLookupTable(wl) else: mapper.ScalarVisibilityOff() actor.GetProperty().SetColor(.6, .6, .6) actor.SetMapper(mapper) if showEdges: actor.GetProperty().EdgeVisibilityOn() return actor
def CreateImageActor(actor, colorWindow, colorLevel): wlut = vtk.vtkWindowLevelLookupTable() wlut.SetWindow(colorWindow) wlut.SetLevel(colorLevel) wlut.Build() # Map the image through the lookup table. color = vtk.vtkImageMapToColors() color.SetLookupTable(wlut) color.SetInputData(actor.GetMapper().GetInput()) actor.GetMapper().SetInputConnection(color.GetOutputPort()) return
def CreateHighlightLookupTable(self): # Create colour table for histogram overlay table = vtk.vtkWindowLevelLookupTable() table.SetSaturationRange(0, 0) table.SetHueRange(0, 0) table.SetValueRange(0, 1) table.SetLevel(0.5) table.SetWindow(1.0) table.SetNumberOfColors(2) table.SetTableValue(0, 0.0, 0.0, 0.0, 0.0) table.SetTableValue(1, 1.0, 0.0, 0.0, 1.0) table.Build() return table
def createPolyDataActor(polydata, showScalar=False, range=None): mapper = vtk.vtkPolyDataMapper() mapper.SetInput(polydata) if showScalar: wl = vtk.vtkWindowLevelLookupTable() if range == None: range = polydata.GetPointData().GetScalars().GetRange() mapper.SetScalarRange(*range) mapper.SetLookupTable(wl) else: mapper.ScalarVisibilityOff() actor = vtk.vtkActor() actor.SetMapper(mapper) return actor
def createClipActor(grid, plane, showScalar=False, range=None, showEdges=False, colorMap='GrayScale'): clipMapper = vtk.vtkDataSetMapper() clipMapper.SetInput(grid) cutMapper = vtk.vtkDataSetMapper() clipActor = vtk.vtkActor() cutActor = vtk.vtkActor() if showScalar: scalars = grid.GetPointData().GetScalars() if scalars==None: scalars = grid.GetCellData().GetScalars() if scalars==None: scalars = grid.GetPointData().GetArray(0) grid.GetPointData().SetScalars(scalars) if scalars==None: scalars = grid.GetCellData().GetArray(0) grid.GetCellData().SetScalars(scalars) if range==None: range = [scalars.GetRange()[0],.8*scalars.GetRange()[1]] cutMapper.SetScalarRange(*range) clipMapper.SetScalarRange(*range) if colorMap=='GrayScale':wl = vtk.vtkWindowLevelLookupTable() else: wl = vtk.vtkLookupTable() wl.SetScaleToLinear() wl.SetRange(*range) wl.SetHueRange(0.6667,0.0)#blue to red wl.Build() clipMapper.SetLookupTable(wl) cutMapper.SetLookupTable(wl) else: clipMapper.ScalarVisibilityOff() cutMapper.ScalarVisibilityOff() clipActor.GetProperty().SetColor(.6, .6, .6) cutActor.GetProperty().SetColor(.6, .6, .6) clipMapper.AddClippingPlane(plane) clipActor.SetMapper(clipMapper) cutter = vtk.vtkCutter() cutter.SetCutFunction(plane) cutter.SetInput(grid) cutter.Update() cutMapper.SetInputConnection(cutter.GetOutputPort()) cutActor.SetMapper(cutMapper) if showEdges: cutActor.GetProperty().EdgeVisibilityOn() clipActor.GetProperty().EdgeVisibilityOn() return [cutActor,clipActor]
def __init__( self, color, plane, reslice, range ): self._color = color self._source = plane self._mapper = vtk.vtkPolyDataMapper() self._mapper.SetInput( self._source.GetOutput() ) self.SetMapper( self._mapper ) self._lut = vtk.vtkWindowLevelLookupTable() self._lut.SetNumberOfColors( 256 ) self._lut.SetTableRange( range[0], range[1] ) self._lut.SetHueRange( 0.0, 0.0 ) self._lut.SetSaturationRange( 0.0, 0.0 ) self._lut.SetValueRange( 0.0, 1.0 ) self._lut.SetAlphaRange( 0.0, 1.0 ) self._lut.Build() self._colormap = vtk.vtkImageMapToColors() self._colormap.SetInputConnection( reslice.GetOutputPort() ) self._colormap.SetLookupTable( self._lut ) self._colormap.SetOutputFormatToRGBA() self._colormap.PassAlphaToOutputOn() self._texture = vtk.vtkTexture() self._texture.SetInterpolate( True ) self._texture.SetQualityTo32Bit() self._texture.MapColorScalarsThroughLookupTableOff() self._texture.RepeatOff() self._texture.SetInput( self._colormap.GetOutput() ) self._texture.SetLookupTable( self._lut ) self.SetTexture( self._texture ) self._property = vtk.vtkProperty() self._property.SetOpacity( 0.9 ) self._property.EdgeVisibilityOn() self._property.SetEdgeColor( self._color[0], self._color[1], self._color[2] ) self.SetProperty( self._property )
def GetCurrentPeelActor(self, currentPeel): lut = vtk.vtkWindowLevelLookupTable() lut.SetWindow(self.window_width) lut.SetLevel(self.window_level) lut.Build() init = self.window_level - self.window_width / 2 end = self.window_level + self.window_width / 2 # Set mapper auto mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(currentPeel) mapper.SetScalarRange(init, end) mapper.SetLookupTable(lut) mapper.InterpolateScalarsBeforeMappingOn() # Set actor self.currentPeelActor.SetMapper(mapper) self.currentPeelActor.GetProperty().SetBackfaceCulling(1) self.currentPeelActor.GetProperty().SetOpacity(0.5) self.currentPeelActor.GetProperty().SetSpecular(0.25) return self.currentPeelActor
def main(data_folder, slice_number): colors = vtk.vtkNamedColors() path = Path(data_folder) if path.is_dir(): s = '' fn_1 = path.joinpath('frog').with_suffix('.mhd') if not fn_1.is_file(): s += 'The file: {:s} does not exist.\n'.format(str(fn_1)) print(s) fn_2 = path.joinpath('frogtissue').with_suffix('.mhd') if not fn_2.is_file(): s += 'The file: {:s} does not exist.'.format(str(fn_2)) if s: print(s) return else: print('Expected a path to frog.mhs and frogtissue.mhd') return so = SliceOrder() # Now create the RenderWindow, Renderer and Interactor # ren1 = vtk.vtkRenderer() ren2 = vtk.vtkRenderer() ren3 = vtk.vtkRenderer() ren_win = vtk.vtkRenderWindow() ren_win.AddRenderer(ren1) ren_win.AddRenderer(ren2) ren_win.AddRenderer(ren3) ren_win.SetWindowName('FrogSlice') iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(ren_win) grey_reader = vtk.vtkMetaImageReader() grey_reader.SetFileName(str(fn_1)) grey_reader.Update() grey_padder = vtk.vtkImageConstantPad() grey_padder.SetInputConnection(grey_reader.GetOutputPort()) grey_padder.SetOutputWholeExtent(0, 511, 0, 511, slice_number, slice_number) grey_padder.SetConstant(0) grey_plane = vtk.vtkPlaneSource() grey_transform = vtk.vtkTransformPolyDataFilter() grey_transform.SetTransform(so.get('hfsi')) grey_transform.SetInputConnection(grey_plane.GetOutputPort()) grey_normals = vtk.vtkPolyDataNormals() grey_normals.SetInputConnection(grey_transform.GetOutputPort()) grey_normals.FlipNormalsOff() wllut = vtk.vtkWindowLevelLookupTable() wllut.SetWindow(255) wllut.SetLevel(128) wllut.SetTableRange(0, 255) wllut.Build() grey_mapper = vtk.vtkPolyDataMapper() grey_mapper.SetInputConnection(grey_plane.GetOutputPort()) grey_texture = vtk.vtkTexture() grey_texture.SetInputConnection(grey_padder.GetOutputPort()) grey_texture.SetLookupTable(wllut) grey_texture.SetColorModeToMapScalars() grey_texture.InterpolateOn() grey_actor = vtk.vtkActor() grey_actor.SetMapper(grey_mapper) grey_actor.SetTexture(grey_texture) segment_reader = vtk.vtkMetaImageReader() segment_reader.SetFileName(str(fn_2)) segment_reader.Update() segment_padder = vtk.vtkImageConstantPad() segment_padder.SetInputConnection(segment_reader.GetOutputPort()) segment_padder.SetOutputWholeExtent(0, 511, 0, 511, slice_number, slice_number) segment_padder.SetConstant(0) segment_plane = vtk.vtkPlaneSource() segment_transform = vtk.vtkTransformPolyDataFilter() segment_transform.SetTransform(so.get('hfsi')) segment_transform.SetInputConnection(segment_plane.GetOutputPort()) segment_normals = vtk.vtkPolyDataNormals() segment_normals.SetInputConnection(segment_transform.GetOutputPort()) segment_normals.FlipNormalsOn() lut = create_frog_lut(colors) segment_mapper = vtk.vtkPolyDataMapper() segment_mapper.SetInputConnection(segment_plane.GetOutputPort()) segment_texture = vtk.vtkTexture() segment_texture.SetInputConnection(segment_padder.GetOutputPort()) segment_texture.SetLookupTable(lut) segment_texture.SetColorModeToMapScalars() segment_texture.InterpolateOff() segment_actor = vtk.vtkActor() segment_actor.SetMapper(segment_mapper) segment_actor.SetTexture(segment_texture) segment_overlay_actor = vtk.vtkActor() segment_overlay_actor.SetMapper(segment_mapper) segment_overlay_actor.SetTexture(segment_texture) segment_overlay_actor.GetProperty().SetOpacity(.5) ren1.SetBackground(0, 0, 0) ren1.SetViewport(0, 0.5, 0.5, 1) ren_win.SetSize(640, 480) ren1.AddActor(grey_actor) ren2.SetBackground(0, 0, 0) ren2.SetViewport(0.5, 0.5, 1, 1) ren2.AddActor(segment_actor) cam1 = vtk.vtkCamera() cam1.SetViewUp(0, -1, 0) cam1.SetPosition(0, 0, -1) ren1.SetActiveCamera(cam1) ren1.ResetCamera() cam1.SetViewUp(0, -1, 0) cam1.SetPosition(0.0554068, -0.0596001, -0.491383) cam1.SetFocalPoint(0.0554068, -0.0596001, 0) ren1.ResetCameraClippingRange() ren3.AddActor(grey_actor) ren3.AddActor(segment_overlay_actor) segment_overlay_actor.SetPosition(0, 0, -0.01) ren1.SetBackground(colors.GetColor3d('SlateGray')) ren2.SetBackground(colors.GetColor3d('SlateGray')) ren3.SetBackground(colors.GetColor3d('SlateGray')) ren3.SetViewport(0, 0, 1, 0.5) ren2.SetActiveCamera(ren1.GetActiveCamera()) ren3.SetActiveCamera(ren1.GetActiveCamera()) ren_win.Render() iren.Start()
def main(): # colors = vtk.vtkNamedColors() filename1 = "C:\\Users\\chenjiaxing\\Desktop\\Python\\Edge220\\" filename2 = "C:\\Users\\chenjiaxing\\Desktop\\Python\\JPEG220\\6\\" rows = 128 columns = 128 slicenumber = 19 start_slice = 1 end_slice = 51 pixel_size = 1 spacing = 1 endx = rows - 1 endy = columns - 1 endz = end_slice - 1 sliceOrder = SliceOrder() # Now create the RenderWindow, Renderer and Interactor # ren1 = vtk.vtkRenderer() ren2 = vtk.vtkRenderer() ren3 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) renWin.AddRenderer(ren2) renWin.AddRenderer(ren3) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) greyReader = vtk.vtkJPEGReader() greyReader.SetFilePrefix(filename1) greyReader.SetFilePattern("%s%d.jpg") greyReader.SetDataSpacing(pixel_size, pixel_size, spacing) greyReader.SetDataExtent(0, endx, 0, endy, slicenumber, slicenumber) greyReader.DebugOn() greyPadder = vtk.vtkImageConstantPad() greyPadder.SetInputConnection(greyReader.GetOutputPort()) greyPadder.SetOutputWholeExtent(0, 127, 0, 127, slicenumber, slicenumber) greyPadder.SetConstant(0) greyPlane = vtk.vtkPlaneSource() greyTransform = vtk.vtkTransformPolyDataFilter() greyTransform.SetTransform(sliceOrder["hfsi"]) greyTransform.SetInputConnection(greyPlane.GetOutputPort()) greyNormals = vtk.vtkPolyDataNormals() greyNormals.SetInputConnection(greyTransform.GetOutputPort()) greyNormals.FlipNormalsOff() wllut = vtk.vtkWindowLevelLookupTable() wllut.SetWindow(255) wllut.SetLevel(128) wllut.SetTableRange(0, 255) wllut.Build() greyMapper = vtk.vtkPolyDataMapper() greyMapper.SetInputConnection(greyPlane.GetOutputPort()) greyTexture = vtk.vtkTexture() greyTexture.SetInputConnection(greyPadder.GetOutputPort()) greyTexture.SetLookupTable(wllut) greyTexture.SetColorModeToMapScalars() greyTexture.InterpolateOn() greyActor = vtk.vtkActor() greyActor.SetMapper(greyMapper) greyActor.SetTexture(greyTexture) segmentReader = vtk.vtkJPEGReader() segmentReader.SetFilePrefix(filename2) segmentReader.SetFilePattern("%s%d.jpg") segmentReader.SetDataSpacing(pixel_size, pixel_size, spacing) #segmentReader.SetDataVOI(0, endx, 0, endy, slicenumber, slicenumber) segmentReader.SetDataExtent(0, endx, 0, endy, slicenumber, slicenumber) segmentReader.Update() #imageThreshold filter LowThreshold = 1 UpperThreshold = 16 selectTissue = vtk.vtkImageThreshold() selectTissue.ThresholdBetween(LowThreshold, UpperThreshold) selectTissue.SetInValue(UpperThreshold) selectTissue.SetOutValue(0) selectTissue.SetInputConnection(segmentReader.GetOutputPort()) segmentPadder = vtk.vtkImageConstantPad() #segmentPadder.SetInputConnection(segmentReader.GetOutputPort()) segmentPadder.SetInputConnection(selectTissue.GetOutputPort()) segmentPadder.SetOutputWholeExtent(0, 127, 0, 127, slicenumber, slicenumber) segmentPadder.SetConstant(0) segmentPlane = vtk.vtkPlaneSource() segmentTransform = vtk.vtkTransformPolyDataFilter() segmentTransform.SetTransform(sliceOrder["hfsi"]) segmentTransform.SetInputConnection(segmentPlane.GetOutputPort()) segmentNormals = vtk.vtkPolyDataNormals() segmentNormals.SetInputConnection(segmentTransform.GetOutputPort()) segmentNormals.FlipNormalsOn() colorLut = CreateFrogLut() segmentMapper = vtk.vtkPolyDataMapper() segmentMapper.SetInputConnection(segmentPlane.GetOutputPort()) segmentTexture = vtk.vtkTexture() segmentTexture.SetInputConnection(segmentPadder.GetOutputPort()) segmentTexture.SetLookupTable(colorLut) segmentTexture.SetColorModeToMapScalars() segmentTexture.InterpolateOff() segmentActor = vtk.vtkActor() segmentActor.SetMapper(segmentMapper) segmentActor.SetTexture(segmentTexture) segmentOverlayActor = vtk.vtkActor() segmentOverlayActor.SetMapper(segmentMapper) segmentOverlayActor.SetTexture(segmentTexture) segmentOverlayActor.GetProperty().SetOpacity(.5) ren1.SetBackground(0, 0, 0) ren1.SetViewport(0, .5, .5, 1) renWin.SetSize(640, 480) ren1.AddActor(greyActor) ren2.SetBackground(0, 0, 0) ren2.SetViewport(.5, .5, 1, 1) ren2.AddActor(segmentActor) cam1 = vtk.vtkCamera() cam1.SetViewUp(0, -1, 0) cam1.SetPosition(0, 0, -1) ren1.SetActiveCamera(cam1) ren1.ResetCamera() cam1.SetViewUp(0, -1, 0) cam1.SetPosition(0.0554068, -0.0596001, -0.491383) cam1.SetFocalPoint(0.0554068, -0.0596001, 0) ren1.ResetCameraClippingRange() ren3.AddActor(greyActor) ren3.AddActor(segmentOverlayActor) segmentOverlayActor.SetPosition(0, 0, -.01) ren3.SetBackground(0, 0, 0) ren3.SetViewport(0, 0, 1, .5) ren2.SetActiveCamera(ren1.GetActiveCamera()) ren3.SetActiveCamera(ren1.GetActiveCamera()) renWin.Render() iren.Start()
# lookup table ''' table = vtk.vtkWindowLevelLookupTable() range_l = img1.GetOutput().GetScalarRange[0] range_h = img1.GetOutput().GetScalarRange[1] print range_h print range_l ''' colorSeries = vtk.vtkColorSeries() colorSeriesEnum = colorSeries.BREWER_QUALITATIVE_SET3 colorSeries.SetColorScheme(colorSeriesEnum) lut = vtk.vtkLookupTable() colorSeries.BuildLookupTable(lut) lut.SetNanColor(1,0,0,1) # lut = vtk.vtkWindowLevelLookupTable() lut.SetWindow(1000) lut.SetLevel(500) mapper1 = vtk.vtkDataSetMapper() mapper1.SetInputData(slicer1.GetOutput()) mapper1.SetLookupTable(lut) mapper2 = vtk.vtkDataSetMapper() mapper2.SetInputData(slicer2.GetOutput()) mapper2.SetLookupTable(lut) actor1 = vtk.vtkActor() actor1.SetMapper(mapper1)
def main(): # colors = vtk.vtkNamedColors() fileName1, fileName2, sliceNumber = get_program_parameters() sliceOrder = SliceOrder() # Now create the RenderWindow, Renderer and Interactor # ren1 = vtk.vtkRenderer() ren2 = vtk.vtkRenderer() ren3 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) renWin.AddRenderer(ren2) renWin.AddRenderer(ren3) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) greyReader = vtk.vtkMetaImageReader() greyReader.SetFileName(fileName1) greyReader.Update() greyPadder = vtk.vtkImageConstantPad() greyPadder.SetInputConnection(greyReader.GetOutputPort()) greyPadder.SetOutputWholeExtent(0, 511, 0, 511, sliceNumber, sliceNumber) greyPadder.SetConstant(0) greyPlane = vtk.vtkPlaneSource() greyTransform = vtk.vtkTransformPolyDataFilter() greyTransform.SetTransform(sliceOrder["hfsi"]) greyTransform.SetInputConnection(greyPlane.GetOutputPort()) greyNormals = vtk.vtkPolyDataNormals() greyNormals.SetInputConnection(greyTransform.GetOutputPort()) greyNormals.FlipNormalsOff() wllut = vtk.vtkWindowLevelLookupTable() wllut.SetWindow(255) wllut.SetLevel(128) wllut.SetTableRange(0, 255) wllut.Build() greyMapper = vtk.vtkPolyDataMapper() greyMapper.SetInputConnection(greyPlane.GetOutputPort()) greyTexture = vtk.vtkTexture() greyTexture.SetInputConnection(greyPadder.GetOutputPort()) greyTexture.SetLookupTable(wllut) greyTexture.SetColorModeToMapScalars() greyTexture.InterpolateOn() greyActor = vtk.vtkActor() greyActor.SetMapper(greyMapper) greyActor.SetTexture(greyTexture) segmentReader = vtk.vtkMetaImageReader() segmentReader.SetFileName(fileName2) segmentReader.Update() segmentPadder = vtk.vtkImageConstantPad() segmentPadder.SetInputConnection(segmentReader.GetOutputPort()) segmentPadder.SetOutputWholeExtent(0, 511, 0, 511, sliceNumber, sliceNumber) segmentPadder.SetConstant(0) segmentPlane = vtk.vtkPlaneSource() segmentTransform = vtk.vtkTransformPolyDataFilter() segmentTransform.SetTransform(sliceOrder["hfsi"]) segmentTransform.SetInputConnection(segmentPlane.GetOutputPort()) segmentNormals = vtk.vtkPolyDataNormals() segmentNormals.SetInputConnection(segmentTransform.GetOutputPort()) segmentNormals.FlipNormalsOn() colorLut = CreateFrogLut() segmentMapper = vtk.vtkPolyDataMapper() segmentMapper.SetInputConnection(segmentPlane.GetOutputPort()) segmentTexture = vtk.vtkTexture() segmentTexture.SetInputConnection(segmentPadder.GetOutputPort()) segmentTexture.SetLookupTable(colorLut) segmentTexture.SetColorModeToMapScalars() segmentTexture.InterpolateOff() segmentActor = vtk.vtkActor() segmentActor.SetMapper(segmentMapper) segmentActor.SetTexture(segmentTexture) segmentOverlayActor = vtk.vtkActor() segmentOverlayActor.SetMapper(segmentMapper) segmentOverlayActor.SetTexture(segmentTexture) segmentOverlayActor.GetProperty().SetOpacity(.5) ren1.SetBackground(0, 0, 0) ren1.SetViewport(0, .5, .5, 1) renWin.SetSize(640, 480) ren1.AddActor(greyActor) ren2.SetBackground(0, 0, 0) ren2.SetViewport(.5, .5, 1, 1) ren2.AddActor(segmentActor) cam1 = vtk.vtkCamera() cam1.SetViewUp(0, -1, 0) cam1.SetPosition(0, 0, -1) ren1.SetActiveCamera(cam1) ren1.ResetCamera() cam1.SetViewUp(0, -1, 0) cam1.SetPosition(0.0554068, -0.0596001, -0.491383) cam1.SetFocalPoint(0.0554068, -0.0596001, 0) ren1.ResetCameraClippingRange() ren3.AddActor(greyActor) ren3.AddActor(segmentOverlayActor) segmentOverlayActor.SetPosition(0, 0, -.01) ren3.SetBackground(0, 0, 0) ren3.SetViewport(0, 0, 1, .5) ren2.SetActiveCamera(ren1.GetActiveCamera()) ren3.SetActiveCamera(ren1.GetActiveCamera()) renWin.Render() iren.Start()
def main(): # colors = vtk.vtkNamedColors() fileName1 = "C:\\Users\\chenjiaxing\\Desktop\\VTKTextBook\\Data\\Frog\\frog" fileName2 = "C:\\Users\\chenjiaxing\\Desktop\\VTKTextBook\\Data\\Frog\\frogTissue" rows = 500 columns = 470 sliceNumber = 33 start_slice = 1 end_slice = 138 pixel_size = 1 spacing = 1.5 endX = rows - 1 endY = columns - 1 endZ = end_slice - 1 sliceOrder = SliceOrder() # Now create the RenderWindow, Renderer and Interactor # ren1 = vtk.vtkRenderer() ren2 = vtk.vtkRenderer() ren3 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) renWin.AddRenderer(ren2) renWin.AddRenderer(ren3) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) #greyReader = vtk.vtkMetaImageReader() #greyReader.SetFileName(fileName1) #greyReader.Update() greyReader = vtk.vtkPNMReader() greyReader.SetFilePrefix(fileName1) greyReader.SetDataSpacing(pixel_size, pixel_size, spacing) greyReader.SetDataVOI(0, endX, 0, endY, sliceNumber, sliceNumber) greyReader.SetDataExtent(0, endX, 0, endY, sliceNumber, sliceNumber) greyReader.DebugOn() greyPadder = vtk.vtkImageConstantPad() greyPadder.SetInputConnection(greyReader.GetOutputPort()) greyPadder.SetOutputWholeExtent(0, 511, 0, 511, 0, 0) greyPadder.SetConstant(0) greyPlane = vtk.vtkPlaneSource() greyTransform = vtk.vtkTransformPolyDataFilter() greyTransform.SetTransform(sliceOrder["hfsi"]) greyTransform.SetInputConnection(greyPlane.GetOutputPort()) greyNormals = vtk.vtkPolyDataNormals() greyNormals.SetInputConnection(greyTransform.GetOutputPort()) greyNormals.FlipNormalsOff() wllut = vtk.vtkWindowLevelLookupTable() wllut.SetWindow(255) wllut.SetLevel(128) wllut.SetTableRange(0, 255) wllut.Build() greyMapper = vtk.vtkPolyDataMapper() greyMapper.SetInputConnection(greyPlane.GetOutputPort()) greyTexture = vtk.vtkTexture() greyTexture.SetInputConnection(greyPadder.GetOutputPort()) greyTexture.SetLookupTable(wllut) greyTexture.SetColorModeToMapScalars() greyTexture.InterpolateOn() greyActor = vtk.vtkActor() greyActor.SetMapper(greyMapper) greyActor.SetTexture(greyTexture) segmentReader = vtk.vtkPNMReader() segmentReader.SetFilePrefix(fileName2) segmentReader.SetDataSpacing(pixel_size, pixel_size, spacing) segmentReader.SetDataVOI(0, endX, 0, endY, sliceNumber, sliceNumber) segmentReader.SetDataExtent(0, endX, 0, endY, sliceNumber, sliceNumber) segmentReader.Update() segmentPadder = vtk.vtkImageConstantPad() segmentPadder.SetInputConnection(segmentReader.GetOutputPort()) segmentPadder.SetOutputWholeExtent(0, 511, 0, 511, 0, 0) segmentPadder.SetConstant(0) segmentPlane = vtk.vtkPlaneSource() segmentTransform = vtk.vtkTransformPolyDataFilter() segmentTransform.SetTransform(sliceOrder["hfsi"]) segmentTransform.SetInputConnection(segmentPlane.GetOutputPort()) segmentNormals = vtk.vtkPolyDataNormals() segmentNormals.SetInputConnection(segmentTransform.GetOutputPort()) segmentNormals.FlipNormalsOn() colorLut = CreateFrogLut() segmentMapper = vtk.vtkPolyDataMapper() segmentMapper.SetInputConnection(segmentPlane.GetOutputPort()) segmentTexture = vtk.vtkTexture() segmentTexture.SetInputConnection(segmentPadder.GetOutputPort()) segmentTexture.SetLookupTable(colorLut) segmentTexture.SetColorModeToMapScalars() segmentTexture.InterpolateOff() segmentActor = vtk.vtkActor() segmentActor.SetMapper(segmentMapper) segmentActor.SetTexture(segmentTexture) segmentOverlayActor = vtk.vtkActor() segmentOverlayActor.SetMapper(segmentMapper) segmentOverlayActor.SetTexture(segmentTexture) segmentOverlayActor.GetProperty().SetOpacity(.5) ren1.SetBackground(0, 0, 0) ren1.SetViewport(0, .5, .5, 1) renWin.SetSize(640, 480) ren1.AddActor(greyActor) ren2.SetBackground(0, 0, 0) ren2.SetViewport(.5, .5, 1, 1) ren2.AddActor(segmentActor) cam1 = vtk.vtkCamera() cam1.SetViewUp(0, -1, 0) cam1.SetPosition(0, 0, -1) ren1.SetActiveCamera(cam1) ren1.ResetCamera() cam1.SetViewUp(0, -1, 0) cam1.SetPosition(0.0554068, -0.0596001, -0.491383) cam1.SetFocalPoint(0.0554068, -0.0596001, 0) ren1.ResetCameraClippingRange() ren3.AddActor(greyActor) ren3.AddActor(segmentOverlayActor) segmentOverlayActor.SetPosition(0, 0, -.01) ren3.SetBackground(0, 0, 0) ren3.SetViewport(0, 0, 1, .5) ren2.SetActiveCamera(ren1.GetActiveCamera()) ren3.SetActiveCamera(ren1.GetActiveCamera()) renWin.Render() iren.Start()
def __init__(self, parent, *args, **kw): VTKPlotWindowGUI.VTKPlotWindowGUI.__init__(self, parent) # ------------------------------------------------------------------------------------ # Set up some zope event handlers # ------------------------------------------------------------------------------------ component.provideHandler(self.OnImageChangeEvent) self._voxel_volume_set = False self._voxel_volume = 1.0 if ('bar' in kw): self._usebar = True else: self._usebar = False self._scale = 1.0 self._unit = kw.get('units', 'pixels') self._dragging = 0 # is this needed? self._x0 = None self._y0 = None self._x1 = None self._y1 = None self._select_x0 = None self._select_x1 = None self._select_i0 = None self._select_i1 = None self.xdata = [] self.ydata = [] self._line = [None, None] self._liney = None self._title = "This is the title" self.filename = '' self._xlabel = "" self._ylabel = "" self._usesymbols = False # indicates whether nearest data point should be highlighted or not self._use_highlight_data = True self._isROI = False self._linelength = None self._inputname = "" self._highlight_visible = False self.__shortname__ = 'VTKPlot' self._otsu_threshold = None self._otsu_marker = None self._unit_scalings = {'pixels': 1.0, 'mm': 1.0, 'wavelength': 1.0} self.plot_data = None self.legend = None if ('scale' in kw): self._scale = float(kw['scale']) if ('title' in kw): self._title = kw['title'] if ('xlabel' in kw): self._xlabel = kw['xlabel'] if ('ylabel' in kw): self._ylabel = kw['ylabel'] # get icon factory self._stockicons = StockItems.StockIconFactory() # create info panel - it'll depend on whether this is a line plot or # histogram window self.lower_panel = self.CreateLowerPanel() self.GetSizer().Add(self.lower_panel, 0, wx.EXPAND, 5) # create a matplotlib panel self.dpi = 100 self.fig = Figure((3.0, 3.0), dpi=self.dpi) # self.fig.subplots_adjust(left=0.07, right=0.97, bottom=0.08, top=0.95) self.fig.subplots_adjust(left=0.0, right=1, bottom=0.0, top=1) self.axes = self.fig.add_subplot(111) # default labels self.axes.set_title(self._title, size=10) self.axes.set_ylabel(self._ylabel, size=8) self.line_tool = None # A red, horizontal line tool self.end_markers = None # Markers that go at the end of each line self.data_markers = None # Marks that indicate nearest data point pylab.setp(self.axes.get_xticklabels(), fontsize=8) pylab.setp(self.axes.get_yticklabels(), fontsize=8) sizer = wx.BoxSizer(wx.VERTICAL) # create canvas for plot widget self.canvas = FigCanvas(self.m_panelMatplotPanel, -1, self.fig) self.m_panelMatplotPanel.SetSizer(sizer) self.m_panelMatplotPanel.Fit() # activate interactive navigation self.toolbar = MicroViewNavigationToolbar(self.canvas) self.toolbar.Realize() tw, th = self.toolbar.GetSizeTuple() fw, fh = self.canvas.GetSizeTuple() self.toolbar.SetSize(wx.Size(fw, th)) sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND) sizer.Add(self.canvas, 1, wx.EXPAND) self.toolbar.update() # adjust toolbar self.EnableToolbar() # wire up events self.canvas.mpl_connect('motion_notify_event', self.MouseMoveEvent) self.canvas.mpl_connect('key_press_event', self.KeyPressEvent) self.canvas.mpl_connect('figure_leave_event', self.LeaveCanvasEvent) # wire up events here self.toolbar.Bind( wx.EVT_TOOL, self.SaveData, id=self.toolbar.SAVE_DATA) self.toolbar.Bind( wx.EVT_TOOL, self.SaveSnapShot, id=self.toolbar.SAVE_SNAPSHOT) self.toolbar.Bind( wx.EVT_TOOL, self.symbolsOnOff, id=self.toolbar.VIEW_SYMBOLS) # self.toolbar.Bind(wx.EVT_TOOL, self.Reset, id=self.toolbar.RESET_VIEW) self.toolbar.Bind( wx.EVT_TOOL, self.AutoThreshold, id=self.toolbar.AUTO_THRESHOLD) self.toolbar.Bind( wx.EVT_TOOL, self.onCopyHighlightToolbarButton, id=self.toolbar.COPY_HIGHLIGHT) self.toolbar.Bind( wx.EVT_TOOL, self.onShowHighlightToolbarToogle, id=self.toolbar.SHOW_HIGHLIGHT) self.toolbar.Bind( wx.EVT_TOOL, self.NearestDataSymbolsOnOff, id=self.toolbar.VIEW_NEARESTDATA) self.toolbar.Bind( wx.EVT_TOOL, self.select_roi, id=self.toolbar.SELECT_ROI) # listen to size events self.Bind(wx.EVT_PAINT, self.OnPaint) self._plotData = None # This table is used to make the input invisible self._wlTableInvisible = vtk.vtkWindowLevelLookupTable() self._wlTableInvisible.SetSaturationRange(0, 0) self._wlTableInvisible.SetHueRange(0, 0) self._wlTableInvisible.SetValueRange(0, 1) self._wlTableInvisible.SetNumberOfColors(2) self._wlTableInvisible.SetTableValue(0, 0.0, 0.0, 0.0, 0.0) self._wlTableInvisible.SetTableValue(1, 0.0, 0.0, 0.0, 0.0) self._wlTableInvisible.Build() # Invoke an event to react to currently loaded image mv = component.getUtility(IMicroViewMainFrame) current_image = mv.GetCurrentImageIndex() number_images_displayed = mv.GetNumberOfImagesCurrentlyLoaded() title = mv.GetCurrentImageTitle() self.OnImageChangeEvent(CurrentImageChangeEvent( current_image, number_images_displayed, title))