# 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
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.25014325976371765, "perplexity": 1198.1516262169037}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-49/segments/1637964363125.46/warc/CC-MAIN-20211204215252-20211205005252-00578.warc.gz"}