def test_graph_from_image(visual=False): im = imread("segmentation.inr.gz") graph = graph_from_image(im) if visual: Viewer.display( graph2pglscene(graph, graph.vertex_property('barycenter'), graph.vertex_property('L1')))
def test_graph_from_simple_image(visual=False): im = np.array([[1, 2, 7, 7, 1, 1], [1, 6, 5, 7, 3, 3], [2, 2, 1, 7, 3, 3], [1, 1, 1, 4, 1, 1]]) borders = SpatialImageAnalysis(im).cells_in_image_margins() print '**', SpatialImageAnalysis(im).boundingbox(real=False) #~ print borders #~ print np.unique(im) graph = graph_from_image(im) #~ print list(graph.vertices()) #~ print map(graph.edge_vertices,graph.edges()) #~ print list(graph.vertex_property_names()) #~ print list(graph.edge_property_names()) for propname in graph.vertex_property_names(): print propname print graph.vertex_property(propname) #~ for propname in graph.edge_property_names(): #~ print propname #~ print [(i,graph.edge_vertices(i),j) for i,j in graph.edge_property(propname).iteritems()] if visual: Viewer.display( graph2pglscene(graph, graph.vertex_property('barycenter'), graph.vertex_property('border')))
def test_graph_from_simple_image(visual = False): im = np.array([[1, 2, 7, 7, 1, 1], [1, 6, 5, 7, 3, 3], [2, 2, 1, 7, 3, 3], [1, 1, 1, 4, 1, 1]]) borders = SpatialImageAnalysis(im).cells_in_image_margins() print '**', SpatialImageAnalysis(im).boundingbox(real=False) #~ print borders #~ print np.unique(im) graph = graph_from_image(im) #~ print list(graph.vertices()) #~ print map(graph.edge_vertices,graph.edges()) #~ print list(graph.vertex_property_names()) #~ print list(graph.edge_property_names()) for propname in graph.vertex_property_names(): print propname print graph.vertex_property(propname) #~ for propname in graph.edge_property_names(): #~ print propname #~ print [(i,graph.edge_vertices(i),j) for i,j in graph.edge_property(propname).iteritems()] if visual : Viewer.display(graph2pglscene(graph,graph.vertex_property('barycenter'),graph.vertex_property('border')))
def link_polydata_triangle_cells(polydata,img,img_graph=None): if img_graph is None: from openalea.image.algo.graph_from_image import graph_from_image img_graph = graph_from_image(img,spatio_temporal_properties=['barycenter','volume'],ignore_cells_at_stack_margins = False,property_as_real=True) polydata_cell_data = polydata.GetCellData().GetArray(0) start_time = time() print " --> Listing points" polydata_points = np.array([polydata.GetPoints().GetPoint(i) for i in xrange(polydata.GetPoints().GetNumberOfPoints())]) end_time = time() print " <-- Listing points [",end_time - start_time,"s]" start_time = time() print " --> Merging points" point_ids = {} for p in xrange(polydata.GetPoints().GetNumberOfPoints()): point_ids[tuple(polydata_points[p])] = [] for p in xrange(polydata.GetPoints().GetNumberOfPoints()): point_ids[tuple(polydata_points[p])] += [p] unique_points = array_unique(polydata_points) n_points = unique_points.shape[0] point_unique_id = {} for p in xrange(n_points): for i in point_ids[tuple(unique_points[p])]: point_unique_id[i] = p end_time = time() print " <-- Merging points [",end_time - start_time,"s]" triangle_cell_start_time = time() print " --> Listing triangles" print " - ",polydata.GetNumberOfCells()," triangles" polydata_triangles = np.sort([[polydata.GetCell(t).GetPointIds().GetId(i) for i in xrange(3)] for t in xrange(polydata.GetNumberOfCells())]) print " - ",array_unique(polydata_triangles).shape[0]," unique triangles" # polydata_triangle_points = [polydata.GetCell(t).GetPointIds() for t in xrange(polydata.GetNumberOfCells())] # polydata_triangles = np.sort([[triangle_points.GetId(i) for i in xrange(3)] for triangle_points in polydata_triangle_points]) polydata_triangles = np.sort(array_dict(point_unique_id).values(polydata_triangles)) print " - ",array_unique(polydata_triangles).shape[0]," unique triangles (merged vertices)" triangle_cell_end_time = time() print " <-- Listing triangles [",triangle_cell_end_time - triangle_cell_start_time,"s]" raw_input() triangle_cell_start_time = time() print " --> Initializing triangle cells" triangle_cells = {} for t in xrange(polydata.GetNumberOfCells()): triangle_cells[tuple(polydata_triangles[t])] = [] for i in xrange(10): if t == (i*polydata.GetNumberOfCells())/10: print " --> Initializing triangle cells (",10*i,"%)" triangle_cell_end_time = time() print " <-- Initializing triangle cells [",triangle_cell_end_time - triangle_cell_start_time,"s]" triangle_cell_start_time = time() for t in xrange(polydata.GetNumberOfCells()): triangle_cells[tuple(polydata_triangles[t])] += list(polydata_cell_data.GetTuple(t)) for i in xrange(100): if t == (i*polydata.GetNumberOfCells())/100: triangle_cell_end_time = time() print " --> Listing triangle cells (",i,"%) [",(triangle_cell_end_time-triangle_cell_start_time)/(polydata.GetNumberOfCells()/100.),"s]" triangle_cell_start_time = time() triangle_cell_start_time = time() print " --> Cleaning triangle cells" for t in xrange(polydata.GetNumberOfCells()): triangle_cells[tuple(polydata_triangles[t])] = np.unique(triangle_cells[tuple(polydata_triangles[t])]) for i in xrange(10): if t == (i*polydata.GetNumberOfCells())/10: print " --> Cleaning triangle cells (",10*i,"%)" triangle_cell_end_time = time() print " <-- Cleaning triangle cells [",triangle_cell_end_time - triangle_cell_start_time,"s]" # triangle_cell_start_time = time() # print " --> Listing triangle cells" # triangle_cell = np.array([polydata_cell_data.GetTuple(t)[0] for t in xrange(polydata.GetNumberOfCells())],np.uint16) # triangle_cell_end_time = time() # print " <-- Listing triangle cells [",triangle_cell_end_time - triangle_cell_start_time,"s]" # triangle_cells = {} # for t in xrange(polydata.GetNumberOfCells()): # triangle_cells[t] = [] # for c in considered_cells: # cell_triangles = np.where(triangle_cell == c)[0] # for t in cell_triangles: # triangle_cells[t] += [c] # print " Cell ",c," : ",cell_triangles.shape[0]," triangles" # neighbor_triangles = where_list(triangle_cell,list(img_graph.neighbors(c)))[0] # neighbor_triangle_matching = vq(polydata_triangles[cell_triangles],polydata_triangles[neighbor_triangles]) # cell_double_triangles = neighbor_triangles[neighbor_triangle_matching[0][np.where(neighbor_triangle_matching[1]==0)[0]]] # for t in cell_double_triangles: # triangle_cells[t] += [c] # print triangle_cells.values() # raw_input() # unique_triangles,unique_triangle_rows = array_unique(polydata_triangles,return_index=True) # n_triangles = unique_triangles.shape[0] # triangle_unique = array_dict(np.arange(n_triangles),unique_triangle_rows) # triangle_id = (polydata_triangles * np.array([contour.GetOutput().GetPoints().GetNumberOfPoints(),1,1./contour.GetOutput().GetPoints().GetNumberOfPoints()])).sum(axis=1) # unique_triangle_id = (unique_triangles * np.array([contour.GetOutput().GetPoints().GetNumberOfPoints(),1,1./contour.GetOutput().GetPoints().GetNumberOfPoints()])).sum(axis=1) # triangle_number = nd.sum(np.ones_like(triangle_id),triangle_id,index=triangle_id) # double_triangle_rows = np.where(triangle_number==2)[0] # print " ",double_triangle_rows.shape[0]," double triangles / ",contour.GetOutput().GetNumberOfCells() # double_triangles = polydata_triangles[double_triangle_rows] # unique_triangle_number = nd.sum(np.ones_like(triangle_id),triangle_id,index=unique_triangle_id) # double_unique_triangle_rows = np.where(unique_triangle_number==2)[0] # double_unique_triangles = unique_triangles[double_unique_triangle_rows] # triangle_triangle_matching = (vq(double_triangles,double_unique_triangles)[0]) # triangle_triangle_matching = array_dict(double_unique_triangle_rows[triangle_triangle_matching],double_triangle_rows) # unique_triangle_cells = {} # for t in xrange(n_triangles): # unique_triangle_cells[t] = [] # for t in xrange(polydata.GetNumberOfCells()): # if triangle_number[t] == 1: # unique_triangle_cells[triangle_unique[t]] += list(polydata_cell_data.GetTuple(t)) # elif triangle_number[t] == 2: # unique_triangle_cells[triangle_triangle_matching[t]] += list(polydata_cell_data.GetTuple(t)) # triangle_cells = {} # for t in xrange(polydata.GetNumberOfCells()): # if triangle_number[t] == 1: # triangle_cells[t] = np.array(unique_triangle_cells[triangle_unique[t]],np.uint16) # elif triangle_number[t] == 2: # triangle_cells[t] = np.array(unique_triangle_cells[triangle_triangle_matching[t]],np.uint16) cell_data = vtk.vtkFloatArray() cell_data.SetNumberOfComponents(2) cell_data.SetNumberOfTuples(polydata.GetNumberOfCells()) for t in xrange(polydata.GetNumberOfCells()): triangle_key = tuple(polydata_triangles[t]) if len(triangle_cells[triangle_key]) == 2: cell_data.SetTuple(t,np.sort(triangle_cells[triangle_key])) else: cell_data.SetTuple(t,np.concatenate([triangle_cells[triangle_key],[1]])) polydata.GetCellData().SetScalars(cell_data) return triangle_cells
def test_graph_from_image(visual = False): im = imread("segmentation.inr.gz") graph = graph_from_image(im) if visual : Viewer.display(graph2pglscene(graph,graph.vertex_property('barycenter'),graph.vertex_property('L1')))