user3470496
user3470496

Reputation: 171

Finding shortest length between to regions of interest

I have a binary image with several regions of interest which I have identified via bwconncomp. I am trying to find the shortest point connecting each of these regions. I was considering using dilation with larger and larger kernel sizes within a loop with code similar to below, pausing the loop when the number of connected components drops, then maybe identifying those which have connected by sizable changes in centroids and using the number of iterations times two to gives the approximate distance? I feel like there should be a better way of doing this?

distancebetweenROIS=[];
M11=tempBimage;

for c=1:50          
    TT=bwconncomp(M11);
    seDil=strel('disk',c);
    M11=imdilate(tempBimage,seDil);
    YY=bwconncomp(M11);
    if length(TT.PixelIdxList)>length(YY.PixelIdxList)
        distancebetweenROIS(end+1)=c*2;
    end

end

enter image description here

enter image description here

enter image description here

Upvotes: 3

Views: 450

Answers (1)

Jonas
Jonas

Reputation: 74940

Using bwdist and bwlabel, you can find the shortest distance of any feature to all other features. All you have to do is then to loop through the features.

%// labeledImage is 1 on feature #1, 2 on feature #2, etc

labeledImage = bwlabel(yourBinaryImage);

nLabels = max(labeledImage(:));

%// find the distance between each label and all other labels

distMat = zeros(nLabels, nLabels);

for iLabel = 1:nLabels

%// distance transform - every pixel is the distance to the nearest 
%//   non-zero pixel, i.e. the location of label iLabel

dist = bwdist(labeledImage==iLabel);

%// use accumarray b/c we can
%// get rid of the zeros in labeledImage, though, as there is no index 0

distMat(:,iLabel) = accumarray(dist(labeledImage>0),labeledImage(labeledImage>0),[],@min);

end

Note that the distance is equivalent to "how many hops do I need at minimum if I start on feature X and hop from pixel to pixel onto feature Y". If you need the distance between centroids, regionprops(yourBinaryImage,'Centroids') followd by pdist2 is the better approach.

Upvotes: 2

Related Questions