1

Consider this binary image:

0 1 0 0 0
0 1 0 0 0
0 1 1 0 0
0 0 0 0 0
0 0 0 1 0

I am looking for a function with two coordinates as parameters, and a boolean return value, which states, if the two pixels are connected (by 4- or 8-connectivity), like this:

f([1,2],[3,3]) -> true;
f([1,2],[5,4]) -> false;

I know, that there must be an easy algorithm, and there are some functions in Matlab, which do much more (bwdist, bwconncomp), but I'm looking for a simpler way.

Thanks for the help!

  • A simpler way than a single line using `bwconncomp` or `bwlabel`? What is simpler? a function that uses less characters? – Ander Biguri Mar 30 '17 at 10:38
  • Just `bwlabel` the image, and check if `f(1,2)==f(4,5)`. There is no simpler way than that. – Adiel Mar 30 '17 at 12:00

1 Answers1

1

Your alternatives are to flood fill from one pixel, then check the other, to label all connected components and check the label, or to do A* pathfinding. A* will probably produce the fastest results if most of the pairs are close together but in large shapes, it's also the most complicated of the three methods.

Matlab has labelconnected components built in. It's not a particularly complicated algorithm. If you check my binary image processing library you can find implementations in C of all three methods.

https://github.com/MalcolmMcLean/binaryimagelibrary

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18