# How to determine edgeweights from width of skeleton using MorphologicalGraph (or workaround)?

Background

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["http://i.stack.imgur.com/Z6RtX.png"]


g = MorphologicalGraph[img, EdgeWeight -> Automatic]


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


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.

Question

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?

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

• What is you input? An image or a Graph? Feb 10 '12 at 16:20
• @belisarius My input is an image (actually a photo of vascular networks). 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]


• Thanks, this solves the additional question then. Feb 10 '12 at 16:37