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["https://i.sstatic.net/Z6RtX.png"]


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?

  • $\begingroup$ What is you input? An image or a Graph? $\endgroup$ Feb 10, 2012 at 16:20
  • $\begingroup$ @belisarius My input is an image (actually a photo of vascular networks). $\endgroup$
    – kejace
    Feb 10, 2012 at 16:36

1 Answer 1


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

  • $\begingroup$ Thanks, this solves the additional question then. $\endgroup$
    – kejace
    Feb 10, 2012 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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