# How to creat a high-quality graph image for MorphologicalGraph to convert to graphs (it can't detect some edges in mine)?

I'm using MorphologicalGraph to convert images to graph. This is my source image:

Following this and this, I tried

i = Thinning[Binarize@Import["https://i.stack.imgur.com/WfVsG.png"],
3, Method -> "MedialAxis"];
g = MorphologicalGraph[i,
EdgeStyle -> Directive[Orange, Opacity[.5], Thickness[.01]],
GraphStyle -> "ThickEdge"];
Show[Dilation[i, 1], g]


Below is the output, some edges are not detected while some are detected wrongly.

Could anyone help me with this? Is this caused by the image quality?

• Try converting the image to grayscale, then dilate, binarize and thin i.e. ColorConvert[i,"Grayscale"]//Dilation[#,1]&//Thinning then try MorphologicalGraph. BTW you've got two graphs in the image - the outer box and the inner more connected object Jul 10 '21 at 10:02
• For MorphologicalGraph, a "vertex" is a branching point. The "corner" in the lower left of the image is not a branching point, so it will not be a vertex in the result. This is not incorrect behaviour. If you want that point detected, as a first step you would need to come up with a clear definition of what you consider a vertex. Jul 10 '21 at 10:55
• The other issue is that MorphologicalGraph does not support multi-edges at this point. This is an unfortunate limitation. I have requested this feature from Wolfram multiple times. They are more likely to add it if more people request it. Jul 10 '21 at 10:55
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. Jul 10 '21 at 15:05