# How to obtain the adjacency matrix of morphological components?

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.

• See the documentation of ComponentMeasurements -> Application section -> last example. That might help. Dec 14 '20 at 12:30
• Just to put it here: the reason you're getting an empty list of neighbors with ComponentMeasurements is because of the 0's separating each component in morphoComp. Use MaxFilter[morphoComp, 1] and it should work. Mar 18 at 0:06
• What exactly do you mean by asking for it do be done such that only neighbors up to a certain distance are considered adjacent? Aren't all neighbors already 0 distance apart? Do you mean that even neighbors that might be not touching, but below a certain distance apart, ought to be considered adjacent? Mar 18 at 0:09
• @thorimur Oh, you're right. I'll reformulate this part. I'm thinking of a given component, if "grown" n pixels around its borders (dilation), it considers a neighbor whatever other component(s) it got in touch with. This sort of distance I'm thinking about. I don't think it is a "watershed distance", maybe there's a better term? Would this need to be done simultaneously for all components? P.S. Yeah, using MaxFilter[morphoComp, 1] seems to work, thanks! Mar 18 at 0:15
• So a = ComponentMeasurements[MaxFilter[morphoComp, 1], "Neighbors"]; SparseArray[Join @@ (Thread@*List @@@ a) -> 1] would construct the adjacency matrix, right? Mar 20 at 10:12