Mathematica Asked on June 18, 2021
I am wondering if there’s a simple way to obtain the adjacency matrix of the morphological components of a segmented image. Consider the following example (originally, from this question):
(*Import binary image*)
image = Import["http://i.stack.imgur.com/iab6u.png"];
(*Obtain the distance transform*)
disT = DistanceTransform[ColorNegate[image]];
(*Visualize the watershed morphological components*)
wComp = WatershedComponents[disT];
morphoComp = MorphologicalComponents[wComp];
imgMorphoC = Dilation[morphoComp // Colorize, 1];
ImageMultiply[imgMorphoC, ColorNegate[image]]
Is there a simple way to obtain the adjacency matrix of the resulting components? I suspect there’s a way to transform morphoComp
into an adjacency matrix, or a graph/network, but I’m not sure where to start. Any help would be appreciated.
Thanks!
Edit 1: I tried to simply do ComponentMeasurements[image, "Neighbors"]
, but it retrieves an empty list of neighbors. From the documentation it is unclear what is meant by "Neighbors"
, are these only touching components?
Edit 1.1: Doing ComponentMeasurements[MaxFilter[morphoComp, 1], "Neighbors"]
works:
{1 -> {3, 4, 5, 7}, 2 -> {5, 6}, 3 -> {1, 7, 8}, 4 -> {1, 5, 6, 7, 8},
5 -> {1, 2, 4, 6}, 6 -> {2, 4, 5, 8, 9}, 7 -> {1, 3, 4, 8},
8 -> {3, 4, 6, 7}, 9 -> {6}}
Edit 2: Could this be done so that only neighbors up to a certain distance are considered adjacent? Say, that from the original morphological components, they are grown n number of pixels around their border (with Dilation[]
, for instance), and only these components coming in contact after this dilation would be considered neighbors.
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP