Gaizer Bence
Gaizer Bence

Reputation: 13

What is the easiest way on a binary image to check, if two pixels are connected? (In Matlab)

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!

Upvotes: 1

Views: 533

Answers (1)

Malcolm McLean
Malcolm McLean

Reputation: 6406

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

Upvotes: 1

Related Questions