Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


I'm trying to create a workaround for what seems to be a bug in MMA's graph functions related to edge weights:

SO: How to Label Graph Edges with their weights

According to MMA's documentation for MorphologicalGraph:

For EdgeWeight->Automatic the weight of each edge is set to the number of pixels in the corresponding skeleton connection. If more than one connection exists, the shortest one is chosen to represent the weight of the edge.

However, looking at the AdjacencyGraph, it seems to always fill in 0's and 1's, so I assume this not to be working.

What I am looking for can be illustrated with the following code:

img = Import[""]


g = MorphologicalGraph[img, EdgeWeight -> Automatic]


    SkeletonTransform@img)~MinFilter~2.~Colorize~(ColorFunction -> 

image manipulated with edge colour corresponding to width

This last image shows that it is possible to compute the edge weights using SkeletonTransform, and I assume these to be the ones that MorphologicalGraph is supposed to return.


How can I get the corresponding edge weights in the graph (or, for example as an AdjacencyMatrix), that corresponds to the pixelwidth of each edge in the image?

Additional question

How can I visualize these graph weights as thickened coloured edges so that a GraphPlot corresponds to the coloured image above?

share|improve this question
What is you input? An image or a Graph? – Dr. belisarius Feb 10 '12 at 16:20
@belisarius My input is an image (actually a photo of vascular networks). – kejace Feb 10 '12 at 16:36

This is more of a remark than a solution, but it was too long for a comment.

First of all, although it's not visible in the plot, MorphologicalGraph with EdgeWeight -> Automatic does set edge weights for the edges. However, these are not based on the thickness of the edges but on their length. For example, for the example in the original post the edge weights of g are equal to

g = MorphologicalGraph[img, EdgeWeight -> Automatic];
weights = OptionValue[Options[g, EdgeWeight], EdgeWeight]
{22, 20, 5, 11, 5, 34, 49, 13, 41, 12, 7, 6, 9, 34, 44, 24, 29, 54, 
  65, 17, 34, 26, 38, 21, 43, 41, 13, 52, 3, 14, 6, 47, 19, 24, 9, 14, 
  82, 8, 13, 13, 52, 23, 63, 99, 73, 84, 40}

Using a custom EdgeRenderingFunction for GraphPlot to colour the edges and change their thickness according to weights you get something like

vertices = VertexList[g];
crds = OptionValue[Options[g, VertexCoordinates], VertexCoordinates];
edges = List @@@ EdgeList[g];

GraphPlot[Rule @@@ edges,
  VertexCoordinateRules -> Thread[vertices -> crds],
  EdgeRenderingFunction -> (With[{w = 
        Pick[Rescale[weights], edges, (#2 | Reverse[#2])][[1]]},
      {Thickness[.02 (.1 + w)],
       ColorData["SunsetColors"][.9 w], Line[#1]}] &), 
  AspectRatio -> 1]

Mathematica graphics

share|improve this answer
Thanks, this solves the additional question then. – kejace Feb 10 '12 at 16:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.