I have the following image
Is it possible to extract the lines from this image and then convert those lines to a graph representation? I tried using ImagesLines[]
with Binarize[]
but got very bad results.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityg = IndexGraph[
MorphologicalGraph[
Thinning[ColorNegate[Binarize[pic]], Method -> "MedialAxis"]]]
Then you can merge vertices that are close each other:
pts = GraphEmbedding[g];
Median[EuclideanDistance[pts[[#[[1]]]], pts[[#[[2]]]]] & /@
EdgeList[g]]
13.0384
f = Nearest[Thread[pts -> Range[Length[pts]]]]
merge = Select[f[#, {All, 10}] & /@ pts, Length[#] > 1 &];
You can check the grouping of vertices:
HighlightGraph[g, merge, VertexSize -> 1]
and final graph:
final = Fold[VertexContract, g, merge]
First, use the method of deleting the branch points developed in this answer:
img = Import["https://i.stack.imgur.com/aLhGVm.png"];
imb = Thinning[ColorNegate@Binarize@img];
edges = DeleteSmallComponents@MorphologicalTransform[imb,
If[#[[2, 2]] == 1 && Total[#, 2] == 3, 1, 0] &];
edgesLabeled = MorphologicalComponents[edges];
edgesLabeled // Colorize
Second, extract the branch points (nodes
) and separate them from the edges by making their labels sufficiently large, as I did here:
nodes = imb - edges;
threshold = 10^Ceiling[Log10[Max[edgesLabeled] + 1]];
nodesLabeled = MorphologicalComponents[nodes] * threshold;
nodesLabeled // Colorize
edgesPlusNodesLabeled = edgesLabeled + nodesLabeled;
edgesPlusNodesLabeled // Colorize
Now it is easy to reconstruct the graph:
neighbors =
ComponentMeasurements[edgesPlusNodesLabeled,
"Neighbors", #Label < threshold &];
coords = ComponentMeasurements[edgesPlusNodesLabeled,
"BoundingDiskCenter", #Label >= threshold &];
Graph[UndirectedEdge @@@ neighbors[[All, 2]],
VertexCoordinates -> coords, VertexSize -> .2]
Overlay on the original image:
HighlightImage[img, {Thick, Line /@ neighbors[[All, 2]] /. coords,
Green, Translate[Disk[{0, 0}, Offset[2]], coords[[All, 2]]]}]
ImageLines[...,Method-> "Segmented"->True]
$\endgroup$MorphologicalGraph@Thinning@Invert@i
? $\endgroup$ColorNegate
, notInvert
$\endgroup$