0

I have been looking for a Pythonic implementation that returns a 4 and 8-connected path where True is the 4 or 8 adjacency and False is not, given a binary image and x and y coordinates of a pixel. As an example, here's the binary image:

[[ True False False False ]
 [False  True  True  True ]
 [False  True  True False ]]

I need to find a way to return a binary image that represents all the pixels belonging to the 4 or 8-connected path starting from a given pixel.

As an example, given the image above and x, y coordinates (1,1) the 4-connected path should be:

[[ True False False False ]
 [False  False  False  False ]
 [False  False  False False ]]

Given x, y coordinates (3,2), it would be:

[[ False False False False ]
 [False  True  True  True ]
 [False  True  True False ]]

What are some of the ways to do it in Python?

lowlypalace
  • 137
  • 2
  • 9
  • Since you're looking for an efficient way, one of choices could be `numpy` + `igraph` because they both work in C level. Then you can check out [this answer](https://stackoverflow.com/a/63657018/3044825) for 4-connected paths. – mathfux Oct 14 '20 at 16:25
  • Oh, I misread a question. If pure Python way is good, you can also check a `networkx` part of my answer but it can't compete with libraries such as `numpy`, `cv2` and `igraph` for performance. – mathfux Oct 14 '20 at 16:36
  • "the 4 or 8-connected path starting from a given pixel" is different from the neighborhood of the pixel at (x,y). You also talk about an adjacency matrix, which would exist for all pixels, not for only the one at (x,y). So, please revise your text for clarity, and please include the expected output, as that should clearly illustrate what you're after. – Cris Luengo Oct 14 '20 at 17:47
  • Thanks, I fixed the errors @CrisLuengo – lowlypalace Oct 14 '20 at 17:53
  • 1
    Ah, so you’re looking for connected component analysis? Also called “labeling”. OpenCV does have this, as does skimage and any other image processing library worth their salt. You typically get an image back where all pixels that belong to the same connected component have the same value. You then get the value of your (x,y) start point, then all pixels with the same value are connected to it. – Cris Luengo Oct 14 '20 at 18:10

0 Answers0