dtr43
dtr43

Reputation: 155

Calculating geodesic distance in MATLAB

I want to calculate geodesic distance between non-adjacent nodes in an directed graph. As in the following matrix (W), zeros reperesent that those nodes are non-adjacent and other values show weights of the edges corresponding nodes. I calculated the distance according to the following equation:

I used the MATLAB function graphshortestpath(). However, I'm afraid this function couldn't provide what I am looking for. So the question is what is another way to calculate such a distance? Is there another function in MATLAB for that? Is it possible that making the matrix sparse affects final result?

Program code:

N=6;  % Size of matrix W
W=[0,0.797944993710195,0,0,0,0;0.495326358306295,0,0.164911895411107,0,0,0;0,0.0530273831645896,0,0.00901073533222818,0,0;0,0,0.00709165683115063,0,0.438584093809830,0;0,0,0,0.397895339311598,0,0.000916573989905329;0,0,0,0,0.00104307323830613,0]; %Connectivity matrix
Geo_dist=zeros(N);   %temporary variable for geodesic distance measurement
W_sparse=sparse(W);  %Making W sparse because the matlab function works only with sparse matrix
for g=1:N
for h=1:N
    if W(g,h)==0 & g~=h;
        Geo_dist(g,h)=graphshortestpath(W_sparse,g,h,'directed',false); 
    end
end
 end

Upvotes: 2

Views: 1042

Answers (1)

Alex Taylor
Alex Taylor

Reputation: 1412

Can you use bwdistgeodesic or graydist in the Image Processing Toolbox directly on the image grid?

https://www.mathworks.com/help/images/ref/graydist.html https://www.mathworks.com/help/images/ref/bwdistgeodesic.html

Or if you prefer something smoother, imsegfmm will let you use fast marching method to compute the geodesic distance if you look at the doc example:

https://www.mathworks.com/help/images/ref/imsegfmm.html

I'm assuming in my response that you have gridded image data and that this isn't a graph of arbitrary connectivity since this is tagged "image-processing".

Upvotes: 2

Related Questions