6

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["https://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]]

enter image description here

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.

TumbiSapichu
  • 1,603
  • 8
  • 17
  • 6
    See the documentation of ComponentMeasurements -> Application section -> last example. That might help. – Henrik Schumacher Dec 14 '20 at 12:30
  • 2
    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. – thorimur Mar 18 '21 at 00: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? – thorimur Mar 18 '21 at 00:09
  • 1
    @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! – TumbiSapichu Mar 18 '21 at 00:15
  • So a = ComponentMeasurements[MaxFilter[morphoComp, 1], "Neighbors"]; SparseArray[Join @@ (Thread@*List @@@ a) -> 1] would construct the adjacency matrix, right? – Henrik Schumacher Mar 20 '21 at 10:12
  • For edit 2: could you use DistanceMatrix[ ComponentMeasurements[MaxFilter[morphoComp, 1], "Centroid"][[All, 2]]] ? to select by criteria? little clunky though – Teabelly Mar 21 '21 at 19:02

0 Answers0