2
$\begingroup$

I'm trying to use MorphologicalGrpah to produce a graph, but I tried with a different graph. It does not print the exact graph. Is there a way to fix it? Is there a program where I can draw a graph by my hand and convert it to Wolfram Language?

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ I am commenting on the OP because this addresses comments made in the two current answers: perhaps you may consider the value that would come with learning how to produce these graphs using the Wolfram Language, rather than trying to use the methods you are currently attempting. This is, in its current state, a problem of image recognition. If you continue down this path, you may find value in training a neural network to produce your desired outcome. This too can be done with the Wolfram Language. $\endgroup$ Mar 7 at 9:46
5
$\begingroup$

If you are determined to work from the way the graph has been drawn, then you need to modify the input image to get it into a form MorphologicalGraph can work with. This is one way of doing it - the idea is to fill in the circles where the vertices are then use Thinning:

img=enter image description here

Then (the value of 'r' arrived at from experiment):

With[{r = 12}, TopHatTransform[img, DiskMatrix[r]]] // Binarize // Erosion[#, 2] &;
ComponentMeasurements[%, "Mask", And[#Count > 200, .9 < #Circularity < 1.1] &, "ComponentAssociation"]//Binarize;
infill = ImageAdd[Image /@ % // Values]

enter image description here

Now add this back to the negation of the original image to fill in the disks:

filled = ImageAdd[ColorNegate[img] // Binarize, Dilation[infill, 3]]

enter image description here

Then use Thinning:

thinned = Thinning[filled]

enter image description here

Finally, MorphologicalGraph:

MorphologicalGraph[thinned]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you. If I want to follow this procedure to other graphs, I just need to fix the "r" value? Another way to say it, can I use this procedure for any graph I see in a textbook? $\endgroup$ Mar 7 at 11:24
  • 1
    $\begingroup$ @MubarakAlsaeedi no, the procedure I've shown works for the form of input image you showed, it won't generalise to "any" graph in a textbook, there are no end of ways graphs can be drawn. $\endgroup$
    – EstabanW
    Mar 7 at 11:56
5
$\begingroup$
img = Import["https://i.stack.imgur.com/g4Z5W.png"]

enter image description here

We can identify the circles representing vertices and remove them and fill the spaces of removed vertices to get an image to pass to MorphologicalGraph:

img2 = Colorize @ DeleteBorderComponents @ MorphologicalComponents @ Binarize @ img

enter image description here

img3 = DeleteSmallComponents[img2, 500]

enter image description here

img4 = Binarize @ RemoveBackground @ ColorNegate @ img3

enter image description here

img5 = ColorNegate @ MorphologicalTransform[img4, "BoundingBoxes", Infinity]

enter image description here

MorphologicalGraph[img5, VertexSize -> Small]

enter image description here

Note: The magic number 500 in step 3 is obtained by squinting at the "Area", "Circularity" and "Rectangularity" numbers for the 13 components of img2:

ComponentMeasurements[img2, {"Area", "Circularity", "Rectangularity"}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you. Now can I use this procedure to any graph? $\endgroup$ Mar 7 at 11:21
3
$\begingroup$

The circles in your drawing are recognized as branch point. Therefore, do not draw circles.

Here is an example picture:

enter image description here

With this pictures stored in im:

im = ColorNegate[im]
MorphologicalGraph[im]

We get the following graph:

$\endgroup$
3
  • $\begingroup$ Great, thank you. One last question, is there a way when I study graphs in mathematics textbooks, I can capture them then convert them in Mathematica without drowning them without circles? $\endgroup$ Mar 7 at 9:23
  • $\begingroup$ I am using: duckcapture.de.softonic.com $\endgroup$ Mar 7 at 9:31
  • $\begingroup$ thank you, I’m using Mac to capture easily; what I’m searching for is software like Mathpix (a software convert equations to LaTeX), but I want a software convert image to graph. $\endgroup$ Mar 7 at 9:39
3
$\begingroup$

As per the "Applications" section of the documentation, you can use ColorNegate:

enter image description here

As per your comment: if you want to get Daniel Huber's graph from your image, try MorphologicalGraph[ColorNegate@Binarize@Erosion[i, DiskMatrix[20]]] where i is the image.

enter image description here

$\endgroup$
3
  • $\begingroup$ It's good, but that does not give me the desired graph. The original graph has 9 vertices, but the graph you obtained has 24 Vertex. $\endgroup$ Mar 7 at 8:20
  • $\begingroup$ @MubarakAlsaeedi Why don't you draw your graph in such a way that it is compatible with MorphologicalGraph? Clearly, the purpose of MorphologicalGraph is not to recognize arbitrarily-styles hand-drawn graphs, but to extract a graph from a branching structure. $\endgroup$
    – Szabolcs
    Mar 7 at 8:44
  • $\begingroup$ @Szabolcs I can do that, but I'm searching for a way to convert an image to a graph. I'm currently studying a textbook with lots of graphs, and I want to insert them in Mathematica so I can play with them by capturing the graph and insert it as an image and then convert them to a graph. $\endgroup$ Mar 7 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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