def initImages(self): global show_height, show_width, show_thickness if self.slice_assembly: collectioniteractor1 = vtk.vtkCollectionIterator() collectioniteractor1.SetCollection(self.slice_assembly) collectioniteractor1.InitTraversal() for i in xrange(self.slice_assembly.GetNumberOfItems()): collectioniteractor1.GetCurrentObject().GetViewProp( ).SetVisibility(False) collectioniteractor1.GoToNextItem() if self.ren1AssemblyList[self.line_index]: self.line_assembly_list = self.ren1AssemblyList[self.line_index] else: self.line_assembly_list = [None] * len(self.lines[self.line_index]) self.ren1AssemblyList[self.line_index] = self.line_assembly_list t0 = time.clock() self.images = [None] * len(self.lines[self.line_index]) self.roi_list = [] self.image_slice = 0 self.countRoiInLine() if self.line_assembly_list[self.image_slice]: self.slice_assembly = self.line_assembly_list[self.image_slice] collectioniteractor1 = vtk.vtkCollectionIterator() collectioniteractor1.SetCollection(self.slice_assembly) collectioniteractor1.InitTraversal() for i in xrange(self.slice_assembly.GetNumberOfItems()): collectioniteractor1.GetCurrentObject().GetViewProp( ).SetVisibility(True) collectioniteractor1.GoToNextItem() else: self.slice_assembly = vtk.vtkAssemblyPath() self.line_assembly_list[self.image_slice] = self.slice_assembly self.load_mx_num = 100 if self.load_mx_num > len(self.lines[self.line_index]): real_load_num = len(self.lines[self.line_index]) else: real_load_num = self.load_mx_num # print real_load_num, len(self.lines[self.line_index]) for i in xrange(real_load_num): x0, y0, z0 = self.lines[self.line_index][i][2:5] x_s = int((x0 * 10 / 3 - show_width / 2)) y_s = int((y0 * 10 / 3 - show_height / 2)) z_s = int(z0) tmp_image = np.zeros((show_height, show_width), dtype=np.uint16) for j in xrange((-show_thickness), show_thickness + 1): z_i = z_s + j if z_i < 0: z_i = 0 image_roi = (x_s, y_s, z_i, show_width, show_height, 1) image_j = self.loadImageFromMostd(image_roi) tmp_image = np.maximum(tmp_image, image_j) self.images[i] = tmp_image print time.clock() - t0
def sliceForward(self, obj, e): global swc, main_win, mostd_src, show_width, show_height cur_line_index = main_win.line_index cur_line = main_win.lines[cur_line_index] cur_line_length = len(cur_line) cur_slice = main_win.image_slice if cur_slice + 1 < cur_line_length: next_slice = cur_slice + 1 main_win.show_image = main_win.getImageFromLoaded(next_slice) main_win.image_str = main_win.show_image.tostring() main_win.im_importer.CopyImportVoidPointer(main_win.image_str, len(main_win.image_str)) main_win.im_importer.Modified() main_win.im_importer.Update() main_win.imageLevelFilter.SetInputConnection( main_win.im_importer.GetOutputPort()) main_win.imageLevelFilter.Modified() main_win.imageLevelFilter.Update() main_win.image_actor.SetInput( main_win.imageLevelFilter.GetOutput()) main_win.image_actor.Modified() main_win.image_slice = next_slice if (main_win.image_slice < 5) or (main_win.image_slice > len( main_win.lines[main_win.line_index]) - 5): main_win.sphere_actor_ren2.SetVisibility(False) else: main_win.sphere_actor_ren2.SetVisibility(True) if main_win.slice_assembly: collectionInteractor1 = vtk.vtkCollectionIterator() collectionInteractor1.SetCollection(main_win.slice_assembly) collectionInteractor1.InitTraversal() for i in xrange(main_win.slice_assembly.GetNumberOfItems()): collectionInteractor1.GetCurrentObject().GetViewProp( ).SetVisibility(False) collectionInteractor1.GoToNextItem() main_win.slice_assembly = main_win.line_assembly_list[ main_win.image_slice] if main_win.slice_assembly: collectionInteractor1 = vtk.vtkCollectionIterator() collectionInteractor1.SetCollection(main_win.slice_assembly) collectionInteractor1.InitTraversal() for i in xrange(main_win.slice_assembly.GetNumberOfItems()): collectionInteractor1.GetCurrentObject().GetViewProp( ).SetVisibility(True) collectionInteractor1.GoToNextItem() else: main_win.slice_assembly = vtk.vtkAssemblyPath() main_win.line_assembly_list[ main_win.image_slice] = main_win.slice_assembly main_win.vtk_widget.Render() main_win.renderActorsInSlice() else: next_slice = cur_slice
def initImages(self): global show_height, show_width, show_thickness if self.slice_assembly: collectioniteractor1 = vtk.vtkCollectionIterator() collectioniteractor1.SetCollection(self.slice_assembly) collectioniteractor1.InitTraversal() for i in xrange(self.slice_assembly.GetNumberOfItems()): collectioniteractor1.GetCurrentObject().GetViewProp().SetVisibility(False) collectioniteractor1.GoToNextItem() if self.ren1AssemblyList[self.line_index]: self.line_assembly_list = self.ren1AssemblyList[self.line_index] else: self.line_assembly_list = [None] * len(self.lines[self.line_index]) self.ren1AssemblyList[self.line_index] = self.line_assembly_list t0 = time.clock() self.images = [None] * len(self.lines[self.line_index]) self.roi_list = [] self.image_slice = 0 self.countRoiInLine() if self.line_assembly_list[self.image_slice]: self.slice_assembly = self.line_assembly_list[self.image_slice] collectioniteractor1 = vtk.vtkCollectionIterator() collectioniteractor1.SetCollection(self.slice_assembly) collectioniteractor1.InitTraversal() for i in xrange(self.slice_assembly.GetNumberOfItems()): collectioniteractor1.GetCurrentObject().GetViewProp().SetVisibility(True) collectioniteractor1.GoToNextItem() else: self.slice_assembly = vtk.vtkAssemblyPath() self.line_assembly_list[self.image_slice] = self.slice_assembly self.load_mx_num = 100 if self.load_mx_num > len(self.lines[self.line_index]): real_load_num = len(self.lines[self.line_index]) else: real_load_num = self.load_mx_num # print real_load_num, len(self.lines[self.line_index]) for i in xrange(real_load_num): x0, y0, z0 = self.lines[self.line_index][i][2 : 5] x_s = int((x0 * 10 / 3 - show_width / 2)) y_s = int((y0 * 10 / 3 - show_height / 2)) z_s = int(z0) tmp_image = np.zeros((show_height, show_width), dtype=np.uint16) for j in xrange((- show_thickness), show_thickness + 1): z_i = z_s + j if z_i < 0: z_i = 0 image_roi = (x_s, y_s, z_i, show_width, show_height, 1) image_j = self.loadImageFromMostd(image_roi) tmp_image = np.maximum(tmp_image, image_j); self.images[i] = tmp_image print time.clock() - t0
def loadActorsFromFile(self): global show_height, show_width file1 = open(self.bouttons_src) for line_i in file1: list_i = line_i.strip().split(' ') line_index = int(list_i[0]) slice_index = int(list_i[1]) x1, y1, z1 = self.lines[line_index][slice_index][2:5] x0 = int(x1 * 10 / 3 - show_width / 2) y0 = int(y1 * 10 / 3 - show_height / 2) x = float(list_i[2]) y = float(list_i[3]) z = float(list_i[4]) index_array = vtk.vtkIntArray() index_array.SetNumberOfComponents(1) index_array.InsertNextValue(line_index) index_array.InsertNextValue(slice_index) location_array = vtk.vtkFloatArray() location_array.SetNumberOfComponents(3) location_array.InsertNextTupleValue((x, y, z)) isNewArray = vtk.vtkIntArray() isNewArray.SetNumberOfComponents(1) isNewArray.InsertNextValue(0) index_array.SetName('Indices') location_array.SetName('Locations') isNewArray.SetName('IsNew') actor1 = MyActor() actor1.maFieldData.AddArray(index_array) actor1.maFieldData.AddArray(location_array) actor1.maFieldData.AddArray(isNewArray) actor1.GetProperty().SetColor(255.0, 0, 255.0) sphere1 = vtk.vtkSphereSource() sphere1.SetCenter(x - x0, y - y0, 0) sphere1.SetRadius(self.sphere_radius_ren1) mapper1 = vtk.vtkPolyDataMapper() mapper1.SetInputConnection(sphere1.GetOutputPort()) actor1.SetMapper(mapper1) self.ren1_sphere_actors.AddItem(actor1) matrix1 = vtk.vtkMatrix4x4() actor1.SetVisibility(False) if self.ren1AssemblyList[line_index] is None: self.ren1AssemblyList[line_index] = [None] * len( self.lines[line_index]) if self.ren1AssemblyList[line_index][slice_index] is None: assembly1 = vtk.vtkAssemblyPath() self.ren1AssemblyList[line_index][slice_index] = assembly1 self.ren1AssemblyList[line_index][slice_index].AddNode( actor1, matrix1) self.ren1.AddActor(actor1) file1.close()
def loadActorsFromFile(self): global show_height, show_width file1 = open(self.bouttons_src) for line_i in file1: list_i = line_i.strip().split('\t') line_index = int(list_i[0]) slice_index = int(list_i[1]) x1, y1, z1 = self.lines[line_index][slice_index][2 : 5] x0 = int(x1 * 10 / 3 - show_width / 2) y0 = int(y1 * 10 / 3 - show_height / 2) x = float(list_i[2]) y = float(list_i[3]) z = float(list_i[4]) index_array = vtk.vtkIntArray() index_array.SetNumberOfComponents(1) index_array.InsertNextValue(line_index) index_array.InsertNextValue(slice_index) location_array = vtk.vtkFloatArray() location_array.SetNumberOfComponents(3) location_array.InsertNextTupleValue((x, y, z)) isNewArray = vtk.vtkIntArray() isNewArray.SetNumberOfComponents(1) isNewArray.InsertNextValue(0) index_array.SetName('Indices') location_array.SetName('Locations') isNewArray.SetName('IsNew') actor1 = MyActor() actor1.maFieldData.AddArray(index_array) actor1.maFieldData.AddArray(location_array) actor1.maFieldData.AddArray(isNewArray) actor1.GetProperty().SetColor(255.0, 0, 255.0) sphere1 = vtk.vtkSphereSource() sphere1.SetCenter(x - x0, y - y0, 0) sphere1.SetRadius(self.sphere_radius_ren1) mapper1 = vtk.vtkPolyDataMapper() mapper1.SetInputConnection(sphere1.GetOutputPort()) actor1.SetMapper(mapper1) self.ren1_sphere_actors.AddItem(actor1) matrix1 = vtk.vtkMatrix4x4() actor1.SetVisibility(False) if self.ren1AssemblyList[line_index] is None: self.ren1AssemblyList[line_index] = [None] * len(self.lines[line_index]) if self.ren1AssemblyList[line_index][slice_index] is None: assembly1 = vtk.vtkAssemblyPath() self.ren1AssemblyList[line_index][slice_index] = assembly1 self.ren1AssemblyList[line_index][slice_index].AddNode(actor1, matrix1) self.ren1.AddActor(actor1) file1.close()
def sliceForward(self, obj, e): global swc, main_win, mostd_src, show_width, show_height cur_line_index = main_win.line_index cur_line = main_win.lines[cur_line_index] cur_line_length = len(cur_line) cur_slice = main_win.image_slice if cur_slice + 1 < cur_line_length: next_slice = cur_slice + 1 main_win.show_image = main_win.getImageFromLoaded(next_slice) main_win.image_str = main_win.show_image.tostring() main_win.im_importer.CopyImportVoidPointer(main_win.image_str, len(main_win.image_str)) main_win.im_importer.Modified() main_win.im_importer.Update() main_win.imageLevelFilter.SetInputConnection(main_win.im_importer.GetOutputPort()) main_win.imageLevelFilter.Modified() main_win.imageLevelFilter.Update() main_win.image_actor.SetInput(main_win.imageLevelFilter.GetOutput()) main_win.image_actor.Modified() main_win.image_slice = next_slice if (main_win.image_slice < 5) or (main_win.image_slice > len(main_win.lines[main_win.line_index]) - 5): main_win.sphere_actor_ren2.SetVisibility(False) else: main_win.sphere_actor_ren2.SetVisibility(True) if main_win.slice_assembly: collectionInteractor1 = vtk.vtkCollectionIterator() collectionInteractor1.SetCollection(main_win.slice_assembly) collectionInteractor1.InitTraversal() for i in xrange(main_win.slice_assembly.GetNumberOfItems()): collectionInteractor1.GetCurrentObject().GetViewProp().SetVisibility(False) collectionInteractor1.GoToNextItem() main_win.slice_assembly = main_win.line_assembly_list[main_win.image_slice] if main_win.slice_assembly: collectionInteractor1 = vtk.vtkCollectionIterator() collectionInteractor1.SetCollection(main_win.slice_assembly) collectionInteractor1.InitTraversal() for i in xrange(main_win.slice_assembly.GetNumberOfItems()): collectionInteractor1.GetCurrentObject().GetViewProp().SetVisibility(True) collectionInteractor1.GoToNextItem() else: main_win.slice_assembly = vtk.vtkAssemblyPath() main_win.line_assembly_list[main_win.image_slice] = main_win.slice_assembly main_win.vtk_widget.Render() main_win.renderActorsInSlice() else: next_slice = cur_slice
import vtk c = vtk.vtkCollection() i = vtk.vtkCollectionIterator() a1 = vtk.vtkActor() a2 = vtk.vtkActor() c.AddItem(a1) c.AddItem(a2) c.InitTraversal() ai = c.GetNextItemAsObject() matrix1 = vtk.vtkMatrix4x4() print matrix1 s = vtk.vtkAssemblyPath() s.AddNode(a1, matrix1) s.AddNode(a2, matrix1) s.InitTraversal() ai = s.GetNextNode() while ai: ai = s.GetNextNode()
R = vtk.vtkMath.Random(0.0, 1.0) G = vtk.vtkMath.Random(0.0, 1.0) B = vtk.vtkMath.Random(0.0, 1.0) sphere1.SetCenter(x, y, z) sphere1.SetRadius(r) mapper1 = vtk.vtkPolyDataMapper() mapper1.SetInput(sphere1.GetOutput()) actor1 = vtk.vtkActor() actor1.SetMapper(mapper1) actor1.GetProperty().SetColor(R, G, B) return actor1 if __name__ == '__main__': global iren, ren, aspath aspath = vtk.vtkAssemblyPath() ren = vtk.vtkRenderer() iren = vtk.vtkRenderWindowInteractor() renWin = vtk.vtkRenderWindow() iren.SetRenderWindow(renWin) renWin.AddRenderer(ren) assembly1 = vtk.vtkAssembly() matrix1 = vtk.vtkMatrix4x4() print matrix1 for i in xrange(30): actor_i = creatActor() ren.AddActor(actor_i) aspath.AddNode(actor_i, matrix1) iren.AddObserver('LeftButtonPressEvent', pickActor) iren.Initialize() iren.Start()