Lay
Lay

Reputation: 249

How do I calculate the distance to the nearest point along a path?

I am looking for how to calculate the distance along a path in a binary array. I imported a map as a matrix in matlab. There is a binary image of a river crossing two cities. I only found out how to calculate the distance from the river points to the nearest city but I don't manage to compute the shortest distance along the river. I made a vector with the indices of all river points but I don't know how to get the distance to the nearest city from that...

enter image description here

Image So I am looking for the shortest distance through the red line towards one of the light blue points it crosses !

Thnx

Upvotes: 0

Views: 370

Answers (1)

matheburg
matheburg

Reputation: 2180

If I understand you in the right way it is not very difficult: Just do a dfs or bfs (8-neighbourhood) starting at each river-town and add sqrt(2) if you go diagonal and 1 if you go to a 4-neighbour. At each river pixel you can finally decide by taking the minimum value. You can develop it further stopping at river pixels with already smaller distance to another city...

I really hope I got you in the right way :)

Upvotes: 0

Related Questions