Created
May 30, 2014 21:29
-
-
Save jeanpat/2a99c6a794b6d4948eeb to your computer and use it in GitHub Desktop.
Cell of an ipython notebook containing a function to convert the image of a skeleton defined on a neighborhood of 8 pixels, into a graph (networkx). Depends on numpy, mahotas, networkx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import mahotas as mh | |
| import networkx as nx | |
| import numpy as np | |
| def C8skeleton_to_graph(skeletonC8): | |
| #images processing: extraction of branchedpoints, end-points, edges | |
| ep = endPoints(skeletonC8) | |
| bp = branchedPoints(skeletonC8, showSE = False) | |
| ## Label branched-points | |
| l_bp,_ = mh.label(bp) | |
| ## Label the edges | |
| l_edges = edges_from_C8skel(skeletonC8) | |
| ##Make end-points with the same label than their edge | |
| l_ep = ep*l_edges | |
| ##edges between branched-points | |
| endpoints_labels = np.where(mh.histogram.fullhistogram(np.uint16(l_ep))[:]==1)[0] | |
| edges_bp = np.copy(l_edges) | |
| for l in endpoints_labels: | |
| edges_bp[np.where(edges_bp == l)]=0 | |
| #edges between end-points and branched points | |
| edges_ep_to_bp = l_edges * np.logical_not(edges_bp > 0) | |
| #Building graph | |
| ## Add branched points first | |
| G=nx.Graph() | |
| lab_bpTonode = add_bp_to_graph(G, l_bp) | |
| ## Add end-points | |
| lab_epTonode = add_ep_to_graph(G, l_ep) | |
| ##Link end-points to branched-points | |
| ###labels of bp | |
| branchedpoints_labels = np.where(mh.histogram.fullhistogram(np.uint16(l_bp))[:]==1)[0] | |
| for lab in branchedpoints_labels: | |
| pos = np.where(l_bp == lab) | |
| row = int(pos[0]) | |
| col = int(pos[1]) | |
| #search label(s) of edges in image containing edges between ep and bp | |
| ## first get the neighborhood of the curent bp | |
| neigh_epbp = edges_ep_to_bp[row-1:row+1+1,col-1:col+1+1] | |
| labels_in_neigh = np.where(mh.histogram.fullhistogram(np.uint16(neigh_epbp))[:]<>0)[0] | |
| #print neigh_epbp, labels_in_neigh[1:] | |
| #get node(s) of attribute label= labels_in_neigh ! may be more than one, think to a list | |
| for lab_ep in labels_in_neigh[1:]: | |
| #search for nodes f attribute label= lab_ep | |
| w = np.sum(l_edges==lab_ep) | |
| print 'linking ',lab, lab_ep, ' weight ',w | |
| G.add_edge(lab_bpTonode[lab],lab_epTonode[lab_ep],weight=w)# | |
| ## | |
| ##Now try to link branched points between them | |
| ## | |
| bps_neighborhood = {} | |
| branchedpoints_labels = np.where(mh.histogram.fullhistogram(np.uint16(l_bp))[:]==1)[0] | |
| for lab in branchedpoints_labels: | |
| pos = np.where(l_bp == lab) | |
| row = int(pos[0]) | |
| col = int(pos[1]) | |
| #search label(s) of edges in image containing edges between ep and bp | |
| ## first get the neighborhood of the curent bp | |
| neigh_epbp = edges_bp[row-1:row+1+1,col-1:col+1+1] | |
| labels_in_neigh = np.where(mh.histogram.fullhistogram(np.uint16(neigh_epbp))[:]<>0)[0] | |
| bps_neighborhood[lab]=labels_in_neigh[1:].tolist() | |
| print bps_neighborhood | |
| ## Build the dictionnary of edges see (http://stackoverflow.com/questions/21375146/pythonic-inverse-dict-non-unique-mappings) | |
| invert_is_edges = {item: [key for key in bps_neighborhood if item in bps_neighborhood[key]] for value in bps_neighborhood.values() for item in value} | |
| ## Addeges to graph | |
| for ed in invert_is_edges.keys(): | |
| ## first get edge size -> its weight | |
| w = np.sum(edges_bp==ed) | |
| vertex1 = invert_is_edges[ed][0] | |
| vertex2 = invert_is_edges[ed][1] | |
| #print ed,w | |
| G.add_edge(vertex1,vertex2,weight=w) | |
| ## This is it !! | |
| return G | |
| skeletonB = mh.thin(imB) | |
| Bgraph = C8skeleton_to_graph(skeletonB) | |
| skeletonH = mh.thin(imH) | |
| Hgraph = C8skeleton_to_graph(skeletonH) | |
| figsize(6,6) | |
| subplot(221,xticks=[] | |
| ,yticks=[]) | |
| imshow(imB, interpolation='nearest') | |
| subplot(222,xticks=[],yticks=[]) | |
| nx.draw(Bgraph) | |
| subplot(223,xticks=[],yticks=[]) | |
| imshow(imH, interpolation='nearest') | |
| subplot(224,xticks=[],yticks=[]) | |
| nx.draw(Hgraph) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting !!
I have tried to find the branch points or Junction points of a flow chart but did not get. In parts by understanding the code I tried. Will you pls try on flow chart and chk how cAN i GET junction point of shape and edge to transform flowchart into graph.